-
Notifications
You must be signed in to change notification settings - Fork 2
/
infer.py
executable file
·422 lines (347 loc) · 16.9 KB
/
infer.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
#!/usr/bin/env python3
import argparse
import json
import os
import sys
from pathlib import Path
from datetime import datetime
import torch
from torchvision import transforms
from vocabulary import paragraph_ids_to_words, caption_ids_ext_to_words, \
remove_duplicate_sentences, remove_incomplete_sentences, get_vocab
from dataset import get_loader, DatasetParams
from model.encoder_decoder import ModelParams, EncoderDecoder
from utils import basename
try:
from tqdm import tqdm
except ImportError as e:
print('WARNING: tqdm module not found. Install it if you want a fancy progress bar :-)')
def tqdm(x, disable=False): return x
class AttributeDict(dict):
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__
# Device configuration now in infer()
device = None
class infer_object:
def __init__(self, args):
# print('__init__() :', args)
if 'cpu' not in args:
args['cpu'] = False
if 'vocab' not in args:
args['vocab'] = None
if 'ext_features' not in args:
args['ext_features'] = None
if 'ext_persist_features' not in args:
args['ext_persist_features'] = None
if 'lemma_pos_rules' not in args:
args['lemma_pos_rules'] = None
global device
device = torch.device('cuda' if torch.cuda.is_available() and
not args['cpu'] else 'cpu')
vi = sys.version_info
print('Python version {}.{}.{}, torch version {}'.format(vi[0], vi[1], vi[2],
torch.__version__))
# Build models
print('Bulding model for device {}'.format(device.type))
try:
self.state = torch.load(args['model'], map_location=device)
except AttributeError:
print('WARNING: Old model found. '+
'Please use model_update.py in the model before executing this script.')
exit(1)
self.params = ModelParams(self.state)
if args['ext_features']:
self.params.update_ext_features(args['ext_features'])
if args['ext_persist_features']:
self.params.update_ext_persist_features(args['ext_persist_features'])
print('Loaded model parameters from <{}>:'.format(args['model']))
print(self.params)
# Load the vocabulary:
if args['vocab'] is not None:
# Loading vocabulary from file path supplied by the user:
args_attr = AttributeDict(args)
self.vocab = get_vocab(args_attr)
elif self.params.vocab is not None:
print('Loading vocabulary stored in the model file.')
self.vocab = self.params.vocab
else:
print('ERROR: you must either load a model that contains vocabulary or '
'specify a vocabulary with the --vocab option!')
sys.exit(1)
print('Size of the vocabulary is {}'.format(len(self.vocab)))
ef_dims = None
self.model = EncoderDecoder(self.params, device, len(self.vocab), self.state, ef_dims).eval()
# print(self.params.ext_features_dim)
self.lemma_pos_rules = {}
self.pos_names = set()
if args['lemma_pos_rules'] is not None:
self.read_lemma_pos_rules(args['lemma_pos_rules'])
def external_features(self):
ef_dims = self.params.ext_features_dim
ret = [(self.state['features'].external, ef_dims[0]),
(self.state['persist_features'].external, ef_dims[1])]
# print(ret)
return ret
def read_lemma_pos_rules(self, file):
# print('Reading lemma_pos_rules from <{}>'.format(file))
with open(file) as fp:
n = 0
for line in fp:
ll = line.split(' ')
l, p, w = ll[:3]
# print (l, p, w)
if p not in self.lemma_pos_rules:
self.lemma_pos_rules[p] = {}
assert l not in self.lemma_pos_rules[p], 'l in lemma_pos_rules[p]'
self.lemma_pos_rules[p][l] = w
n += 1
self.pos_names = self.lemma_pos_rules.keys()
print('Read {} lemma_pos_rules for {} pos from <{}>'.
format(n, len(self.pos_names), file))
assert len(self.pos_names)>0, 'failed reading lemma_pos_rules from <{}>'.format(file)
def apply_lemma_pos_rules(self, caption):
if len(self.pos_names)==0:
return caption
w = caption.split(' ')
x = []
for i in range(len(w)):
if w[i] in self.pos_names:
continue
if i+1<len(w) and w[i+1] in self.pos_names:
if w[i] in self.lemma_pos_rules[w[i+1]]:
x.append(self.lemma_pos_rules[w[i+1]][w[i]])
else:
x.append(w[i])
else:
x.append(w[i])
return ' '.join(x)
def infer(self, args):
# print('infer() :', args)
if 'image_features' not in args:
args['image_features'] = None
# Image preprocessing
transform = transforms.Compose([
transforms.Resize((args['resize'], args['resize'])),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406),
(0.229, 0.224, 0.225))])
# Get dataset parameters:
dataset_configs = DatasetParams(args['dataset_config_file'])
dataset_params = dataset_configs.get_params(args['dataset'],
args['image_dir'],
args['image_files'],
args['image_features'])
if self.params.has_external_features() and \
any(dc.name == 'generic' for dc in dataset_params):
print('WARNING: you cannot use external features without specifying all datasets in '
'datasets.conf.')
print('Hint: take a look at datasets/datasets.conf.default.')
# Build data loader
print("Loading dataset: {}".format(args['dataset']))
# Update dataset params with needed model params:
for i in dataset_params:
i.config_dict['skip_start_token'] = self.params.skip_start_token
# For visualizing attention we need file names instead of IDs in our output:
if args['store_image_paths']:
i.config_dict['return_image_file_name'] = True
ext_feature_sets = [self.params.features.external, self.params.persist_features.external]
if args['dataset']=='incore':
ext_feature_sets = None
# We ask it to iterate over images instead of all (image, caption) pairs
data_loader, ef_dims = get_loader(dataset_params, vocab=None, transform=transform,
batch_size=args['batch_size'], shuffle=False,
num_workers=args['num_workers'],
ext_feature_sets=ext_feature_sets,
skip_images=not self.params.has_internal_features(),
iter_over_images=True)
self.data_loader = data_loader
# Create model directory
if not os.path.exists(args['results_path']):
os.makedirs(args['results_path'])
scorers = {}
if args['scoring'] is not None:
for s in args['scoring'].split(','):
s = s.lower().strip()
if s == 'cider':
from eval.cider import Cider
scorers['CIDEr'] = Cider(df='corpus')
# Store captions here:
output_data = []
gts = {}
res = {}
print('Starting inference, max sentence length: {} num_workers: {}'.\
format(args['max_seq_length'], args['num_workers']))
show_progress = sys.stderr.isatty() and not args['verbose'] \
and ext_feature_sets is not None
for i, (images, ref_captions, lengths, image_ids,
features) in enumerate(tqdm(self.data_loader, disable=not show_progress)):
if len(scorers) > 0:
for j in range(len(ref_captions)):
jid = image_ids[j]
if jid not in gts:
gts[jid] = []
rcs = ref_captions[j]
if type(rcs) is str:
rcs = [rcs]
for rc in rcs:
gts[jid].append(rc.lower())
images = images.to(device)
init_features = features[0].to(device) if len(features) > 0 and \
features[0] is not None else None
persist_features = features[1].to(device) if len(features) > 1 and \
features[1] is not None else None
# Generate a caption from the image
sampled_batch = self.model.sample(images, init_features, persist_features,
max_seq_length=args['max_seq_length'],
start_token_id=self.vocab('<start>'),
end_token_id=self.vocab('<end>'),
alternatives=args['alternatives'],
probabilities=args['probabilities'])
sampled_ids_batch = sampled_batch
for i in range(len(sampled_ids_batch)):
sampled_ids = sampled_ids_batch[i]
# Convert word_ids to words
if self.params.hierarchical_model:
# assert False, 'paragraph_ids_to_words() need to be updated'
caption = paragraph_ids_to_words(sampled_ids, self.vocab,
skip_start_token=True)
else:
caption = caption_ids_ext_to_words(sampled_ids, self.vocab,
skip_start_token=True,
capitalize=not args['no_capitalize'])
if args['no_repeat_sentences']:
caption = remove_duplicate_sentences(caption)
if args['only_complete_sentences']:
caption = remove_incomplete_sentences(caption)
if args['verbose']:
print('=>', caption)
if True:
caption = self.apply_lemma_pos_rules(caption)
if args['verbose']:
print('#>', caption)
output_data.append({'image_id': image_ids[i],
'caption': caption})
res[image_ids[i]] = [caption.lower()]
for score_name, scorer in scorers.items():
score = scorer.compute_score(gts, res)[0]
print('Test', score_name, score)
# Decide output format, fall back to txt
if args['output_format'] is not None:
output_format = args['output_format']
elif args['output_file'] and args['output_file'].endswith('.json'):
output_format = 'json'
else:
output_format = 'txt'
# Create a sensible default output path for results:
output_file = None
if not args['output_file'] and not args['print_results']:
model_name_path = Path(args['model'])
is_in_same_folder = len(model_name_path.parents) == 1
if not is_in_same_folder:
model_name = args['model'].split(os.sep)[-2]
model_epoch = basename(args['model'])
output_file = '{}-{}.{}'.format(model_name, model_epoch,
output_format)
else:
output_file = model_name_path.stem + '.' + output_format
else:
output_file = args['output_file']
if output_file:
output_path = os.path.join(args['results_path'], output_file)
if output_format == 'json':
json.dump(output_data, open(output_path, 'w'))
else:
with open(output_path, 'w') as fp:
for data in output_data:
print(data['image_id'], data['caption'], file=fp)
print('Wrote generated captions to {} as {}'.
format(output_path, args['output_format']))
if args['print_results']:
for d in output_data:
print('{}: {}'.format(d['image_id'], d['caption']))
return output_data
def inferx(a):
args = argparse.Namespace()
args.image_files = []
args.dataset = 'incore'
args.resize = 224
args.batch_size = 128
args.num_workers = 2
args.max_seq_length = 20
args.results_path = '/tmp'
args.ext_features = []
args.ext_persist_features = []
args.store_image_paths = ''
args.dataset_config_file = ''
args.scoring = ''
args.image_dir = ''
args.vocab = None
args.output_format = None
args.output_file = False
args.verbose = False
args.no_repeat_sentences = False
args.only_complete_sentences = False
args.print_results = True
d = vars(args)
for k, v in a.items():
d[k] = v;
return main(args)
def main(args):
a = vars(args)
infobj = infer_object(a)
infobj.infer(a)
def parse_args(ext_args=None):
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, default='generic',
help='which dataset to use')
parser.add_argument('--dataset_config_file', type=str,
default='datasets/datasets.conf',
help='location of dataset configuration file')
parser.add_argument('--resize', type=int, default=224,
help='resize input image to this size')
parser.add_argument('--batch_size', type=int, default=128)
parser.add_argument('--num_workers', type=int, default=2)
parser.add_argument('--alternatives', type=int, default=1)
parser.add_argument('--probabilities', action='store_true')
parser.add_argument('image_files', type=str, nargs='*')
parser.add_argument('--image_dir', type=str,
help='input image dir for generating captions')
parser.add_argument('--model', type=str, required=True,
help='path to existing model')
parser.add_argument('--vocab', type=str, help='path for vocabulary wrapper')
parser.add_argument('--no_capitalize', action='store_true',
help='prevent final capitalization of the result')
parser.add_argument('--lemma_pos_rules', type=str,
help='file containing rules to map "lemma POS" to "word"')
parser.add_argument('--ext_features', type=str,
help='paths for the external features, overrides the '
'paths in the model ckpt file (which are the ones '
'used for training), comma separated')
parser.add_argument('--ext_persist_features', type=str,
help='paths for external persist features')
parser.add_argument('--store_image_paths', action='store_true',
help='Save paths to images in the output file')
parser.add_argument('--output_file', type=str,
help='path for output file, default: model_name.txt')
parser.add_argument('--output_format', type=str, help='format of the output file')
parser.add_argument('--verbose', help='verbose output',
action='store_true')
parser.add_argument('--results_path', type=str, default='output/results/',
help='path for saving results')
parser.add_argument('--print_results', action='store_true')
parser.add_argument('--scoring', type=str)
parser.add_argument('--max_seq_length', type=int, default=20,
help='maximum allowed length of the decoded sequence')
parser.add_argument('--no_repeat_sentences', action='store_true',
help='allow repeating sentences inside a paragraph')
parser.add_argument('--only_complete_sentences', action='store_true')
parser.add_argument('--cpu', action="store_true",
help="Use CPU even when GPU is available")
return parser.parse_args(ext_args)
if __name__ == '__main__':
args = parse_args()
begin = datetime.now()
print('Started inference at {}.'.format(begin))
main(args=args)
end = datetime.now()
print('Inference ended at {}. Total time: {}.'.format(end, end - begin))