-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
431 lines (378 loc) · 15.9 KB
/
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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import torch, math, time, argparse, os
import random, dataset, utils, losses, net
import numpy as np
from dataset.Inshop import Inshop_Dataset
from net.resnet import *
from net.googlenet import *
from net.bn_inception import *
from dataset import sampler
from torch.utils.data.sampler import BatchSampler
from torch.utils.data.dataloader import default_collate
from tqdm import *
import wandb
seed = 1
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # set random seed for all gpus
parser = argparse.ArgumentParser(description=
'Official implementation of `Proxy Anchor Loss for Deep Metric Learning`'
+ 'Our code is modified from `https://github.com/dichotomies/proxy-nca`'
)
# export directory, training and val datasets, test datasets
parser.add_argument('--LOG_DIR',
default='../logs',
help = 'Path to log folder'
)
parser.add_argument('--dataset',
default='cub',
help = 'Training dataset, e.g. cub, cars, SOP, Inshop'
)
parser.add_argument('--embedding-size', default = 512, type = int,
dest = 'sz_embedding',
help = 'Size of embedding that is appended to backbone model.'
)
parser.add_argument('--batch-size', default = 150, type = int,
dest = 'sz_batch',
help = 'Number of samples per batch.'
)
parser.add_argument('--epochs', default = 100, type = int,
dest = 'nb_epochs',
help = 'Number of training epochs.'
)
parser.add_argument('--gpu-id', default = 0, type = int,
help = 'ID of GPU that is used for training.'
)
parser.add_argument('--workers', default = 4, type = int,
dest = 'nb_workers',
help = 'Number of workers for dataloader.'
)
parser.add_argument('--model', default = 'bn_inception',
help = 'Model for training'
)
parser.add_argument('--loss', default = 'Proxy_Anchor',
help = 'Criterion for training'
)
parser.add_argument('--optimizer', default = 'adamw',
help = 'Optimizer setting'
)
parser.add_argument('--lr', default = 1e-4, type =float,
help = 'Learning rate setting'
)
parser.add_argument('--weight-decay', default = 1e-4, type =float,
help = 'Weight decay setting'
)
parser.add_argument('--lr-decay-step', default = 10, type =int,
help = 'Learning decay step setting'
)
parser.add_argument('--lr-decay-gamma', default = 0.5, type =float,
help = 'Learning decay gamma setting'
)
parser.add_argument('--alpha', default = 32, type = float,
help = 'Scaling Parameter setting'
)
parser.add_argument('--mrg', default = 0.1, type = float,
help = 'Margin parameter setting'
)
parser.add_argument('--IPC', default=0, type = int,
help = 'Balanced sampling, images per class'
)
parser.add_argument('--warm', default = 1, type = int,
help = 'Warmup training epochs'
)
parser.add_argument('--bn-freeze', default = 1, type = int,
help = 'Batch normalization parameter freeze'
)
parser.add_argument('--l2-norm', default = 1, type = int,
help = 'L2 normlization'
)
parser.add_argument('--remark', default = '',
help = 'Any reamrk'
)
parser.add_argument('--thresh', default = 0.5, type=float,
help = 'MS margin'
)
parser.add_argument('--epsilon', default = 0.1, type=float,
help = 'MS epsilon'
)
parser.add_argument('--scale_pos', default = 2, type=float,
help = 'MS scale for positives'
)
parser.add_argument('--scale_neg', default = 50, type=float,
help = 'MS scale for negatives'
)
parser.add_argument('--margin', default = 0.01, type=float,
help = 'ST margin'
)
parser.add_argument('--lamda', default = 20, type=float,
help = 'ST scale'
)
parser.add_argument('--gamma', default = 1./0.1, type=float,
help = 'ST gamma'
)
parser.add_argument('--tau', default = 0.4, type=float,
help = 'ST tau for weight reg'
)
args = parser.parse_args()
if args.gpu_id != -1:
torch.cuda.set_device(args.gpu_id)
# Directory for Log
LOG_DIR = args.LOG_DIR + '/logs_{}/{}_{}_embedding{}_alpha{}_mrg{}_{}_lr{}_batch{}{}'.format(args.dataset, args.model, args.loss, args.sz_embedding, args.alpha,
args.mrg, args.optimizer, args.lr, args.sz_batch, args.remark)
# Wandb Initialization
#wandb.init(project=args.dataset + '_ProxyAnchor', notes=LOG_DIR)
#wandb.config.update(args)
data_root = os.path.join(os.getcwd(), 'data')
# Dataset Loader and Sampler
if args.dataset != 'Inshop':
trn_dataset = dataset.load(
name = args.dataset,
root = data_root,
mode = 'train',
transform = dataset.utils.make_transform(
is_train = True,
is_inception = (args.model == 'bn_inception')
))
else:
trn_dataset = Inshop_Dataset(
root = data_root,
mode = 'train',
transform = dataset.utils.make_transform(
is_train = True,
is_inception = (args.model == 'bn_inception')
))
if args.IPC:
balanced_sampler = sampler.BalancedSampler(trn_dataset, batch_size=args.sz_batch, images_per_class = args.IPC)
batch_sampler = BatchSampler(balanced_sampler, batch_size = args.sz_batch, drop_last = True)
dl_tr = torch.utils.data.DataLoader(
trn_dataset,
num_workers = args.nb_workers,
pin_memory = True,
batch_sampler = batch_sampler
)
print('Balanced Sampling')
else:
dl_tr = torch.utils.data.DataLoader(
trn_dataset,
batch_size = args.sz_batch,
shuffle = True,
num_workers = args.nb_workers,
drop_last = True,
pin_memory = True
)
print('Random Sampling')
if args.dataset != 'Inshop':
ev_dataset = dataset.load(
name = args.dataset,
root = data_root,
mode = 'eval',
transform = dataset.utils.make_transform(
is_train = False,
is_inception = (args.model == 'bn_inception')
))
dl_ev = torch.utils.data.DataLoader(
ev_dataset,
batch_size = args.sz_batch,
shuffle = False,
num_workers = args.nb_workers,
pin_memory = True
)
else:
query_dataset = Inshop_Dataset(
root = data_root,
mode = 'query',
transform = dataset.utils.make_transform(
is_train = False,
is_inception = (args.model == 'bn_inception')
))
dl_query = torch.utils.data.DataLoader(
query_dataset,
batch_size = args.sz_batch,
shuffle = False,
num_workers = args.nb_workers,
pin_memory = True
)
gallery_dataset = Inshop_Dataset(
root = data_root,
mode = 'gallery',
transform = dataset.utils.make_transform(
is_train = False,
is_inception = (args.model == 'bn_inception')
))
dl_gallery = torch.utils.data.DataLoader(
gallery_dataset,
batch_size = args.sz_batch,
shuffle = False,
num_workers = args.nb_workers,
pin_memory = True
)
nb_classes = trn_dataset.nb_classes()
'''
# Visualization Tool
import matplotlib.pyplot as plt
dataiter=iter(dl_tr)
images,labels=dataiter.next()
for i in range(100):
images[i]=(1/(2.25*2.5))*images[i]+0.5
plt.imsave('sop_samples/1'+str(i)+'.jpg',np.transpose(images[i].cpu().detach().numpy(), (1,2,0)))
'''
# Backbone Model
if args.model.find('googlenet')+1:
model = googlenet(embedding_size=args.sz_embedding, pretrained=True, is_norm=args.l2_norm, bn_freeze = args.bn_freeze)
elif args.model.find('bn_inception')+1:
model = bn_inception(embedding_size=args.sz_embedding, pretrained=True, is_norm=args.l2_norm, bn_freeze = args.bn_freeze)
elif args.model.find('resnet18')+1:
model = Resnet18(embedding_size=args.sz_embedding, pretrained=True, is_norm=args.l2_norm, bn_freeze = args.bn_freeze)
elif args.model.find('resnet50')+1:
model = Resnet50(embedding_size=args.sz_embedding, pretrained=True, is_norm=args.l2_norm, bn_freeze = args.bn_freeze)
elif args.model.find('resnet101')+1:
model = Resnet101(embedding_size=args.sz_embedding, pretrained=True, is_norm=args.l2_norm, bn_freeze = args.bn_freeze)
model = model.cuda()
if args.gpu_id == -1:
model = nn.DataParallel(model)
# DML Losses
if args.loss == 'Proxy_Anchor':
criterion = losses.Proxy_Anchor(nb_classes = nb_classes, sz_embed = args.sz_embedding, mrg = args.mrg, alpha = args.alpha).cuda()
elif args.loss == 'Proxy_NCA':
criterion = losses.Proxy_NCA(nb_classes = nb_classes, sz_embed = args.sz_embedding).cuda()
elif args.loss == 'MS':
criterion = losses.MultiSimilarityLoss(thresh = args.thresh, epsilon = args.epsilon, scale_pos = args.scale_pos, scale_neg = args.scale_neg).cuda()
elif args.loss == 'Contrastive':
criterion = losses.ContrastiveLoss().cuda()
elif args.loss == 'Triplet':
criterion = losses.TripletLoss().cuda()
elif args.loss == 'Npair':
criterion = losses.NPairLoss().cuda()
elif args.loss == 'ArcFace':
criterion = losses.ArcFaceLoss(nb_classes = nb_classes, sz_embed = args.sz_embedding).cuda()
elif args.loss == 'SoftTriple':
criterion = losses.SoftTripleLoss(margin = args.margin, lamda =args.lamda, gamma=args.gamma , tau=args.tau ,nb_classes = nb_classes, sz_embed = args.sz_embedding).cuda()
elif args.loss == 'LiftedStructure':
criterion = losses.LiftedStructureLoss().cuda()
elif args.loss == 'Margin':
criterion = losses.MarginLoss().cuda()
#import pdb
#pdb.set_trace()
# Train Parameters
param_groups = [
{'params': list(set(model.parameters()).difference(set(model.model.embedding.parameters()))) if args.gpu_id != -1 else
list(set(model.module.parameters()).difference(set(model.module.model.embedding.parameters())))},
{'params': model.model.embedding.parameters() if args.gpu_id != -1 else model.module.model.embedding.parameters(), 'lr':float(args.lr) * 1},
]
if args.loss == 'Proxy_Anchor':
param_groups.append({'params': criterion.proxies, 'lr':float(args.lr) * 100})
#this used to make alpha learnable
#param_groups.append({'params': criterion.alpha, 'lr':float(args.lr) * 500})
if args.loss == 'SoftTriple':
param_groups.append({'params': criterion.fc, 'lr':float(args.lr) * 100})
# Optimizer Setting
if args.optimizer == 'sgd':
opt = torch.optim.SGD(param_groups, lr=float(args.lr), weight_decay = args.weight_decay, momentum = 0.9, nesterov=True)
elif args.optimizer == 'adam':
opt = torch.optim.Adam(param_groups, lr=float(args.lr), weight_decay = args.weight_decay)
elif args.optimizer == 'rmsprop':
opt = torch.optim.RMSprop(param_groups, lr=float(args.lr), alpha=0.9, weight_decay = args.weight_decay, momentum = 0.9)
elif args.optimizer == 'adamw':
opt = torch.optim.AdamW(param_groups, lr=float(args.lr), weight_decay = args.weight_decay)
scheduler = torch.optim.lr_scheduler.StepLR(opt, step_size=args.lr_decay_step, gamma = args.lr_decay_gamma)
print("Training parameters: {}".format(vars(args)))
print("Training for {} epochs.".format(args.nb_epochs))
losses_list = []
best_recall=[0]
best_epoch = 0
for epoch in range(0, args.nb_epochs):
model.train()
bn_freeze = args.bn_freeze
if bn_freeze:
modules = model.model.modules() if args.gpu_id != -1 else model.module.model.modules()
for m in modules:
if isinstance(m, nn.BatchNorm2d):
m.eval()
losses_per_epoch = []
# Warmup: Train only new params, helps stabilize learning.
if args.warm > 0:
if args.gpu_id != -1:
unfreeze_model_param = list(model.model.embedding.parameters()) + list(criterion.parameters())
else:
unfreeze_model_param = list(model.module.model.embedding.parameters()) + list(criterion.parameters())
if epoch == 0:
for param in list(set(model.parameters()).difference(set(unfreeze_model_param))):
param.requires_grad = False
if epoch == args.warm:
for param in list(set(model.parameters()).difference(set(unfreeze_model_param))):
param.requires_grad = True
pbar = tqdm(enumerate(dl_tr))
for batch_idx, (x, y) in pbar:
m = model(x.squeeze().cuda())
loss = criterion(m, y.squeeze().cuda())
opt.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_value_(model.parameters(), 10)
if args.loss == 'Proxy_Anchor':
torch.nn.utils.clip_grad_value_(criterion.parameters(), 10)
losses_per_epoch.append(loss.data.cpu().numpy())
opt.step()
pbar.set_description(
'Train Epoch: {} [{}/{} ({:.0f}%)] Loss: {:.6f}'.format(
epoch, batch_idx + 1, len(dl_tr),
100. * batch_idx / len(dl_tr),
loss.item()))
losses_list.append(np.mean(losses_per_epoch))
#wandb.log({'loss': losses_list[-1]}, step=epoch)
scheduler.step()
if(epoch >= 0):
with torch.no_grad():
print("**Evaluating...**")
if args.dataset == 'Inshop':
Recalls = utils.evaluate_cos_Inshop(model, dl_query, dl_gallery)
elif args.dataset != 'SOP':
Recalls = utils.evaluate_cos(model, dl_ev)
else:
Recalls = utils.evaluate_cos_SOP(model, dl_ev)
# Logging Evaluation Score
#if args.dataset == 'Inshop':
# for i, K in enumerate([1,10,20,30,40,50]):
# wandb.log({"R@{}".format(K): Recalls[i]}, step=epoch)
#elif args.dataset != 'SOP':
# for i in range(6):
# wandb.log({"R@{}".format(2**i): Recalls[i]}, step=epoch)
#else:
# for i in range(4):
# wandb.log({"R@{}".format(10**i): Recalls[i]}, step=epoch)
# Best model save
if best_recall[0] < Recalls[0]:
best_recall = Recalls
best_epoch = epoch
# that's added in order to save the proxies with the best_recall in a file
#if not os.path.exists('proxies'):
# os.makedirs('proxies')
#torch.save(P, 'proxies/proxies.pt')
if not os.path.exists('{}'.format(LOG_DIR)):
os.makedirs('{}'.format(LOG_DIR))
torch.save({'model_state_dict':model.state_dict()}, '{}/{}_{}_best.pth'.format(LOG_DIR, args.dataset, args.model))
with open('{}/{}_{}_best_results.txt'.format(LOG_DIR, args.dataset, args.model), 'w') as f:
f.write('Best Epoch: {}\n'.format(best_epoch))
if args.dataset == 'Inshop':
for i, K in enumerate([1,10,20,30,40,50]):
f.write("Best Recall@{}: {:.4f}\n".format(K, best_recall[i] * 100))
elif args.dataset != 'SOP':
for i in range(6):
f.write("Best Recall@{}: {:.4f}\n".format(2**i, best_recall[i] * 100))
else:
for i in range(4):
f.write("Best Recall@{}: {:.4f}\n".format(10**i, best_recall[i] * 100))
# For hyperparameter searching
if not os.path.exists('{}'.format(LOG_DIR)):
os.makedirs('{}'.format(LOG_DIR))
if args.loss == 'Proxy_Anchor':
with open('{}/{}_{}_{}_mrg{}_alpha{}.txt'.format(LOG_DIR, args.dataset, args.model, args.loss, args.mrg, args.alpha), 'w') as f:
for i in range(4):
f.write("Best Recall@{}: {:.4f}\n".format(10**i, best_recall[i] * 100))
if args.loss == 'MS':
with open('{}/{}_{}_{}_thresh{}_epsilon{}_scalepos{}_scaleneg{}.txt'.format(LOG_DIR, args.dataset, args.model, args.loss, args.thresh, args.epsilon, args.scale_pos, args.scale_neg), 'w') as f:
for i in range(4):
f.write("Best Recall@{}: {:.4f}\n".format(10**i, best_recall[i] * 100))
if args.loss == 'SoftTriple':
with open('{}/{}_{}_{}_margin{}_lamda{}_gamma{}_tau{}.txt'.format(LOG_DIR, args.dataset, args.model, args.loss, args.margin, args.lamda, args.gamma, args.tau), 'w') as f:
for i in range(4):
f.write("Best Recall@{}: {:.4f}\n".format(10**i, best_recall[i] * 100))