def __init__(self, path=DEFAULT_MODEL_PATH):
     # TODO Replace this part with SavedModel
     g = tf.Graph()
     with g.as_default():
         model = inference_wrapper.InferenceWrapper()
         restore_fn = model.build_graph_from_config(configuration.ModelConfig(),
                                                    path)
     g.finalize()
     self.model = model
     sess = tf.Session(graph=g)
     # Load the model from checkpoint.
     restore_fn(sess)
     self.sess = sess
Ejemplo n.º 2
0
def main(_):
    # Build the inference graph.
    g = tf.Graph()
    FLAGS.checkpoint_path = 'checkpoint/model2.ckpt-2000000'
    FLAGS.input_files = '../assets/COCO_val2014_000000224477.jpg'
    FLAGS.vocab_file = '../assets/word_counts.txt'

    # TODO Replace this part with SavedModel
    with g.as_default():
        model = inference_wrapper.InferenceWrapper()
        restore_fn = model.build_graph_from_config(configuration.ModelConfig(),
                                                   FLAGS.checkpoint_path)
    g.finalize()

    # Create the vocabulary.
    vocab = vocabulary.Vocabulary(FLAGS.vocab_file)

    filenames = []
    for file_pattern in FLAGS.input_files.split(","):
        filenames.extend(tf.gfile.Glob(file_pattern))
    tf.logging.info("Running caption generation on %d files matching %s",
                    len(filenames), FLAGS.input_files)

    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)

        for filename in filenames:
            with tf.gfile.GFile(filename, "rb") as f:
                image = f.read()
            captions = generator.beam_search(sess, image)
            print("Captions for image %s:" % os.path.basename(filename))
            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)
                print("  %d) %s (p=%f)" %
                      (i, sentence, math.exp(caption.logprob)))