-
Notifications
You must be signed in to change notification settings - Fork 1
/
trainer.py
538 lines (441 loc) · 24.7 KB
/
trainer.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
from __future__ import print_function
import pickle
import os
import sys
import time
import shutil
import json
import numpy as np
import torch
import evaluation
import util.data_provider as data
from util.vocab import Vocabulary
from util.text2vec import get_text_encoder
from model import get_model, get_we_parameter
import logging
import tensorboard_logger as tb_logger
import argparse
from basic.constant import ROOT_PATH
from basic.bigfile import BigFile
from basic.common import makedirsforfile, checkToSkip
from basic.util import read_dict, AverageMeter, LogCollector
from basic.generic_utils import Progbar
import random
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
INFO = __file__
def parse_args():
# Hyper Parameters
parser = argparse.ArgumentParser()
parser.add_argument('--multi_flag', type=int, default=0)
parser.add_argument('--rootpath', type=str, default=ROOT_PATH,
help='path to datasets. (default: %s)'%ROOT_PATH)
parser.add_argument('--trainCollection', type=str, default='tgif', help='source train collection')
parser.add_argument('--trainCollection2', type=str, default='vatex', help='source2 train collection')
parser.add_argument('--targettrainCollection', type=str, default='msrvtt10ktrain', help='target train collection')
parser.add_argument('--valCollection', type=str, default='msrvtt10kval', help='validation collection')
parser.add_argument('--testCollection', type=str, default='msrvtt10ktest', help='test collection')
parser.add_argument('--n_caption', type=int, default=20, help='number of captions of each image/video (default: 1)')
parser.add_argument('--overwrite', type=int, default=0, choices=[0,1], help='overwrite existed file. (default: 0)')
parser.add_argument('--plot', action='store_true')
parser.add_argument('--domain_weight', type=float, default=0.01, help='weight for domain alignment')
parser.add_argument('--modality_weight', type=float, default=0.01, help='weight for modality alignment')
# model
parser.add_argument('--model', type=str, default='dual_encoding', help='model name. (default: dual_encoding)')
parser.add_argument('--concate', type=str, default='full', help='feature concatenation style. (full|reduced) full=level 1+2+3; reduced=level 2+3')
parser.add_argument('--measure', type=str, default='cosine', help='measure method. (default: cosine)')
parser.add_argument('--dropout', default=0.2, type=float, help='dropout rate (default: 0.2)')
# text-side multi-level encoding
parser.add_argument('--vocab', type=str, default='word_vocab_5', help='word vocabulary. (default: word_vocab_5)')
parser.add_argument('--word_dim', type=int, default=500, help='word embedding dimension')
parser.add_argument('--text_rnn_size', type=int, default=512, help='text rnn encoder size. (default: 1024)')
parser.add_argument('--text_kernel_num', default=512, type=int, help='number of each kind of text kernel')
parser.add_argument('--text_kernel_sizes', default='2-3-4', type=str, help='dash-separated kernel size to use for text convolution')
parser.add_argument('--text_norm', action='store_true', help='normalize the text embeddings at last layer')
# video-side multi-level encoding
parser.add_argument('--visual_feature', type=str, default='pyresnext-101_rbps13k,flatten0_output,os', help='visual feature.')
parser.add_argument('--visual_rnn_size', type=int, default=1024, help='visual rnn encoder size')
parser.add_argument('--visual_kernel_num', default=512, type=int, help='number of each kind of visual kernel')
parser.add_argument('--visual_kernel_sizes', default='2-3-4-5', type=str, help='dash-separated kernel size to use for visual convolution')
parser.add_argument('--visual_norm', action='store_true', help='normalize the visual embeddings at last layer')
# common space learning
parser.add_argument('--text_mapping_layers', type=str, default='0-2048', help='text fully connected layers for common space learning. (default: 0-2048)')
parser.add_argument('--visual_mapping_layers', type=str, default='0-2048', help='visual fully connected layers for common space learning. (default: 0-2048)')
# loss
parser.add_argument('--loss_fun', type=str, default='mrl', help='loss function')
parser.add_argument('--margin', type=float, default=0.2, help='rank loss margin')
parser.add_argument('--direction', type=str, default='all', help='retrieval direction (all|t2i|i2t)')
parser.add_argument('--max_violation', action='store_true', help='use max instead of sum in the rank loss')
parser.add_argument('--cost_style', type=str, default='sum', help='cost style (sum, mean). (default: sum)')
# optimizer
parser.add_argument('--optimizer', type=str, default='adam', help='optimizer. (default: rmsprop)')
parser.add_argument('--learning_rate', type=float, default=0.0001, help='initial learning rate')
parser.add_argument('--lr_decay_rate', default=0.99, type=float, help='learning rate decay rate. (default: 0.99)')
parser.add_argument('--grad_clip', type=float, default=2, help='gradient clipping threshold')
parser.add_argument('--resume', default='', type=str, metavar='PATH', help='path to latest checkpoint (default: none)')
parser.add_argument('--val_metric', default='recall', type=str, help='performance metric for validation (mir|recall)')
# misc
parser.add_argument('--num_epochs', default=50, type=int, help='Number of training epochs.')
parser.add_argument('--batch_size', default=128, type=int, help='Size of a training mini-batch.')
parser.add_argument('--workers', default=5, type=int, help='Number of data loader workers.')
parser.add_argument('--postfix', default='runs_0', help='Path to save the model and Tensorboard log.')
parser.add_argument('--log_step', default=10, type=int, help='Number of steps to print and record the log.')
parser.add_argument('--cv_name', default='baseline', type=str, help='')
# add da
parser.add_argument('--cross_domain_training', default=0, type=int, help='')
args = parser.parse_args()
return args
def fix_seed(seed):
torch.cuda.manual_seed_all(seed)
torch.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(seed)
random.seed(seed)
def main():
opt = parse_args()
print(json.dumps(vars(opt), indent = 2))
multi_flag = opt.multi_flag
rootpath = opt.rootpath
trainCollection = opt.trainCollection
if multi_flag:
trainCollection2 = opt.trainCollection2
targettrainCollection = opt.targettrainCollection
valCollection = opt.valCollection
testCollection = opt.testCollection
if opt.loss_fun == "mrl" and opt.measure == "cosine":
assert opt.text_norm is True
assert opt.visual_norm is True
# checkpoint path
model_info = '%s_concate_%s_dp_%.1f_measure_%s' % (opt.model, opt.concate, opt.dropout, opt.measure)
# text-side multi-level encoding info
text_encode_info = 'vocab_%s_word_dim_%s_text_rnn_size_%s_text_norm_%s' % \
(opt.vocab, opt.word_dim, opt.text_rnn_size, opt.text_norm)
text_encode_info += "_kernel_sizes_%s_num_%s" % (opt.text_kernel_sizes, opt.text_kernel_num)
# video-side multi-level encoding info
visual_encode_info = 'visual_feature_%s_visual_rnn_size_%d_visual_norm_%s' % \
(opt.visual_feature, opt.visual_rnn_size, opt.visual_norm)
visual_encode_info += "_kernel_sizes_%s_num_%s" % (opt.visual_kernel_sizes, opt.visual_kernel_num)
# common space learning info
mapping_info = "mapping_text_%s_img_%s" % (opt.text_mapping_layers, opt.visual_mapping_layers)
loss_info = 'loss_func_%s_margin_%s_direction_%s_max_violation_%s_cost_style_%s' % \
(opt.loss_fun, opt.margin, opt.direction, opt.max_violation, opt.cost_style)
optimizer_info = 'optimizer_%s_lr_%s_decay_%.2f_grad_clip_%.1f_val_metric_%s' % \
(opt.optimizer, opt.learning_rate, opt.lr_decay_rate, opt.grad_clip, opt.val_metric)
opt.logger_name = os.path.join(rootpath, trainCollection, opt.cv_name, valCollection, model_info, text_encode_info,
visual_encode_info, mapping_info, loss_info, optimizer_info, opt.postfix)
print(opt.logger_name)
if checkToSkip(os.path.join(opt.logger_name, 'model_best.pth.tar'), opt.overwrite):
sys.exit(0)
if checkToSkip(os.path.join(opt.logger_name, 'val_metric.txt'), opt.overwrite):
sys.exit(0)
makedirsforfile(os.path.join(opt.logger_name, 'val_metric.txt'))
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
tb_logger.configure(opt.logger_name, flush_secs=5)
opt.text_kernel_sizes = map(int, opt.text_kernel_sizes.split('-'))
opt.visual_kernel_sizes = map(int, opt.visual_kernel_sizes.split('-'))
# collections: trian, val
if multi_flag == 0:
collections = {'train': trainCollection, 'val': valCollection, 'test': testCollection}
cap_file = {'train': '%s.caption.txt'%trainCollection,
'val': '%s.caption.txt'%valCollection,
'test': '%s.caption.txt'%testCollection}
else:
collections = {'train': trainCollection, 'train2': trainCollection2, 'val': valCollection, 'test': testCollection}
cap_file = {'train': '%s.caption.txt'%trainCollection,
'train2': '%s.caption.txt'%trainCollection2,
'val': '%s.caption.txt'%valCollection,
'test': '%s.caption.txt'%testCollection}
# caption
caption_files = { x: os.path.join(rootpath, collections[x], 'TextData', cap_file[x])
for x in collections }
# Load visual features
visual_feat_path = {x: os.path.join(rootpath, collections[x], 'FeatureData', opt.visual_feature)
for x in collections }
visual_feats = {x: BigFile(visual_feat_path[x]) for x in visual_feat_path}
opt.visual_feat_dim = visual_feats['train'].ndims
# set bow vocabulary and encoding
bow_vocab_file = os.path.join(rootpath, opt.trainCollection, 'TextData', 'vocabulary', 'bow', opt.vocab+'.pkl')
bow_vocab = pickle.load(open(bow_vocab_file, 'rb'))
bow2vec = get_text_encoder('bow')(bow_vocab)
opt.bow_vocab_size = len(bow_vocab)
# set rnn vocabulary
rnn_vocab_file = os.path.join(rootpath, opt.trainCollection, 'TextData', 'vocabulary', 'rnn', opt.vocab+'.pkl')
rnn_vocab = pickle.load(open(rnn_vocab_file, 'rb'))
opt.vocab_size = len(rnn_vocab)
# initialize word embedding
opt.we_parameter = None
if opt.word_dim == 500:
w2v_data_path = os.path.join(rootpath, "word2vec", 'flickr', 'vec500flickr30m')
opt.we_parameter = get_we_parameter(rnn_vocab, w2v_data_path)
# mapping layer structure
opt.text_mapping_layers = map(int, opt.text_mapping_layers.split('-'))
opt.visual_mapping_layers = map(int, opt.visual_mapping_layers.split('-'))
if opt.concate == 'full':
opt.text_mapping_layers[0] = opt.bow_vocab_size + opt.text_rnn_size*2 + opt.text_kernel_num * len(opt.text_kernel_sizes)
opt.visual_mapping_layers[0] = opt.visual_feat_dim + opt.visual_rnn_size*2 + opt.visual_kernel_num * len(opt.visual_kernel_sizes)
elif opt.concate == 'reduced':
opt.text_mapping_layers[0] = opt.text_rnn_size*2 + opt.text_kernel_num * len(opt.text_kernel_sizes)
opt.visual_mapping_layers[0] = opt.visual_rnn_size*2 + opt.visual_kernel_num * len(opt.visual_kernel_sizes)
else:
raise NotImplementedError('Model %s not implemented'%opt.model)
video2frames = {x: read_dict(os.path.join(rootpath, collections[x], 'FeatureData', opt.visual_feature, 'video2frames.txt'))
for x in collections }
if opt.cross_domain_training == True:
video2frames_target = {'train': read_dict(os.path.join(rootpath, targettrainCollection, 'FeatureData', opt.visual_feature, 'video2frames.txt'))}
visual_feat_path_target = {'train': os.path.join(rootpath, targettrainCollection, 'FeatureData', opt.visual_feature)}
visual_feats_target = {'train': BigFile(visual_feat_path_target['train'])}
caption_file_target = os.path.join(rootpath, targettrainCollection, 'TextData', targettrainCollection+'.caption.txt')
data_loaders = data.get_data_loaders(
caption_files, visual_feats, rnn_vocab, bow2vec, opt.batch_size, opt.workers, opt.n_caption, video2frames=video2frames, video2frames_target=video2frames_target, visual_feats_target=visual_feats_target, caption_file_target=caption_file_target, multi_flag=multi_flag)
else:
data_loaders = data.get_data_loaders(
caption_files, visual_feats, rnn_vocab, bow2vec, opt.batch_size, opt.workers, opt.n_caption, video2frames=video2frames)
# Construct the model
model = get_model(opt.model)(opt)
opt.we_parameter = None
# optionally resume from a checkpoint
if opt.resume:
if os.path.isfile(opt.resume):
print("=> loading checkpoint '{}'".format(opt.resume))
checkpoint = torch.load(opt.resume)
start_epoch = checkpoint['epoch']
best_rsum = checkpoint['best_rsum']
model.load_state_dict(checkpoint['model'])
# Eiters is used to show logs as the continuation of another
# training
model.Eiters = checkpoint['Eiters']
print("=> loaded checkpoint '{}' (epoch {}, best_rsum {})"
.format(opt.resume, start_epoch, best_rsum))
# validate(opt, data_loaders['val'], model, measure=opt.measure)
else:
print("=> no checkpoint found at '{}'".format(opt.resume))
if opt.plot:
validate(opt, data_loaders['val'], model, measure=opt.measure, plot=opt.plot, target_loader=data_loaders['test'])
sys.exit(0)
# Train the Model
best_rsum = 0
no_impr_counter = 0
lr_counter = 0
best_epoch = None
fout_val_metric_hist = open(os.path.join(opt.logger_name, 'val_metric_hist.txt'), 'w')
for epoch in range(opt.num_epochs):
print('Epoch[{0} / {1}] LR: {2}'.format(epoch, opt.num_epochs, get_learning_rate(model.optimizer)[0]))
print('-'*10)
# train for one epoch
train(opt, data_loaders['train'], model, epoch)
rsum = validate(opt, data_loaders['val'], model, measure=opt.measure)
# evaluate on validation set
# remember best R@ sum and save checkpoint
is_best = rsum > best_rsum
best_rsum = max(rsum, best_rsum)
print(' * Current perf: {}'.format(rsum))
print(' * Best perf: {}'.format(best_rsum))
print('')
fout_val_metric_hist.write('epoch_%d: %f\n' % (epoch, rsum))
fout_val_metric_hist.flush()
if is_best:
save_checkpoint({
'epoch': epoch + 1,
'model': model.state_dict(),
'best_rsum': best_rsum,
'opt': opt,
'Eiters': model.Eiters,
}, is_best, filename='checkpoint_epoch_%s.pth.tar'%epoch, prefix=opt.logger_name + '/', best_epoch=best_epoch)
best_epoch = epoch
lr_counter += 1
decay_learning_rate(opt, model.optimizer, opt.lr_decay_rate)
if not is_best:
# Early stop occurs if the validation performance does not improve in ten consecutive epochs
no_impr_counter += 1
if no_impr_counter > 10:
print('Early stopping happended.\n')
break
# When the validation performance decreased after an epoch,
# we divide the learning rate by 2 and continue training;
# but we use each learning rate for at least 3 epochs.
if lr_counter > 2:
decay_learning_rate(opt, model.optimizer, 0.5)
lr_counter = 0
else:
no_impr_counter = 0
fout_val_metric_hist.close()
print('best performance on validation: {}\n'.format(best_rsum))
with open(os.path.join(opt.logger_name, 'val_metric.txt'), 'w') as fout:
fout.write('best performance on validation: ' + str(best_rsum))
# generate evaluation shell script
if testCollection == 'iacc.3':
templete = ''.join(open( 'util/TEMPLATE_do_predict.sh').readlines())
striptStr = templete.replace('@@@query_sets@@@', 'tv16.avs.txt,tv17.avs.txt,tv18.avs.txt')
else:
templete = ''.join(open( 'util/TEMPLATE_do_test.sh').readlines())
striptStr = templete.replace('@@@n_caption@@@', str(opt.n_caption))
striptStr = striptStr.replace('@@@rootpath@@@', rootpath)
striptStr = striptStr.replace('@@@testCollection@@@', testCollection)
striptStr = striptStr.replace('@@@logger_name@@@', opt.logger_name)
striptStr = striptStr.replace('@@@overwrite@@@', str(opt.overwrite))
# perform evaluation on test set
runfile = 'do_test_%s_%s.sh' % (opt.model, testCollection)
open(runfile, 'w').write(striptStr + '\n')
os.system('chmod +x %s' % runfile)
# os.system('./'+runfile)
def train(opt, train_loader, model, epoch):
# average meters to record the training statistics
batch_time = AverageMeter()
data_time = AverageMeter()
train_logger = LogCollector()
# switch to train mode
model.train_start()
progbar = Progbar(len(train_loader.dataset))
end = time.time()
for i, train_data in enumerate(train_loader):
# measure data loading time
data_time.update(time.time() - end)
# make sure train logger is used
model.logger = train_logger
# Update the model
if opt.cross_domain_training == True:
if opt.multi_flag == True:
b_size, loss = model.train_emb_ada_multi(*train_data)
else:
b_size, loss = model.train_emb_ada(*train_data)
else:
b_size, loss = model.train_emb(*train_data)
progbar.add(b_size, values=[('loss', loss)])
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
# Record logs in tensorboard
tb_logger.log_value('epoch', epoch, step=model.Eiters)
tb_logger.log_value('step', i, step=model.Eiters)
tb_logger.log_value('batch_time', batch_time.val, step=model.Eiters)
tb_logger.log_value('data_time', data_time.val, step=model.Eiters)
model.logger.tb_log(tb_logger, step=model.Eiters)
from matplotlib import cm
#def plot_with_labels(low_video_embs, low_cap_embs, target_low_video_embs, target_low_cap_embs, embs):
def plot_with_labels(low_video_embs, target_low_video_embs, embs):
plt.cla()
X, Y = embs[:, 0], embs[:, 1]
X1, Y1 = low_video_embs[:, 0], low_video_embs[:, 1]
# X2, Y2 = low_cap_embs[:, 0], low_cap_embs[:, 1]
X3, Y3 = target_low_video_embs[:, 0], target_low_video_embs[:, 1]
# X4, Y4 = target_low_video_embs[:, 0], target_low_video_embs[:, 1]
plt.scatter(X1, Y1, s=10, c='b', marker='o')
# plt.scatter(X2, Y2, s=10, c='b', marker='o')
plt.scatter(X3, Y3, s=10, c='r', marker='o')
# plt.scatter(X4, Y4, s=10, c='r', marker='o')
plt.xlim(X.min(), X.max())
plt.ylim(Y.min(), Y.max())
plt.savefig("test.jpg")
def validate(opt, val_loader, model, measure='cosine', plot=False, target_loader=None):
# compute the encoding for all the validation video and captions
video_embs, cap_embs, video_ids, caption_ids = evaluation.encode_data(model, val_loader, opt.log_step, logging.info)
if plot:
target_video_embs, target_cap_embs, _, _ = evaluation.encode_data2(model, target_loader, opt.log_step, logging.info)
tsne = TSNE(n_components=2, init='pca', random_state=0)
low_video_embs = tsne.fit_transform(video_embs)
# low_cap_embs = tsne.fit_transform(cap_embs)
target_low_video_embs = tsne.fit_transform(target_video_embs)
# target_low_cap_embs = tsne.fit_transform(target_cap_embs)
label0 = np.full((1000, 1), 0)
label1 = np.full((1000, 1), 1)
label2 = np.full((1000, 1), 2)
label3 = np.full((1000, 1), 3)
# print(low_video_embs.shape, low_cap_embs.shape, target_low_video_embs.shape, target_low_cap_embs.shape)
# embs = np.vstack((low_video_embs, low_cap_embs, target_low_video_embs, target_low_cap_embs))
embs = np.vstack((low_video_embs, target_low_video_embs))
# labels = np.vstack((label0, label1, label2, label3))
# print(embs.shape, labels.shape)
# plot_with_labels(low_video_embs, low_cap_embs, target_low_video_embs, target_low_cap_embs, embs)
plot_with_labels(low_video_embs, target_low_video_embs, embs)
print('debug finish')
return
# we load data as video-sentence pairs
# but we only need to forward each video once for evaluation
# so we get the video set and mask out same videos with feature_mask
feature_mask = []
evaluate_videos = set()
for video_id in video_ids:
feature_mask.append(video_id not in evaluate_videos)
evaluate_videos.add(video_id)
video_embs = video_embs[feature_mask]
video_ids = [x for idx, x in enumerate(video_ids) if feature_mask[idx] is True]
c2i_all_errors = evaluation.cal_error(video_embs, cap_embs, measure)
if opt.val_metric == "recall":
# video retrieval
(r1i, r5i, r10i, medri, meanri) = evaluation.t2i(c2i_all_errors, n_caption=opt.n_caption)
print(" * Text to video:")
print(" * r_1_5_10: {}".format([round(r1i, 3), round(r5i, 3), round(r10i, 3)]))
print(" * medr, meanr: {}".format([round(medri, 3), round(meanri, 3)]))
print(" * "+'-'*10)
# caption retrieval
(r1, r5, r10, medr, meanr) = evaluation.i2t(c2i_all_errors, n_caption=opt.n_caption)
print(" * Video to text:")
print(" * r_1_5_10: {}".format([round(r1, 3), round(r5, 3), round(r10, 3)]))
print(" * medr, meanr: {}".format([round(medr, 3), round(meanr, 3)]))
print(" * "+'-'*10)
# record metrics in tensorboard
tb_logger.log_value('r1', r1, step=model.Eiters)
tb_logger.log_value('r5', r5, step=model.Eiters)
tb_logger.log_value('r10', r10, step=model.Eiters)
tb_logger.log_value('medr', medr, step=model.Eiters)
tb_logger.log_value('meanr', meanr, step=model.Eiters)
tb_logger.log_value('r1i', r1i, step=model.Eiters)
tb_logger.log_value('r5i', r5i, step=model.Eiters)
tb_logger.log_value('r10i', r10i, step=model.Eiters)
tb_logger.log_value('medri', medri, step=model.Eiters)
tb_logger.log_value('meanri', meanri, step=model.Eiters)
elif opt.val_metric == "map":
i2t_map_score = evaluation.i2t_map(c2i_all_errors, n_caption=opt.n_caption)
t2i_map_score = evaluation.t2i_map(c2i_all_errors, n_caption=opt.n_caption)
tb_logger.log_value('i2t_map', i2t_map_score, step=model.Eiters)
tb_logger.log_value('t2i_map', t2i_map_score, step=model.Eiters)
print('i2t_map', i2t_map_score)
print('t2i_map', t2i_map_score)
currscore = 0
if opt.val_metric == "recall":
if opt.direction == 'i2t' or opt.direction == 'all':
currscore += (r1 + r5 + r10)
if opt.direction == 't2i' or opt.direction == 'all':
currscore += (r1i + r5i + r10i)
elif opt.val_metric == "map":
if opt.direction == 'i2t' or opt.direction == 'all':
currscore += i2t_map_score
if opt.direction == 't2i' or opt.direction == 'all':
currscore += t2i_map_score
tb_logger.log_value('rsum', currscore, step=model.Eiters)
return currscore
def save_checkpoint(state, is_best, filename='checkpoint.pth.tar', prefix='', best_epoch=None):
"""save checkpoint at specific path"""
torch.save(state, prefix + filename)
if is_best:
shutil.copyfile(prefix + filename, prefix + 'model_best.pth.tar')
if best_epoch is not None:
os.remove(prefix + 'checkpoint_epoch_%s.pth.tar'%best_epoch)
def decay_learning_rate(opt, optimizer, decay):
"""decay learning rate to the last LR"""
for param_group in optimizer.param_groups:
param_group['lr'] = param_group['lr']*decay
def get_learning_rate(optimizer):
"""Return learning rate"""
lr_list = []
for param_group in optimizer.param_groups:
lr_list.append(param_group['lr'])
return lr_list
def accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
if __name__ == '__main__':
main()