地址
https://blog.csdn.net/mooneve/article/details/81943904
https://blog.csdn.net/WYXHAHAHA123/article/details/87924745
https://www.cnblogs.com/hechangchun/p/10399868.html 标准化后可以提高模型的收敛速度
https://blog.csdn.net/qq_19329785/article/details/84569604 通过减去数据对应维度的统计平均值,来消除公共的部分,以凸显个体之间的特征和差异
https://blog.csdn.net/weixin_37251044/article/details/81911079
https://blog.csdn.net/majinlei121/article/details/78851213
验证代码
代码一
import os
import numpy as np
import cv2
# 这个是你图片的根目录,注意不要带中文路径,楼主就因为这个调试了很久。
image_path = 'data/COCO2017/train2017'
#image_path = 'image'
file_names = os.listdir(image_path)
count = 0
mean = np.zeros(3, np.int64)
for i in file_names:
img = cv2.imread(image_path + '/' + i)
count += 1
if count % 50 == 0:
print(count)
mean += np.sum(img, axis=(0, 1)).astype(int)
h, w = img.shape[0:-1]
print(h, w, count)
means = mean / (1.0 * h * w * count)
print('b, g, r = ', means)
下图是我用上述代码在COCO2017上的测试
其他论文里面的代码给出的是pixel_means = np.array([122.7717, 115.9465, 102.9801]) # RGB,有较小的差距,可能是最后除以h和w时的问题。
代码二
import os
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
from scipy.misc import imread
filepath = '/home/JPEGImages' # 数据集目录
pathDir = os.listdir(filepath)
R_channel = 0
G_channel = 0
B_channel = 0
for idx in xrange(len(pathDir)):
filename = pathDir[idx]
img = imread(os.path.join(filepath, filename))
R_channel = R_channel + np.sum(img[:,:,0])
G_channel = G_channel + np.sum(img[:,:,1])
B_channel = B_channel + np.sum(img[:,:,2])
num = len(pathDir) * 384 * 512 # 这里(384,512)是每幅图片的大小,所有图片尺寸都一样
R_mean = R_channel / num
G_mean = G_channel / num
B_mean = B_channel / num
print("R_mean is %f, G_mean is %f, B_mean is %f" %(R_mean, G_mean, B_mean))