forked from 4uiiurz1/pytorch-adacos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
omniglot_train.py
282 lines (224 loc) · 8.86 KB
/
omniglot_train.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
import os
import argparse
import numpy as np
from tqdm import tqdm
import pandas as pd
import joblib
from collections import OrderedDict
from glob import glob
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
import torch
import torch.backends.cudnn as cudnn
import torch.nn as nn
from torch.nn import functional as F
import torch.optim as optim
from torch.optim import lr_scheduler
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from utils import *
from omniglot import archs, dataset
import metrics
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--name', default=None,
help='model name: (default: arch+timestamp)')
parser.add_argument('--arch', default='ResNet_IR',
choices=archs.__all__,
help='model architecture')
parser.add_argument('--backbone', default='resnet18')
parser.add_argument('--metric', default='adacos',
choices=['adacos', 'arcface', 'sphereface', 'cosface', 'softmax'])
parser.add_argument('--num-features', default=512, type=int,
help='dimention of embedded features')
parser.add_argument('--num-classes', default=1623, type=int)
parser.add_argument('-b', '--batch-size', default=32, type=int,
metavar='N', help='mini-batch size (default: 32)')
parser.add_argument('--epochs', default=50, type=int)
parser.add_argument('--lr', '--learning-rate', default=1e-1, type=float)
parser.add_argument('--min-lr', default=1e-3, type=float)
parser.add_argument('--momentum', default=0.9, type=float)
parser.add_argument('--weight-decay', default=1e-4, type=float)
parser.add_argument('--nesterov', default=False, type=str2bool)
parser.add_argument('--cpu', default=False, type=str2bool)
args = parser.parse_args()
return args
def train(args, train_loader, model, metric_fc, criterion, optimizer):
losses = AverageMeter()
acc1s = AverageMeter()
acc5s = AverageMeter()
model.train()
metric_fc.train()
for i, (input, target) in tqdm(enumerate(train_loader), total=len(train_loader)):
if args.cpu:
input = input.cpu()
target = target.long().cpu()
else:
input = input.cuda()
target = target.long().cuda()
feature = model(input)
if args.metric == 'softmax':
output = metric_fc(feature)
else:
output = metric_fc(feature, target)
loss = criterion(output, target)
acc1, acc5 = accuracy(output, target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
acc1s.update(acc1.item(), input.size(0))
acc5s.update(acc5.item(), input.size(0))
# compute gradient and do optimizing step
optimizer.zero_grad()
loss.backward()
optimizer.step()
log = OrderedDict([
('loss', losses.avg),
('acc@1', acc1s.avg),
('acc@5', acc5s.avg),
])
return log
def validate(args, val_loader, model, metric_fc, criterion):
losses = AverageMeter()
acc1s = AverageMeter()
acc5s = AverageMeter()
# switch to evaluate mode
model.eval()
metric_fc.eval()
with torch.no_grad():
for i, (input, target) in tqdm(enumerate(val_loader), total=len(val_loader)):
if args.cpu:
input = input.cpu()
target = target.long().cpu()
else:
input = input.cuda()
target = target.long().cuda()
feature = model(input)
output = metric_fc(feature)
loss = criterion(output, target)
acc1, acc5 = accuracy(output, target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
acc1s.update(acc1.item(), input.size(0))
acc5s.update(acc5.item(), input.size(0))
log = OrderedDict([
('loss', losses.avg),
('acc@1', acc1s.avg),
('acc@5', acc5s.avg),
])
return log
def main():
args = parse_args()
if args.name is None:
args.name = 'omniglot_%s_%s_%dd' % (
args.arch, args.metric, args.num_features)
if not os.path.exists('models/%s' % args.name):
os.makedirs('models/%s' % args.name)
print('Config -----')
for arg in vars(args):
print('%s: %s' % (arg, getattr(args, arg)))
print('------------')
with open('models/%s/args.txt' % args.name, 'w') as f:
for arg in vars(args):
print('%s: %s' % (arg, getattr(args, arg)), file=f)
joblib.dump(args, 'models/%s/args.pkl' % args.name)
if args.cpu:
criterion = nn.CrossEntropyLoss().cpu()
else:
criterion = nn.CrossEntropyLoss().cuda()
cudnn.benchmark = True
img_paths = glob('omniglot/omniglot/python/images_background/*/*/*.png')
img_paths.extend(
glob('omniglot/omniglot/python/images_evaluation/*/*/*.png'))
labels = LabelEncoder().fit_transform(
[p.split('/')[-3] + '_' + p.split('/')[-2] for p in img_paths])
print(len(np.unique(labels)))
train_img_paths, test_img_paths, train_labels, test_labels = train_test_split(
img_paths, labels, test_size=0.2, random_state=41, stratify=labels)
transform_train = transforms.Compose([
transforms.RandomResizedCrop(114),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])
])
transform_test = transforms.Compose([
transforms.Resize(114),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])
])
train_set = dataset.Omniglot(
train_img_paths,
train_labels,
transform=transform_train)
test_set = dataset.Omniglot(
test_img_paths,
test_labels,
transform=transform_test)
train_loader = torch.utils.data.DataLoader(
train_set,
batch_size=args.batch_size,
shuffle=True,
num_workers=8)
test_loader = torch.utils.data.DataLoader(
test_set,
batch_size=args.batch_size,
shuffle=False,
num_workers=8)
# create model
model = archs.__dict__[args.arch](args)
if args.cpu:
model = model.cpu()
else:
model = model.cuda()
if args.metric == 'adacos':
metric_fc = metrics.AdaCos(
num_features=args.num_features, num_classes=args.num_classes)
elif args.metric == 'arcface':
metric_fc = metrics.ArcFace(
num_features=args.num_features, num_classes=args.num_classes)
elif args.metric == 'sphereface':
metric_fc = metrics.SphereFace(
num_features=args.num_features, num_classes=args.num_classes)
elif args.metric == 'cosface':
metric_fc = metrics.CosFace(
num_features=args.num_features, num_classes=args.num_classes)
else:
metric_fc = nn.Linear(args.num_features, args.num_classes)
if args.cpu:
metric_fc = metric_fc.cpu()
else:
metric_fc = metric_fc.cuda()
optimizer = optim.SGD(filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr,
momentum=args.momentum, weight_decay=args.weight_decay)
scheduler = lr_scheduler.CosineAnnealingLR(optimizer,
T_max=args.epochs, eta_min=args.min_lr)
log = pd.DataFrame(index=[], columns=[
'epoch', 'lr', 'loss', 'acc@1', 'acc@5', 'val_loss', 'val_acc1', 'val_acc5'
])
best_loss = float('inf')
for epoch in range(args.epochs):
print('Epoch [%d/%d]' % (epoch + 1, args.epochs))
scheduler.step()
# train for one epoch
train_log = train(args, train_loader, model,
metric_fc, criterion, optimizer)
# evaluate on validation set
val_log = validate(args, test_loader, model, metric_fc, criterion)
print('loss %.4f - acc@1 %.4f - acc@5 %.4f - val_loss %.4f - val_acc@1 %.4f - val_acc@5 %.4f'
%(train_log['loss'], train_log['acc@1'], train_log['acc@5'], val_log['loss'], val_log['acc@1'], val_log['acc@5']))
tmp = pd.Series([
epoch,
scheduler.get_lr()[0],
train_log['loss'],
train_log['acc@1'],
train_log['acc@5'],
val_log['loss'],
val_log['acc@1'],
val_log['acc@5'],
], index=['epoch', 'lr', 'loss', 'acc@1', 'acc@5', 'val_loss', 'val_acc1', 'val_acc5'])
log = log.append(tmp, ignore_index=True)
log.to_csv('models/%s/log.csv' % args.name, index=False)
if val_log['loss'] < best_loss:
torch.save(model.state_dict(), 'models/%s/model.pth' %args.name)
best_loss = val_log['loss']
print("=> saved best model")
if __name__ == '__main__':
main()