pytorch 对图片进行归一化处理

如题,神经网络通常使用浮点数张量作为输入,我们要做的第一件事情就是将图片转化为浮点数,并且做归一化操作。

import torch
import imageio
import os

data_dir='F:\\work\\deep_learning\\pytorch\\dlwpt-code-master\\data\\p1ch4\\image-cats'
print(data_dir)
file_names=[name for name in os.listdir(data_dir) ] #if os.path.splitext(name)[-1]=='png'

batch_size=3
batch=torch.zeros(batch_size,3,256,256,dtype=torch.uint8)

for i ,file_name in enumerate(file_names):
    img_arr=imageio.imread(os.path.join(data_dir,file_name))
    img_t=torch.from_numpy(img_arr)
    img_t=img_t.permute(2,0,1)

    img_t=img_t[:3]
    batch[i]=img_t
    
#获取通道数量
n_channels=batch.shape[1]

#print(n_channels)#一般为3

#将图片转为float类型
batch=batch.float()

for c in range(n_channels):
    print(batch[:,c].shape)
    #对3张图片所有的c通道求平均值和方差
    mean=torch.mean(batch[:,c])
    std=torch.std(batch[:,c])
    batch[:,c]=(batch[:,c]-mean)/std
    print(mean)
    print(std)

print(batch.shape)

print(batch)


结果如下:

torch.Size([3, 256, 256])
tensor(147.8829)
tensor(56.4011)
torch.Size([3, 256, 256])
tensor(114.5765)
tensor(52.7250)
torch.Size([3, 256, 256])
tensor(90.6373)
tensor(49.2352)

torch.Size([3, 3, 256, 256])

你可能感兴趣的:(AI,pytorch,人工智能,python)