-
Notifications
You must be signed in to change notification settings - Fork 1
/
cal_mean_and_std.py
57 lines (41 loc) · 1.15 KB
/
cal_mean_and_std.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# ref:https://zhuanlan.zhihu.com/p/30966663
# for https://www.kaggle.com/c/human-protein-atlas-image-classification
import numpy as np
import os
from PIL import Image
r = 0 # r mean
g = 0 # g mean
b = 0 # b mean
r_2 = 0 # r^2
g_2 = 0 # g^2
b_2 = 0 # b^2
total = 0
root_dir = './image'
im_names = os.listdir(root_dir)
for i,name in enumerate(im_names):
im_path = os.path.join(root_dir, name)
img = np.array(Image.open(im_path))
img = img.astype('float32') / 255.
total += img.shape[0] * img.shape[1]
r += img[:, :, 0].sum()
g += img[:, :, 1].sum()
b += img[:, :, 2].sum()
r_2 += (img[:, :, 0]**2).sum()
g_2 += (img[:, :, 1]**2).sum()
b_2 += (img[:, :, 2]**2).sum()
print(i)
r_mean = r / total
g_mean = g / total
b_mean = b / total
r_var = r_2 / total - r_mean ** 2
g_var = g_2 / total - g_mean ** 2
b_var = b_2 / total - b_mean ** 2
r_std = r_var ** 0.5
g_std = g_var ** 0.5
b_std = b_var ** 0.5
print(r_mean, g_mean, b_mean)
print(r_std, g_std, b_std)
""" Reference:Imagenet
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
"""