-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
321 lines (263 loc) · 11 KB
/
models.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
'''
lightly MoCo model
source: https://docs.lightly.ai/tutorials/package/tutorial_moco_memory_bank.html
'''
import pytorch_lightning as pl
import lightly
import torch
from torch import nn
from utils import knn_predict, BenchmarkModule
class MocoModel(pl.LightningModule):
def __init__(self,
backbone_type='resnet-18',
max_epochs=100,
memory_bank_size=4096,
num_ftrs=512,
batch_shuffle=True,
lr=6e-2,
momentum=0.9,
weight_decay=5e-4,
loss_temperature=0.1
):
super().__init__()
self.lr = lr
self.weight_decay = weight_decay
self.momentum = momentum
self.max_epochs = max_epochs
# create a ResNet backbone and remove the classification head
resnet = lightly.models.ResNetGenerator(backbone_type, 1, num_splits=8)
backbone = nn.Sequential(
*list(resnet.children())[:-1],
nn.AdaptiveAvgPool2d(1),
)
# create a moco based on ResNet
self.resnet_moco = lightly.models.MoCo(backbone, num_ftrs=num_ftrs, m=0.99, batch_shuffle=batch_shuffle)
# create our loss with the optional memory bank
self.criterion = lightly.loss.NTXentLoss(
temperature=loss_temperature,
memory_bank_size=memory_bank_size)
def forward(self, x):
self.resnet_moco(x)
# We provide a helper method to log weights in tensorboard
# which is useful for debugging.
def custom_histogram_weights(self):
for name, params in self.named_parameters():
self.logger.experiment.add_histogram(
name, params, self.current_epoch)
def training_step(self, batch, batch_idx):
(x0, x1), _, _ = batch
y0, y1 = self.resnet_moco(x0, x1)
loss = self.criterion(y0, y1)
self.log('train_loss_ssl', loss)
self.log('train_moco_lr',self.scheduler.optimizer.param_groups[0]['lr'])
return loss
def training_epoch_end(self, outputs):
self.custom_histogram_weights()
def configure_optimizers(self):
optim = torch.optim.SGD(self.resnet_moco.parameters(),
lr=self.lr,
momentum=self.momentum,
weight_decay=self.weight_decay
)
self.scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optim, self.max_epochs)
return [optim], [self.scheduler]
class BartonTwins(BenchmarkModule):
'''
adopted from:
https://github.com/IgorSusmelj/barlowtwins
'''
def __init__(self, dataloader_kNN,
gpus,
classes=10,
knn_k=200,
knn_t=0.1,
max_epochs=100,
backbone_type='resnet-18',
num_ftrs=512,
num_mlp_layers=3,
momentum=0.9,
weight_decay=5e-4,
lr=1e-3
):
super().__init__(dataloader_kNN, gpus, classes, knn_k, knn_t)
self.lr = lr
self.weight_decay = weight_decay
self.momentum = momentum
self.max_epochs = max_epochs
# create a ResNet backbone and remove the classification head
resnet = lightly.models.ResNetGenerator(backbone_type)
self.backbone = nn.Sequential(
*list(resnet.children())[:-1],
nn.AdaptiveAvgPool2d(1),
)
# create a simsiam model based on ResNet
# note that bartontwins has the same architecture
self.resnet_simsiam = lightly.models.SimSiam(self.backbone,
num_ftrs=num_ftrs,
num_mlp_layers=num_mlp_layers)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.criterion = BarlowTwinsLoss(device=device)
def forward(self, x):
self.resnet_simsiam(x)
def training_step(self, batch, batch_idx):
(x0, x1), _, _ = batch
x0, x1 = self.resnet_simsiam(x0, x1)
# our simsiam model returns both (features + projection head)
z_a, _ = x0
z_b, _ = x1
loss = self.criterion(z_a, z_b)
self.log('train_loss_ssl', loss)
return loss
# learning rate warm-up
def optimizer_steps(self,
epoch=None,
batch_idx=None,
optimizer=None,
optimizer_idx=None,
optimizer_closure=None,
on_tpu=None,
using_native_amp=None,
using_lbfgs=None):
# 120 steps ~ 1 epoch
if self.trainer.global_step < 1000:
lr_scale = min(1., float(self.trainer.global_step + 1) / 1000.)
for pg in optimizer.param_groups:
pg['lr'] = lr_scale * self.lr
# update params
optimizer.step()
optimizer.zero_grad()
def configure_optimizers(self):
optim = torch.optim.SGD(self.resnet_simsiam.parameters(),
lr=self.lr,
momentum=self.momentum,
weight_decay=self.weight_decay)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optim, self.max_epochs)
return [optim], [scheduler]
class BarlowTwinsLoss(torch.nn.Module):
def __init__(self, device, lambda_param=5e-3):
super(BarlowTwinsLoss, self).__init__()
self.lambda_param = lambda_param
self.device = device
def forward(self, z_a: torch.Tensor, z_b: torch.Tensor):
# normalize repr. along the batch dimension
z_a_norm = (z_a - z_a.mean(0)) / z_a.std(0) # NxD
z_b_norm = (z_b - z_b.mean(0)) / z_b.std(0) # NxD
N = z_a.size(0)
D = z_a.size(1)
# cross-correlation matrix
c = torch.mm(z_a_norm.T, z_b_norm) / N # DxD
# loss
c_diff = (c - torch.eye(D,device=self.device)).pow(2) # DxD
# multiply off-diagonal elems of c_diff by lambda
c_diff[~torch.eye(D, dtype=bool)] *= self.lambda_param
loss = c_diff.sum()
return loss
class SimpleResnet(pl.LightningModule):
def __init__(self, backbone_type='resnet-18'):
super().__init__()
# create a ResNet backbone and remove the classification head
self.resnet = lightly.models.ResNetGenerator(backbone_type, 1, num_splits=8)
self.backbone = nn.Sequential(
*list(self.resnet.children())[:-1],
nn.AdaptiveAvgPool2d(1),
)
class Classifier(pl.LightningModule):
def __init__(self, model, lr=30., max_epochs=100, freeze_backbone=True):
super().__init__()
self.lr = lr
self.max_epochs = max_epochs
# create a moco based on ResNet
self.resnet_ssl = model
if freeze_backbone==True:
# freeze the layers of moco
for p in self.resnet_ssl.parameters(): # reset requires_grad
p.requires_grad = False
# we create a linear layer for our downstream classification
# model
self.fc = nn.Linear(512, 10)
self.accuracy = pl.metrics.Accuracy()
def forward(self, x):
with torch.no_grad():
y_hat = self.resnet_ssl.backbone(x).squeeze()
y_hat = nn.functional.normalize(y_hat, dim=1)
y_hat = self.fc(y_hat)
return y_hat
# We provide a helper method to log weights in tensorboard
# which is useful for debugging.
def custom_histogram_weights(self):
for name, params in self.named_parameters():
self.logger.experiment.add_histogram(
name, params, self.current_epoch)
def training_step(self, batch, batch_idx):
x, y, _ = batch
y_hat = self.forward(x)
loss = nn.functional.cross_entropy(y_hat, y)
self.log('train_loss_fc', loss)
self.log('train_fc_lr',self.scheduler.optimizer.param_groups[0]['lr'])
return loss
def training_epoch_end(self, outputs):
self.custom_histogram_weights()
def validation_step(self, batch, batch_idx):
x, y, _ = batch
y_hat = self.forward(x)
y_hat = torch.nn.functional.softmax(y_hat, dim=1)
self.accuracy(y_hat, y)
self.log('val_acc', self.accuracy.compute(),
on_epoch=True, prog_bar=True)
def configure_optimizers(self):
optim = torch.optim.SGD(self.fc.parameters(), lr=self.lr)
self.scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optim, self.max_epochs)
return [optim], [self.scheduler]
class Classifier2(pl.LightningModule):
def __init__(self, model, lr=0.1, max_epochs=100, weight_decay=0.0001, momentum=0.9, freeze_backbone=True):
super().__init__()
self.lr = lr
self.max_epochs = max_epochs
self.weight_decay=weight_decay
self.momentum=momentum
self.last_acc = 0.0
self.model=model
if freeze_backbone==True:
for p in model.parameters():
p.requires_grad = False
# we create a linear layer for our downstream classification
# model
self.fc = nn.Linear(512, 10)
self.accuracy = pl.metrics.Accuracy()
def forward(self, x):
with torch.no_grad():
y_hat = self.model.backbone(x).squeeze()
y_hat = nn.functional.normalize(y_hat, dim=1)
y_hat = self.fc(y_hat)
return y_hat
# We provide a helper method to log weights in tensorboard
# which is useful for debugging.
def custom_histogram_weights(self):
for name, params in self.named_parameters():
self.logger.experiment.add_histogram(
name, params, self.current_epoch)
def training_step(self, batch, batch_idx):
x, y, _ = batch
y_hat = self.forward(x)
loss = nn.functional.cross_entropy(y_hat, y)
self.log('train_loss_fc', loss)
self.log('train_fc_lr',self.scheduler.optimizer.param_groups[0]['lr'])
return loss
def training_epoch_end(self, outputs):
self.custom_histogram_weights()
def validation_step(self, batch, batch_idx):
x, y, _ = batch
y_hat = self.forward(x)
y_hat = torch.nn.functional.softmax(y_hat, dim=1)
self.accuracy(y_hat, y)
acc = self.accuracy.compute()
self.log('val_acc', acc,
on_epoch=True, prog_bar=True)
self.last_acc=acc
def configure_optimizers(self):
optim = torch.optim.SGD(self.fc.parameters(),
lr=self.lr,
weight_decay=self.weight_decay,
momentum=self.momentum)
self.scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optim, self.max_epochs)
return [optim], [self.scheduler]