计算pytorch标准化(Normalize)所需要数据集的均值和
⽅差实例
pytorch做标准化利⽤transforms.Normalize(mean_vals, std_vals),其中常⽤数据集的均值⽅差有:
if 'coco' in args.dataset:
mean_vals = [0.471, 0.448, 0.408]
std_vals = [0.234, 0.239, 0.242]
elif 'imagenet' in args.dataset:
mean_vals = [0.485, 0.456, 0.406]
std_vals = [0.229, 0.224, 0.225]
计算⾃⼰数据集图像像素的均值⽅差:
import numpy as np
import cv2
import random
# calculate means and std
train_txt_path = './train_'
CNum = 10000 # 挑选多少图⽚进⾏计算
img_h, img_w = 32, 32
imgs = np.zeros([img_w, img_h, 3, 1])
means, stdevs = [], []
with open(train_txt_path, 'r') as f:
lines = f.readlines()
random.shuffle(lines) # shuffle , 随机挑选图⽚
for i in tqdm_notebook(range(CNum)):
img_path = os.path.join('./train', lines[i].rstrip().split()[0])
img = cv2.imread(img_path)
img = size(img, (img_h, img_w))
img = img[:, :, :, np.newaxis]
imgs = np.concatenate((imgs, img), axis=3)
# print(i)
imgs = imgs.astype(np.float32)/255.
for i in tqdm_notebook(range(3)):
pixels = imgs[:,:,i,:].ravel() # 拉成⼀⾏
means.an(pixels))
numpy库是标准库吗stdevs.append(np.std(pixels))
# cv2 读取的图像格式为BGR,PIL/Skimage读取到的都是RGB不⽤转
print("normMean = {}".format(means))
print("normStd = {}".format(stdevs))
print('transforms.Normalize(normMean = {}, normStd = {})'.format(means, stdevs))
以上这篇计算pytorch标准化(Normalize)所需要数据集的均值和⽅差实例就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论