-
Notifications
You must be signed in to change notification settings - Fork 2
/
prepareData.py
60 lines (40 loc) · 1.3 KB
/
prepareData.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
# -*- coding: utf-8 -*-
"""
@Author : wyl
@Email : wangyl306@163.com
"""
import numpy as np
import cv2
import random
import os
"""
随机挑选CNum张图片,进行按通道计算均值mean和标准差std
先将像素从0~255归一化至 0-1 再计算
"""
train_txt_path = os.path.join("./train.txt")
CNum = 100 # 挑选多少图片进行计算
img_h, img_w = 2048,2048
imgs = np.zeros([img_w, img_h, 3, 1])
means, stdevs = [], []
with open(train_txt_path, 'r') as f:
lines = f.readlines()
random.shuffle(lines) # shuffle , 随机挑选图片
for i in range(CNum):
img_path = lines[i].rstrip().split()[0]
img = cv2.imread(img_path)
img = cv2.resize(img, (img_h, img_w))
img = img[:, :, :, np.newaxis]
imgs = np.concatenate((imgs, img), axis=3)
print(i)
imgs = imgs.astype(np.float32) #255
for i in range(3):
pixels = imgs[:,:,i,:].ravel() # 拉成一行
means.append(np.mean(pixels))
stdevs.append(np.std(pixels))
means.reverse() # BGR --> RGB
stdevs.reverse()
print("normMean = {}".format(means))
print("normStd = {}".format(stdevs))
print('transforms.Normalize(normMean = {}, normStd = {})'.format(means, stdevs))
#normMean = [0.55819243, 0.45633563, 0.2586601]
#normStd = [0.3098905, 0.22576328, 0.07425958]