-
Notifications
You must be signed in to change notification settings - Fork 31
/
augmentations.py
372 lines (285 loc) · 10.8 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
366
367
368
369
370
371
372
import math
import numbers
import random
import numpy as np
from PIL import Image, ImageOps
import torch
import torchvision.transforms.functional as tf
class Compose(object):
def __init__(self, augmentations):
self.augmentations = augmentations
self.PIL2Numpy = False
def __call__(self, img, mask):
if isinstance(img, np.ndarray):
img = Image.fromarray(img, mode="RGB")
mask = Image.fromarray(mask, mode="L")
self.PIL2Numpy = True
# assert img.size == mask.size
for a in self.augmentations:
img, mask = a(img, mask)
if self.PIL2Numpy:
img, mask = np.array(img), np.array(mask, dtype=np.uint8)
return img, mask
class RandomCrop(object):
def __init__(self, size, padding=0):
if isinstance(size, numbers.Number):
self.size = (int(size), int(size))
else:
self.size = size
self.padding = padding
def __call__(self, img, mask):
if self.padding > 0:
img = ImageOps.expand(img, border=self.padding, fill=0)
mask = ImageOps.expand(mask, border=self.padding, fill=0)
assert img.size == mask.size
w, h = img.size
th, tw = self.size
if w == tw and h == th:
return img, mask
if w < tw or h < th:
return (img.resize((tw, th), Image.BILINEAR), mask.resize((tw, th), Image.NEAREST))
x1 = random.randint(0, w - tw)
y1 = random.randint(0, h - th)
return (img.crop((x1, y1, x1 + tw, y1 + th)), mask.crop((x1, y1, x1 + tw, y1 + th)))
class AdjustGamma(object):
def __init__(self, gamma):
self.gamma = gamma
def __call__(self, img, mask):
assert img.size == mask.size
return tf.adjust_gamma(img, random.uniform(1, 1 + self.gamma)), mask
class AdjustSaturation(object):
def __init__(self, saturation):
self.saturation = saturation
def __call__(self, img, mask):
assert img.size == mask.size
return (
tf.adjust_saturation(img, random.uniform(
1 - self.saturation, 1 + self.saturation)),
mask,
)
class AdjustHue(object):
def __init__(self, hue):
self.hue = hue
def __call__(self, img, mask):
assert img.size == mask.size
return tf.adjust_hue(img, random.uniform(-self.hue, self.hue)), mask
class AdjustBrightness(object):
def __init__(self, bf):
self.bf = bf
def __call__(self, img, mask):
assert img.size == mask.size
return tf.adjust_brightness(img, random.uniform(1 - self.bf, 1 + self.bf)), mask
class AdjustContrast(object):
def __init__(self, cf):
self.cf = cf
def __call__(self, img, mask):
assert img.size == mask.size
return tf.adjust_contrast(img, random.uniform(1 - self.cf, 1 + self.cf)), mask
class CenterCrop(object):
def __init__(self, size):
if isinstance(size, numbers.Number):
self.size = (int(size), int(size))
else:
self.size = size
def __call__(self, img, mask):
assert img.size == mask.size
w, h = img.size
th, tw = self.size
x1 = int(round((w - tw) / 2.0))
y1 = int(round((h - th) / 2.0))
return (img.crop((x1, y1, x1 + tw, y1 + th)), mask.crop((x1, y1, x1 + tw, y1 + th)))
class RandomHorizontallyFlip(object):
def __init__(self, p):
self.p = p
def __call__(self, img, mask):
if random.random() < self.p:
# return (img.transpose(Image.FLIP_LEFT_RIGHT), mask.transpose(Image.FLIP_LEFT_RIGHT))
# Need to pay attention to the index problem !!!
img = img.transpose(Image.FLIP_LEFT_RIGHT)
mask = mask.transpose(Image.FLIP_LEFT_RIGHT)
mask_copy = np.array(mask).copy()
right_idx = [5, 7, 9]
left_idx = [4, 6, 8]
for i in range(3):
right_pos = np.where(mask_copy == right_idx[i])
left_pos = np.where(mask_copy == left_idx[i])
mask_copy[right_pos[0], right_pos[1]] = left_idx[i]
mask_copy[left_pos[0], left_pos[1]] = right_idx[i]
return img, Image.fromarray(mask_copy)
return img, mask
class RandomVerticallyFlip(object):
def __init__(self, p):
self.p = p
def __call__(self, img, mask):
if random.random() < self.p:
return (img.transpose(Image.FLIP_TOP_BOTTOM), mask.transpose(Image.FLIP_TOP_BOTTOM))
return img, mask
class FreeScale(object):
def __init__(self, size):
self.size = size
def __call__(self, img, mask):
# assert img.size == mask.size
return (img.resize((self.size, self.size), Image.BILINEAR), mask.resize((self.size, self.size), Image.NEAREST))
class RandomTranslate(object):
def __init__(self, offset):
# tuple (delta_x, delta_y)
self.offset = offset
def __call__(self, img, mask):
assert img.size == mask.size
x_offset = int(2 * (random.random() - 0.5) * self.offset[0])
y_offset = int(2 * (random.random() - 0.5) * self.offset[1])
x_crop_offset = x_offset
y_crop_offset = y_offset
if x_offset < 0:
x_crop_offset = 0
if y_offset < 0:
y_crop_offset = 0
cropped_img = tf.crop(
img,
y_crop_offset,
x_crop_offset,
img.size[1] - abs(y_offset),
img.size[0] - abs(x_offset),
)
if x_offset >= 0 and y_offset >= 0:
padding_tuple = (0, 0, x_offset, y_offset)
elif x_offset >= 0 and y_offset < 0:
padding_tuple = (0, abs(y_offset), x_offset, 0)
elif x_offset < 0 and y_offset >= 0:
padding_tuple = (abs(x_offset), 0, 0, y_offset)
elif x_offset < 0 and y_offset < 0:
padding_tuple = (abs(x_offset), abs(y_offset), 0, 0)
return (
tf.pad(cropped_img, padding_tuple, padding_mode="reflect"),
tf.affine(
mask,
translate=(-x_offset, -y_offset),
scale=1.0,
angle=0.0,
shear=0.0,
fillcolor=250,
),
)
class RandomRotate(object):
def __init__(self, degree):
self.degree = degree
def __call__(self, img, mask):
rotate_degree = random.random() * 2 * self.degree - self.degree
return (
tf.affine(
img,
translate=(0, 0),
scale=1.0,
angle=rotate_degree,
resample=Image.BILINEAR,
fillcolor=(0, 0, 0),
shear=0.0,
),
tf.affine(
mask,
translate=(0, 0),
scale=1.0,
angle=rotate_degree,
resample=Image.NEAREST,
fillcolor=250,
shear=0.0,
),
)
class Scale(object):
def __init__(self, size):
self.size = size
def __call__(self, img, mask):
assert img.size == mask.size
w, h = img.size
if (w >= h and w == self.size) or (h >= w and h == self.size):
return img, mask
if w > h:
ow = self.size
oh = int(self.size * h / w)
return (img.resize((ow, oh), Image.BILINEAR), mask.resize((ow, oh), Image.NEAREST))
else:
oh = self.size
ow = int(self.size * w / h)
return (img.resize((ow, oh), Image.BILINEAR), mask.resize((ow, oh), Image.NEAREST))
class RandomSizedCrop(object):
def __init__(self, size):
self.size = size
def __call__(self, img, mask):
assert img.size == mask.size
for attempt in range(10):
area = img.size[0] * img.size[1]
target_area = random.uniform(0.45, 1.0) * area
aspect_ratio = random.uniform(0.5, 2.0)
w = int(round(math.sqrt(target_area * aspect_ratio)))
h = int(round(math.sqrt(target_area / aspect_ratio)))
if random.random() < 0.5:
w, h = h, w
if w <= img.size[0] and h <= img.size[1]:
x1 = random.randint(0, img.size[0] - w)
y1 = random.randint(0, img.size[1] - h)
img = img.crop((x1, y1, x1 + w, y1 + h))
mask = mask.crop((x1, y1, x1 + w, y1 + h))
assert img.size == (w, h)
return (
img.resize((self.size, self.size), Image.BILINEAR),
mask.resize((self.size, self.size), Image.NEAREST),
)
# Fallback
scale = Scale(self.size)
crop = CenterCrop(self.size)
return crop(*scale(img, mask))
class RandomSized(object):
def __init__(self, size):
self.size = size
self.scale = Scale(self.size)
self.crop = RandomCrop(self.size)
def __call__(self, img, mask):
assert img.size == mask.size
w = int(random.uniform(0.5, 2) * img.size[0])
h = int(random.uniform(0.5, 2) * img.size[1])
img, mask = (img.resize((w, h), Image.BILINEAR),
mask.resize((w, h), Image.NEAREST))
return self.crop(*self.scale(img, mask))
def img_transform(img):
# 0-255 to 0-1
# img = np.float32(np.array(img)) / 255.
# img = img.transpose((2, 0, 1))
# img = torch.from_numpy(img.copy())
import torchvision.transforms as transforms
transformer = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
img = transformer(img)
return img
def mask_transform(segm):
# to tensor
segm = torch.from_numpy(np.array(segm)).long()
return segm
def edge_contour(label, edge_width=3):
import cv2 as cv
h, w = label.shape
edge = np.zeros(label.shape)
# right
edge_right = edge[1:h, :]
edge_right[(label[1:h, :] != label[:h - 1, :]) & (label[1:h, :] != 255)
& (label[:h - 1, :] != 255)] = 1
# up
edge_up = edge[:, :w - 1]
edge_up[(label[:, :w - 1] != label[:, 1:w])
& (label[:, :w - 1] != 255)
& (label[:, 1:w] != 255)] = 1
# upright
edge_upright = edge[:h - 1, :w - 1]
edge_upright[(label[:h - 1, :w - 1] != label[1:h, 1:w])
& (label[:h - 1, :w - 1] != 255)
& (label[1:h, 1:w] != 255)] = 1
# bottomright
edge_bottomright = edge[:h - 1, 1:w]
edge_bottomright[(label[:h - 1, 1:w] != label[1:h, :w - 1])
& (label[:h - 1, 1:w] != 255)
& (label[1:h, :w - 1] != 255)] = 1
kernel = cv.getStructuringElement(cv.MORPH_RECT, (edge_width, edge_width))
edge = cv.dilate(edge, kernel)
return edge