-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_simclr.py
175 lines (123 loc) · 5.8 KB
/
train_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
165
166
167
168
169
170
171
172
173
174
175
import os,sys
import tqdm
import time
import wandb
import torch, numpy as np
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from common.meter import Meter
from common.utils import detect_grad_nan, set_seed, setup_run, by
from models.dataloaders.data_utils import dataset_builder
from models.resnet_simclr import SimCLR
class NTXentLoss(torch.nn.Module):
def __init__(self, device, batch_size, temperature, use_cosine_similarity):
super(NTXentLoss, self).__init__()
self.batch_size = batch_size
self.temperature = temperature
self.device = device
self.mask_samples_from_same_repr = self._get_correlated_mask().type(torch.bool)
self.similarity_function = self._get_similarity_function(use_cosine_similarity)
self.criterion = torch.nn.CrossEntropyLoss(reduction="sum")
def recall_rpr(self):
self.mask_samples_from_same_repr = self._get_correlated_mask().type(torch.bool)
def _get_similarity_function(self, use_cosine_similarity):
if use_cosine_similarity:
self._cosine_similarity = torch.nn.CosineSimilarity(dim=-1)
return self._cosine_simililarity
else:
return self._dot_simililarity
def _get_correlated_mask(self):
diag = np.eye(2 * self.batch_size)
l1 = np.eye((2 * self.batch_size), 2 * self.batch_size, k=-self.batch_size)
l2 = np.eye((2 * self.batch_size), 2 * self.batch_size, k=self.batch_size)
mask = torch.from_numpy((diag + l1 + l2))
mask = (1 - mask).type(torch.bool)
return mask.to(self.device)
@staticmethod
def _dot_simililarity(x, y):
v = torch.tensordot(x.unsqueeze(1), y.T.unsqueeze(0), dims=2)
# x shape: (N, 1, C)
# y shape: (1, C, 2N)
# v shape: (N, 2N)
return v
def _cosine_simililarity(self, x, y):
# x shape: (N, 1, C)
# y shape: (1, 2N, C)
# v shape: (N, 2N)
v = self._cosine_similarity(x.unsqueeze(1), y.unsqueeze(0))
return v
def forward(self, representations):
representations = F.normalize(representations,dim=1)
similarity_matrix = self.similarity_function(representations, representations)
# filter out the scores from the positive samples
l_pos = torch.diag(similarity_matrix, self.batch_size)
r_pos = torch.diag(similarity_matrix, -self.batch_size)
positives = torch.cat([l_pos, r_pos]).view(2 * self.batch_size, 1)
negatives = similarity_matrix[self.mask_samples_from_same_repr].view(2 * self.batch_size, -1)
logits = torch.cat((positives, negatives), dim=1)
logits /= self.temperature
labels = torch.zeros(2 * self.batch_size).to(self.device).long()
loss = self.criterion(logits, labels)
return loss / (2 * self.batch_size)
def train(epoch, model, loader, optimizer, args=None):
model.train()
loss_meter = Meter()
tqdm_gen = tqdm.tqdm(loader)
nxt_loss = NTXentLoss(torch.device('cuda:0'), args.batch, temperature=0.5, use_cosine_similarity=True)
for _, (data_i, data_j) in enumerate(tqdm_gen):
data = torch.cat([data_i, data_j], 0).cuda()
optimizer.zero_grad()
features = model(data)
loss = nxt_loss(features)
loss_meter.update(loss.item())
tqdm_gen.set_description(f'[train] epo:{epoch:>3} | avg.loss:{loss_meter.avg():.8f}')
loss.backward()
optimizer.step()
return loss_meter.avg()
def train_main(args):
Dataset = dataset_builder(args)
lib_root = args.data_dir
trainset = Dataset(root=lib_root, split='train',nslides=-1)
train_loader = DataLoader(dataset=trainset, batch_size=args.batch,
shuffle=True, num_workers=8, pin_memory=False, drop_last=True)
set_seed(args.seed)
if args.model_name == 'simclr':
model = SimCLR(args).cuda()
else:
raise ValueError('Model not found')
model = nn.DataParallel(model, device_ids=args.device_ids)
if not args.no_wandb:
wandb.watch(model)
print()
print(model.module.head)
print()
if not args.use_adam:
optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=0.9, nesterov=True, weight_decay=args.wd)
else:
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, betas=(0.9,0.999), weight_decay=args.wd)
lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=args.max_epoch, eta_min=0,last_epoch=-1)
max_loss, max_epoch = 100.0, 0
set_seed(args.seed)
print('Training :::::\n')
for epoch in range(1, args.max_epoch + 1):
start_time = time.time()
train_loss = train(epoch, model, train_loader, optimizer, args)
if not args.no_wandb:
wandb.log({f'train/loss': train_loss}, step=epoch)
if train_loss <= max_loss:
max_loss, max_epoch = train_loss, epoch
torch.save(dict(params=model.state_dict(), epoch=epoch), os.path.join(args.save_path, f'max_acc.pth'))
torch.save(optimizer.state_dict(), os.path.join(args.save_path, f'optimizer_max_acc.pth'))
if args.save_all:
torch.save(dict(params=model.state_dict(), epoch=epoch), os.path.join(args.save_path, f'epoch_{epoch}.pth'))
torch.save(optimizer.state_dict(), os.path.join(args.save_path, f'optimizer_epoch_{epoch}.pth'))
epoch_time = time.time() - start_time
time_left = f'{(args.max_epoch - epoch) / 3600. * epoch_time:.2f} h left\n'
print(f'[ log ] saving @ {args.save_path}')
print(f'[ log ] roughly {time_left}')
lr_scheduler.step()
return model
if __name__ == '__main__':
args = setup_run(arg_mode='train')
train_main(args)