-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaugmentations.py
365 lines (299 loc) · 13.2 KB
/
augmentations.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import random
import PIL
import numpy as np
import torch
import cv2 as cv
import skimage.morphology
import skimage.filters
import skimage.measure
import skimage.transform
cv.setNumThreads(0)
def to_tuple(var, size=2):
if isinstance(var, (int, float)):
if size == 2:
var = (var, var)
elif size == 3:
var = (var, var, var)
elif isinstance(var, list):
var = tuple(var)
return var
def find_bounding_box(img):
try:
positive = np.where(img != 0)
bbox = np.min(positive[0]), np.max(positive[0]), np.min(positive[1]), np.max(positive[1])
except Exception:
bbox = (0, img.shape[0], 0, img.shape[1])
return bbox
def max_one(image):
image = np.array(image)
if np.amax(image) > 1:
image = image/255
return image
def keep_first_channel(image):
image = max_one(image)
if len(image.shape) > 2:
image = image[:, :, 0]
return image
def thresh_function(thresh_mode):
if isinstance(thresh_mode, list):
thresh_mode = [x.lower() for x in thresh_mode]
if 'all' in thresh_mode or 'normal' in thresh_mode:
thresh_mode = 'all'
else:
thresh_mode = thresh_mode[random.randint(0, len(thresh_mode)-1)]
if isinstance(thresh_mode, str):
try:
thresh_mode = int(thresh_mode)
except Exception:
thresh_mode = thresh_mode.lower()
if thresh_mode in ['normal', 'all', 'n', 'a']:
thresh_mode_function = random.choice([skimage.filters.threshold_otsu,
skimage.filters.threshold_yen,
skimage.filters.threshold_mean,
skimage.filters.threshold_isodata,
skimage.filters.threshold_li])
elif thresh_mode in ['yen', 'y']:
thresh_mode_function = skimage.filters.threshold_yen
elif thresh_mode in ['mean', 'm']:
thresh_mode_function = skimage.filters.threshold_mean
elif thresh_mode in ['isodata', 'i', 'id']:
thresh_mode_function = skimage.filters.threshold_isodata
elif thresh_mode in ['li', 'l']:
thresh_mode_function = skimage.filters.threshold_li
else:
thresh_mode_function = skimage.filters.threshold_otsu
if thresh_mode not in ['otsu', 'o']:
print(thresh_mode, 'not a valid thresh mode, Otsu was used by default')
return thresh_mode_function
class ToStackTensor:
def __init__(self):
self.to_tensor = ToTensor()
def __call__(self, images):
if isinstance(images, list):
return torch.stack([self.to_tensor(image) for image in images])
return self.to_tensor(images)
class ListTransform:
def __init__(self, transform):
self.transform = transform
def __call__(self, images):
if isinstance(images, list):
return [self.transform(image) for image in images]
return self.transform(images)
class Square(object):
def __call__(self, image):
height, width = image.size
size = max(height, width)
new_im = PIL.Image.new('RGB', (size, size), (0, 0, 0))
new_im.paste(image, (int((size - height) / 2), int((size - width) / 2)))
return new_im
class SquareNP(object):
def __call__(self, image):
image = keep_first_channel(image)
height, width = image.shape
size = max(height, width)
offset = ((size - height) // 2, (size - width) // 2)
new_image = np.zeros((size, size))
if np.sum(image) > np.sum(1-image):
new_image = np.ones((size, size))
new_image[offset[0]:offset[0]+height, offset[1]:offset[1]+width] = image
return new_image
class ResizeNP(object):
def __init__(self, size=224):
self.size = size
def __call__(self, image):
image = keep_first_channel(image)
height, width = image.shape
if height >= width:
new_image = skimage.transform.resize(image, (int(self.size),
int((width/height)*self.size)))
else:
new_image = skimage.transform.resize(image, (int((height/width)*self.size),
int(self.size)))
return new_image
class Pad(object):
def __init__(self, percent=(0, 12), color=(0, 0, 0)):
self.percent = to_tuple(percent, 2)
self.color = to_tuple(color, 3)
def __call__(self, image):
percent = random.randint(self.percent[0], self.percent[1])/100
height, width = image.size
new_height = int(height + percent*height)
new_width = int(width + percent*width)
new_im = PIL.Image.new('RGB', (new_height, new_width), self.color)
new_im.paste(image, (int((new_height - height) / 2), int((new_width - width) / 2)))
return new_im
class BlackBackground(object):
def __call__(self, image):
image = max_one(image)
if np.sum(image) > np.sum(1-image):
image = 1-image
return image
class MultiScale(object):
def __init__(self, size=224, size_multipliers=(0.9, 0.75, 0.5), return_white_bg=False):
if isinstance(size_multipliers, tuple):
size_multipliers = list(size_multipliers)
elif isinstance(size_multipliers, (int, float)):
size_multipliers = [size_multipliers]
self.size = size
self.size_multipliers = size_multipliers
self.return_white_bg = return_white_bg
def __call__(self, image):
image = max_one(image)
if np.sum(image) > np.sum(1-image):
image = 1-image
images = [image]
if len(image.shape) > 2:
images = [image[:, :, 0]]
for count, img in enumerate(images):
height, width = img.shape
img[:, :3], img[:, height-3:], img[:3, :], img[width-3:, :] = 0, 0, 0, 0
max_value = np.amax(img)
img[img < max_value*0.1] = 0
box = find_bounding_box(img)
img = img[box[0]:box[1], box[2]:box[3]]
height, width = img.shape
images[count] = img
new_images = []
for count, img in enumerate(images):
for multi in self.size_multipliers:
new_image = np.zeros((self.size, self.size))
try:
height, width = img.shape
if height >= width:
tmpimg = skimage.transform.resize(img,
(int(self.size*multi),
int((width/height)*self.size*multi)))
else:
tmpimg = skimage.transform.resize(img,
(int((height/width)*self.size*multi),
int(self.size*multi)))
except Exception:
print('one image was empty')
tmpimg = np.zeros((int(self.size*multi), int(self.size*multi)))
height, width = tmpimg.shape
offset = ((self.size - height) // 2, (self.size - width) // 2)
new_image[offset[0]:offset[0]+height, offset[1]:offset[1]+width] = tmpimg
if self.return_white_bg:
new_image = 1-new_image
new_images.append(new_image)
return new_images
class EdgeDetector(object):
def __init__(self, edge_mode='normal'):
self.edge_mode = edge_mode
def __call__(self, image):
image = max_one(image)
if isinstance(self.edge_mode, list):
edge_mode = [x.lower() for x in self.edge_mode]
if 'all' in edge_mode or 'normal' in edge_mode or 'a' in edge_mode or 'n' in edge_mode:
edge_mode = random.choice(['dollar', 'hed', 'bdcn'])
else:
edge_mode = edge_mode[random.randint(0, len(edge_mode)-1)]
else:
edge_mode = self.edge_mode.lower()
if edge_mode in ['all', 'normal', 'a', 'n']:
edge_mode = random.choice(['dollar', 'hed', 'bdcn'])
if edge_mode in ['bdcn', 'b']:
image = image[:, :, 0]
elif edge_mode in ['hed', 'h']:
image = image[:, :, 1]
else:
image = image[:, :, 2]
if edge_mode not in ['dollar', 'dol', 'd']:
print(self.edge_mode, 'not a valid edge mode, Dollar was used by default')
image = np.dstack((image, image, image))
return image
class OriNMS(object):
def __init__(self, model=None, prob=100, radious=2, bound_radious=0, multi=1.0):
self.model = model
self.prob = prob
self.bound_radious = bound_radious
self.radious = radious
self.multi = multi
def __call__(self, image):
image = keep_first_channel(image)
if self.model is not None:
edgemap_original = np.float32(np.array(image))
orimap_original = self.model.computeOrientation(edgemap_original)
if random.randint(1, 100) <= self.prob:
edgemap_original = self.model.edgesNms(edgemap_original, orimap_original,
r=self.radious, s=self.bound_radious,
m=self.multi)
return np.dstack((edgemap_original, edgemap_original, edgemap_original))
return np.dstack((image, image, image))
class Thresholder(object):
def __init__(self, thresh_rand=10, thresh_mode='normal', hyst_par=(0.5, 1.5), hyst_pert=0.2,
hyst_prob=100, thinning=False):
self.thresh_mode = thresh_mode
self.thresh_rand = thresh_rand
self.hyst_par = to_tuple(hyst_par, 2)
self.hyst_pert = hyst_pert
self.hyst_prob = hyst_prob
self.thinning = thinning
def __call__(self, image):
image = keep_first_channel(image)
image = image*255
if isinstance(self.thresh_mode, int):
thresh = self.thresh_mode + random.normalvariate(0, self.thresh_rand/2)
thresh = max(min(thresh, np.amax(image)), np.amin(image))
else:
try:
exact_thresh = thresh_function(self.thresh_mode)(image)
except Exception:
exact_thresh = 0.5
thresh = exact_thresh + random.normalvariate(0, self.thresh_rand/2)
if thresh >= np.amax(image) or thresh <= np.amin(image):
thresh = exact_thresh
if random.randint(1, 100) <= self.hyst_prob:
per = random.normalvariate(0, self.hyst_pert)
lower, upper = [max(0.1, self.hyst_par[0] - per), min(2, self.hyst_par[1] + per)]
if lower > upper:
lower, upper = upper, lower
binary = skimage.filters.apply_hysteresis_threshold(image/255, lower*thresh/255,
upper*thresh/255)*1
if np.amin(binary) == 1:
binary = skimage.filters.apply_hysteresis_threshold(image/255,
self.hyst_par[0]*thresh/255,
self.hyst_par[1]*thresh/255)*1
if self.thinning:
binary = skimage.morphology.skeletonize(binary)
else:
binary = (image > thresh)*1
if self.thinning:
binary = skimage.morphology.skeletonize(binary)
binary = np.dstack((binary, binary, binary))
return binary
class Cleaner(object):
def __init__(self, percent_of_cc=(80, 100), del_less_than=(0, 10)):
self.percent_of_cc = to_tuple(percent_of_cc, 2)
self.del_less_than = to_tuple(del_less_than, 2)
def __call__(self, image):
image = keep_first_channel(image)
percent_of_cc = random.randint(self.percent_of_cc[0], self.percent_of_cc[1])
del_less_than = random.randint(self.del_less_than[0], self.del_less_than[1])
blobs_labels = skimage.measure.label(image > 0.5, background=0)
unique, counts = np.unique(blobs_labels, return_counts=True)
hist = dict(zip(unique, counts))
hist[0] = 0
colors = []
counts = []
counter = 0
for component in sorted(hist, key=hist.get, reverse=True):
if counter == 0:
colors.append(component)
counts.append(hist[component])
elif (sum(counts)/sum(hist.values()) < percent_of_cc/100
and hist[component] > del_less_than):
colors.append(component)
counts.append(hist[component])
counter += 1
outimage = np.zeros_like(blobs_labels)
for i in colors:
outimage = outimage + (blobs_labels == i)
return np.dstack((outimage, outimage, outimage))
class ToTensor(object):
def __call__(self, image):
image = np.array(image)
image = keep_first_channel(image)
image = np.dstack((image, image, image))
image = torch.from_numpy(image).type('torch.FloatTensor').permute(2, 0, 1)
return image