• 常见的Transforms(一)Compose & Normalize


    一、常见的transforms

    其实就是更好的使用transform中各种各样的类

    在这里插入图片描述

    1.1 不同的类有不同的作用

    • 观察输入
    • 输出
    • 作用

    1.2 数据类型的转换

    • PIL转换为Totensor类型的 使用transforms
    • array矩阵的,通过opencv类型转换

    二、Compose类

    2.1 源码

    学习它的3个内置函数
    在这里插入图片描述

    lass Compose(object):
        """Composes several transforms together.
    
        Args:
            transforms (list of ``Transform`` objects): list of transforms to compose.
    
        Example:
            >>> transforms.Compose([
            >>>     transforms.CenterCrop(10),
            >>>     transforms.ToTensor(),
            >>> ])
        """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    过程

    1. 把不同的transforms组合起来
    2. 通过CenterCrop(10)进行中心裁剪
    3. 通过totensor进行输出

    2.2 学习python中__call__的用法

    代码

    
    
    • 1

    比较

    • call的方式可以直接使用对象+括号的方式进行调用
    • 自定义的方法可以通过对象+.的方式进行调用

    输出结果

    在这里插入图片描述

    内置call可以直接用对象名()的方式调用

    2.3 小技巧

    不用记
    在这里插入图片描述
    直接用Ctrl+P根据提示调用内置CALL来完成操作

    ide可以更好的提示,先这样理解,其他交给时间

    三、ToTensor

    3.1 代码

    from PIL import Image
    from torch.utils.tensorboard import SummaryWriter
    from torchvision import transforms  # 导入的transforms是一个py文件
    
    # 打开一张图片
    img = Image.open('Images/pytorch_logo.png')
    print('img的数据类型是:' + str(type(img)))
    print(img)
    
    # 使用transforms中的totensor
    trans_tensor = transforms.ToTensor()
    img_tensor = trans_tensor(img)
    
    # 使用tensorboadr将图片进行输出
    # 1. 创建好summarywriter
    writer = SummaryWriter('logs')
    # 2. 使用writer添加新图像
    writer.add_image('ToTensor', img_tensor)
    # 3. 使用完毕后要关闭writer
    writer.close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    3.2 结果

    在这里插入图片描述

    四、Normalize的使用

    归一化

    4.1 源码

    class Normalize(object):
        """Normalize a tensor image with mean and standard deviation.
        Given mean: ``(M1,...,Mn)`` and std: ``(S1,..,Sn)`` for ``n`` channels, this transform
        will normalize each channel of the input ``torch.*Tensor`` i.e.
        ``input[channel] = (input[channel] - mean[channel]) / std[channel]``
    
        .. note::
            This transform acts out of place, i.e., it does not mutates the input tensor.
    
        Args:
            mean (sequence): Sequence of means for each channel.
            std (sequence): Sequence of standard deviations for each channel.
            inplace(bool,optional): Bool to make this operation in-place.
    
        """
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4.2 代码

    from PIL import Image
    from torch.utils.tensorboard import SummaryWriter
    from torchvision import transforms  # 导入的transforms是一个py文件
    
    # 打开一张图片
    img = Image.open('Images/pytorch_logo.png')
    print('img的数据类型是:' + str(type(img)))
    print(img)
    
    # 使用transforms中的totensor进行totensor处理
    trans_totensor = transforms.ToTensor()
    img_tensor = trans_totensor(img)
    
    # 使用tensorboard将图片进行输出
    # 1. 创建好SummaryWriter
    writer = SummaryWriter('logs')
    # 2. 使用writer添加新图像
    writer.add_image('ToTensor', img_tensor)
    
    # Normalize归一化
    # 归一化之前图片像素的值
    print(img_tensor[0][0][0])
    trans_norm = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
    img_norm = trans_norm(img_tensor)
    # 输出归一化后的像素的第一层第一行第一列的数值
    print(img_norm[0][0][0])
    writer.add_image('Normalize', img_norm)
    
    # 3. 使用完毕后要关闭writer
    writer.close()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    4.3 归一化的公式

    input[channel] = (input[channel] - mean[channel]) / std[channel]

    我们这里让均值和标准差都是0.5

    在这里插入图片描述

    4.4 结果

    无法正常运行

    在这里插入图片描述


    专业英语

    • occurence 已经发生的东西
  • 相关阅读:
    洛谷 P4956 [COCI2017-2018#6] Davor
    京津冀国际光伏展
    论文撰写必备!16个免费查重网站助你成为学术精英
    2023秋招--游族--游戏客户端--二面面经
    Spring事务处理以及集成Mybatis
    python dict字典如何增加若干相同重复的key键的item子项?
    鼠标监视 | 拖拽方块 | Vue
    Docker学习笔记(三)
    PHP之linux、apache和nginx与安全优化面试题
    计算机毕业设计ssm计算机类图书管理系统ln698系统+程序+源码+lw+远程部署
  • 原文地址:https://blog.csdn.net/weixin_44943389/article/details/127730610