-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdata.py
223 lines (198 loc) · 8.01 KB
/
data.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import logging
import os
import random
from io import BytesIO
import mxnet as mx
import numpy as np
from PIL import Image
from mxnet import io
from mxnet import ndarray as nd
from mxnet import recordio
logger = logging.getLogger()
class FaceImageIter(io.DataIter):
def __init__(self, batch_size, data_shape,
path_imgrec=None,
shuffle=False, aug_list=None, mean=None,
rand_mirror=False, cutoff=0, color_jittering=0,
data_name='data', label_name='softmax_label', **kwargs):
super(FaceImageIter, self).__init__()
assert path_imgrec
logging.info('loading recordio %s...', path_imgrec)
path_imgidx = path_imgrec[0:-4] + ".idx"
self.imgrec = recordio.MXIndexedRecordIO(path_imgidx, path_imgrec, 'r')
self.seq = list(self.imgrec.keys)
logging.info("%s 数据大小:%d", path_imgrec, len(self.seq))
self.mean = mean
self.nd_mean = None
if self.mean:
self.mean = np.array(self.mean, dtype=np.float32).reshape(1, 1, 3)
self.nd_mean = mx.nd.array(self.mean).reshape((1, 1, 3))
self.check_data_shape(data_shape)
self.provide_data = [(data_name, (batch_size,) + data_shape)]
self.batch_size = batch_size
self.data_shape = data_shape
self.shuffle = shuffle
self.image_size = '%d,%d' % (data_shape[1], data_shape[2])
self.rand_mirror = rand_mirror
logging.info('是否随机翻转图片:%s', rand_mirror)
self.cutoff = cutoff
self.color_jittering = color_jittering
self.CJA = mx.image.ColorJitterAug(0.125, 0.125, 0.125)
self.provide_label = [(label_name, (batch_size, 101))]
self.cur = 0
self.nbatch = 0
self.is_init = False
def reset(self):
"""Resets the iterator to the beginning of the data."""
print('call reset()')
self.cur = 0
if self.shuffle:
random.shuffle(self.seq)
if self.seq is None and self.imgrec is not None:
self.imgrec.reset()
def num_samples(self):
return len(self.seq)
def next_sample(self):
if self.cur >= len(self.seq):
raise StopIteration
idx = self.seq[self.cur]
self.cur += 1
s = self.imgrec.read_idx(idx)
header, img = recordio.unpack(s)
label = header.label
return label, img, None, None
def brightness_aug(self, src, x):
alpha = 1.0 + random.uniform(-x, x)
src *= alpha
return src
def contrast_aug(self, src, x):
alpha = 1.0 + random.uniform(-x, x)
coef = nd.array([[[0.299, 0.587, 0.114]]])
gray = src * coef
gray = (3.0 * (1.0 - alpha) / gray.size) * nd.sum(gray)
src *= alpha
src += gray
return src
def saturation_aug(self, src, x):
alpha = 1.0 + random.uniform(-x, x)
coef = nd.array([[[0.299, 0.587, 0.114]]])
gray = src * coef
gray = nd.sum(gray, axis=2, keepdims=True)
gray *= (1.0 - alpha)
src *= alpha
src += gray
return src
def color_aug(self, img, x):
return self.CJA(img)
def mirror_aug(self, img):
_rd = random.randint(0, 1)
if _rd == 1:
for c in range(img.shape[2]):
img[:, :, c] = np.fliplr(img[:, :, c])
return img
def compress_aug(self, img):
buf = BytesIO()
img = Image.fromarray(img.asnumpy(), 'RGB')
q = random.randint(2, 20)
img.save(buf, format='JPEG', quality=q)
buf = buf.getvalue()
img = Image.open(BytesIO(buf))
return nd.array(np.asarray(img, 'float32'))
def next(self):
if not self.is_init:
self.reset()
self.is_init = True
"""Returns the next batch of data."""
self.nbatch += 1
batch_size = self.batch_size
c, h, w = self.data_shape
batch_data = nd.empty((batch_size, c, h, w))
if self.provide_label is not None:
batch_label = nd.empty(self.provide_label[0][1])
i = 0
try:
while i < batch_size:
label, s, bbox, landmark = self.next_sample()
gender = int(label[0])
age = int(label[1])
assert age >= 0
assert gender == 1 or gender == 0
plabel = np.zeros(shape=(101,), dtype=np.float32)
plabel[0] = gender
if age == 0:
age = 1
if age > 100:
age = 100
plabel[1:age + 1] = 1
label = plabel
_data = self.imdecode(s)
if _data.shape[0] != self.data_shape[1]:
_data = mx.image.resize_short(_data, self.data_shape[1])
if self.rand_mirror:
_rd = random.randint(0, 1)
if _rd == 1:
_data = mx.ndarray.flip(data=_data, axis=1)
if self.color_jittering > 0:
if self.color_jittering > 1:
_rd = random.randint(0, 1)
if _rd == 1:
_data = self.compress_aug(_data)
_data = _data.astype('float32', copy=False)
_data = self.color_aug(_data, 0.125)
if self.nd_mean is not None:
_data = _data.astype('float32', copy=False)
_data -= self.nd_mean
_data *= 0.0078125
if self.cutoff > 0:
_rd = random.randint(0, 1)
if _rd == 1:
centerh = random.randint(0, _data.shape[0] - 1)
centerw = random.randint(0, _data.shape[1] - 1)
half = self.cutoff // 2
starth = max(0, centerh - half)
endh = min(_data.shape[0], centerh + half)
startw = max(0, centerw - half)
endw = min(_data.shape[1], centerw + half)
_data[starth:endh, startw:endw, :] = 128
data = [_data]
for datum in data:
assert i < batch_size, 'Batch size must be multiples of augmenter output length'
batch_data[i][:] = self.postprocess_data(datum)
batch_label[i][:] = label
i += 1
except StopIteration:
if i < batch_size:
raise StopIteration
return io.DataBatch([batch_data], [batch_label], batch_size - i)
def check_data_shape(self, data_shape):
"""Checks if the input data shape is valid"""
if not len(data_shape) == 3:
raise ValueError('data_shape should have length 3, with dimensions CxHxW')
if not data_shape[0] == 3:
raise ValueError('This iterator expects inputs to have 3 channels.')
def check_valid_image(self, data):
"""Checks if the input data is valid"""
if len(data[0].shape) == 0:
raise RuntimeError('Data shape is wrong')
def imdecode(self, s):
"""Decodes a string or byte string to an NDArray.
See mx.img.imdecode for more details."""
img = mx.image.imdecode(s) # mx.ndarray
return img
def read_image(self, fname):
"""Reads an input image `fname` and returns the decoded raw bytes.
Example usage:
----------
>>> dataIter.read_image('Face.jpg') # returns decoded raw bytes.
"""
with open(os.path.join(self.path_root, fname), 'rb') as fin:
img = fin.read()
return img
def augmentation_transform(self, data):
"""Transforms input data with specified augmentation."""
for aug in self.auglist:
data = [ret for src in data for ret in aug(src)]
return data
def postprocess_data(self, datum):
"""Final postprocessing step before image is loaded into the batch."""
return nd.transpose(datum, axes=(2, 0, 1))