forked from nagizeroiw/prpr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_engine.py
144 lines (117 loc) · 3.11 KB
/
data_engine.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import os.path
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import common
class data_engine(object):
def __init__(self):
base_dir = './Nosie/'
self.train_dir = base_dir + 'TRAIN/'
self.test_dir = base_dir + 'TEST/'
end
def load(self):
# load train data
train_x, train_y = [], []
i, disp_freq = 0, 1e10
for parent, dirnames, filenames in os.walk(self.train_dir):
for filename in filenames:
img = mpimg.imread(os.path.join(parent, filename)).astype('float32')
# cut 3-channel img to 1-channel
if len(img.shape) == 3:
img = img[:, :, 0]
end
# cut img -> (32, 32)
img = img[:32, :32]
# assure img shape
try:
assert img.shape == (32, 32)
except:
print img.shape
end
# regularization
if np.max(img) > 1:
img /= 255.
if parent.endswith('digits'):
label = int(filename[0])
elif parent.endswith('hjk_picture'):
try:
label = int(filename[filename.find('.') + 1])
except:
label = int(filename[0])
end
elif parent.endswith('Wanjin'):
try:
label = int(filename[filename.find('-') + 1])
except:
label = int(filename[0])
end
else: # number
label = int(filename[filename.find('.') + 1])
end
train_x.append(img)
train_y.append(label)
i += 1
if i % disp_freq == 0:
print os.path.join(parent, filename)
plt.imshow(img, cmap='gray')
plt.title(label)
plt.show()
end
end
end
self.train_x = np.asarray(train_x)
self.train_y = np.asarray(train_y)
print 'loaded train set, shape', self.train_x.shape
# print np.max(self.train_x) # 1.0
print 'loaded train label, shape', self.train_y.shape
# load test data
test_x, test_y = [], []
i, disp_freq = 0, 1e20
for parent, dirnames, filenames in os.walk(self.test_dir):
for filename in filenames:
# There are 5 .png files that matplotlib cannot read.
try:
img = mpimg.imread(os.path.join(parent, filename)).astype('float32')
except:
# print 'imread error', filename
continue
end
# cut 3-channel img to 1-channel
if len(img.shape) == 3:
img = img[:, :, 0]
end
# cut img -> (32, 32)
img = img[:32, :32]
# assure img shape
try:
assert img.shape == (32, 32)
except:
print img.shape
end
# regularization
if np.max(img) > 1:
img /= 255.
label = int(filename[filename.find('[') + 1])
test_x.append(img)
test_y.append(label)
i += 1
if i % disp_freq == 0:
print os.path.join(parent, filename)
plt.imshow(img, cmap='gray')
plt.title(label)
plt.show()
end
end
end
self.test_x = np.asarray(test_x)
self.test_y = np.asarray(test_y)
print 'loaded test set, shape', self.test_x.shape
# print np.max(self.test_x) # 1.0
print 'loaded test label, shape', self.test_y.shape
end # def
end # class
if __name__ == '__main__':
engine = data_engine()
engine.load()
end