-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrats_collate.py
275 lines (232 loc) · 8.48 KB
/
brats_collate.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
""" Collate Functions """
from typing import List, Tuple, Union
import numpy as np
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as T
from utils import ZeroOneNormalization
class BaseCollateFunction(nn.Module):
"""Base class for other collate implementations.
Takes a batch of images as input and transforms each image into two
different augmentations with the help of random transforms. The images are
then concatenated such that the output batch is exactly twice the length
of the input batch.
Attributes:
transform:
A set of torchvision transforms which are randomly applied to
each image.
"""
def __init__(self, transform: torchvision.transforms.Compose):
super(BaseCollateFunction, self).__init__()
self.transform = transform
def forward(self, batch: List[tuple]):
"""Turns a batch of tuples into a tuple of batches.
Args:
batch:
A batch of tuples of images, labels, and filenames which
is automatically provided if the dataloader is built from
a LightlyDataset.
Returns:
A tuple of images, labels, and filenames. The images consist of
two batches corresponding to the two transformations of the
input images.
Examples:
>>> # define a random transformation and the collate function
>>> transform = ... # some random augmentations
>>> collate_fn = BaseCollateFunction(transform)
>>>
>>> # input is a batch of tuples (here, batch_size = 1)
>>> input = [(img, 0, 'my-image.png')]
>>> output = collate_fn(input)
>>>
>>> # output consists of two random transforms of the images,
>>> # the labels, and the filenames in the batch
>>> (img_t0, img_t1), label, filename = output
"""
batch_size = len(batch)
# list of transformed images
transforms = [
self.transform(batch[i % batch_size][0]).unsqueeze_(0)
for i in range(2 * batch_size)
]
# list of labels
labels = torch.LongTensor(np.array([item[1] for item in batch]))
# list of filenames
fnames = [item[2] for item in batch]
# tuple of transforms
transforms = (
torch.cat(transforms[:batch_size], 0),
torch.cat(transforms[batch_size:], 0),
)
return transforms, labels, fnames
class ImageCollateFunction(BaseCollateFunction):
"""Implementation of a collate function for images.
This is an implementation of the BaseCollateFunction with a concrete
set of transforms.
The set of transforms is inspired by the SimCLR paper as it has shown
to produce powerful embeddings.
Attributes:
input_size:
Size of the input image in pixels.
min_scale:
Minimum size of the randomized crop relative to the input_size.
kernel_size:
Sigma of gaussian blur is kernel_size * input_size.
vf_prob:
Probability that vertical flip is applied.
hf_prob:
Probability that horizontal flip is applied.
"""
def __init__(
self,
input_size: int = 64,
min_scale: float = 0.15,
kernel_size: float = 0.1,
vf_prob: float = 0.0,
hf_prob: float = 0.5,
):
if isinstance(input_size, tuple):
input_size_ = max(input_size)
else:
input_size_ = input_size
assert 0.0 < kernel_size < 1.0
kernel_size = int(kernel_size * input_size_)
if kernel_size % 2 == 0:
kernel_size += 1
transform = T.Compose(
[
T.RandomResizedCrop(
size=input_size, scale=(min_scale, 1.0), antialias=True
),
T.RandomHorizontalFlip(p=hf_prob),
T.RandomVerticalFlip(p=vf_prob),
T.GaussianBlur(kernel_size=kernel_size),
ZeroOneNormalization(),
]
)
super(ImageCollateFunction, self).__init__(transform)
class SimCLRCollateFunction(ImageCollateFunction):
"""Implements the transformations for SimCLR.
Attributes:
input_size:
Size of the input image in pixels.
min_scale:
Minimum size of the randomized crop relative to the input_size.
vf_prob:
Probability that vertical flip is applied.
hf_prob:
Probability that horizontal flip is applied.
kernel_size:
Sigma of gaussian blur is kernel_size * input_size.
"""
def __init__(
self,
input_size: int = 224,
min_scale: float = 0.08,
vf_prob: float = 0.0,
hf_prob: float = 0.5,
kernel_size: float = 0.1,
):
super(SimCLRCollateFunction, self).__init__(
input_size=input_size,
min_scale=min_scale,
vf_prob=vf_prob,
hf_prob=hf_prob,
kernel_size=kernel_size,
)
class MoCoCollateFunction(ImageCollateFunction):
"""Implements the transformations for MoCo v1.
For MoCo v2, simply use the SimCLR settings.
Attributes:
input_size:
Size of the input image in pixels.
min_scale:
Minimum size of the randomized crop relative to the input_size.
kernel_size:
Sigma of gaussian blur is kernel_size * input_size.
vf_prob:
Probability that vertical flip is applied.
hf_prob:
Probability that horizontal flip is applied.
Examples:
>>> # MoCo v1 for ImageNet
>>> collate_fn = MoCoCollateFunction()
>>>
>>> # MoCo v1 for CIFAR-10
>>> collate_fn = MoCoCollateFunction(
>>> input_size=32,
>>> )
"""
def __init__(
self,
input_size: int = 224,
min_scale: float = 0.2,
kernel_size: float = 0.1,
vf_prob: float = 0.0,
hf_prob: float = 0.5,
):
super(MoCoCollateFunction, self).__init__(
input_size=input_size,
min_scale=min_scale,
kernel_size=kernel_size,
vf_prob=vf_prob,
hf_prob=hf_prob,
)
class MultiViewCollateFunction(nn.Module):
"""Generates multiple views for each image in the batch.
Attributes:
transforms:
List of transformation functions. Each function is used to generate
one view of the back.
"""
def __init__(self, transforms: List[torchvision.transforms.Compose]):
super().__init__()
self.transforms = transforms
def forward(self, batch: List[tuple]):
"""Turns a batch of tuples into a tuple of batches.
Args:
batch:
The input batch.
Returns:
A (views, labels, fnames) tuple where views is a list of tensors
with each tensor containing one view of the batch.
"""
views = []
for transform in self.transforms:
view = torch.stack([transform(img) for img, _, _ in batch])
views.append(view)
# list of labels
labels = torch.LongTensor(
np.concatenate([label for _, label, _ in batch], axis=0)
)
# list of filenames
fnames = [fname for _, _, fname in batch]
return views, labels, fnames
class MAECollateFunction(MultiViewCollateFunction):
"""Implements the view augmentation for MAE [0].
- [0]: Masked Autoencoder, 2021, https://arxiv.org/abs/2111.06377
Attributes:
input_size:
Size of the input image in pixels.
min_scale:
Minimum size of the randomized crop relative to the input_size.
normalize:
Dictionary with 'mean' and 'std' for torchvision.transforms.Normalize.
"""
def __init__(
self,
input_size: Union[int, Tuple[int, int]] = 224,
min_scale: float = 0.2,
):
transforms = [
T.RandomResizedCrop(
input_size, scale=(min_scale, 1.0), interpolation=3, antialias=True
), # 3 is bicubic # type: ignore
T.RandomHorizontalFlip(),
]
super().__init__([T.Compose(transforms)])
def forward(self, batch: List[tuple]):
views, labels, fnames = super().forward(batch)
# Return only first view as MAE needs only a single view per image.
return views[0], labels, fnames