LabAssignment2Report - aaz000966/CS5542 GitHub Wiki

CS5542 Big Data Analytics and App Lab Assignment #1 Report Zakari, Abdulmuhaymin (29)

This is a lab assignment report for generating captions of a dataset using the Show and Tell model. Main(): Taking input files from the project profiles. These files are the model, the captions file, and the input images.

  1. Disclaimer: This program is inspired directly by the codes that were given in CS5542 (UMKC Class) By Ms. Mayanka Shekar, and on it we're trying to improve and develop!

  1. #required libraries
  2. from future import absolute_import
  3. from future import division
  4. from future import print_function
  5. import nltk
  6. import nltk.translate.gleu_score as gleu
  7. import numpy
  8. import os
  9. try:
  10. nltk.data.find('tokenizers/punkt')
  11. except LookupError:
  12. nltk.download('punkt')
  13. import nltk
  14. try:
  15. nltk.data.find('tokenizers/punkt')
  16. except LookupError:
  17. nltk.download('punkt')
  18. from nltk.translate.bleu_score import sentence_bleu
  19. import logging
  20. import math
  21. import tensorflow as tf
  22. from main.caption_generator import CaptionGenerator
  23. from main.model import ShowAndTellModel
  24. from main.vocabulary import Vocabulary
  25. FLAGS = tf.flags.FLAGS
  26. setting input files and model location

  27. tf.flags.DEFINE_string("model_path", "..\show-and-tell.pb", "Model path")
  28. tf.flags.DEFINE_string("vocab_file", "..\word_counts.txt", "vocabularies.")
  29. tf.flags.DEFINE_string("input_files", r"..\in3.jpg", "input")
  30. logging.basicConfig(level=logging.INFO)
  31. logger = logging.getLogger(name)
  32. def main(_):
  33. model = ShowAndTellModel(FLAGS.model_path)
    
  34. vocab = Vocabulary(FLAGS.vocab_file)
    
  35. filenames = _load_filenames()
    
  36. can1 = "a table with different kinds of food"
    
  37. candidate=can1.split()
    
  38. generator = CaptionGenerator(model, vocab)
    
  39. for filename in filenames:
    
  40.     with tf.gfile.GFile(filename, "rb") as f:
    
  41.         image = f.read()
    
  42.     captions = generator.beam_search(image)
    
  43.     print("Captions: ")
    
  44.     for i, caption in enumerate(captions):
    
  45.         sentence = [vocab.id_to_token(w) for w in caption.sentence[1:-1]]
    
  46.         sentence = " ".join(sentence)
    
  47.         temp = "  %d) %s (p=%f)" % (i+1, sentence, math.exp(caption.logprob))
    
  48.         print(temp)
    
  49.         comp = [sentence.split()]
    
  50.         # Calculating The Blue Score
    
  51.         print('Blue cumulative 1-gram: %f' % sentence_bleu(comp, candidate, weights=(1, 0, 0, 0)))
    
  52.         print('Blue cumulative 2-gram: %f' % sentence_bleu(comp, candidate, weights=(0.5, 0.5, 0, 0)))
    
  53.         # Glue Score
    
  54.         G = gleu.sentence_gleu(comp, candidate, min_len=1, max_len=2)
    
  55.         print("Glue score for this sentence: {}".format(G))
    
  56. def _load_filenames():
  57. filenames = []
    
  58. for file_pattern in FLAGS.input_files.split(","):
    
  59.     filenames.extend(tf.gfile.Glob(file_pattern))
    
  60. logger.info("Running caption generation on %d files matching %s",
    
  61.             len(filenames), FLAGS.input_files)
    
  62. return filenames
    
  63. if name == "main":
  64. tf.app.run()
    

The model file:

  1. import logging
  2. import os
  3. import tensorflow as tf
  4. class ShowAndTellModel(object):
  5.  def __init__(self, model_path):
    
  6.      self._model_path = model_path
    
  7.     self.logger = logging.getLogger(__name__)
    
  8.     self._load_model(model_path)
    
  9.     self._sess = tf.Session(graph=tf.get_default_graph())
    
  10. def _load_model(self, frozen_graph_path):
    
  11.     model_exp = os.path.expanduser(frozen_graph_path)
    
  12.     if os.path.isfile(model_exp):
    
  13.         self.logger.info('Loading model filename: %s' % model_exp)
    
  14.         with tf.gfile.FastGFile(model_exp, 'rb') as f:
    
  15.             graph_def = tf.GraphDef()
    
  16.             graph_def.ParseFromString(f.read())
    
  17.             tf.import_graph_def(graph_def, name='')
    
  18.     else:
    
  19.         raise RuntimeError("Missing model file at path: {}".format(frozen_graph_path))
    
  20. def feed_image(self, encoded_image):
    
  21.     initial_state = self._sess.run(fetches="lstm/initial_state:0",
    
  22.                                    feed_dict={"image_feed:0": encoded_image})
    
  23.     return initial_state
    
  24. def inference_step(self, input_feed, state_feed):
    
  25.     softmax_output, state_output = self._sess.run(
    
  26.         fetches=["softmax:0", "lstm/state:0"],
    
  27.         feed_dict={
    
  28.             "input_feed:0": input_feed,
    
  29.             "lstm/state_feed:0": state_feed,
    
  30.         })
    
  31.     return softmax_output, state_output, None
    

vocabulary.py

  1. from future import absolute_import
  2. from future import division
  3. from future import print_function
  4. import logging
  5. import os
  6. class Vocabulary(object):
  7. def __init__(self,
    
  8.              vocab_file_path,
    
  9.              start_token="<S>",
    
  10.              end_token="</S>",
    
  11.              unk_token="<UNK>"):
    
  12.     self.logger = logging.getLogger(__name__)
    
  13.     if not os.path.exists(vocab_file_path):
    
  14.         self.logger.exception("Vocab file %s not found.", vocab_file_path)
    
  15.         raise RuntimeError
    
  16.     self.logger.info("Initializing vocabulary from file: %s", vocab_file_path)
    
  17.     with open(vocab_file_path, mode="r") as f:
    
  18.         reverse_vocab = list(f.readlines())
    
  19.     reverse_vocab = [line.split()[0] for line in reverse_vocab]
    
  20.     assert start_token in reverse_vocab
    
  21.     assert end_token in reverse_vocab
    
  22.     if unk_token not in reverse_vocab:
    
  23.         reverse_vocab.append(unk_token)
    
  24.     vocab = dict([(x, y) for (y, x) in enumerate(reverse_vocab)])
    
  25.     self.logger.info("Created vocabulary with %d words" % len(vocab))
    
  26.     self.vocab = vocab
    
  27.     self.reverse_vocab = reverse_vocab
    
  28.     self.start_id = vocab[start_token]
    
  29.     self.end_id = vocab[end_token]
    
  30.     self.unk_id = vocab[unk_token]
    
  31. def token_to_id(self, token_id):
    
  32.     if token_id in self.vocab:
    
  33.         return self.vocab[token_id]
    
  34.     else:
    
  35.         return self.unk_id
    
  36. def id_to_token(self, token_id):
    
  37.     if token_id >= len(self.reverse_vocab):
    
  38.         return self.reverse_vocab[self.unk_id]
    
  39.     else:
    
  40.         return self.reverse_vocab[token_id]
    

screenshots:

⚠️ **GitHub.com Fallback** ⚠️