-
Notifications
You must be signed in to change notification settings - Fork 0
/
augment.py
233 lines (183 loc) · 8.75 KB
/
augment.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
import random
import math
import torch
import torchvision.transforms.functional as TF
from torchvision import transforms
thickness_augment_types = ['identity', 'rotation', 'shift',
'scale',
'all',
'trivialaugment']
fundus_augment_types = ['identity', 'rotation', 'shift',
'scale',
'brightness_contrast_saturation',
'all',
'trivialaugment']
def identity(x):
return x
def rotation(x, degree=(-15, 15)):
p0, p1 = degree
p = torch.randint(low=p0, high=p1 + 1, size=(1,)).item()
x = TF.rotate(x, angle=p)
return x
def shift(x, param=0.1):
# RandomCrop
w, h = TF.get_image_size(x)
h_offset = int(h * param)
w_offset = int(w * param)
i = torch.randint(low=-h_offset, high=h_offset + 1, size=(1,)).item()
j = torch.randint(low=-w_offset, high=w_offset + 1, size=(1,)).item()
x = TF.crop(x, i, j, h, w)
return x
# ratio=(0.9, 1.1)
# ratio=(1.0, 1.0)
# 3.0 / 4.0, 4.0 / 3.0
def scale(x, scale=(0.9, 1.1), ratio=(3.0 / 4.0, 4.0 / 3.0)):
# RandomResizedCrop
width, height = TF.get_image_size(x)
area = width * height
i, j, h, w = None, None, None, None
log_ratio = torch.log(torch.tensor(ratio))
for _ in range(10):
target_area = area * torch.empty(1).uniform_(scale[0], scale[1]).item()
aspect_ratio = torch.exp(torch.empty(1).uniform_(log_ratio[0], log_ratio[1])).item()
w = int(round(math.sqrt(target_area * aspect_ratio)))
h = int(round(math.sqrt(target_area / aspect_ratio)))
# print(_, h, w)
if 0 < w <= width and 0 < h <= height:
i = torch.randint(0, height - h + 1, size=(1,)).item()
j = torch.randint(0, width - w + 1, size=(1,)).item()
# print(_, i, j, h, w)
# return i, j, h, w
if i is None or j is None or h is None or w is None:
# Fallback to central crop
in_ratio = float(width) / float(height)
if in_ratio < min(ratio):
w = width
h = int(round(w / min(ratio)))
elif in_ratio > max(ratio):
h = height
w = int(round(h * max(ratio)))
else: # whole image
w = width
h = height
i = (height - h) // 2
j = (width - w) // 2
x = TF.resized_crop(x, i, j, h, w, [height, width])
# print(i, j, h, w)
# print(TF.to_tensor(x).shape)
# print(type(x))
return x
def brightness_contrast_saturation(x, param=(0.75, 1.25)):
# ColorJitter
l, h = param
b = torch.empty((1,)).uniform_(l, h).item()
c = torch.empty((1,)).uniform_(l, h).item()
s = torch.empty((1,)).uniform_(l, h).item()
x = TF.adjust_brightness(x, b)
x = TF.adjust_contrast(x, c)
x = TF.adjust_saturation(x, s)
return x
# def noise(x, var=0.07):
# # GaussianBlur
# if isinstance(x, Image.Image):
# x = TF.to_tensor(x)
#
# assert isinstance(x, torch.Tensor)
#
# print(x.var(dim=0).sqrt().mean())
#
# x += torch.randn(x.size()) * math.sqrt(var)
#
# x = TF.to_pil_image(x)
#
# return x
FUNDUS_TRANSFORMS = [identity,
rotation,
shift,
scale,
brightness_contrast_saturation]
THICKNESS_TRANSFORMS = [identity,
rotation,
shift,
scale]
def trivial_augment(x, ops):
# op = random.choices(ALL_TRANSFORMS, k=1)
op = random.choice(ops)
# print(op)
x = op(x)
return x
def fundus_trivial_augment(x):
return trivial_augment(x, FUNDUS_TRANSFORMS)
def thickness_trivial_augment(x):
return trivial_augment(x, THICKNESS_TRANSFORMS)
def load_fundus_transforms(fundus_augment, input_resolution):
if fundus_augment == 'identity':
fundus_train_transform = transforms.Compose([transforms.Resize(size=input_resolution),
transforms.CenterCrop(size=input_resolution),
transforms.ToTensor()])
elif fundus_augment == 'rotation':
fundus_train_transform = transforms.Compose([transforms.Resize(size=input_resolution),
transforms.CenterCrop(size=input_resolution),
rotation,
transforms.ToTensor()])
elif fundus_augment == 'shift':
fundus_train_transform = transforms.Compose([transforms.Resize(size=input_resolution),
transforms.CenterCrop(size=input_resolution),
shift,
transforms.ToTensor()])
elif fundus_augment == 'scale':
fundus_train_transform = transforms.Compose([transforms.Resize(size=input_resolution),
transforms.CenterCrop(size=input_resolution),
scale,
transforms.ToTensor()])
elif fundus_augment == 'brightness_contrast_saturation':
fundus_train_transform = transforms.Compose([transforms.Resize(size=input_resolution),
transforms.CenterCrop(size=input_resolution),
brightness_contrast_saturation,
transforms.ToTensor()])
elif fundus_augment == 'all':
fundus_train_transform = transforms.Compose([transforms.Resize(size=input_resolution),
transforms.CenterCrop(size=input_resolution),
transforms.Compose(FUNDUS_TRANSFORMS),
transforms.ToTensor()])
elif fundus_augment == 'trivialaugment':
fundus_train_transform = transforms.Compose([transforms.Resize(size=input_resolution),
transforms.CenterCrop(size=input_resolution),
fundus_trivial_augment,
transforms.ToTensor()])
else:
raise NotImplementedError
fundus_test_transform = transforms.Compose([transforms.Resize(size=input_resolution),
transforms.CenterCrop(size=input_resolution),
transforms.ToTensor()])
return fundus_train_transform, fundus_test_transform
def load_thickness_transforms(thickness_augment, input_resolution):
# for thickness
if thickness_augment == 'identity':
thickness_train_transform = transforms.Compose([transforms.ToTensor(),
transforms.Resize(size=input_resolution)])
elif thickness_augment == 'rotation':
thickness_train_transform = transforms.Compose([transforms.ToTensor(),
transforms.Resize(size=input_resolution),
rotation])
elif thickness_augment == 'shift':
thickness_train_transform = transforms.Compose([transforms.ToTensor(),
transforms.Resize(size=input_resolution),
shift])
elif thickness_augment == 'scale':
thickness_train_transform = transforms.Compose([transforms.ToTensor(),
transforms.Resize(size=input_resolution),
scale])
elif thickness_augment == 'all':
thickness_train_transform = transforms.Compose([transforms.ToTensor(),
transforms.Resize(size=input_resolution),
transforms.Compose(THICKNESS_TRANSFORMS)])
elif thickness_augment == 'trivialaugment':
thickness_train_transform = transforms.Compose([transforms.ToTensor(),
transforms.Resize(size=input_resolution),
thickness_trivial_augment])
else:
raise NotImplementedError
thickness_test_transform = transforms.Compose([transforms.ToTensor(),
transforms.Resize(size=input_resolution)])
return thickness_train_transform, thickness_test_transform