-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_inference.py
81 lines (68 loc) · 2.76 KB
/
run_inference.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
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
r"""Generate captions for images using default beam search parameters."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import math
import os
import tensorflow as tf
import inference_wrapper
# Folder Lib Import
import sys
sys.path.insert(0, os.getcwd() + '/inference_utils')
# Folder Lib Import
import configuration
import caption_generator
import vocabulary
tf.logging.set_verbosity(tf.logging.INFO)
# Function to generate captions
def img_captions(file_inputs):
# Build the inference graph.
g = tf.Graph()
with g.as_default():
model = inference_wrapper.InferenceWrapper()
restore_fn = model.build_graph_from_config(configuration.ModelConfig(),
file_inputs[0])
g.finalize()
# Create the vocabulary.
vocab = vocabulary.Vocabulary(file_inputs[1])
filenames = []
for file_pattern in file_inputs[2].split(","):
filenames.extend(tf.gfile.Glob(file_pattern))
tf.logging.info("Running caption generation on %d files matching %s",
len(filenames), file_inputs[2])
with tf.Session(graph=g) as sess:
# Load the model from checkpoint.
restore_fn(sess)
# Prepare the caption generator. Here we are implicitly using the default
# beam search parameters. See caption_generator.py for a description of the
# available beam search parameters.
generator = caption_generator.CaptionGenerator(model, vocab)
caption_list = list()
prob_list = list()
for filename in filenames:
with tf.gfile.GFile(filename, "rb") as f:
image = f.read()
captions, probs = generator.beam_search(sess, image)
prob_list.append('['+", ".join(map(str, probs))+']')
loc_cap_list = list()
for i, caption in enumerate(captions):
# Ignore begin and end words.
sentence = [vocab.id_to_word(w) for w in caption.sentence[1:-1]]
sentence = " ".join(sentence).split('<S>')[0]
loc_cap_list.append([sentence, math.exp(caption.logprob)])
caption_list.append(loc_cap_list)
return prob_list, caption_list