Newer
Older
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
# ============================================================================================= #
# Author: Lucia Hradecká #
# Copyright: Lucia Hradecká : lucia.d.hradecka@gmail.com #
# #
# MIT License. #
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy #
# of this software and associated documentation files (the "Software"), to deal #
# in the Software without restriction, including without limitation the rights #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
# copies of the Software, and to permit persons to whom the Software is #
# furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in all #
# copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
# SOFTWARE. #
# ============================================================================================= #
import random
import numpy as np
from src import AffineTransform, RandomBrightnessContrast, HistogramEqualization, Compose
# You should show that Bio-Volumentations works with existing automatic augmentation frameworks
# such as "AutoAugment: Learning Augmentation Strategies from Data"
def get_func(func_name, param_value=None, p=1, fillcolor=128):
"""
Return an instantiated transformation object.
We only use the transforms implemented in Bio-Volumentations: translation, rotation, brightness, contrast, and
histogram equalization.
Compared to the original AutoAugment/RandAugment papers, we are missing shearing, color modification transforms
(color, posterize, solarize, invert), auto-contrast, and sharpening. These transformations have no counterparts
in Bio-Volumentations and their use generally makes no sense for biomedical images.
"""
tr_list = {
'translateX': AffineTransform(translation=(0, 0, param_value), ival=fillcolor, p=p),
'translateY': AffineTransform(translation=(0, param_value, 0), ival=fillcolor, p=p),
'rotate': AffineTransform(angles=(param_value, 0, 0), ival=fillcolor, p=p),
'contrast': RandomBrightnessContrast(brightness_limit=0, contrast_limit=(param_value, param_value), p=p),
'brightness': RandomBrightnessContrast(brightness_limit=(param_value, param_value), contrast_limit=0, p=p),
'equalize': HistogramEqualization(p=p),
}
return tr_list[func_name]
#####################################################################################################
# #
# AUTOAUGMENT #
# #
# Paper: https://arxiv.org/abs/1805.09501 #
# #
# Adapted from unofficial implementation @ https://github.com/DeepVoltaire/AutoAugment/tree/master #
# #
#####################################################################################################
def get_func_param_AA(func_name, param_idx):
"""
Return transformation parameter value.
"""
ranges = {
'translateX': np.linspace(-100, 100, 10),
'translateY': np.linspace(-100, 100, 10),
'rotate': np.linspace(-30, 30, 10),
'contrast': np.linspace(-0.9, 0.9, 10),
'brightness': np.linspace(-30, 30, 10),
'equalize': [0] * 10,
}
return ranges[func_name][param_idx]
def get_policy_AA(p1, operation1, magnitude_idx1, p2, operation2, magnitude_idx2, fillcolor=128):
"""
Instantiate two transforms and return them in a list ( = an AutoAugment policy).
"""
param_value = get_func_param_AA(operation1, magnitude_idx1)
tr1 = get_func(operation1, param_value=param_value, p=p1, fillcolor=fillcolor)
param_value = get_func_param_AA(operation2, magnitude_idx2)
tr2 = get_func(operation2, param_value=param_value, p=p2, fillcolor=fillcolor)
return [tr1, tr2]
class AACIFAR10Policy(object):
""" Randomly choose a policy.
The policy list contains 4 of the best 25 Sub-policies on CIFAR10
and a couple more policies inspired by the top-25 list.
"""
def __init__(self, fillcolor=128, **params):
self.policies = [
Compose(get_policy_AA(0.7, 'rotate', 2, 0.3, 'translateX', 9, fillcolor), **params),
Compose(get_policy_AA(0.6, 'equalize', 5, 0.5, 'equalize', 1, fillcolor), **params),
Compose(get_policy_AA(0.5, 'translateX', 8, 0.2, 'equalize', 0, fillcolor), **params),
Compose(get_policy_AA(0.4, 'translateY', 3, 0.2, 'equalize', 0, fillcolor), **params),
Compose(get_policy_AA(0.2, 'equalize', 0, 0.6, 'contrast', 1, fillcolor), **params),
Compose(get_policy_AA(0.2, 'equalize', 0, 0.6, 'contrast', 8, fillcolor), **params),
Compose(get_policy_AA(0.2, 'equalize', 8, 0.6, 'equalize', 4, fillcolor), **params),
Compose(get_policy_AA(0.9, 'translateY', 9, 0.7, 'translateY', 9, fillcolor), **params),
Compose(get_policy_AA(0.7, 'translateY', 9, 0.9, 'contrast', 2, fillcolor), **params),
Compose(get_policy_AA(0.7, 'translateY', 9, 0.9, 'contrast', 7, fillcolor), **params),
Compose(get_policy_AA(0.7, 'translateY', 9, 0.9, 'brightness', 7, fillcolor), **params)
]
def __call__(self, **data):
policy_idx = random.randint(0, len(self.policies) - 1)
return self.policies[policy_idx](**data)
def run_AA():
# Fetch a data sample
# img : np.array of shape (3, 182, 600, 600)
# mask : np.array of shape (182, 600, 600)
# keypoints: list of 3D coordinates
sample = {'image': np.random.random((3, 182, 600, 600)),
'mask': np.random.randint(0, 5, (182, 600, 600)),
'keypoints': [(0, 0, 0), (100, 300, 300), (10, 500, 12)]}
# Get the policy
policy = AACIFAR10Policy()
# Transform the sample
transformed_sample = policy(**sample)
assert tuple(transformed_sample['image'].shape) == (3, 182, 600, 600) # image shape must not change
assert len(transformed_sample['keypoints']) <= 3 # we can lose some keypoints due to translation/rotation
#####################################################################################################
# #
# RANDAUGMENT #
# #
# Paper: https://arxiv.org/abs/1909.13719 #
# #
# Adapted from unofficial implementation @ https://github.com/ildoonet/pytorch-randaugment #
# #
#####################################################################################################
def augment_list_RA():
return [
('equalize', 0, 1),
('rotate', -30, 30),
('contrast', -0.9, 0.9),
('brightness', -30, 30),
('translateX', -100., 100),
('translateY', -100., 100),
]
class RandAugment:
def __init__(self, n, m):
self.n = n
self.m = m # [0, 30]
self.augment_list = augment_list_RA()
def __call__(self, **data):
# Get a list of transforms to use
ops = random.choices(self.augment_list, k=self.n)
trs = []
for op, minval, maxval in ops:
val = (float(self.m) / 30) * float(maxval - minval) + minval
trs.append(get_func(op, val))
# Apply them and return the result
transform_pipeline = Compose(trs)
return transform_pipeline(**data)
def run_RA():
# Fetch a data sample
# img : np.array of shape (3, 182, 600, 600)
# mask : np.array of shape (182, 600, 600)
# keypoints: list of 3D coordinates
sample = {'image': np.random.random((3, 182, 600, 600)),
'mask': np.random.randint(0, 5, (182, 600, 600)),
'keypoints': [(0, 0, 0), (100, 300, 300), (10, 500, 12)]}
# Get the augmenter
augmenter = RandAugment(n=3, m=20)
# Transform the sample
transformed_sample = augmenter(**sample)
assert tuple(transformed_sample['image'].shape) == (3, 182, 600, 600) # image shape must not change
assert len(transformed_sample['keypoints']) <= 3 # we can lose some keypoints due to translation/rotation
if __name__ == '__main__':
exception_counter = 0
total = 10
for _ in range(total):
try:
# run_AA()
run_RA()
except Exception as e:
exception_counter += 1
print(f'Exception encountered: {e}')
print(f'{total-exception_counter}/{total} runs successful')