记录PyTorch图像处理的归一化和标准化问题和tensor.sub_和tensor.sub方法
起因是写图像数据处理的时候需要给图片加噪声,在归一化的时候研究了一下transforms.nornalize()方法,核心是这个
tensor.sub_(mean).div_(std)
手册里sub_()方法和sub()方法是一样的,给出的是
out
i
=
input
i
−
alpha
×
others
i
\text{out}_i =\text{input}_i-\text{alpha}\times\text{others}_i
outi=inputi−alpha×othersi
这么分析的话normalize方法其实是单样本的Z-Score标准化
再谈到torch的归一化,其实在transforms方法的ToTensor方法中已经内嵌了归一化的过程,ToTensor方法的注释如下
'''Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor. This transform does not support torchscript.
Converts a PIL Image or numpy.ndarray (H x W x C) in the range [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0]'''