一、常见的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(),
>>> ])
"""
过程
- 把不同的transforms组合起来
- 通过CenterCrop(10)进行中心裁剪
- 通过totensor进行输出
2.2 学习python中__call__的用法
代码
比较
- 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()
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.
"""
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()
4.3 归一化的公式
input[channel] = (input[channel] - mean[channel]) / std[channel]
我们这里让均值和标准差都是0.5
4.4 结果
无法正常运行
专业英语
- occurence 已经发生的东西