-
Notifications
You must be signed in to change notification settings - Fork 8
/
simclr.py
164 lines (135 loc) · 7.54 KB
/
simclr.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
import logging
import os
import sys
import torch
import torch.nn.functional as F
from datetime import datetime
from torch.cuda.amp import GradScaler, autocast
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm
from utils import save_config_file, accuracy, save_checkpoint
torch.manual_seed(0)
class SimCLR(object):
def __init__(self, *args, **kwargs):
self.args = kwargs['args']
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model = kwargs['model']
self.optimizer = kwargs['optimizer']
self.scheduler = kwargs['scheduler']
if self.args.train_from:
checkpoint = torch.load(os.path.join(self.args.log_dir, self.args.train_from),
map_location='cpu')
log_dir = self.args.log_dir
self.epo_0 = int(self.args.train_from[-12:-8]) 1
model_state_dict = checkpoint['model_state_dict']
for k in list(model_state_dict.keys()):
if k.startswith('module.'):
model_state_dict[k[len("module."):]] = model_state_dict[k]
del model_state_dict[k]
self.model.load_state_dict(model_state_dict)
if torch.cuda.device_count() > 1:
print('multiple gpu')
self.model = torch.nn.DataParallel(kwargs['model'])
self.model = self.model.to(self.args.device)
optimizer_state_dict = checkpoint['optimizer']
self.optimizer.load_state_dict(optimizer_state_dict)
if self.scheduler:
scheduler_state_dict = checkpoint['scheduler']
self.scheduler.load_state_dict(scheduler_state_dict)
del checkpoint
elif self.args.from_pretrained==False:
now = datetime.now()
dt_string = now.strftime("%d-%m-%Y-%H-%M-%S")
log_dir=os.path.join('results',f"{self.args.dataset_name}", f"{self.args.batch_size}_larsSimCLR", dt_string)
self.epo_0 = 0
if torch.cuda.device_count() > 1:
print('multiple gpu')
self.model = torch.nn.DataParallel(kwargs['model'])
self.model = self.model.to(self.args.device)
else:
print('from pretrained')
checkpoint = torch.load('results/imagenet/512_SimCLR/checkpoint_0050.pth.tar',
map_location=self.args.device)
model_state_dict = checkpoint['model_state_dict']
self.model.load_state_dict(model_state_dict)
now = datetime.now()
dt_string = now.strftime("%d-%m-%Y-%H-%M-%S")
log_dir=os.path.join('results',f"{self.args.dataset_name}", f"{self.args.batch_size}_adamSimCLR", dt_string)
self.epo_0 = 0
self.writer = SummaryWriter(log_dir)
logging.basicConfig(filename=os.path.join(self.writer.log_dir, 'training.log'), level=logging.DEBUG)
self.criterion = torch.nn.CrossEntropyLoss().to(self.args.device)
def info_nce_loss(self, features):
labels = torch.cat([torch.arange(self.args.batch_size) for i in range(self.args.n_views)], dim=0)
labels = (labels.unsqueeze(0) == labels.unsqueeze(1)).float()
labels = labels.to(self.args.device)
features = F.normalize(features, dim=1)
similarity_matrix = torch.matmul(features, features.T)
# assert similarity_matrix.shape == (
# self.args.n_views * self.args.batch_size, self.args.n_views * self.args.batch_size)
# assert similarity_matrix.shape == labels.shape
# discard the main diagonal from both: labels and similarities matrix
mask = torch.eye(labels.shape[0], dtype=torch.bool).to(self.args.device)
labels = labels[~mask].view(labels.shape[0], -1)
similarity_matrix = similarity_matrix[~mask].view(similarity_matrix.shape[0], -1)
# assert similarity_matrix.shape == labels.shape
# select and combine multiple positives
positives = similarity_matrix[labels.bool()].view(labels.shape[0], -1)
# select only the negatives the negatives
negatives = similarity_matrix[~labels.bool()].view(similarity_matrix.shape[0], -1)
logits = torch.cat([positives, negatives], dim=1)
labels = torch.zeros(logits.shape[0], dtype=torch.long).to(self.args.device)
logits = logits / self.args.temperature
return logits, labels
def train(self, train_loader):
scaler = GradScaler(enabled=self.args.fp16_precision)
# save config file
save_config_file(self.writer.log_dir, self.args)
logging.info(f"Start SimCLR training for {self.args.epochs} epochs.")
logging.info(f"Training with gpu: {self.args.disable_cuda}.")
for epoch_counter in range(self.epo_0, self.args.epochs):
for i, (images, _) in enumerate(tqdm(train_loader)):
images = torch.cat(images, dim=0)
images = images.to(self.args.device)
with autocast(enabled=self.args.fp16_precision):
hidden, features = self.model(images)
logits, labels = self.info_nce_loss(features)
loss = self.criterion(logits, labels)
self.optimizer.zero_grad()
scaler.scale(loss).backward()
scaler.step(self.optimizer)
self.scheduler.step()
scaler.update()
top1, top5 = accuracy(logits, labels, topk=(1, 5))
print('epoch', epoch_counter)
print('loss', loss.item())
print('acc/top1', top1[0].item())
print('acc/top5', top5[0].item())
print('model_learning_rate', self.scheduler.get_lr()[0])
self.writer.add_scalar('loss', loss, global_step=epoch_counter)
self.writer.add_scalar('acc/top1', top1[0], global_step=epoch_counter)
self.writer.add_scalar('acc/top5', top5[0], global_step=epoch_counter)
self.writer.add_scalar('learning_rate', self.scheduler.get_lr()[0], global_step=epoch_counter)
if epoch_counter%self.args.save_every_n_epochs ==0:
checkpoint_name = 'checkpoint_{:04d}.pth.tar'.format(epoch_counter)
save_checkpoint({
'batch_size': self.args.batch_size,
'epoch': self.args.epochs,
'model': self.args.model,
'model_state_dict': self.model.state_dict(),
'optimizer': self.optimizer.state_dict(),
'scheduler': self.scheduler.state_dict(),
}, is_best=False, filename=os.path.join(self.writer.log_dir, checkpoint_name))
logging.debug(f"Epoch: {epoch_counter}\tLoss: {loss}\tTop1 accuracy: {top1[0]}")
logging.info("Training has finishfed.")
# save model checkpoints
checkpoint_name = 'checkpoint_{:04d}.pth.tar'.format(self.args.epochs)
save_checkpoint({
'batch_size': self.args.batch_size,
'epoch': self.args.epochs,
'model': self.args.model,
'model_state_dict': self.model.state_dict(),
'optimizer': self.optimizer.state_dict(),
'scheduler': self.scheduler.state_dict(),
}, is_best=False, filename=os.path.join(self.writer.log_dir, checkpoint_name))
logging.info(f"Model checkpoint and metadata has been saved at {self.writer.log_dir}.")