-
Notifications
You must be signed in to change notification settings - Fork 11
/
simclr.py
136 lines (118 loc) · 5.94 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
'''Train an encoder using Contrastive Learning.'''
import argparse
import os
import subprocess
import torch
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.optim as optim
from torchlars import LARS
from tqdm import tqdm
from configs import get_datasets
from critic import LinearCritic
from evaluate import save_checkpoint, encode_train_set, train_clf, test
from models import *
from scheduler import CosineAnnealingWithLinearRampLR
parser = argparse.ArgumentParser(description='PyTorch Contrastive Learning.')
parser.add_argument('--base-lr', default=0.25, type=float, help='base learning rate, rescaled by batch_size/256')
parser.add_argument("--momentum", default=0.9, type=float, help='SGD momentum')
parser.add_argument('--resume', '-r', type=str, default='', help='resume from checkpoint with this filename')
parser.add_argument('--dataset', '-d', type=str, default='cifar10', help='dataset',
choices=['cifar10', 'cifar100', 'stl10', 'imagenet'])
parser.add_argument('--temperature', type=float, default=0.5, help='InfoNCE temperature')
parser.add_argument("--batch-size", type=int, default=512, help='Training batch size')
parser.add_argument("--num-epochs", type=int, default=100, help='Number of training epochs')
parser.add_argument("--cosine-anneal", action='store_true', help="Use cosine annealing on the learning rate")
parser.add_argument("--arch", type=str, default='resnet50', help='Encoder architecture',
choices=['resnet18', 'resnet34', 'resnet50'])
parser.add_argument("--num-workers", type=int, default=2, help='Number of threads for data loaders')
parser.add_argument("--test-freq", type=int, default=10, help='Frequency to fit a linear clf with L-BFGS for testing'
'Not appropriate for large datasets. Set 0 to avoid '
'classifier only training here.')
parser.add_argument("--filename", type=str, default='ckpt.pth', help='Output file name')
args = parser.parse_args()
args.lr = args.base_lr * (args.batch_size / 256)
args.git_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD'])
args.git_diff = subprocess.check_output(['git', 'diff'])
device = 'cuda' if torch.cuda.is_available() else 'cpu'
best_acc = 0 # best test accuracy
start_epoch = 0 # start from epoch 0 or last checkpoint epoch
clf = None
print('==> Preparing data..')
trainset, testset, clftrainset, num_classes, stem = get_datasets(args.dataset)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=args.batch_size, shuffle=True,
num_workers=args.num_workers, pin_memory=True)
testloader = torch.utils.data.DataLoader(testset, batch_size=1000, shuffle=False, num_workers=args.num_workers,
pin_memory=True)
clftrainloader = torch.utils.data.DataLoader(clftrainset, batch_size=1000, shuffle=False, num_workers=args.num_workers,
pin_memory=True)
# Model
print('==> Building model..')
##############################################################
# Encoder
##############################################################
if args.arch == 'resnet18':
net = ResNet18(stem=stem)
elif args.arch == 'resnet34':
net = ResNet34(stem=stem)
elif args.arch == 'resnet50':
net = ResNet50(stem=stem)
else:
raise ValueError("Bad architecture specification")
net = net.to(device)
##############################################################
# Critic
##############################################################
critic = LinearCritic(net.representation_dim, temperature=args.temperature).to(device)
if device == 'cuda':
repr_dim = net.representation_dim
net = torch.nn.DataParallel(net)
net.representation_dim = repr_dim
cudnn.benchmark = True
if args.resume:
# Load checkpoint.
print('==> Resuming from checkpoint..')
assert os.path.isdir('checkpoint'), 'Error: no checkpoint directory found!'
resume_from = os.path.join('./checkpoint', args.resume)
checkpoint = torch.load(resume_from)
net.load_state_dict(checkpoint['net'])
critic.load_state_dict(checkpoint['critic'])
best_acc = checkpoint['acc']
start_epoch = checkpoint['epoch']
criterion = nn.CrossEntropyLoss()
base_optimizer = optim.SGD(list(net.parameters()) + list(critic.parameters()), lr=args.lr, weight_decay=1e-6,
momentum=args.momentum)
if args.cosine_anneal:
scheduler = CosineAnnealingWithLinearRampLR(base_optimizer, args.num_epochs)
encoder_optimizer = LARS(base_optimizer, trust_coef=1e-3)
# Training
def train(epoch):
print('\nEpoch: %d' % epoch)
net.train()
critic.train()
train_loss = 0
t = tqdm(enumerate(trainloader), desc='Loss: **** ', total=len(trainloader), bar_format='{desc}{bar}{r_bar}')
for batch_idx, (inputs, _, _) in t:
x1, x2 = inputs
x1, x2 = x1.to(device), x2.to(device)
encoder_optimizer.zero_grad()
representation1, representation2 = net(x1), net(x2)
raw_scores, pseudotargets = critic(representation1, representation2)
loss = criterion(raw_scores, pseudotargets)
loss.backward()
encoder_optimizer.step()
train_loss += loss.item()
t.set_description('Loss: %.3f ' % (train_loss / (batch_idx + 1)))
for epoch in range(start_epoch, start_epoch + args.num_epochs):
train(epoch)
if (args.test_freq > 0) and (epoch % args.test_freq == (args.test_freq - 1)):
X, y = encode_train_set(clftrainloader, device, net)
clf = train_clf(X, y, net.representation_dim, num_classes, device, reg_weight=1e-5)
acc = test(testloader, device, net, clf)
if acc > best_acc:
best_acc = acc
save_checkpoint(net, clf, critic, epoch, args, os.path.basename(__file__))
elif args.test_freq == 0:
save_checkpoint(net, clf, critic, epoch, args, os.path.basename(__file__))
if args.cosine_anneal:
scheduler.step()