-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_mean_var.py
69 lines (57 loc) · 1.61 KB
/
get_mean_var.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
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding=utf-8 -*-
from PIL import Image
import numpy as np
from tqdm import tqdm
train_root = '/media/hp208/4t/zhaoxingjie/data/HUAWEIAI/train_val500/train'
def get_files(dir):
import os
if not os.path.exists(dir):
return []
if os.path.isfile(dir):
return [dir]
result = []
for subdir in os.listdir(dir):
sub_path = os.path.join(dir, subdir)
result += get_files(sub_path)
return result
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
files = get_files(train_root)
files.sort()
count = len(files)
for image_file in tqdm(files):
# print('Process: %d/%d' % (i, count))
img = Image.open(image_file)#.convert('RGB')
img = img.resize((224, 224))
img = np.asarray(img)
img = img.astype('float32') / 255.
total += img.shape[0] * img.shape[1]
try:
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()
except:
print(image_file)
continue
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
print("R, G, B:")
print('Mean is %.3f, %.3f, %.3f' % (r_mean, g_mean, b_mean))
print('Var is %.3f, %.3f, %.3f' % (r_var, g_var, b_var))
print('Std is %.3f, %.3f, %.3f' % (np.sqrt(r_var), np.sqrt(g_var), np.sqrt(b_var)))
# R, G, B:
# Mean is 0.574, 0.510, 0.444
# Var is 0.091, 0.093, 0.105
# Std is 0.301, 0.306, 0.325