-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_baseline.py
417 lines (322 loc) · 16.2 KB
/
train_baseline.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import argparse
import os
import random
import time
from torch.utils.data import DataLoader
import numpy as np
from sklearn.cluster import KMeans
import torch
from torch.optim import SGD, lr_scheduler
from project_utils.cluster_utils import mixed_eval, AverageMeter
import vision_transformer as vits
from project_utils.general_utils import init_experiment, get_mean_lr, str2bool, get_dino_head_weights
from data.augmentations import get_transform
from data.get_datasets import get_datasets, get_class_splits
from tqdm import tqdm
from torch.nn import functional as F
import torch.nn as nn
from project_utils.cluster_and_log_utils import log_accs_from_preds
from config import exp_root, dino_pretrain_path
class SupConLoss(torch.nn.Module):
"""Supervised Contrastive Learning: https://arxiv.org/pdf/2004.11362.pdf.
It also supports the unsupervised contrastive loss in SimCLR
From: https://github.com/HobbitLong/SupContrast"""
def __init__(self, temperature=0.07, contrast_mode='all',
base_temperature=0.07):
super(SupConLoss, self).__init__()
self.temperature = temperature
self.contrast_mode = contrast_mode
self.base_temperature = base_temperature
def forward(self, features, labels=None, mask=None):
"""Compute loss for model. If both `labels` and `mask` are None,
it degenerates to SimCLR unsupervised loss:
https://arxiv.org/pdf/2002.05709.pdf
Args:
features: hidden vector of shape [bsz, n_views, ...].
labels: ground truth of shape [bsz].
mask: contrastive mask of shape [bsz, bsz], mask_{i,j}=1 if sample j
has the same class as sample i. Can be asymmetric.
Returns:
A loss scalar.
"""
device = (torch.device('cuda')
if features.is_cuda
else torch.device('cpu'))
if len(features.shape) < 3:
raise ValueError('`features` needs to be [bsz, n_views, ...],'
'at least 3 dimensions are required')
if len(features.shape) > 3:
features = features.view(features.shape[0], features.shape[1], -1)
batch_size = features.shape[0]
if labels is not None and mask is not None:
raise ValueError('Cannot define both `labels` and `mask`')
elif labels is None and mask is None:
mask = torch.eye(batch_size, dtype=torch.float32).to(device)
elif labels is not None:
labels = labels.contiguous().view(-1, 1)
if labels.shape[0] != batch_size:
raise ValueError('Num of labels does not match num of features')
mask = torch.eq(labels, labels.T).float().to(device)
else:
mask = mask.float().to(device)
contrast_count = features.shape[1]
contrast_feature = torch.cat(torch.unbind(features, dim=1), dim=0)
if self.contrast_mode == 'one':
anchor_feature = features[:, 0]
anchor_count = 1
elif self.contrast_mode == 'all':
anchor_feature = contrast_feature
anchor_count = contrast_count
else:
raise ValueError('Unknown mode: {}'.format(self.contrast_mode))
# compute logits
anchor_dot_contrast = torch.div(
torch.matmul(anchor_feature, contrast_feature.T),
self.temperature)
# for numerical stability
logits_max, _ = torch.max(anchor_dot_contrast, dim=1, keepdim=True)
logits = anchor_dot_contrast - logits_max.detach()
# tile mask
mask = mask.repeat(anchor_count, contrast_count)
# mask-out self-contrast cases
logits_mask = torch.scatter(
torch.ones_like(mask),
1,
torch.arange(batch_size * anchor_count).view(-1, 1).to(device),
0
)
mask = mask * logits_mask
# compute log_prob
exp_logits = torch.exp(logits) * logits_mask
log_prob = logits - torch.log(exp_logits.sum(1, keepdim=True))
# compute mean of log-likelihood over positive
mean_log_prob_pos = (mask * log_prob).sum(1) / mask.sum(1)
# loss
loss = - (self.temperature / self.base_temperature) * mean_log_prob_pos
loss = loss.view(anchor_count, batch_size).mean()
return loss
class ContrastiveLearningViewGenerator(object):
"""Take two random crops of one image as the query and key."""
def __init__(self, base_transform, n_views=2):
self.base_transform = base_transform
self.n_views = n_views
def __call__(self, x):
return [self.base_transform(x) for i in range(self.n_views)]
def train(projection_head, model, train_loader, test_loader, unlabelled_train_loader, args):
optimizer = SGD(list(projection_head.parameters()) + list(model.parameters()), lr=args.lr, momentum=args.momentum,
weight_decay=args.weight_decay)
exp_lr_scheduler = lr_scheduler.CosineAnnealingLR(
optimizer,
T_max=args.epochs,
eta_min=args.lr * 1e-3,
)
sup_con_crit = SupConLoss()
best_test_acc_lab = 0
for epoch in range(args.epochs):
loss_record = AverageMeter()
train_acc_record = AverageMeter()
projection_head.train()
model.train()
for batch_idx, batch in enumerate(tqdm(train_loader)):
images, class_labels, uq_idxs = batch
class_labels = class_labels.to(device)
images = torch.cat(images, dim=0).to(device)
features = model(images)
features, _, _ = projection_head(features)
features = torch.nn.functional.normalize(features, dim=-1)
f1, f2 = [f for f in features.chunk(2)]
sup_con_feats = torch.cat([f1.unsqueeze(1), f2.unsqueeze(1)], dim=1)
sup_con_labels = class_labels
sup_con_loss = sup_con_crit(sup_con_feats, labels=sup_con_labels)
loss = sup_con_loss
loss_record.update(loss.item(), class_labels.size(0))
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('Train Epoch: {} Avg Loss: {:.4f} | Seen Class Acc: {:.4f} '.format(epoch, loss_record.avg,
train_acc_record.avg))
with torch.no_grad():
all_acc, old_acc, new_acc = test_on_the_fly(model, projection_head, unlabelled_train_loader,
epoch=epoch, save_name='Train ACC Unlabelled',
args=args)
# ----------------
# LOG
# ----------------
args.writer.add_scalar('Loss', loss_record.avg, epoch)
args.writer.add_scalar('Train Acc Labelled Data', train_acc_record.avg, epoch)
args.writer.add_scalar('LR', get_mean_lr(optimizer), epoch)
print('Train Accuracies: All {:.4f} | Old {:.4f} | New {:.4f}'.format(all_acc, old_acc,
new_acc))
print('Test Accuracies: All {:.4f} | Old {:.4f} | New {:.4f}'.format(all_acc_test, old_acc_test,
new_acc_test))
# Step schedule
exp_lr_scheduler.step()
torch.save(model.state_dict(), args.model_path)
print("model saved to {}.".format(args.model_path))
torch.save(projection_head.state_dict(), args.model_path[:-3] + '_proj_head.pt')
print("projection head saved to {}.".format(args.model_path[:-3] + '_proj_head.pt'))
if old_acc_test > best_test_acc_lab:
print(f'Best ACC on old Classes on disjoint test set: {old_acc_test:.4f}...')
print('Best Train Accuracies: All {:.4f} | Old {:.4f} | New {:.4f}'.format(all_acc, old_acc,
new_acc))
torch.save(model.state_dict(), args.model_path[:-3] + f'_best.pt')
print("model saved to {}.".format(args.model_path[:-3] + f'_best.pt'))
torch.save(projection_head.state_dict(), args.model_path[:-3] + f'_proj_head_best.pt')
print("projection head saved to {}.".format(args.model_path[:-3] + f'_proj_head_best.pt'))
best_test_acc_lab = old_acc_test
def test_on_the_fly(model, projection_head, test_loader,
epoch, save_name,
args):
model.eval()
projection_head.eval()
all_feats = []
targets = np.array([])
mask = np.array([])
print('Collating features...')
# First extract all features
for batch_idx, (images, label, _) in enumerate(tqdm(test_loader)):
images = images.cuda()
# Pass features through base model and then additional learnable transform (linear layer)
feats = model(images)
_, feats, _ = projection_head(feats)
feats = torch.nn.functional.normalize(feats, dim=-1)[:, :]
# print(feats.shape)
all_feats.append(feats.cpu().numpy())
targets = np.append(targets, label.cpu().numpy())
mask = np.append(mask, np.array([True if x.item() in range(len(args.train_classes))
else False for x in label]))
# -----------------------
# On-The-Fly
# -----------------------
all_feats = np.concatenate(all_feats)
feats_hash = torch.Tensor(all_feats > 0).float().tolist()
preds = []
hash_dict = []
for feat in feats_hash:
if not feat in hash_dict:
hash_dict.append(feat)
preds.append(hash_dict.index(feat))
preds = np.array(preds)
print(len(list(set(preds))), len(preds))
# -----------------------
# EVALUATE
# -----------------------
all_acc, old_acc, new_acc = log_accs_from_preds(y_true=targets, y_pred=preds, mask=mask,
T=epoch, eval_funcs=args.eval_funcs, save_name=save_name,
writer=args.writer)
return all_acc, old_acc, new_acc
def seed_torch(seed):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed) # 为了禁止hash随机化,使得实验可复现
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='cluster',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--batch_size', default=128, type=int)
parser.add_argument('--num_workers', default=0, type=int)
parser.add_argument('--eval_funcs', nargs='+', help='Which eval functions to use', default=['v1', 'v2'])
parser.add_argument('--warmup_model_dir', type=str, default=None)
parser.add_argument('--model_name', type=str, default='vit_dino', help='Format is {model_name}_{pretrain}')
parser.add_argument('--dataset_name', type=str, default='scars', help='options: cifar10, cifar100, scars')
parser.add_argument('--prop_train_labels', type=float, default=0.5)
parser.add_argument('--use_ssb_splits', type=str2bool, default=False)
parser.add_argument('--grad_from_block', type=int, default=11)
parser.add_argument('--lr', type=float, default=0.1)
parser.add_argument('--save_best_thresh', type=float, default=None)
parser.add_argument('--gamma', type=float, default=0.1)
parser.add_argument('--momentum', type=float, default=0.9)
parser.add_argument('--weight_decay', type=float, default=1e-4)
parser.add_argument('--epochs', default=20, type=int)
parser.add_argument('--exp_root', type=str, default=exp_root)
parser.add_argument('--transform', type=str, default='imagenet')
parser.add_argument('--seed', default=1, type=int)
parser.add_argument('--base_model', type=str, default='vit_dino')
parser.add_argument('--temperature', type=float, default=1.0)
parser.add_argument('--sup_con_weight', type=float, default=0.5)
parser.add_argument('--n_views', default=2, type=int)
parser.add_argument('--contrast_unlabel_only', type=str2bool, default=False)
# ----------------------
# Multiple Runs
# ----------------------
for run in range(0, 10):
# ----------------------
# INIT
# ----------------------
seed_torch(run)
args = parser.parse_args()
device = torch.device('cuda:0')
args = get_class_splits(args)
args.num_labeled_classes = len(args.train_classes)
args.num_unlabeled_classes = len(args.unlabeled_classes)
init_experiment(args, runner_name=['checkpoints'])
print(f'Using evaluation function {args.eval_funcs[0]} to print results')
# ----------------------
# BASE MODEL
# ----------------------
if args.base_model == 'vit_dino':
args.interpolation = 3
args.crop_pct = 0.875
pretrain_path = dino_pretrain_path
model = vits.__dict__['vit_base']()
state_dict = torch.load(pretrain_path, map_location='cpu')
model.load_state_dict(state_dict)
if args.warmup_model_dir is not None:
print(f'Loading weights from {args.warmup_model_dir}')
model.load_state_dict(torch.load(args.warmup_model_dir, map_location='cpu'))
model.to(device)
# NOTE: Hardcoded image size as we do not finetune the entire ViT model
args.image_size = 224
args.feat_dim = 768
args.num_mlp_layers = 3
args.code_dim = 12
args.mlp_out_dim = None
# ----------------------
# HOW MUCH OF BASE MODEL TO FINETUNE
# ----------------------
for m in model.parameters():
m.requires_grad = False
# Only finetune layers from block 'args.grad_from_block' onwards
for name, m in model.named_parameters():
if 'block' in name:
block_num = int(name.split('.')[1])
if block_num >= args.grad_from_block:
m.requires_grad = True
else:
raise NotImplementedError
# --------------------
# CONTRASTIVE TRANSFORM
# --------------------
train_transform, test_transform = get_transform(args.transform, image_size=args.image_size, args=args)
train_transform = ContrastiveLearningViewGenerator(base_transform=train_transform, n_views=args.n_views)
# --------------------
# DATASETS
# --------------------
train_dataset, test_dataset, unlabelled_train_examples_test, datasets, labelled_dataset = get_datasets(args.dataset_name,
train_transform,
test_transform,
args)
# --------------------
# DATALOADERS
# --------------------
labelled_train_loader = DataLoader(labelled_dataset, num_workers=args.num_workers, batch_size=args.batch_size,
shuffle=True, drop_last=True)
unlabelled_train_loader = DataLoader(unlabelled_train_examples_test, num_workers=args.num_workers,
batch_size=args.batch_size, shuffle=False)
test_loader = DataLoader(test_dataset, num_workers=args.num_workers,
batch_size=args.batch_size, shuffle=False)
# ----------------------
# PROJECTION HEAD
# ----------------------
projection_head = vits.__dict__['BASEHead'](in_dim=args.feat_dim,
out_dim=args.mlp_out_dim, nlayers=args.num_mlp_layers, code_dim=args.code_dim, class_num=args.num_labeled_classes)
projection_head.to(device)
# ----------------------
# TRAIN
# ----------------------
train(projection_head, model, labelled_train_loader, test_loader, unlabelled_train_loader, args)