【记录】求取批量图片的均值和方差

在训练神经网络的时候,需要对输入的图片进行归一化操作。对于单通道灰度图可以进行采样并求取采样图片的均值和方差。
假设训练所用的图片名都存在figure_file.txt中。

import os 
import numpy as np
from PIL import Image

channel_mean = 0
channel_square_root = 0
pixels_num = 0
count = 0 # 总共图片个数
count_add = 0 # 一共采样次数
root_path = $ROOT_PATH # 绝对路径
figure_file = $FIGURE_FILE # 存图片名的txt文件
ff = open(figure_file, 'r')
for line in ff.readlines():
    count += 1
    if count % 4 == 0: # 每4张图进行一次采样
        filename = os.path.join(root_path, line.strip())
        data = np.array(Image.open(filename))
        h, w = data.shape[0], data.shape[1]
        pixels_num += h * w
        print(filename, h, w)
        
        channel_mean += np.sum(data)
        channel_square_root += np.sum(np.power(data, 2))
        count_add += 1
        
channel_mean = channel_mean / pixels_num
channel_square_root = np.sqrt(channel_square_root / pixels_num - channel_mean * channel_mean)
print(count, count_add, channel_mean, channel_square_root)

对于求取方差可以用以下数学公式推导:
S 2 = Σ ( x − μ ) 2 N = Σ ( x 2 + μ 2 − 2 x μ ) 2 N S^2 = \frac{\Sigma(x-\mu)^2}{N} = \frac{\Sigma(x^2+\mu ^2 - 2x\mu)^2}{N} S2=NΣ(xμ)2=NΣ(x2+μ22xμ)2
= Σ x 2 + N μ 2 − 2 μ Σ x N =\frac{\Sigma x^2 + N\mu ^2 - 2\mu \Sigma x}{N} =NΣx2+Nμ22μΣx
= Σ x 2 + N μ 2 − 2 μ N μ N =\frac{\Sigma x^2 + N\mu ^2 - 2\mu N \mu}{N} =NΣx2+Nμ22μNμ
= Σ x 2 N − μ 2 =\frac{\Sigma x^2}{N} - \mu ^2 =NΣx2μ2

你可能感兴趣的:(记录,均值算法,python)