-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.py
147 lines (127 loc) · 4.37 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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import numpy as np
import pytorch_lightning as pl
import lightly
from loss import BarlowTwinsLoss
from utils import knn_predict, BenchmarkModule
num_workers = 8
max_epochs = 800
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
device = 'cuda' if gpus else 'cpu'
# 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'],
)
])
dataset_train_ssl = lightly.data.LightlyDataset.from_torch_dataset(
torchvision.datasets.CIFAR10(
root='data',
train=True,
download=True))
dataset_train_kNN = lightly.data.LightlyDataset.from_torch_dataset(torchvision.datasets.CIFAR10(
root='data',
train=True,
transform=test_transforms,
download=True))
dataset_test = lightly.data.LightlyDataset.from_torch_dataset(torchvision.datasets.CIFAR10(
root='data',
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
)
class BartonTwins(BenchmarkModule):
def __init__(self, dataloader_kNN, gpus, classes, knn_k, knn_t):
super().__init__(dataloader_kNN, gpus, classes, knn_k, knn_t)
# 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 simsiam model based on ResNet
# note that bartontwins has the same architecture
self.resnet_simsiam = \
lightly.models.SimSiam(self.backbone, num_ftrs=512, num_mlp_layers=3)
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 * 1e-3
# update params
optimizer.step()
optimizer.zero_grad()
def configure_optimizers(self):
optim = torch.optim.SGD(self.resnet_simsiam.parameters(), lr=1e-3,
momentum=0.9, weight_decay=5e-4)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optim, max_epochs)
return [optim], [scheduler]
model = BartonTwins(dataloader_train_kNN, gpus=gpus, classes=classes, knn_k=knn_k, knn_t=knn_t)
trainer = pl.Trainer(max_epochs=max_epochs, gpus=gpus,
progress_bar_refresh_rate=100)
trainer.fit(
model,
train_dataloader=dataloader_train_ssl,
val_dataloaders=dataloader_test
)
print(f'Highest test accuracy: {model.max_accuracy:.4f}')