-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataIter.py
341 lines (278 loc) · 11.2 KB
/
DataIter.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import numpy as np
import mindspore
from mindspore import Tensor
from mindspore.dataset import Dataset
import random
import os
import time
import cv2
from scipy.ndimage.interpolation import rotate
import scipy.ndimage
import mindspore.ops.operations as P
import mindspore.nn as nn
class MBDataIterSensi_Resis(Dataset):
def __init__(self, data_file, data_dir, phase="train", crop_size=48, crop_depth=16, sample_size=64, aug=1, sample_phase='over'):
self.phase = phase
self.data_arr = np.load(data_file, allow_pickle=True).tolist()
self.data_dir = data_dir
sensitivity_lst = [] # mal
resistant_lst = [] # ben
for i in range(len(self.data_arr)):
if 'ds' in self.data_arr[i]['path']:
sensitivity_lst.append(self.data_arr[i])
else:
resistant_lst.append(self.data_arr[i])
print(len(sensitivity_lst), len(resistant_lst))
if phase == "train":
minus_ben = len(resistant_lst) - len(sensitivity_lst)
if sample_phase == 'over':
random.shuffle(sensitivity_lst)
if minus_ben > len(sensitivity_lst):
minus_ben = minus_ben - len(sensitivity_lst)
mal_cop = sensitivity_lst[:minus_ben] + sensitivity_lst
else:
mal_cop = sensitivity_lst[:minus_ben]
self.data_lst = mal_cop * aug + sensitivity_lst * aug + resistant_lst * aug
elif sample_phase == 'under':
random.shuffle(resistant_lst)
ben_cop = resistant_lst[:len(sensitivity_lst)]
self.data_lst = ben_cop + sensitivity_lst
else:
random.shuffle(resistant_lst)
random.shuffle(sensitivity_lst)
self.data_lst = resistant_lst * aug + sensitivity_lst * aug
else:
self.data_lst = resistant_lst + sensitivity_lst
random.shuffle(self.data_lst)
print("The total samples is %d" % self.__len__())
self.crop = Crop(size=crop_size, zslice=crop_depth, phase=self.phase)
self.augm = Augmentation(phase=self.phase)
def __getitem__(self, idx, split=None):
t = time.time()
np.random.seed(int(str(t % 1)[2:7]))
cur_dir = self.data_lst[idx]['path'].split('@')[0]
label = np.zeros((13,), dtype=np.float32)
if 'ds' in self.data_lst[idx]['path']:
label[0] = 0.0
else:
label[0] = 1.0
label[1:] = self.data_lst[idx]['character']
if self.phase == "train":
cur_idx = idx
else:
cur_idx = idx
imgs = self.crop(cur_dir)
if self.phase == "train":
imgs = self.augm(imgs)
imgs = imgs[np.newaxis, :, :, :]
return Tensor(imgs.astype(np.float32)), Tensor(label.astype(np.float32)), cur_dir
def __len__(self):
return len(self.data_lst)
class Crop:
def __init__(self, size=48, zslice=16, phase='train'):
self.crop_size = size
self.zslice = zslice
self.phase = phase
self.random_crop = RandomCenterCrop(size, zslice)
self.center_crop = CenterCrop(size, zslice)
def normalize(self, img):
MIN_BOUND = -1300
MAX_BOUND = 500
img[img > MAX_BOUND] = MAX_BOUND
img[img < MIN_BOUND] = MIN_BOUND
img = img.astype(np.float32)
img = (img - MIN_BOUND) / (MAX_BOUND - MIN_BOUND)
return img
def __call__(self, img_npy):
img = np.load(img_npy)
if self.phase == "test":
img_r = self.center_crop(img)
else:
img_r = self.random_crop(img)
img_r = self.normalize(img_r)
for shapa_ in img_r.shape[1:]:
if shapa_ not in [16, 32, 48, 64, 96, 112]:
print(shapa_)
return img_r
class RandomCenterCrop:
def __init__(self, size, zslice):
assert size in [16, 32, 48, 64, 96, 112] and zslice in [6, 8, 10, 16, 32, 48, 64]
self.size = (int(size), int(size))
self.zslice = zslice
if size == 16:
self.randseed = 4
elif size == 32:
self.randseed = 6
elif size == 48:
self.randseed = 8
elif size == 64:
self.randseed = 10
elif size == 96:
self.randseed = 12
elif size == 112:
self.randseed = 14
def __call__(self, data):
s, y, x = data.shape
des_w, des_h = self.size
des_s = self.zslice
i = random.randint(-self.randseed, self.randseed)
j = random.randint(-self.randseed, self.randseed)
x_start = max(int(round((x - des_w) / 2.) + i), 0)
x_end = min(x_start + des_w, x)
y_start = max(int(round((y - des_h) / 2.) + j), 0)
y_end = min(y_start + des_h, y)
s_start = max(int(round((s - des_s) / 2.)), 0)
s_end = min(s_start + des_s, s)
data = data[s_start: s_start + des_s,
y_start: y_start + des_h,
x_start: x_start + des_w]
pad_size = (des_s - (s_end - s_start), des_h - (y_end - y_start), des_w - (x_end - x_start))
pad_edge = (
(int(pad_size[0] / 2), pad_size[0] - int(pad_size[0] / 2)),
(int(pad_size[1] / 2), pad_size[1] - int(pad_size[1] / 2)),
(int(pad_size[2] / 2), pad_size[2] - int(pad_size[2] / 2))
)
if np.sum(pad_size) != 0:
data = np.pad(data, pad_edge, 'edge')
data = data.reshape(des_s, des_h, des_w)
return data
class CenterCrop:
def __init__(self, size, zslice):
assert size in [16, 32, 48, 64, 96, 112] and zslice in [6, 8, 10, 16, 32, 48, 64]
self.size = (int(size), int(size))
self.zslice = zslice
def __call__(self, data):
s, y, x = data.shape
des_w, des_h = self.size
des_s = self.zslice
x_start = max(int(round((x - des_w) / 2.)), 0)
x_end = min(x_start + des_w, x)
y_start = max(int(round((y - des_h) / 2.)), 0)
y_end = min(y_start + des_h, y)
s_start = max(int(round((s - des_s) / 2.)), 0)
s_end = min(s_start + des_s, s)
data = data[s_start: s_end,
y_start: y_end,
x_start: x_end]
pad_size = (des_s - (s_end - s_start), des_h - (y_end - y_start), des_w - (x_end - x_start))
pad_edge = (
(int(pad_size[0] / 2), pad_size[0] - int(pad_size[0] / 2)),
(int(pad_size[1] / 2), pad_size[1] - int(pad_size[1] / 2)),
(int(pad_size[2] / 2), pad_size[2] - int(pad_size[2] / 2))
)
if np.sum(pad_size) != 0:
data = np.pad(data, pad_edge, 'edge')
try:
data = data.reshape(des_s, des_h, des_w)
except:
import pdb;
pdb.set_trace()
return data
class RandomCenterCrop:
def __init__(self, size, zslice):
assert size in [16, 32, 48, 64, 96, 112] and zslice in [6, 8, 10, 16, 32, 48, 64]
self.size = (int(size), int(size))
self.zslice = zslice
if size == 16:
self.randseed = 4
elif size == 32:
self.randseed = 6
elif size == 48:
self.randseed = 8
elif size == 64:
self.randseed = 10
elif size == 96:
self.randseed = 12
elif size == 112:
self.randseed = 14
def __call__(self, data):
s, y, x = data.shape
des_w, des_h = self.size
des_s = self.zslice
i = random.randint(-self.randseed, self.randseed)
j = random.randint(-self.randseed, self.randseed)
x_start = max(int(round((x - des_w) / 2.) + i), 0)
x_end = min(x_start + des_w, x)
y_start = max(int(round((y - des_h) / 2.) + j), 0)
y_end = min(y_start + des_h, y)
s_start = max(int(round((s - des_s) / 2.)), 0)
s_end = min(s_start + des_s, s)
data = data[s_start: s_start + des_s,
y_start: y_start + des_h,
x_start: x_start + des_w]
pad_size = (des_s - (s_end - s_start), des_h - (y_end - y_start), des_w - (x_end - x_start))
pad_edge = (
(int(pad_size[0] / 2), pad_size[0] - int(pad_size[0] / 2)),
(int(pad_size[1] / 2), pad_size[1] - int(pad_size[1] / 2)),
(int(pad_size[2] / 2), pad_size[2] - int(pad_size[2] / 2))
)
if np.sum(pad_size) != 0:
data = np.pad(data, pad_edge, 'edge')
data = data.reshape(des_s, des_h, des_w)
return data
class Augmentation:
def __init__(self, phase='train'):
self.phase = phase
def __call__(self, img_r):
if self.phase == "train":
ran_type = random.randint(0, 1)
if ran_type:
angle1 = np.random.rand() * 180
img_r = rotate(img_r, angle1, axes=(1, 2), reshape=False, mode='nearest')
ran_type = random.randint(0, 1)
if ran_type:
img_r = cv2.flip(img_r, 0)
ran_type = random.randint(0, 1)
if ran_type:
img_r = cv2.flip(img_r, 1)
ran_type = random.randint(0, 1)
if ran_type:
img_r = np.flip(img_r, 2)
return img_r
class MBDataIterResisClassify(Dataset):
def __init__(self, data_file, data_dir, phase="train", crop_size=48, crop_depth=16, sample_size=224, aug=1, sample_phase='over'):
self.data_dir = data_dir
self.phase = phase
self.data_arr = np.load(data_file, allow_pickle=True).tolist()
rifr_lst = []
mdr_lst = []
xdr_lst = []
for i in range(len(self.data_arr)):
if 'rifr' in self.data_arr[i]['path']:
rifr_lst.append(self.data_arr[i])
elif 'mdr' in self.data_arr[i]['path']:
mdr_lst.append(self.data_arr[i])
elif 'xdr' in self.data_arr[i]['path']:
xdr_lst.append(self.data_arr[i])
print(len(rifr_lst), len(mdr_lst), len(xdr_lst))
if phase == "train":
self.data_lst = rifr_lst * aug + mdr_lst * aug * 2 + xdr_lst * aug
else:
self.data_lst = rifr_lst + mdr_lst + xdr_lst
random.shuffle(self.data_lst)
print("The total samples is %d" % self.__len__())
self.crop = Crop(size=crop_size, zslice=crop_depth, phase=self.phase)
self.augm = Augmentation(phase=self.phase)
def __getitem__(self, idx, split=None):
t = time.time()
np.random.seed(int(str(t % 1)[2:7]))
cur_dir = self.data_lst[idx]["path"].split('@')[0]
label = np.zeros((13,), dtype=np.float32)
if 'rifr' in self.data_lst[idx]["path"]:
label[0] = 0.0
elif 'mdr' in self.data_lst[idx]["path"]:
label[0] = 1.0
elif 'xdr' in self.data_lst[idx]["path"]:
label[0] = 2.0
label[1:] = self.data_lst[idx]['character']
if self.phase == "train":
cur_idx = idx
else:
cur_idx = idx
imgs = self.crop(cur_dir)
if self.phase == "train":
imgs = self.augm(imgs)
imgs = imgs[np.newaxis, :, :, :]
return Tensor(imgs.astype(np.float32)), Tensor(label.astype(np.float32)), cur_dir
def __len__(self):
return len(self.data_lst)