-
Notifications
You must be signed in to change notification settings - Fork 131
/
eval.py
executable file
·122 lines (104 loc) · 5.01 KB
/
eval.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
import json
import os
import argparse
import torch
from torch import nn
from torch.autograd import Variable
from torch.utils.data import DataLoader
from models import EncoderRNN, DecoderRNN, S2VTAttModel, S2VTModel
from dataloader import VideoDataset
import misc.utils as utils
from misc.cocoeval import suppress_stdout_stderr, COCOScorer
from pandas.io.json import json_normalize
def convert_data_to_coco_scorer_format(data_frame):
gts = {}
for row in zip(data_frame["caption"], data_frame["video_id"]):
if row[1] in gts:
gts[row[1]].append(
{'image_id': row[1], 'cap_id': len(gts[row[1]]), 'caption': row[0]})
else:
gts[row[1]] = []
gts[row[1]].append(
{'image_id': row[1], 'cap_id': len(gts[row[1]]), 'caption': row[0]})
return gts
def test(model, crit, dataset, vocab, opt):
model.eval()
loader = DataLoader(dataset, batch_size=opt["batch_size"], shuffle=True)
scorer = COCOScorer()
gt_dataframe = json_normalize(
json.load(open(opt["input_json"]))['sentences'])
gts = convert_data_to_coco_scorer_format(gt_dataframe)
results = []
samples = {}
for data in loader:
# forward the model to get loss
fc_feats = data['fc_feats'].cuda()
labels = data['labels'].cuda()
masks = data['masks'].cuda()
video_ids = data['video_ids']
# forward the model to also get generated samples for each image
with torch.no_grad():
seq_probs, seq_preds = model(
fc_feats, mode='inference', opt=opt)
sents = utils.decode_sequence(vocab, seq_preds)
for k, sent in enumerate(sents):
video_id = video_ids[k]
samples[video_id] = [{'image_id': video_id, 'caption': sent}]
with suppress_stdout_stderr():
valid_score = scorer.score(gts, samples, samples.keys())
results.append(valid_score)
print(valid_score)
if not os.path.exists(opt["results_path"]):
os.makedirs(opt["results_path"])
with open(os.path.join(opt["results_path"], "scores.txt"), 'a') as scores_table:
scores_table.write(json.dumps(results[0]) + "\n")
with open(os.path.join(opt["results_path"],
opt["model"].split("/")[-1].split('.')[0] + ".json"), 'w') as prediction_results:
json.dump({"predictions": samples, "scores": valid_score},
prediction_results)
def main(opt):
dataset = VideoDataset(opt, "test")
opt["vocab_size"] = dataset.get_vocab_size()
opt["seq_length"] = dataset.max_len
if opt["model"] == 'S2VTModel':
model = S2VTModel(opt["vocab_size"], opt["max_len"], opt["dim_hidden"], opt["dim_word"],
rnn_dropout_p=opt["rnn_dropout_p"]).cuda()
elif opt["model"] == "S2VTAttModel":
encoder = EncoderRNN(opt["dim_vid"], opt["dim_hidden"], bidirectional=opt["bidirectional"],
input_dropout_p=opt["input_dropout_p"], rnn_dropout_p=opt["rnn_dropout_p"])
decoder = DecoderRNN(opt["vocab_size"], opt["max_len"], opt["dim_hidden"], opt["dim_word"],
input_dropout_p=opt["input_dropout_p"],
rnn_dropout_p=opt["rnn_dropout_p"], bidirectional=opt["bidirectional"])
model = S2VTAttModel(encoder, decoder).cuda()
#model = nn.DataParallel(model)
# Setup the model
model.load_state_dict(torch.load(opt["saved_model"]))
crit = utils.LanguageModelCriterion()
test(model, crit, dataset, dataset.get_vocab(), opt)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--recover_opt', type=str, required=True,
help='recover train opts from saved opt_json')
parser.add_argument('--saved_model', type=str, default='',
help='path to saved model to evaluate')
parser.add_argument('--dump_json', type=int, default=1,
help='Dump json with predictions into vis folder? (1=yes,0=no)')
parser.add_argument('--results_path', type=str, default='results/')
parser.add_argument('--dump_path', type=int, default=0,
help='Write image paths along with predictions into vis json? (1=yes,0=no)')
parser.add_argument('--gpu', type=str, default='0',
help='gpu device number')
parser.add_argument('--batch_size', type=int, default=128,
help='minibatch size')
parser.add_argument('--sample_max', type=int, default=1,
help='0/1. whether sample max probs to get next word in inference stage')
parser.add_argument('--temperature', type=float, default=1.0)
parser.add_argument('--beam_size', type=int, default=1,
help='used when sample_max = 1. Usually 2 or 3 works well.')
args = parser.parse_args()
args = vars((args))
opt = json.load(open(args["recover_opt"]))
for k, v in args.items():
opt[k] = v
os.environ['CUDA_VISIBLE_DEVICES'] = opt["gpu"]
main(opt)