forked from PaddlePaddle/PaddleSeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transforms.py
530 lines (440 loc) · 18.1 KB
/
transforms.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import random
import cv2
import numpy as np
from paddleseg.transforms import functional
from paddleseg.cvlibs import manager
from PIL import Image
@manager.TRANSFORMS.add_component
class Compose:
"""
Do transformation on input data with corresponding pre-processing and augmentation operations.
The shape of input data to all operations is [height, width, channels].
"""
def __init__(self, transforms, to_rgb=True):
if not isinstance(transforms, list):
raise TypeError('The transforms must be a list!')
self.transforms = transforms
self.to_rgb = to_rgb
def __call__(self, data):
"""
Args:
data (dict): The data to transform.
Returns:
dict: Data after transformation
"""
if 'trans_info' not in data:
data['trans_info'] = []
for op in self.transforms:
data = op(data)
if data is None:
return None
data['img'] = np.transpose(data['img'], (2, 0, 1))
for key in data.get('gt_fields', []):
if len(data[key].shape) == 2:
continue
data[key] = np.transpose(data[key], (2, 0, 1))
return data
@manager.TRANSFORMS.add_component
class LoadImages:
def __init__(self, to_rgb=True):
self.to_rgb = to_rgb
def __call__(self, data):
if isinstance(data['img'], str):
data['img'] = cv2.imread(data['img'])
for key in data.get('gt_fields', []):
if isinstance(data[key], str):
data[key] = cv2.imread(data[key], cv2.IMREAD_UNCHANGED)
# if alpha and trimap has 3 channels, extract one.
if key in ['alpha', 'trimap']:
if len(data[key].shape) > 2:
data[key] = data[key][:, :, 0]
if self.to_rgb:
data['img'] = cv2.cvtColor(data['img'], cv2.COLOR_BGR2RGB)
for key in data.get('gt_fields', []):
if len(data[key].shape) == 2:
continue
data[key] = cv2.cvtColor(data[key], cv2.COLOR_BGR2RGB)
return data
@manager.TRANSFORMS.add_component
class Resize:
def __init__(self, target_size=(512, 512)):
if isinstance(target_size, list) or isinstance(target_size, tuple):
if len(target_size) != 2:
raise ValueError(
'`target_size` should include 2 elements, but it is {}'.
format(target_size))
else:
raise TypeError(
"Type of `target_size` is invalid. It should be list or tuple, but it is {}"
.format(type(target_size)))
self.target_size = target_size
def __call__(self, data):
data['trans_info'].append(('resize', data['img'].shape[0:2]))
data['img'] = functional.resize(data['img'], self.target_size)
for key in data.get('gt_fields', []):
data[key] = functional.resize(data[key], self.target_size)
return data
@manager.TRANSFORMS.add_component
class ResizeByLong:
"""
Resize the long side of an image to given size, and then scale the other side proportionally.
Args:
long_size (int): The target size of long side.
"""
def __init__(self, long_size):
self.long_size = long_size
def __call__(self, data):
data['trans_info'].append(('resize', data['img'].shape[0:2]))
data['img'] = functional.resize_long(data['img'], self.long_size)
for key in data.get('gt_fields', []):
data[key] = functional.resize_long(data[key], self.long_size)
return data
@manager.TRANSFORMS.add_component
class ResizeByShort:
"""
Resize the short side of an image to given size, and then scale the other side proportionally.
Args:
short_size (int): The target size of short side.
"""
def __init__(self, short_size):
self.short_size = short_size
def __call__(self, data):
data['trans_info'].append(('resize', data['img'].shape[0:2]))
data['img'] = functional.resize_short(data['img'], self.short_size)
for key in data.get('gt_fields', []):
data[key] = functional.resize_short(data[key], self.short_size)
return data
@manager.TRANSFORMS.add_component
class ResizeToIntMult:
"""
Resize to some int muitple, d.g. 32.
"""
def __init__(self, mult_int=32):
self.mult_int = mult_int
def __call__(self, data):
data['trans_info'].append(('resize', data['img'].shape[0:2]))
h, w = data['img'].shape[0:2]
rw = w - w % 32
rh = h - h % 32
data['img'] = functional.resize(data['img'], (rw, rh))
for key in data.get('gt_fields', []):
data[key] = functional.resize(data[key], (rw, rh))
return data
@manager.TRANSFORMS.add_component
class Normalize:
"""
Normalize an image.
Args:
mean (list, optional): The mean value of a data set. Default: [0.5, 0.5, 0.5].
std (list, optional): The standard deviation of a data set. Default: [0.5, 0.5, 0.5].
Raises:
ValueError: When mean/std is not list or any value in std is 0.
"""
def __init__(self, mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)):
self.mean = mean
self.std = std
if not (isinstance(self.mean, (list, tuple))
and isinstance(self.std, (list, tuple))):
raise ValueError(
"{}: input type is invalid. It should be list or tuple".format(
self))
from functools import reduce
if reduce(lambda x, y: x * y, self.std) == 0:
raise ValueError('{}: std is invalid!'.format(self))
def __call__(self, data):
mean = np.array(self.mean)[np.newaxis, np.newaxis, :]
std = np.array(self.std)[np.newaxis, np.newaxis, :]
data['img'] = functional.normalize(data['img'], mean, std)
if 'fg' in data.get('gt_fields', []):
data['fg'] = functional.normalize(data['fg'], mean, std)
if 'bg' in data.get('gt_fields', []):
data['bg'] = functional.normalize(data['bg'], mean, std)
return data
@manager.TRANSFORMS.add_component
class RandomCropByAlpha:
"""
Randomly crop while centered on uncertain area by a certain probability.
Args:
crop_size (tuple|list): The size you want to crop from image.
p (float): The probability centered on uncertain area.
"""
def __init__(self, crop_size=((320, 320), (480, 480), (640, 640)),
prob=0.5):
self.crop_size = crop_size
self.prob = prob
def __call__(self, data):
idex = np.random.randint(low=0, high=len(self.crop_size))
crop_w, crop_h = self.crop_size[idex]
img_h = data['img'].shape[0]
img_w = data['img'].shape[1]
if np.random.rand() < self.prob:
crop_center = np.where((data['alpha'] > 0) & (data['alpha'] < 255))
center_h_array, center_w_array = crop_center
if len(center_h_array) == 0:
return data
rand_ind = np.random.randint(len(center_h_array))
center_h = center_h_array[rand_ind]
center_w = center_w_array[rand_ind]
delta_h = crop_h // 2
delta_w = crop_w // 2
start_h = max(0, center_h - delta_h)
start_w = max(0, center_w - delta_w)
else:
start_h = 0
start_w = 0
if img_h > crop_h:
start_h = np.random.randint(img_h - crop_h + 1)
if img_w > crop_w:
start_w = np.random.randint(img_w - crop_w + 1)
end_h = min(img_h, start_h + crop_h)
end_w = min(img_w, start_w + crop_w)
data['img'] = data['img'][start_h:end_h, start_w:end_w]
for key in data.get('gt_fields', []):
data[key] = data[key][start_h:end_h, start_w:end_w]
return data
@manager.TRANSFORMS.add_component
class RandomCrop:
"""
Randomly crop
Args:
crop_size (tuple|list): The size you want to crop from image.
"""
def __init__(self, crop_size=((320, 320), (480, 480), (640, 640))):
if not isinstance(crop_size[0], (list, tuple)):
crop_size = [crop_size]
self.crop_size = crop_size
def __call__(self, data):
idex = np.random.randint(low=0, high=len(self.crop_size))
crop_w, crop_h = self.crop_size[idex]
img_h, img_w = data['img'].shape[0:2]
start_h = 0
start_w = 0
if img_h > crop_h:
start_h = np.random.randint(img_h - crop_h + 1)
if img_w > crop_w:
start_w = np.random.randint(img_w - crop_w + 1)
end_h = min(img_h, start_h + crop_h)
end_w = min(img_w, start_w + crop_w)
data['img'] = data['img'][start_h:end_h, start_w:end_w]
for key in data.get('gt_fields', []):
data[key] = data[key][start_h:end_h, start_w:end_w]
return data
@manager.TRANSFORMS.add_component
class LimitLong:
"""
Limit the long edge of image.
If the long edge is larger than max_long, resize the long edge
to max_long, while scale the short edge proportionally.
If the long edge is smaller than min_long, resize the long edge
to min_long, while scale the short edge proportionally.
Args:
max_long (int, optional): If the long edge of image is larger than max_long,
it will be resize to max_long. Default: None.
min_long (int, optional): If the long edge of image is smaller than min_long,
it will be resize to min_long. Default: None.
"""
def __init__(self, max_long=None, min_long=None):
if max_long is not None:
if not isinstance(max_long, int):
raise TypeError(
"Type of `max_long` is invalid. It should be int, but it is {}"
.format(type(max_long)))
if min_long is not None:
if not isinstance(min_long, int):
raise TypeError(
"Type of `min_long` is invalid. It should be int, but it is {}"
.format(type(min_long)))
if (max_long is not None) and (min_long is not None):
if min_long > max_long:
raise ValueError(
'`max_long should not smaller than min_long, but they are {} and {}'
.format(max_long, min_long))
self.max_long = max_long
self.min_long = min_long
def __call__(self, data):
h, w = data['img'].shape[:2]
long_edge = max(h, w)
target = long_edge
if (self.max_long is not None) and (long_edge > self.max_long):
target = self.max_long
elif (self.min_long is not None) and (long_edge < self.min_long):
target = self.min_long
if target != long_edge:
data['trans_info'].append(('resize', data['img'].shape[0:2]))
data['img'] = functional.resize_long(data['img'], target)
for key in data.get('gt_fields', []):
data[key] = functional.resize_long(data[key], target)
return data
@manager.TRANSFORMS.add_component
class RandomHorizontalFlip:
"""
Flip an image horizontally with a certain probability.
Args:
prob (float, optional): A probability of horizontally flipping. Default: 0.5.
"""
def __init__(self, prob=0.5):
self.prob = prob
def __call__(self, data):
if random.random() < self.prob:
data['img'] = functional.horizontal_flip(data['img'])
for key in data.get('gt_fields', []):
data[key] = functional.horizontal_flip(data[key])
return data
@manager.TRANSFORMS.add_component
class RandomBlur:
"""
Blurring an image by a Gaussian function with a certain probability.
Args:
prob (float, optional): A probability of blurring an image. Default: 0.1.
"""
def __init__(self, prob=0.1):
self.prob = prob
def __call__(self, data):
if self.prob <= 0:
n = 0
elif self.prob >= 1:
n = 1
else:
n = int(1.0 / self.prob)
if n > 0:
if np.random.randint(0, n) == 0:
radius = np.random.randint(3, 10)
if radius % 2 != 1:
radius = radius + 1
if radius > 9:
radius = 9
data['img'] = cv2.GaussianBlur(data['img'], (radius, radius), 0,
0)
for key in data.get('gt_fields', []):
data[key] = cv2.GaussianBlur(data[key], (radius, radius), 0,
0)
return data
@manager.TRANSFORMS.add_component
class RandomDistort:
"""
Distort an image with random configurations.
Args:
brightness_range (float, optional): A range of brightness. Default: 0.5.
brightness_prob (float, optional): A probability of adjusting brightness. Default: 0.5.
contrast_range (float, optional): A range of contrast. Default: 0.5.
contrast_prob (float, optional): A probability of adjusting contrast. Default: 0.5.
saturation_range (float, optional): A range of saturation. Default: 0.5.
saturation_prob (float, optional): A probability of adjusting saturation. Default: 0.5.
hue_range (int, optional): A range of hue. Default: 18.
hue_prob (float, optional): A probability of adjusting hue. Default: 0.5.
"""
def __init__(self,
brightness_range=0.5,
brightness_prob=0.5,
contrast_range=0.5,
contrast_prob=0.5,
saturation_range=0.5,
saturation_prob=0.5,
hue_range=18,
hue_prob=0.5):
self.brightness_range = brightness_range
self.brightness_prob = brightness_prob
self.contrast_range = contrast_range
self.contrast_prob = contrast_prob
self.saturation_range = saturation_range
self.saturation_prob = saturation_prob
self.hue_range = hue_range
self.hue_prob = hue_prob
def __call__(self, data):
brightness_lower = 1 - self.brightness_range
brightness_upper = 1 + self.brightness_range
contrast_lower = 1 - self.contrast_range
contrast_upper = 1 + self.contrast_range
saturation_lower = 1 - self.saturation_range
saturation_upper = 1 + self.saturation_range
hue_lower = -self.hue_range
hue_upper = self.hue_range
ops = [
functional.brightness, functional.contrast, functional.saturation,
functional.hue
]
random.shuffle(ops)
params_dict = {
'brightness': {
'brightness_lower': brightness_lower,
'brightness_upper': brightness_upper
},
'contrast': {
'contrast_lower': contrast_lower,
'contrast_upper': contrast_upper
},
'saturation': {
'saturation_lower': saturation_lower,
'saturation_upper': saturation_upper
},
'hue': {
'hue_lower': hue_lower,
'hue_upper': hue_upper
}
}
prob_dict = {
'brightness': self.brightness_prob,
'contrast': self.contrast_prob,
'saturation': self.saturation_prob,
'hue': self.hue_prob
}
im = data['img'].astype('uint8')
im = Image.fromarray(im)
for id in range(len(ops)):
params = params_dict[ops[id].__name__]
params['im'] = im
prob = prob_dict[ops[id].__name__]
if np.random.uniform(0, 1) < prob:
im = ops[id](**params)
data['img'] = np.asarray(im)
for key in data.get('gt_fields', []):
if key in ['alpha', 'trimap']:
continue
else:
im = data[key].astype('uint8')
im = Image.fromarray(im)
for id in range(len(ops)):
params = params_dict[ops[id].__name__]
params['im'] = im
prob = prob_dict[ops[id].__name__]
if np.random.uniform(0, 1) < prob:
im = ops[id](**params)
data[key] = np.asarray(im)
return data
if __name__ == "__main__":
transforms = [RandomDistort()]
transforms = Compose(transforms)
fg_path = '/ssd1/home/chenguowei01/github/PaddleSeg/contrib/matting/data/matting/human_matting/Distinctions-646/train/fg/13(2).png'
alpha_path = fg_path.replace('fg', 'alpha')
bg_path = '/ssd1/home/chenguowei01/github/PaddleSeg/contrib/matting/data/matting/human_matting/bg/unsplash_bg/attic/photo-1443884590026-2e4d21aee71c?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMjA3fDB8MXxzZWFyY2h8Nzh8fGF0dGljfGVufDB8fHx8MTYyOTY4MDcxNQ&ixlib=rb-1.2.1&q=80&w=400.jpg'
data = {}
data['fg'] = cv2.imread(fg_path)
data['bg'] = cv2.imread(bg_path)
h, w, c = data['fg'].shape
data['bg'] = cv2.resize(data['bg'], (w, h))
alpha = cv2.imread(alpha_path)
data['alpha'] = alpha[:, :, 0]
alpha = alpha / 255.
data['img'] = alpha * data['fg'] + (1 - alpha) * data['bg']
data['gt_fields'] = ['fg', 'bg']
print(data['img'].shape)
for key in data['gt_fields']:
print(data[key].shape)
# import pdb
# pdb.set_trace()
data = transforms(data)
print(data['img'].dtype, data['img'].shape)
cv2.imwrite('distort_img.jpg', data['img'].transpose([1, 2, 0]))