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

使用transformsopencv类型转换学习它的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(),
>>> ])
"""
过程
代码
比较
输出结果

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

直接用Ctrl+P根据提示调用内置CALL来完成操作
ide可以更好的提示,先这样理解,其他交给时间
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()

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.
"""
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()
input[channel] = (input[channel] - mean[channel]) / std[channel]
我们这里让均值和标准差都是0.5

无法正常运行
