3
3
import imgaug as ia
4
4
from imgaug import augmenters as iaa
5
5
6
- from .utils import get_crop_pad_sequence
6
+ from .utils import get_crop_pad_sequence , reseed
7
7
8
8
9
9
def _perspective_transform_augment_images (self , images , random_state , parents , hooks ):
@@ -35,12 +35,12 @@ def _perspective_transform_augment_images(self, images, random_state, parents, h
35
35
# General
36
36
iaa .SomeOf ((1 , 2 ),
37
37
[iaa .Fliplr (0.5 ),
38
- iaa .Affine (rotate = (- 20 , 20 ),
39
- translate_percent = {"x" : (- 0.1 , 0.1 ), "y" : ( - 0.1 , 0.1 )}, mode = 'symmetric' ),
38
+ iaa .Affine (rotate = (- 10 , 10 ),
39
+ translate_percent = {"x" : (- 0.25 , 0.25 )}, mode = 'symmetric' ),
40
40
]),
41
41
# Deformations
42
- iaa .Sometimes (0.3 , iaa .PiecewiseAffine (scale = (0.02 , 0.04 ))),
43
- iaa .Sometimes (0.3 , iaa .PerspectiveTransform (scale = (0.05 , 0.10 ))),
42
+ iaa .Sometimes (0.3 , iaa .PiecewiseAffine (scale = (0.04 , 0.08 ))),
43
+ iaa .Sometimes (0.3 , iaa .PerspectiveTransform (scale = (0.05 , 0.1 ))),
44
44
], random_order = True )
45
45
46
46
intensity_seq = iaa .Sequential ([
@@ -50,16 +50,16 @@ def _perspective_transform_augment_images(self, images, random_state, parents, h
50
50
iaa .Noop (),
51
51
iaa .Sequential ([
52
52
iaa .OneOf ([
53
- iaa .Add ((- 30 , 30 )),
54
- iaa .AddElementwise ((- 30 , 30 )),
55
- iaa .Multiply ((0.9 , 1.1 )),
56
- iaa .MultiplyElementwise ((0.9 , 1.1 )),
53
+ iaa .Add ((- 10 , 10 )),
54
+ iaa .AddElementwise ((- 10 , 10 )),
55
+ iaa .Multiply ((0.95 , 1.05 )),
56
+ iaa .MultiplyElementwise ((0.95 , 1.05 )),
57
57
]),
58
58
]),
59
59
iaa .OneOf ([
60
- iaa .GaussianBlur (sigma = (0.0 , 2 .0 )),
61
- iaa .AverageBlur (k = (2 , 7 )),
62
- iaa .MedianBlur (k = (3 , 7 ))
60
+ iaa .GaussianBlur (sigma = (0.0 , 1 .0 )),
61
+ iaa .AverageBlur (k = (2 , 5 )),
62
+ iaa .MedianBlur (k = (3 , 5 ))
63
63
])
64
64
])
65
65
], random_order = False )
@@ -126,6 +126,53 @@ def _is_expanded_grey_format(self, img):
126
126
return False
127
127
128
128
129
+ def test_time_augmentation_transform (image , tta_parameters ):
130
+ if tta_parameters ['ud_flip' ]:
131
+ image = np .flipud (image )
132
+ if tta_parameters ['lr_flip' ]:
133
+ image = np .fliplr (image )
134
+ if tta_parameters ['color_shift' ]:
135
+ random_color_shift = reseed (intensity_seq , deterministic = False )
136
+ image = random_color_shift .augment_image (image )
137
+ image = rotate (image , tta_parameters ['rotation' ])
138
+ return image
139
+
140
+
141
+ def test_time_augmentation_inverse_transform (image , tta_parameters ):
142
+ image = per_channel_rotation (image .copy (), - 1 * tta_parameters ['rotation' ])
143
+
144
+ if tta_parameters ['lr_flip' ]:
145
+ image = per_channel_fliplr (image .copy ())
146
+ if tta_parameters ['ud_flip' ]:
147
+ image = per_channel_flipud (image .copy ())
148
+ return image
149
+
150
+
151
+ def per_channel_flipud (x ):
152
+ x_ = x .copy ()
153
+ for i , channel in enumerate (x ):
154
+ x_ [i , :, :] = np .flipud (channel )
155
+ return x_
156
+
157
+
158
+ def per_channel_fliplr (x ):
159
+ x_ = x .copy ()
160
+ for i , channel in enumerate (x ):
161
+ x_ [i , :, :] = np .fliplr (channel )
162
+ return x_
163
+
164
+
165
+ def per_channel_rotation (x , angle ):
166
+ return rotate (x , angle , axes = (1 , 2 ))
167
+
168
+
169
+ def rotate (image , angle , axes = (0 , 1 )):
170
+ if angle % 90 != 0 :
171
+ raise Exception ('Angle must be a multiple of 90.' )
172
+ k = angle // 90
173
+ return np .rot90 (image , k , axes = axes )
174
+
175
+
129
176
class RandomCropFixedSize (iaa .Augmenter ):
130
177
def __init__ (self , px = None , name = None , deterministic = False , random_state = None ):
131
178
super (RandomCropFixedSize , self ).__init__ (name = name , deterministic = deterministic , random_state = random_state )
0 commit comments