1)没有N,只针对每个通道进行归一化。输入图像应该是(C,H,W)个格式
代码:
#%%
import torch
from torchvision import transforms
#%% 数据矩阵
C= 2
H= 2
W= 2
arry = torch.arange(C*H*W,dtype=torch.float32).view([C,H,W])
print('输入矩阵:',arry)
#%% pytorch归一化
mean = [2,2]
std = [2,2]
n = transforms.Normalize(mean=mean,std=std)
print('pytorch归一化:',n(arry))
#%% 公式归一化
for c in range(C):
print('公式归一化,通道%d:'%c,(arry[c]-mean[c]) / std[c])
输出:
输入矩阵: tensor([[[0., 1.], [2., 3.]], [[4., 5.], [6., 7.]]])
pytorch归一化: tensor([[[-1.0000, -0.5000], [ 0.0000, 0.5000]], [[ 1.0000, 1.5000], [ 2.0000, 2.5000]]])
公式归一化,通道0: tensor([[-1.0000, -0.5000], [ 0.0000, 0.5000]]) 公式归一化,通道1: tensor([[1.0000, 1.5000], [2.0000, 2.5000]])
结论:pytorch的transforms.Normalize(mean,std)确实是每个通道减去均值,除以标准差获得归一化图像