-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrain.py
253 lines (207 loc) · 8.44 KB
/
train.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
import os
import numpy as np
import torch
import pytorch_lightning as pl
import torchvision.datasets as dset
import torchvision.transforms as T
from absl import app, flags
from ml_collections.config_flags import config_flags
from pytorch_lightning.callbacks import LearningRateMonitor
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.loggers import WandbLogger
import wandb
from lightning.data import _DATASETS
from lightning.data import get_cc_dataset
from lightning.data import get_dataset
from models import AugModel, TransformLayer
from models import get_model, _MODELS
from lightning.systems import AugClassifier
from lightning.systems import Classifier
import utils
from utils.rand_filter import RandomFilter
from utils.color_jitter import RandomSmoothColor
from utils.diffeomorphism import Diffeo
from utils.augmix import AugMixDataset
from utils.prime import GeneralizedPRIMEModule
from utils.prime import PRIMEAugModule
from setup import setup_all, _setup
_WANDB_USERNAME = "username"
_WANDB_PROJECT = "common-corruptions"
def validate_config(cfg):
if cfg.dataset not in _DATASETS:
raise ValueError(f'Dataset {cfg.dataset} not supported!')
elif cfg.model.name not in _MODELS[cfg.dataset]:
raise ValueError(f'Model {cfg.model.name} not supported!')
assert not (cfg.use_augmix and cfg.use_prime), 'Use only one augmentation!'
if cfg.use_deepaugment:
assert 'imagenet' in cfg.dataset, 'DeepAugment only supported on ImageNet!'
if 'TMPDIR' in os.environ and 'imagenet' in cfg.dataset:
setup_all(cfg.data_dir, cfg.cc_dir)
if cfg.use_deepaugment:
_setup(cfg.data_dir, 'EDSR')
_setup(cfg.data_dir, 'CAE')
cfg.data_dir = os.path.join(os.environ['TMPDIR'], cfg.dataset)
cfg.cc_dir = os.path.join(os.environ['TMPDIR'], f'{cfg.dataset}c')
def main(_):
config = flags.FLAGS.config
validate_config(config)
wandb.init(
project=_WANDB_PROJECT, entity=_WANDB_USERNAME,
name=config.save_dir.split('/')[-2],
settings=wandb.Settings(_disable_stats=True),
)
utils.print_config(config)
wandb.config.update(config.to_dict())
pl.seed_everything(1)
# Setup train & val datasets
dataset = get_dataset(config.dataset)(
config.data_dir,
train_batch_size=config.train_batch_size,
test_batch_size=config.test_batch_size,
num_workers=config.train_num_workers,
)
transforms = [] if config.use_augmix else [T.ToTensor()]
if 'imagenet' in config.dataset:
transforms += [
T.RandomResizedCrop(224), T.RandomHorizontalFlip()
]
if not (config.use_prime or config.use_augmix):
transforms.append(T.Normalize(dataset.mean, dataset.std))
dataset.train_transform = T.Compose(transforms)
dataset.test_transform = T.Compose([
T.ToTensor(), T.Resize(256), T.CenterCrop(224),
T.Normalize(dataset.mean, dataset.std)
])
elif 'cifar' in config.dataset:
transforms += [
T.RandomCrop(32, padding=4), T.RandomHorizontalFlip()
]
if not (config.use_prime or config.use_augmix):
transforms.append(T.Normalize(dataset.mean, dataset.std))
dataset.train_transform = T.Compose(transforms)
dataset.test_transform = T.Compose([
T.ToTensor(), T.Normalize(dataset.mean, dataset.std)
])
dataset.prepare_data()
dataset.setup()
# DeepAugment
if config.use_deepaugment:
edsr = dset.ImageFolder(
os.path.join(dataset.data_dir, 'EDSR'), dataset.train_transform)
cae = dset.ImageFolder(
os.path.join(dataset.data_dir, 'CAE'), dataset.train_transform)
dataset.train_ds = torch.utils.data.ConcatDataset(
[dataset.train_ds, edsr, cae])
dataset.train_ds.transform = dataset.train_transform
# AugMix
if config.use_augmix:
preprocess = T.Compose([
T.ToTensor(), T.Normalize(dataset.mean, dataset.std)])
dataset.train_ds = AugMixDataset(
dataset=dataset.train_ds,
preprocess=preprocess,
mixture_width=config.augmix.mixture_width,
mixture_depth=config.augmix.mixture_depth,
aug_severity=config.augmix.severity,
no_jsd=config.augmix.no_jsd,
img_sz=dataset.dims[1]
)
# PRIME
if config.use_prime:
augmentations = []
if config.enable_aug.diffeo:
diffeo = Diffeo(
sT=config.diffeo.sT, rT=config.diffeo.rT,
scut=config.diffeo.scut, rcut=config.diffeo.rcut,
cutmin=config.diffeo.cutmin, cutmax=config.diffeo.cutmax,
alpha=config.diffeo.alpha, stochastic=True
)
augmentations.append(diffeo)
if config.enable_aug.color_jit:
color = RandomSmoothColor(
cut=config.color_jit.cut, T=config.color_jit.T,
freq_bandwidth=config.color_jit.max_freqs, stochastic=True
)
augmentations.append(color)
if config.enable_aug.rand_filter:
filt = RandomFilter(
kernel_size=config.rand_filter.kernel_size,
sigma=config.rand_filter.sigma, stochastic=True
)
augmentations.append(filt)
prime_module = GeneralizedPRIMEModule(
preprocess=TransformLayer(dataset.mean, dataset.std),
mixture_width=config.augmix.mixture_width,
mixture_depth=config.augmix.mixture_depth,
no_jsd=config.augmix.no_jsd, max_depth=3,
aug_module=PRIMEAugModule(augmentations),
)
# Setup model
base_model = get_model(
config.dataset, config.model.name
)(num_classes=dataset.num_classes, pretrained=config.model.pretrained)
if config.scheduler_t == "cyclic":
config.lr_schedule.steps_per_epoch = int(np.ceil(len(dataset.train_ds) / config.train_batch_size))
opt_cfg = {
'optimizer_cfg': config.optimizer,
'lr_schedule_cfg': config.lr_schedule,
'scheduler_t': config.scheduler_t,
}
if config.use_prime:
model = AugClassifier(
model=AugModel(model=base_model, aug=prime_module),
no_jsd=config.augmix.no_jsd, **opt_cfg,
)
elif config.use_augmix:
model = AugClassifier(
model=base_model, no_jsd=config.augmix.no_jsd, **opt_cfg,
)
else:
model = Classifier(model=base_model, **opt_cfg)
# PL trainer & logging
wandb_logger = WandbLogger(project=_WANDB_PROJECT, entity=_WANDB_USERNAME, log_model=False)
lr_monitor = LearningRateMonitor(logging_interval="step", log_momentum=True)
checkpoint_callback = ModelCheckpoint(
config.save_dir, filename='best',
monitor='val.acc', mode='max', save_last=True,
save_top_k=1, save_weights_only=True
)
trainer = pl.Trainer(
logger=wandb_logger,
log_every_n_steps=config.log_freq,
gpus=-1,
max_epochs=config.epochs,
gradient_clip_val=config.grad_clip,
callbacks=[lr_monitor, checkpoint_callback],
num_sanity_val_steps=0,
fast_dev_run=config.debug,
accelerator=config.accelerator,
benchmark=True
)
trainer.fit(model, dataset)
model.load_state_dict(torch.load(checkpoint_callback.best_model_path)['state_dict'])
if config.use_prime:
del model.model.aug
# Evaluation on common corruptions
dataset_c = get_cc_dataset(config.dataset)(
config.cc_dir, batch_size=config.test_batch_size,
num_workers=config.test_num_workers
)
transforms = [] if 'cifar' in config.dataset else [T.ToTensor()]
transforms.append(T.Normalize(dataset.mean, dataset.std))
dataset_c.transform = T.Compose(transforms)
dataset_c.prepare_data()
dataset_c.setup()
cc_loaders = dataset_c.test_dataloader()
keys = list(cc_loaders.keys())
avg_acc = 0.
for key in keys:
res = trainer.test(model, test_dataloaders=cc_loaders[key])
acc = res[0]["test.acc"]
wandb.run.summary["test.%s" % key] = acc
avg_acc += acc
wandb.run.summary["test_avg.acc"] = avg_acc / len(keys)
wandb.run.summary["val.acc"] = checkpoint_callback.best_model_score
if __name__ == "__main__":
config_flags.DEFINE_config_file('config')
app.run(main)