-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollate.py
237 lines (212 loc) · 8.13 KB
/
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
""" Collate Functions """
# Adopted from lightly.
# Copyright (c) 2020. Lightly AG and its affiliates.
# All Rights Reserved
import torch
import torch.nn as nn
from typing import Dict, List, Optional, Tuple, Union
from PIL import Image
import torchvision
import torchvision.transforms as T
from lightly.transforms import GaussianBlur
from lightly.transforms import Jigsaw
from lightly.transforms import RandomRotate
from lightly.transforms import RandomSolarization
imagenet_normalize = {
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225]
}
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([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.
cj_prob:
Probability that color jitter is applied.
cj_bright:
How much to jitter brightness.
cj_contrast:
How much to jitter constrast.
cj_sat:
How much to jitter saturation.
cj_hue:
How much to jitter hue.
min_scale:
Minimum size of the randomized crop relative to the input_size.
random_gray_scale:
Probability of conversion to grayscale.
gaussian_blur:
Probability of Gaussian blur.
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.
rr_prob:
Probability that random (+90 degree) rotation is applied.
normalize:
Dictionary with 'mean' and 'std' for torchvision.transforms.Normalize.
"""
def __init__(self,
input_size: int = 64,
cj_prob: float = 0.8,
cj_bright: float = 0.7,
cj_contrast: float = 0.7,
cj_sat: float = 0.7,
cj_hue: float = 0.2,
min_scale: float = 0.15,
random_gray_scale: float = 0.2,
gaussian_blur: float = 0.5,
kernel_size: float = 0.1,
vf_prob: float = 0.0,
hf_prob: float = 0.5,
rr_prob: float = 0.0,
normalize: dict = imagenet_normalize):
if isinstance(input_size, tuple):
input_size_ = max(input_size)
else:
input_size_ = input_size
color_jitter = T.ColorJitter(
cj_bright, cj_contrast, cj_sat, cj_hue
)
transform = [
T.RandomResizedCrop(size=input_size,
scale=(min_scale, 1.0)),
RandomRotate(prob=rr_prob),
T.RandomHorizontalFlip(p=hf_prob),
T.RandomVerticalFlip(p=vf_prob),
T.RandomApply([color_jitter], p=cj_prob),
T.RandomGrayscale(p=random_gray_scale),
GaussianBlur(
kernel_size=kernel_size * input_size_,
prob=gaussian_blur),
T.ToTensor()
]
if normalize:
transform += [
T.Normalize(
mean=normalize['mean'],
std=normalize['std'])
]
transform = T.Compose(transform)
super(ImageCollateFunction, self).__init__(transform)
class SimCLRCollateFunction(ImageCollateFunction):
"""Implements the transformations for SimCLR.
Attributes:
input_size:
Size of the input image in pixels.
cj_prob:
Probability that color jitter is applied.
cj_strength:
Strength of the color jitter.
min_scale:
Minimum size of the randomized crop relative to the input_size.
random_gray_scale:
Probability of conversion to grayscale.
gaussian_blur:
Probability of Gaussian blur.
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.
rr_prob:
Probability that random (+90 degree) rotation is applied.
normalize:
Dictionary with 'mean' and 'std' for torchvision.transforms.Normalize.
Examples:
>>> # SimCLR for ImageNet
>>> collate_fn = SimCLRCollateFunction()
>>>
>>> # SimCLR for CIFAR-10
>>> collate_fn = SimCLRCollateFunction(
>>> input_size=32,
>>> gaussian_blur=0.,
>>> )
"""
def __init__(self,
input_size: int = 224,
cj_prob: float = 0.8,
cj_strength: float = 0.5,
min_scale: float = 0.08,
random_gray_scale: float = 0.2,
gaussian_blur: float = 0.5,
kernel_size: float = 0.1,
vf_prob: float = 0.0,
hf_prob: float = 0.5,
rr_prob: float = 0.0,
normalize: dict = imagenet_normalize):
super(SimCLRCollateFunction, self).__init__(
input_size=input_size,
cj_prob=cj_prob,
cj_bright=cj_strength * 0.8,
cj_contrast=cj_strength * 0.8,
cj_sat=cj_strength * 0.8,
cj_hue=cj_strength * 0.2,
min_scale=min_scale,
random_gray_scale=random_gray_scale,
gaussian_blur=gaussian_blur,
kernel_size=kernel_size,
vf_prob=vf_prob,
hf_prob=hf_prob,
rr_prob=rr_prob,
normalize=normalize,
)