This repository has been archived by the owner on Aug 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
256 lines (228 loc) · 9.24 KB
/
main.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import pytorch_lightning as pl
import lightly
from source.nn_memory_bank import NNmemoryBankModule
from nnclr_model import NNCLR
num_workers = 12
max_epochs = 200
knn_k = 200
knn_t = 0.1
classes = 10
batch_size = 512
seed = 1
pl.seed_everything(seed)
# use a GPU if available
gpus = 1 if torch.cuda.is_available() else 0
# Use SimCLR augmentations, additionally, disable blur
collate_fn = lightly.data.SimCLRCollateFunction(
input_size=32,
gaussian_blur=0.,
)
# No additional augmentations for the test set
test_transforms = torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
mean=lightly.data.collate.imagenet_normalize['mean'],
std=lightly.data.collate.imagenet_normalize['std'],
)
])
root_dir = '/_unprotected/datasets/stl10'
root_dir = '/datasets/cifar10'
base_torchvision_dataset = torchvision.datasets.CIFAR10
dataset_train_ssl = lightly.data.LightlyDataset.from_torch_dataset(
base_torchvision_dataset(
root=root_dir,
train=True,
download=True))
dataset_train_kNN = lightly.data.LightlyDataset.from_torch_dataset(base_torchvision_dataset(
root=root_dir,
train=True,
transform=test_transforms,
download=True))
dataset_test = lightly.data.LightlyDataset.from_torch_dataset(base_torchvision_dataset(
root=root_dir,
train=False,
transform=test_transforms,
download=True))
dataloader_train_ssl = torch.utils.data.DataLoader(
dataset_train_ssl,
batch_size=batch_size,
shuffle=True,
collate_fn=collate_fn,
drop_last=True,
num_workers=num_workers
)
dataloader_train_kNN = torch.utils.data.DataLoader(
dataset_train_kNN,
batch_size=batch_size,
shuffle=False,
drop_last=False,
num_workers=num_workers
)
dataloader_test = torch.utils.data.DataLoader(
dataset_test,
batch_size=batch_size,
shuffle=False,
drop_last=False,
num_workers=num_workers
)
# code for kNN prediction from here:
# https://colab.research.google.com/github/facebookresearch/moco/blob/colab-notebook/colab/moco_cifar10_demo.ipynb
def knn_predict(feature, feature_bank, feature_labels, classes: int, knn_k: int, knn_t: float):
"""Helper method to run kNN predictions on features based on a feature bank
Args:
feature: Tensor of shape [N, D] consisting of N D-dimensional features
feature_bank: Tensor of a database of features used for kNN
feature_labels: Labels for the features in our feature_bank
classes: Number of classes (e.g. 10 for CIFAR-10)
knn_k: Number of k neighbors used for kNN
knn_t:
"""
# compute cos similarity between each feature vector and feature bank ---> [B, N]
sim_matrix = torch.mm(feature, feature_bank)
# [B, K]
sim_weight, sim_indices = sim_matrix.topk(k=knn_k, dim=-1)
# [B, K]
sim_labels = torch.gather(feature_labels.expand(feature.size(0), -1), dim=-1, index=sim_indices)
# we do a reweighting of the similarities
sim_weight = (sim_weight / knn_t).exp()
# counts for each class
one_hot_label = torch.zeros(feature.size(0) * knn_k, classes, device=sim_labels.device)
# [B*K, C]
one_hot_label = one_hot_label.scatter(dim=-1, index=sim_labels.view(-1, 1), value=1.0)
# weighted score ---> [B, C]
pred_scores = torch.sum(one_hot_label.view(feature.size(0), -1, classes) * sim_weight.unsqueeze(dim=-1), dim=1)
pred_labels = pred_scores.argsort(dim=-1, descending=True)
return pred_labels
class BenchmarkModule(pl.LightningModule):
"""A PyTorch Lightning Module for automated kNN callback
At the end of every training epoch we create a feature bank by inferencing
the backbone on the dataloader passed to the module.
At every validation step we predict features on the validation data.
After all predictions on validation data (validation_epoch_end) we evaluate
the predictions on a kNN classifier on the validation data using the
feature_bank features from the train data.
We can access the highest accuracy during a kNN prediction using the
max_accuracy attribute.
"""
def __init__(self, dataloader_kNN):
super().__init__()
self.backbone = nn.Module()
self.max_accuracy = 0.0
self.dataloader_kNN = dataloader_kNN
def training_epoch_end(self, outputs):
# update feature bank at the end of each training epoch
self.backbone.eval()
self.feature_bank = []
self.targets_bank = []
with torch.no_grad():
for data in self.dataloader_kNN:
img, target, _ = data
if gpus > 0:
img = img.cuda()
target = target.cuda()
feature = self.backbone(img).squeeze()
feature = F.normalize(feature, dim=1)
self.feature_bank.append(feature)
self.targets_bank.append(target)
self.feature_bank = torch.cat(self.feature_bank, dim=0).t().contiguous()
self.targets_bank = torch.cat(self.targets_bank, dim=0).t().contiguous()
self.backbone.train()
def validation_step(self, batch, batch_idx):
# we can only do kNN predictions once we have a feature bank
if hasattr(self, 'feature_bank') and hasattr(self, 'targets_bank'):
images, targets, _ = batch
feature = self.backbone(images).squeeze()
feature = F.normalize(feature, dim=1)
pred_labels = knn_predict(feature, self.feature_bank, self.targets_bank, classes, knn_k, knn_t)
num = images.size(0)
top1 = (pred_labels[:, 0] == targets).float().sum().item()
return (num, top1)
def validation_epoch_end(self, outputs):
if outputs:
total_num = 0
total_top1 = 0.
for (num, top1) in outputs:
total_num += num
total_top1 += top1
acc = float(total_top1 / total_num)
if acc > self.max_accuracy:
self.max_accuracy = acc
self.log('kNN_accuracy', acc * 100.0, prog_bar=True)
class SimSiamModel(BenchmarkModule):
def __init__(self, dataloader_kNN):
super().__init__(dataloader_kNN)
# create a ResNet backbone and remove the classification head
resnet = torchvision.models.resnet18()
self.backbone = nn.Sequential(
*list(resnet.children())[:-1],
nn.AdaptiveAvgPool2d(1),
)
# create a simsiam model based on ResNet
self.resnet_simsiam = \
lightly.models.SimSiam(self.backbone, num_ftrs=512, num_mlp_layers=2)
loss_function = lightly.loss.SymNegCosineSimilarityLoss()
self.criterion = loss_function
self.nn_replacer = NNmemoryBankModule(size=2 ** 16)
def forward(self, x):
self.resnet_simsiam(x)
def training_step(self, batch, batch_idx):
(x1, x2), _, _ = batch
out1, out2 = self.resnet_simsiam(x1, x2)
z0, p0 = out1
z1, p1 = out2
z0 = self.nn_replacer(z0.detach(), update=False)
z1 = self.nn_replacer(z1.detach(), update=True)
self.criterion((z0, p0), (z1, p1))
self.log('train_loss_ssl', loss)
return loss
def configure_optimizers(self):
optim = torch.optim.SGD(self.resnet_simsiam.parameters(), lr=6e-2,
momentum=0.9, weight_decay=5e-4)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optim, max_epochs)
return [optim], [scheduler]
class NNCLRModel(BenchmarkModule):
def __init__(self, dataloader_kNN):
super().__init__(dataloader_kNN)
# create a ResNet backbone and remove the classification head
resnet = lightly.models.ResNetGenerator('resnet-18')
self.backbone = nn.Sequential(
*list(resnet.children())[:-1],
nn.AdaptiveAvgPool2d(1),
)
# create a nnclr model based on ResNet
self.resnet_nnclr = \
NNCLR(self.backbone,
num_ftrs=512,
num_mlp_layers=2,
proj_hidden_dim=2048,
pred_hidden_dim=512)
self.criterion = lightly.loss.NTXentLoss(temperature=0.1)
self.nn_replacer = NNmemoryBankModule(size=2 ** 16)
def forward(self, x):
self.resnet_nnclr(x)
def training_step(self, batch, batch_idx):
(x0, x1), _, _ = batch
(z0, p0), (z1, p1) = self.resnet_nnclr(x0, x1)
z0 = self.nn_replacer(z0.detach(), update=False)
z1 = self.nn_replacer(z1.detach(), update=True)
loss = 0.5 * (self.criterion(z0, p1) + self.criterion(z1, p0))
self.log('train_loss_ssl', loss)
return loss
def configure_optimizers(self):
optim = torch.optim.SGD(self.resnet_nnclr.parameters(), lr=6e-2,
momentum=0.9, weight_decay=5e-4)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optim, max_epochs)
return [optim], [scheduler]
model = NNCLRModel(dataloader_train_kNN)
trainer = pl.Trainer(max_epochs=max_epochs, gpus=gpus,
progress_bar_refresh_rate=200)
trainer.fit(
model,
train_dataloader=dataloader_train_ssl,
val_dataloaders=dataloader_test
)
print(f'Highest test accuracy: {model.max_accuracy:.4f}')