/
simsiam_module.py
436 lines (353 loc) · 14.6 KB
/
simsiam_module.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import math
from argparse import ArgumentParser
from typing import Callable, Optional
import numpy as np
import pytorch_lightning as pl
import torch
from pytorch_lightning import seed_everything
from pytorch_lightning.utilities import AMPType
from torch.nn import functional as F
from torch.optim.optimizer import Optimizer
from pl_bolts.models.self_supervised.resnets import resnet18, resnet50
from pl_bolts.models.self_supervised.simsiam.models import SiameseArm
from pl_bolts.optimizers.lars_scheduling import LARSWrapper
from pl_bolts.transforms.dataset_normalizations import (
cifar10_normalization,
imagenet_normalization,
stl10_normalization,
)
class SimSiam(pl.LightningModule):
"""
PyTorch Lightning implementation of `Exploring Simple Siamese Representation Learning (SimSiam)
<https://arxiv.org/pdf/2011.10566v1.pdf>`_
Paper authors: Xinlei Chen, Kaiming He.
Model implemented by:
- `Zvi Lapp <https://github.com/zlapp>`_
.. warning:: Work in progress. This implementation is still being verified.
TODOs:
- verify on CIFAR-10
- verify on STL-10
- pre-train on imagenet
Example::
model = SimSiam()
dm = CIFAR10DataModule(num_workers=0)
dm.train_transforms = SimCLRTrainDataTransform(32)
dm.val_transforms = SimCLREvalDataTransform(32)
trainer = pl.Trainer()
trainer.fit(model, datamodule=dm)
Train::
trainer = Trainer()
trainer.fit(model)
CLI command::
# cifar10
python simsiam_module.py --gpus 1
# imagenet
python simsiam_module.py
--gpus 8
--dataset imagenet2012
--data_dir /path/to/imagenet/
--meta_dir /path/to/folder/with/meta.bin/
--batch_size 32
"""
def __init__(
self,
gpus: int,
num_samples: int,
batch_size: int,
dataset: str,
num_nodes: int = 1,
arch: str = 'resnet50',
hidden_mlp: int = 2048,
feat_dim: int = 128,
warmup_epochs: int = 10,
max_epochs: int = 100,
temperature: float = 0.1,
first_conv: bool = True,
maxpool1: bool = True,
optimizer: str = 'adam',
lars_wrapper: bool = True,
exclude_bn_bias: bool = False,
start_lr: float = 0.,
learning_rate: float = 1e-3,
final_lr: float = 0.,
weight_decay: float = 1e-6,
**kwargs
):
"""
Args:
datamodule: The datamodule
learning_rate: the learning rate
weight_decay: optimizer weight decay
input_height: image input height
batch_size: the batch size
num_workers: number of workers
warmup_epochs: num of epochs for scheduler warm up
max_epochs: max epochs for scheduler
"""
super().__init__()
self.save_hyperparameters()
self.gpus = gpus
self.num_nodes = num_nodes
self.arch = arch
self.dataset = dataset
self.num_samples = num_samples
self.batch_size = batch_size
self.hidden_mlp = hidden_mlp
self.feat_dim = feat_dim
self.first_conv = first_conv
self.maxpool1 = maxpool1
self.optim = optimizer
self.lars_wrapper = lars_wrapper
self.exclude_bn_bias = exclude_bn_bias
self.weight_decay = weight_decay
self.temperature = temperature
self.start_lr = start_lr
self.final_lr = final_lr
self.learning_rate = learning_rate
self.warmup_epochs = warmup_epochs
self.max_epochs = max_epochs
self.init_model()
# compute iters per epoch
nb_gpus = len(self.gpus) if isinstance(gpus, (list, tuple)) else self.gpus
assert isinstance(nb_gpus, int)
global_batch_size = self.num_nodes * nb_gpus * self.batch_size if nb_gpus > 0 else self.batch_size
self.train_iters_per_epoch = self.num_samples // global_batch_size
# define LR schedule
warmup_lr_schedule = np.linspace(
self.start_lr, self.learning_rate, self.train_iters_per_epoch * self.warmup_epochs
)
iters = np.arange(self.train_iters_per_epoch * (self.max_epochs - self.warmup_epochs))
cosine_lr_schedule = np.array([
self.final_lr + 0.5 * (self.learning_rate - self.final_lr) *
(1 + math.cos(math.pi * t / (self.train_iters_per_epoch * (self.max_epochs - self.warmup_epochs))))
for t in iters
])
self.lr_schedule = np.concatenate((warmup_lr_schedule, cosine_lr_schedule))
def init_model(self):
if self.arch == 'resnet18':
backbone = resnet18
elif self.arch == 'resnet50':
backbone = resnet50
encoder = backbone(first_conv=self.first_conv, maxpool1=self.maxpool1, return_all_feature_maps=False)
self.online_network = SiameseArm(
encoder, input_dim=self.hidden_mlp, hidden_size=self.hidden_mlp, output_dim=self.feat_dim
)
def forward(self, x):
y, _, _ = self.online_network(x)
return y
def cosine_similarity(self, a, b):
b = b.detach() # stop gradient of backbone + projection mlp
a = F.normalize(a, dim=-1)
b = F.normalize(b, dim=-1)
sim = -1 * (a * b).sum(-1).mean()
return sim
def training_step(self, batch, batch_idx):
(img_1, img_2, _), y = batch
# Image 1 to image 2 loss
_, z1, h1 = self.online_network(img_1)
_, z2, h2 = self.online_network(img_2)
loss = self.cosine_similarity(h1, z2) / 2 + self.cosine_similarity(h2, z1) / 2
# log results
self.log_dict({"loss": loss})
return loss
def validation_step(self, batch, batch_idx):
(img_1, img_2, _), y = batch
# Image 1 to image 2 loss
_, z1, h1 = self.online_network(img_1)
_, z2, h2 = self.online_network(img_2)
loss = self.cosine_similarity(h1, z2) / 2 + self.cosine_similarity(h2, z1) / 2
# log results
self.log_dict({"loss": loss})
return loss
def exclude_from_wt_decay(self, named_params, weight_decay, skip_list=['bias', 'bn']):
params = []
excluded_params = []
for name, param in named_params:
if not param.requires_grad:
continue
elif any(layer_name in name for layer_name in skip_list):
excluded_params.append(param)
else:
params.append(param)
return [
{
'params': params,
'weight_decay': weight_decay
},
{
'params': excluded_params,
'weight_decay': 0.
},
]
def configure_optimizers(self):
if self.exclude_bn_bias:
params = self.exclude_from_wt_decay(self.named_parameters(), weight_decay=self.weight_decay)
else:
params = self.parameters()
if self.optim == 'sgd':
optimizer = torch.optim.SGD(params, lr=self.learning_rate, momentum=0.9, weight_decay=self.weight_decay)
elif self.optim == 'adam':
optimizer = torch.optim.Adam(params, lr=self.learning_rate, weight_decay=self.weight_decay)
if self.lars_wrapper:
optimizer = LARSWrapper(
optimizer,
eta=0.001, # trust coefficient
clip=False
)
return optimizer
def optimizer_step(
self,
epoch: int,
batch_idx: int,
optimizer: Optimizer,
optimizer_idx: int,
optimizer_closure: Optional[Callable] = None,
on_tpu: bool = False,
using_native_amp: bool = False,
using_lbfgs: bool = False,
) -> None:
# warm-up + decay schedule placed here since LARSWrapper is not optimizer class
# adjust LR of optim contained within LARSWrapper
if self.lars_wrapper:
for param_group in optimizer.optim.param_groups:
param_group["lr"] = self.lr_schedule[self.trainer.global_step]
else:
for param_group in optimizer.param_groups:
param_group["lr"] = self.lr_schedule[self.trainer.global_step]
# log LR (LearningRateLogger callback doesn't work with LARSWrapper)
self.log('learning_rate', self.lr_schedule[self.trainer.global_step], on_step=True, on_epoch=False)
# from lightning
if self.trainer.amp_backend == AMPType.NATIVE:
optimizer_closure()
self.trainer.scaler.step(optimizer)
elif self.trainer.amp_backend == AMPType.APEX:
optimizer_closure()
optimizer.step()
else:
optimizer.step(closure=optimizer_closure)
@staticmethod
def add_model_specific_args(parent_parser):
parser = ArgumentParser(parents=[parent_parser], add_help=False)
# model params
parser.add_argument("--arch", default="resnet50", type=str, help="convnet architecture")
# specify flags to store false
parser.add_argument("--first_conv", action="store_false")
parser.add_argument("--maxpool1", action="store_false")
parser.add_argument("--hidden_mlp", default=2048, type=int, help="hidden layer dimension in projection head")
parser.add_argument("--feat_dim", default=128, type=int, help="feature dimension")
parser.add_argument("--online_ft", action="store_true")
parser.add_argument("--fp32", action="store_true")
# transform params
parser.add_argument("--gaussian_blur", action="store_true", help="add gaussian blur")
parser.add_argument("--jitter_strength", type=float, default=1.0, help="jitter strength")
parser.add_argument("--dataset", type=str, default="cifar10", help="stl10, cifar10")
parser.add_argument("--data_dir", type=str, default=".", help="path to download data")
# training params
parser.add_argument("--num_workers", default=8, type=int, help="num of workers per GPU")
parser.add_argument("--optimizer", default="adam", type=str, help="choose between adam/sgd")
parser.add_argument("--lars_wrapper", action="store_true", help="apple lars wrapper over optimizer used")
parser.add_argument("--exclude_bn_bias", action="store_true", help="exclude bn/bias from weight decay")
parser.add_argument("--warmup_epochs", default=10, type=int, help="number of warmup epochs")
parser.add_argument("--batch_size", default=128, type=int, help="batch size per gpu")
parser.add_argument("--temperature", default=0.1, type=float, help="temperature parameter in training loss")
parser.add_argument("--weight_decay", default=1e-6, type=float, help="weight decay")
parser.add_argument("--learning_rate", default=1e-3, type=float, help="base learning rate")
parser.add_argument("--start_lr", default=0, type=float, help="initial warmup learning rate")
parser.add_argument("--final_lr", type=float, default=1e-6, help="final learning rate")
return parser
def cli_main():
from pl_bolts.callbacks.ssl_online import SSLOnlineEvaluator
from pl_bolts.datamodules import CIFAR10DataModule, ImagenetDataModule, STL10DataModule
from pl_bolts.models.self_supervised.simclr import SimCLREvalDataTransform, SimCLRTrainDataTransform
seed_everything(1234)
parser = ArgumentParser()
# trainer args
parser = pl.Trainer.add_argparse_args(parser)
# model args
parser = SimSiam.add_model_specific_args(parser)
args = parser.parse_args()
# pick data
dm = None
# init datamodule
if args.dataset == "stl10":
dm = STL10DataModule(data_dir=args.data_dir, batch_size=args.batch_size, num_workers=args.num_workers)
dm.train_dataloader = dm.train_dataloader_mixed
dm.val_dataloader = dm.val_dataloader_mixed
args.num_samples = dm.num_unlabeled_samples
args.maxpool1 = False
args.first_conv = True
args.input_height = dm.size()[-1]
normalization = stl10_normalization()
args.gaussian_blur = True
args.jitter_strength = 1.0
elif args.dataset == "cifar10":
val_split = 5000
if args.num_nodes * args.gpus * args.batch_size > val_split:
val_split = args.num_nodes * args.gpus * args.batch_size
dm = CIFAR10DataModule(
data_dir=args.data_dir,
batch_size=args.batch_size,
num_workers=args.num_workers,
val_split=val_split,
)
args.num_samples = dm.num_samples
args.maxpool1 = False
args.first_conv = False
args.input_height = dm.size()[-1]
args.temperature = 0.5
normalization = cifar10_normalization()
args.gaussian_blur = False
args.jitter_strength = 0.5
elif args.dataset == "imagenet":
args.maxpool1 = True
args.first_conv = True
normalization = imagenet_normalization()
args.gaussian_blur = True
args.jitter_strength = 1.0
args.batch_size = 64
args.num_nodes = 8
args.gpus = 8 # per-node
args.max_epochs = 800
args.optimizer = "sgd"
args.lars_wrapper = True
args.learning_rate = 4.8
args.final_lr = 0.0048
args.start_lr = 0.3
args.online_ft = True
dm = ImagenetDataModule(data_dir=args.data_dir, batch_size=args.batch_size, num_workers=args.num_workers)
args.num_samples = dm.num_samples
args.input_height = dm.size()[-1]
else:
raise NotImplementedError("other datasets have not been implemented till now")
dm.train_transforms = SimCLRTrainDataTransform(
input_height=args.input_height,
gaussian_blur=args.gaussian_blur,
jitter_strength=args.jitter_strength,
normalize=normalization,
)
dm.val_transforms = SimCLREvalDataTransform(
input_height=args.input_height,
gaussian_blur=args.gaussian_blur,
jitter_strength=args.jitter_strength,
normalize=normalization,
)
model = SimSiam(**args.__dict__)
# finetune in real-time
online_evaluator = None
if args.online_ft:
# online eval
online_evaluator = SSLOnlineEvaluator(
drop_p=0.0,
hidden_dim=None,
z_dim=args.hidden_mlp,
num_classes=dm.num_classes,
dataset=args.dataset,
)
trainer = pl.Trainer.from_argparse_args(
args,
sync_batchnorm=True if args.gpus > 1 else False,
callbacks=[online_evaluator] if args.online_ft else None,
)
trainer.fit(model, datamodule=dm)
if __name__ == "__main__":
cli_main()