前言
该函数是对图片进行均一化处理的
有3个输入函数
分别是mean, std, inplace
mean是每个channel的平均值序列
std是每个channel的标准差序列
inplace是指是否改变原有的变量
官方Api介绍
_Normalize a tensor image with mean and standard deviation.
This transform does not support PIL Image.
Given mean: (mean[1],...,mean[n])
and std: (std[1],..,std[n])
for n
channels, this transform will normalize each channel of the inputtorch.*Tensor
i.e.,output[channel] = (input[channel] - mean[channel]) / std[channel]
.. note::
This transform acts out of place, i.e., it does not mutate 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._
需准备的东西
演示代码
# 导入库
import torchvision.transforms as transforms
import torchvision as tv
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
# 设置字体 这两行需要手动设置
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5), inplace=False)
])
# 读取图片
picTensor = tv.io.read_image('testpic.png')
print(type(picTensor))
# 转换图片
picTransformed = transform(picTensor.permute(1, 2, 0).numpy())
print(picTransformed.dtype)
print(picTransformed)
# 显示
print('图像处理之前的图片大小:', picTensor.shape)
print('图像处理之后的图片大小:', picTransformed.shape)
picNumpy = picTensor.permute(1, 2, 0).numpy()
plt.imshow(picNumpy)
plt.title('图像处理之前的图片')
plt.show()
picNumpy = picTransformed.permute(1, 2, 0).numpy()
plt.imshow(picNumpy)
plt.title('图像处理之后的图片')
plt.show()
总结
transforms.Normalize()经常和transforms.ToTensor()配合使用
那transform.Normalize()是怎么工作的呢?以上面代码为例,ToTensor()能够把灰度范围从0-255变换到0-1之间,而后面的transform.Normalize()则把0-1变换到(-1,1).具体地说,对每个通道而言,Normalize执行以下操作:
image=(image-mean)/std
其中mean和std分别通过(0.5,0.5,0.5)和(0.5,0.5,0.5)进行指定。原来的0-1最小值0则变成(0-0.5)/0.5=-1,而最大值1则变成(1-0.5)/0.5=1.