forked from puke3615/SceneClassify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mean_var_fetcher.py
59 lines (46 loc) · 1.34 KB
/
mean_var_fetcher.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
from PIL import Image
import numpy as np
import config
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(config.PATH_TRAIN_IMAGES)
count = len(files)
for i, image_file in enumerate(files):
print('Process: %d/%d' % (i, count))
img = Image.open(image_file)
# img = img.resize((299, 299))
img = np.asarray(img)
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()
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('Mean is %s' % ([r_mean, g_mean, b_mean]))
print('Var is %s' % ([r_var, g_var, b_var]))
# Mean is [0.4960301824223457, 0.47806493084428053, 0.44767167301470545]
# Var is [0.084966025569294362, 0.082005493489533315, 0.088877477602068156]