-
Notifications
You must be signed in to change notification settings - Fork 96
/
augmentation.py
135 lines (107 loc) · 4.48 KB
/
augmentation.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
import cv2
import numpy as np
from imgaug import augmenters as iaa
fast_seq = iaa.SomeOf((1, 2),
[iaa.Fliplr(0.5),
iaa.Flipud(0.5),
iaa.Affine(rotate=(-10, 10),
translate_percent=(-0.1, 0.1)),
], random_order=True)
color_seq = iaa.Sequential([
# Color
iaa.OneOf([
iaa.Sequential([
iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
iaa.WithChannels(0, iaa.Add((0, 100))),
iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")]),
iaa.Sequential([
iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
iaa.WithChannels(1, iaa.Add((0, 100))),
iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")]),
iaa.Sequential([
iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
iaa.WithChannels(2, iaa.Add((0, 100))),
iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")]),
iaa.WithChannels(0, iaa.Add((0, 100))),
iaa.WithChannels(1, iaa.Add((0, 100))),
iaa.WithChannels(2, iaa.Add((0, 100)))
])
], random_order=True)
def crop_seq(crop_size):
seq = iaa.Sequential([fast_seq,
RandomCropFixedSize(px=crop_size)], random_order=False)
return seq
def padding_seq(pad_size, pad_method):
seq = iaa.Sequential([PadFixed(pad=pad_size, pad_method=pad_method),
]).to_deterministic()
return seq
class PadFixed(iaa.Augmenter):
PAD_FUNCTION = {'reflect': cv2.BORDER_REFLECT_101,
'replicate': cv2.BORDER_REPLICATE,
}
def __init__(self, pad=None, pad_method=None, name=None, deterministic=False, random_state=None):
super().__init__(name, deterministic, random_state)
self.pad = pad
self.pad_method = pad_method
def _augment_images(self, images, random_state, parents, hooks):
result = []
for i, image in enumerate(images):
image_pad = self._pad(image)
result.append(image_pad)
return result
def _augment_keypoints(self, keypoints_on_images, random_state, parents, hooks):
result = []
return result
def _pad(self, img):
img_ = img.copy()
if self._is_expanded_grey_format(img):
img_ = np.squeeze(img_, axis=-1)
h_pad, w_pad = self.pad
img_ = cv2.copyMakeBorder(img_.copy(), h_pad, h_pad, w_pad, w_pad, PadFixed.PAD_FUNCTION[self.pad_method])
if self._is_expanded_grey_format(img):
img_ = np.expand_dims(img_, axis=-1)
return img_
def get_parameters(self):
return []
def _is_expanded_grey_format(self, img):
if len(img.shape) == 3 and img.shape[2] == 1:
return True
else:
return False
class RandomCropFixedSize(iaa.Augmenter):
def __init__(self, px=None, name=None, deterministic=False, random_state=None):
super(RandomCropFixedSize, self).__init__(name=name, deterministic=deterministic, random_state=random_state)
self.px = px
if isinstance(self.px, tuple):
self.px_h, self.px_w = self.px
elif isinstance(self.px, int):
self.px_h = self.px
self.px_w = self.px
else:
raise NotImplementedError
def _augment_images(self, images, random_state, parents, hooks):
result = []
seeds = random_state.randint(0, 10 ** 6, (len(images),))
for i, image in enumerate(images):
seed = seeds[i]
image_cr = self._random_crop(seed, image)
result.append(image_cr)
return result
def _augment_keypoints(self, keypoints_on_images, random_state, parents, hooks):
result = []
return result
def _random_crop(self, seed, image):
height, width = image.shape[:2]
np.random.seed(seed)
crop_top = np.random.randint(height - self.px_h)
crop_bottom = crop_top + self.px_h
np.random.seed(seed + 1)
crop_left = np.random.randint(width - self.px_w)
crop_right = crop_left + self.px_w
if len(image.shape) == 2:
image_cropped = image[crop_top:crop_bottom, crop_left:crop_right]
else:
image_cropped = image[crop_top:crop_bottom, crop_left:crop_right, :]
return image_cropped
def get_parameters(self):
return []