-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatasets_finetune.py
284 lines (254 loc) · 9.07 KB
/
datasets_finetune.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
276
277
278
279
280
281
282
283
284
import numpy as np
import pytorch_lightning as pl
import torch
from PIL import Image
from timm.data.auto_augment import (
augment_and_mix_transform,
auto_augment_transform,
rand_augment_transform,
)
from timm.data.random_erasing import RandomErasing
from timm.data.transforms import (
RandomResizedCropAndInterpolation,
ToNumpy,
str_to_pil_interp,
)
from torchvision import datasets, transforms
IMAGENET_MEAN_PER_CHANNEL = (0.485, 0.456, 0.406)
IMAGENET_STD_PER_CHANNEL = (0.229, 0.224, 0.225)
imagenet_train_dir_path = "path to train dir"
imagenet_val_dir_path = "path to val dir"
class create_imagenet_DataModule(pl.LightningDataModule):
def __init__(
self,
img_size,
hflip,
vflip,
eval_crop_ratio,
color_jitter,
auto_augment,
interpolation,
re_prob,
re_mode,
re_count,
batch_size: int = 64,
num_workers: int = 12,
):
super().__init__()
self.transform_train = RGBAugmentation(
is_train=True,
img_size=img_size,
hflip=hflip,
vflip=vflip,
interpolation=interpolation,
color_jitter=color_jitter,
auto_augment=auto_augment,
re_prob=re_prob,
re_mode=re_mode,
re_count=re_count,
)
self.transform_val = RGBAugmentation(
is_train=False, img_size=img_size, eval_crop_ratio=eval_crop_ratio
)
self.batch_size = batch_size
self.num_workers = num_workers
# for CIFAR dataset
# self.dataset_train = datasets.CIFAR100('/path/to/save', train=True, transform=self.transform_train, download=True)
# self.dataset_val = datasets.CIFAR100('/path/to/save', train=False, transform=self.transform_val)
# self.dataset_test = datasets.CIFAR100('/path/to/save', train=False, transform=self.transform_val)
# for ImageNet-1k dataset
self.dataset_train = datasets.ImageFolder(
imagenet_train_dir_path, transform=self.transform_train
)
self.dataset_val = datasets.ImageFolder(
imagenet_val_dir_path, transform=self.transform_val
)
self.dataset_test = datasets.ImageFolder(
imagenet_val_dir_path, transform=self.transform_val
)
def train_dataloader(self):
return torch.utils.data.DataLoader(
self.dataset_train,
batch_size=self.batch_size,
num_workers=self.num_workers,
shuffle=True,
pin_memory=False,
drop_last=True,
)
def val_dataloader(self):
return torch.utils.data.DataLoader(
self.dataset_val,
batch_size=self.batch_size,
num_workers=self.num_workers,
shuffle=False,
pin_memory=False,
drop_last=False,
)
def test_dataloader(self):
return torch.utils.data.DataLoader(
self.dataset_test,
batch_size=self.batch_size,
num_workers=self.num_workers,
shuffle=False,
pin_memory=False,
drop_last=False,
)
def predict_dataloader(self):
return torch.utils.data.DataLoader(
self.dataset_test,
batch_size=self.batch_size,
num_workers=self.num_workers,
shuffle=False,
pin_memory=False,
drop_last=False,
)
def load_DataModule(
batch_size: int = 128,
num_workers: int = 1,
img_size=224,
hflip=0.5,
vflip=0.0,
eval_crop_ratio=0.875,
color_jitter=0.3,
auto_augment=None,
interpolation="bicubic",
re_prob=0.0,
re_mode="pixel",
re_count=1,
) -> pl.LightningDataModule:
"""Load the Imagenet dataset."""
return create_imagenet_DataModule(
img_size,
hflip,
vflip,
eval_crop_ratio,
color_jitter,
auto_augment,
interpolation,
re_prob,
re_mode,
re_count,
batch_size,
num_workers,
)
class RGBAugmentation:
"""Transformations for standard RBG images"""
def __init__(
self,
is_train: bool = False,
img_size: int = 224,
hflip: float = 0.5,
vflip: float = 0.0,
eval_crop_ratio=0.875,
scale=None,
ratio=None,
color_jitter=0.0,
auto_augment=None,
interpolation="bicubic",
use_prefetcher=False,
mean=IMAGENET_MEAN_PER_CHANNEL,
std=IMAGENET_STD_PER_CHANNEL,
re_prob=0.0,
re_mode="const",
re_count=1,
re_num_splits=0,
):
self.is_train = is_train
if is_train:
scale = tuple(scale or (0.08, 1.0)) # default imagenet scale range
ratio = tuple(
ratio or (3.0 / 4.0, 4.0 / 3.0)
) # default imagenet ratio range
primary_tfl = [
RandomResizedCropAndInterpolation(
img_size, scale=scale, ratio=ratio, interpolation=interpolation
)
]
if hflip > 0.0:
primary_tfl += [transforms.RandomHorizontalFlip(p=hflip)]
if vflip > 0.0:
primary_tfl += [transforms.RandomVerticalFlip(p=vflip)]
secondary_tfl = []
if auto_augment:
print("Auto augment on")
assert isinstance(auto_augment, str)
if isinstance(img_size, (tuple, list)):
img_size_min = min(img_size)
else:
img_size_min = img_size
aa_params = dict(
translate_const=int(img_size_min * 0.45),
img_mean=tuple([min(255, round(255 * x)) for x in mean]),
)
if interpolation and interpolation != "random":
aa_params["interpolation"] = str_to_pil_interp(interpolation)
if auto_augment.startswith("rand"):
secondary_tfl += [rand_augment_transform(auto_augment, aa_params)]
elif auto_augment.startswith("augmix"):
aa_params["translate_pct"] = 0.3
secondary_tfl += [
augment_and_mix_transform(auto_augment, aa_params)
]
else:
secondary_tfl += [auto_augment_transform(auto_augment, aa_params)]
# MAE removes color jitter in fine tuning and just use random aug
# elif color_jitter is not None:
# # color jitter is enabled when not using AA
# if isinstance(color_jitter, (list, tuple)):
# # color jitter should be a 3-tuple/list if spec brightness/contrast/saturation
# # or 4 if also augmenting hue
# assert len(color_jitter) in (3, 4)
# else:
# # if it's a scalar, duplicate for brightness, contrast, and saturation, no hue
# color_jitter = (float(color_jitter),) * 3
# secondary_tfl += [transforms.ColorJitter(*color_jitter)]
final_tfl = []
if use_prefetcher:
# prefetcher and collate will handle tensor conversion and norm
final_tfl += [ToNumpy()]
else:
final_tfl += [
transforms.ToTensor(),
transforms.Normalize(
mean=torch.tensor(mean), std=torch.tensor(std)
),
]
if re_prob > 0.0:
final_tfl.append(
RandomErasing(
re_prob,
mode=re_mode,
max_count=re_count,
num_splits=re_num_splits,
device="cpu",
)
)
self.primary_transform = transforms.Compose(primary_tfl)
self.secondary_transform = transforms.Compose(secondary_tfl)
self.final_transform = transforms.Compose(final_tfl)
else:
t = []
if img_size > 224:
eval_crop_ratio = 1.0 # this setting taken from MAE paper
size = int(img_size / eval_crop_ratio)
t.append(
transforms.Resize(
size, interpolation=3
), # to maintain same ratio w.r.t. 224 images
)
t.append(transforms.CenterCrop(img_size))
t.append(transforms.ToTensor())
t.append(transforms.Normalize(mean, std))
self.transform = transforms.Compose(t)
def __call__(self, img) -> torch.Tensor:
img = Image.fromarray(np.asarray(img))
if self.is_train:
img = self.primary_transform(img)
try:
img = self.secondary_transform(img)
except Exception as e:
print("-----------secondary_transform failed-----------", e)
img = self.final_transform(img)
else:
img = self.transform(img)
return img