深度学习图像预处理中为什么使用零均值化(zero-mean)

地址
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))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值