Advertisement
Guest User

Untitled

a guest
May 24th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. """unit test for CaptionGenerator"""
  2. import math
  3. import numpy as np
  4. import tensorflow as tf
  5. from im2txt.inference_utils import caption_generator
  6.  
  7.  
  8. class FakeVocab(object):
  9. """fake vocabulary for testing purposes"""
  10.  
  11. def __init__(self):
  12. self.start_id = 0 # word id denoting sentence start
  13. self.end_id = 1 # word id denoting snetence end
  14.  
  15.  
  16. class FakeModel(object):
  17. def __init__(self):
  18. # number of words in the vocab
  19. self._vocab_size = 12
  20. # dimensionality of the nominal model state
  21. self._state_size = 1
  22. # map of previous word to the probability distribution of the next word
  23. self._probabilities = {
  24. 0: {1: 0.1,
  25. 2: 0.2,
  26. 3: 0.3,
  27. 4: 0.4},
  28. 2: {5: 0.1,
  29. 6: 0.9},
  30. 3: {1: 0.1,
  31. 7: 0.4,
  32. 8: 0.5},
  33. 4: {1: 0.3,
  34. 9: 0.3,
  35. 10: 0.4},
  36. 5: {1: 1.0},
  37. 6: {1: 1.0},
  38. 7: {1: 1.0},
  39. 8: {1: 1.0},
  40. 9: {1: 0.5,
  41. 11: 0.5},
  42. 10: {1: 1.0},
  43. 11: {1: 1.0},
  44. }
  45.  
  46. # pylint:disable = unused - argument
  47. def feed_image(self, sess, encoded_image):
  48. # return a nominal model state
  49. return np.zeros([1, self._state_size])
  50.  
  51. def inference_step(self, sess, input_feed, state_feed):
  52. # compute the matrix of softmax distributions for the next batch of words
  53. batch_size = input_feed.shape[0]
  54. softmax_output = np.zeros([batch_size, self._vocab_size])
  55. for batch_index, word_id in enumerate(input_feed):
  56. for next_word, probability in self._probabilities[word_id].items():
  57. softmax_output[batch_index, next_word] = probability
  58. # nominal state and metadata
  59. new_state = np.zeros([batch_size, self._state_size])
  60. metadata = None
  61. return softmax_output, new_state, metadata
  62. # pylint: enable=unused - argument
  63.  
  64.  
  65. class CaptionGeneratorTest(tf.test.TestCase):
  66. def _assertExpectedCaptions(self, expected_captions, beam_size=3,
  67. max_caption_length=20,
  68. length_normalization_factor=0):
  69. """tests that beam search generates the expected captions
  70. expected_captions:a sequence of pairs(sentence,probability),where sentence is a list of integer
  71. ids and probability is a float in [0,1]
  72. beam_size:parameter passed to the beam_search()
  73. max_caption_length:parameter passed to the beam_search()
  74. length_nomalization_factor:parameter passed to the beam_search()
  75. """
  76. expected_sentences = [c[0] for c in expected_captions]
  77. expected_probabilities = [c[1] for c in expected_captions]
  78. # generate captions
  79. generator = caption_generator.CaptionGenerator(
  80. model=FakeModel(),
  81. vocab=FakeVocab(),
  82. beam_size=beam_size,
  83. max_caption_length=max_caption_length,
  84. length_normalization_factor=length_normalization_factor)
  85. actual_captions = generator.beam_search(sess=None, encoded_image=None)
  86. actual_sentences = [c.sentence for c in actual_captions]
  87. actual_probabilities = [math.exp(c.logprob) for c in actual_captions]
  88.  
  89. self.assertEqual(expected_sentences, actual_sentences)
  90. self.assertAllClose(expected_probabilities, actual_probabilities)
  91.  
  92. def testBeamSize(self):
  93. # beam size =1
  94. expected = [([0, 4, 10, 1], 0.16)]
  95. self._assertExpectedCaptions(expected, beam_size=1)
  96. # beam size =2
  97. expected = [([0, 4, 10, 1], 0.16), ([0, 3, 8, 1], 0.15)]
  98. self._assertExpectedCaptions(expected, beam_size=2)
  99. # beam size=3
  100. expected = [([0, 2, 6, 1], 0.18), ([0, 4, 10, 1], 0.16), ([0, 3, 8, 1], 0.15)]
  101. self._assertExpectedCaptions(expected, beam_size=3)
  102.  
  103. def testMaxLength(self):
  104. # max length =1
  105. expected = [([0], 1.0)]
  106. self._assertExpectedCaptions(expected, max_caption_length=1)
  107. # max length =2
  108. # there are no complete sentences,so partial sentences are returned
  109. expected = [([0, 4], 0.4), ([0, 3], 0.3), ([0, 2], 0.2)]
  110. self._assertExpectedCaptions(expected, max_caption_length=2)
  111. # max length =3
  112. # there is at least one sentence,so only complete sentences are returned
  113. expected = [([0, 4, 1], 0.12), ([0, 3, 1], 0.03)]
  114. self._assertExpectedCaptions(expected, max_caption_length=3)
  115. # max length =4
  116. expected = [([0, 2, 6, 1], 0.18), ([0, 4, 10, 1], 0.16), ([0, 3, 8, 1], 0.15)]
  117. self._assertExpectedCaptions(expected, max_caption_length=4)
  118.  
  119. def testLengthNormalization(self):
  120. # length normalization factor =3
  121. # the longest caption is returned first,despite having low probability,
  122. # because it has the highest log(probability)/length**3
  123. expected = [
  124. ([0, 4, 9, 11, 1], 0.06),
  125. ([0, 2, 6, 1], 0.18),
  126. ([0, 4, 10, 1], 0.16),
  127. ([0, 3, 8, 1], 0.15),
  128. ]
  129. self._assertExpectedCaptions(expected, beam_size=4, length_normalization_factor=3)
  130.  
  131.  
  132. if __name__ == '__main__':
  133. tf.test.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement