Skip to content

Commit a1445e4

Browse files
dmitriy-serdyukruotianluo
authored andcommitted
Fix eval script with python3 and pytorch0.4 (#72)
* Refactor * Fix typo
1 parent 7ad027f commit a1445e4

File tree

3 files changed

+46
-40
lines changed

3 files changed

+46
-40
lines changed

eval.py

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python
12
from __future__ import absolute_import
23
from __future__ import division
34
from __future__ import print_function
@@ -21,59 +22,59 @@
2122
# Input arguments and options
2223
parser = argparse.ArgumentParser()
2324
# Input paths
24-
parser.add_argument('--model', type=str, default='',
25-
help='path to model to evaluate')
25+
parser.add_argument('--model', type=str, required=True,
26+
help='path to model to evaluate')
2627
parser.add_argument('--cnn_model', type=str, default='resnet101',
27-
help='resnet101, resnet152')
28-
parser.add_argument('--infos_path', type=str, default='',
29-
help='path to infos to evaluate')
28+
help='resnet101, resnet152')
29+
parser.add_argument('--infos_path', type=str, required=True,
30+
help='path to infos to evaluate')
3031
# Basic options
3132
parser.add_argument('--batch_size', type=int, default=0,
32-
help='if > 0 then overrule, otherwise load from checkpoint.')
33+
help='if > 0 then overrule, otherwise load from checkpoint.')
3334
parser.add_argument('--num_images', type=int, default=-1,
34-
help='how many images to use when periodically evaluating the loss? (-1 = all)')
35+
help='how many images to use when periodically evaluating the loss? (-1 = all)')
3536
parser.add_argument('--language_eval', type=int, default=0,
36-
help='Evaluate language as well (1 = yes, 0 = no)? BLEU/CIDEr/METEOR/ROUGE_L? requires coco-caption code from Github.')
37+
help='Evaluate language as well (1 = yes, 0 = no)? BLEU/CIDEr/METEOR/ROUGE_L? requires coco-caption code from Github.')
3738
parser.add_argument('--dump_images', type=int, default=1,
38-
help='Dump images into vis/imgs folder for vis? (1=yes,0=no)')
39+
help='Dump images into vis/imgs folder for vis? (1=yes,0=no)')
3940
parser.add_argument('--dump_json', type=int, default=1,
40-
help='Dump json with predictions into vis folder? (1=yes,0=no)')
41+
help='Dump json with predictions into vis folder? (1=yes,0=no)')
4142
parser.add_argument('--dump_path', type=int, default=0,
42-
help='Write image paths along with predictions into vis json? (1=yes,0=no)')
43+
help='Write image paths along with predictions into vis json? (1=yes,0=no)')
4344

4445
# Sampling options
4546
parser.add_argument('--sample_max', type=int, default=1,
46-
help='1 = sample argmax words. 0 = sample from distributions.')
47+
help='1 = sample argmax words. 0 = sample from distributions.')
4748
parser.add_argument('--beam_size', type=int, default=2,
48-
help='used when sample_max = 1, indicates number of beams in beam search. Usually 2 or 3 works well. More is not better. Set this to 1 for faster runtime but a bit worse performance.')
49+
help='used when sample_max = 1, indicates number of beams in beam search. Usually 2 or 3 works well. More is not better. Set this to 1 for faster runtime but a bit worse performance.')
4950
parser.add_argument('--temperature', type=float, default=1.0,
50-
help='temperature when sampling from distributions (i.e. when sample_max = 0). Lower = "safer" predictions.')
51+
help='temperature when sampling from distributions (i.e. when sample_max = 0). Lower = "safer" predictions.')
5152
# For evaluation on a folder of images:
52-
parser.add_argument('--image_folder', type=str, default='',
53-
help='If this is nonempty then will predict on the images in this folder path')
54-
parser.add_argument('--image_root', type=str, default='',
55-
help='In case the image paths have to be preprended with a root path to an image folder')
53+
parser.add_argument('--image_folder', type=str, default='',
54+
help='If this is nonempty then will predict on the images in this folder path')
55+
parser.add_argument('--image_root', type=str, default='',
56+
help='In case the image paths have to be preprended with a root path to an image folder')
5657
# For evaluation on MSCOCO images from some split:
5758
parser.add_argument('--input_fc_dir', type=str, default='',
58-
help='path to the h5file containing the preprocessed dataset')
59+
help='path to the h5file containing the preprocessed dataset')
5960
parser.add_argument('--input_att_dir', type=str, default='',
60-
help='path to the h5file containing the preprocessed dataset')
61+
help='path to the h5file containing the preprocessed dataset')
6162
parser.add_argument('--input_label_h5', type=str, default='',
62-
help='path to the h5file containing the preprocessed dataset')
63-
parser.add_argument('--input_json', type=str, default='',
64-
help='path to the json file containing additional info and vocab. empty = fetch from model checkpoint.')
65-
parser.add_argument('--split', type=str, default='test',
66-
help='if running on MSCOCO images, which split to use: val|test|train')
67-
parser.add_argument('--coco_json', type=str, default='',
68-
help='if nonempty then use this file in DataLoaderRaw (see docs there). Used only in MSCOCO test evaluation, where we have a specific json file of only test set images.')
63+
help='path to the h5file containing the preprocessed dataset')
64+
parser.add_argument('--input_json', type=str, default='',
65+
help='path to the json file containing additional info and vocab. empty = fetch from model checkpoint.')
66+
parser.add_argument('--split', type=str, default='test',
67+
help='if running on MSCOCO images, which split to use: val|test|train')
68+
parser.add_argument('--coco_json', type=str, default='',
69+
help='if nonempty then use this file in DataLoaderRaw (see docs there). Used only in MSCOCO test evaluation, where we have a specific json file of only test set images.')
6970
# misc
70-
parser.add_argument('--id', type=str, default='',
71-
help='an id identifying this run/job. used only if language_eval = 1 for appending to intermediate files')
71+
parser.add_argument('--id', type=str, default='',
72+
help='an id identifying this run/job. used only if language_eval = 1 for appending to intermediate files')
7273

7374
opt = parser.parse_args()
7475

7576
# Load infos
76-
with open(opt.infos_path) as f:
77+
with open(opt.infos_path, 'rb') as f:
7778
infos = cPickle.load(f)
7879

7980
# override and collect parameters
@@ -106,9 +107,9 @@
106107

107108
# Create the Data Loader instance
108109
if len(opt.image_folder) == 0:
109-
loader = DataLoader(opt)
110+
loader = DataLoader(opt)
110111
else:
111-
loader = DataLoaderRaw({'folder_path': opt.image_folder,
112+
loader = DataLoaderRaw({'folder_path': opt.image_folder,
112113
'coco_json': opt.coco_json,
113114
'batch_size': opt.batch_size,
114115
'cnn_model': opt.cnn_model})
@@ -118,12 +119,13 @@
118119

119120

120121
# Set sample options
121-
loss, split_predictions, lang_stats = eval_utils.eval_split(model, crit, loader,
122+
loss, split_predictions, lang_stats = eval_utils.eval_split(
123+
model, crit, loader,
122124
vars(opt))
123125

124126
print('loss: ', loss)
125127
if lang_stats:
126-
print(lang_stats)
128+
print(lang_stats)
127129

128130
if opt.dump_json == 1:
129131
# dump the json

models/CaptionModel.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,20 @@ def beam_step(logprobsf, beam_size, t, beam_seq, beam_seq_logprobs, beam_logprob
3737
#beam_seq_logprobs : log-probability of each decision made, same size as beam_seq
3838
#beam_logprobs_sum : joint log-probability of each beam
3939

40-
ys,ix = torch.sort(logprobsf,1,True)
40+
ys, ix = torch.sort(logprobsf, 1, True)
4141
candidates = []
4242
cols = min(beam_size, ys.size(1))
4343
rows = beam_size
4444
if t == 0:
4545
rows = 1
4646
for c in range(cols): # for each column (word, essentially)
4747
for q in range(rows): # for each beam expansion
48-
#compute logprob of expanding beam q with word in (sorted) position c
49-
local_logprob = ys[q,c]
50-
candidate_logprob = beam_logprobs_sum[q] + local_logprob
51-
candidates.append({'c':ix[q,c], 'q':q, 'p':candidate_logprob, 'r':local_logprob})
48+
# compute logprob of expanding beam q with word in (sorted) position c
49+
local_logprob = ys[q, c]
50+
candidate_logprob = beam_logprobs_sum[q] + local_logprob.cpu()
51+
candidates.append(dict(c=ix[q, c], q=q,
52+
p=candidate_logprob,
53+
r=local_logprob))
5254
candidates = sorted(candidates, key=lambda x: -x['p'])
5355

5456
new_state = [_.clone() for _ in state]
@@ -80,7 +82,8 @@ def beam_step(logprobsf, beam_size, t, beam_seq, beam_seq_logprobs, beam_logprob
8082

8183
beam_seq = torch.LongTensor(self.seq_length, beam_size).zero_()
8284
beam_seq_logprobs = torch.FloatTensor(self.seq_length, beam_size).zero_()
83-
beam_logprobs_sum = torch.zeros(beam_size) # running sum of logprobs for each beam
85+
# running sum of logprobs for each beam
86+
beam_logprobs_sum = torch.zeros(beam_size)
8487
done_beams = []
8588

8689
for t in range(self.seq_length):

train.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python
12
from __future__ import absolute_import
23
from __future__ import division
34
from __future__ import print_function

0 commit comments

Comments
 (0)