inception_v1_test.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # Copyright 2016 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """Tests for nets.inception_v1."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import numpy as np
  20. import tensorflow as tf
  21. from nets import inception
  22. slim = tf.contrib.slim
  23. class InceptionV1Test(tf.test.TestCase):
  24. def testBuildClassificationNetwork(self):
  25. batch_size = 5
  26. height, width = 224, 224
  27. num_classes = 1000
  28. inputs = tf.random_uniform((batch_size, height, width, 3))
  29. logits, end_points = inception.inception_v1(inputs, num_classes)
  30. self.assertTrue(logits.op.name.startswith('InceptionV1/Logits'))
  31. self.assertListEqual(logits.get_shape().as_list(),
  32. [batch_size, num_classes])
  33. self.assertTrue('Predictions' in end_points)
  34. self.assertListEqual(end_points['Predictions'].get_shape().as_list(),
  35. [batch_size, num_classes])
  36. def testBuildBaseNetwork(self):
  37. batch_size = 5
  38. height, width = 224, 224
  39. inputs = tf.random_uniform((batch_size, height, width, 3))
  40. mixed_6c, end_points = inception.inception_v1_base(inputs)
  41. self.assertTrue(mixed_6c.op.name.startswith('InceptionV1/Mixed_5c'))
  42. self.assertListEqual(mixed_6c.get_shape().as_list(),
  43. [batch_size, 7, 7, 1024])
  44. expected_endpoints = ['Conv2d_1a_7x7', 'MaxPool_2a_3x3', 'Conv2d_2b_1x1',
  45. 'Conv2d_2c_3x3', 'MaxPool_3a_3x3', 'Mixed_3b',
  46. 'Mixed_3c', 'MaxPool_4a_3x3', 'Mixed_4b', 'Mixed_4c',
  47. 'Mixed_4d', 'Mixed_4e', 'Mixed_4f', 'MaxPool_5a_2x2',
  48. 'Mixed_5b', 'Mixed_5c']
  49. self.assertItemsEqual(end_points.keys(), expected_endpoints)
  50. def testBuildOnlyUptoFinalEndpoint(self):
  51. batch_size = 5
  52. height, width = 224, 224
  53. endpoints = ['Conv2d_1a_7x7', 'MaxPool_2a_3x3', 'Conv2d_2b_1x1',
  54. 'Conv2d_2c_3x3', 'MaxPool_3a_3x3', 'Mixed_3b', 'Mixed_3c',
  55. 'MaxPool_4a_3x3', 'Mixed_4b', 'Mixed_4c', 'Mixed_4d',
  56. 'Mixed_4e', 'Mixed_4f', 'MaxPool_5a_2x2', 'Mixed_5b',
  57. 'Mixed_5c']
  58. for index, endpoint in enumerate(endpoints):
  59. with tf.Graph().as_default():
  60. inputs = tf.random_uniform((batch_size, height, width, 3))
  61. out_tensor, end_points = inception.inception_v1_base(
  62. inputs, final_endpoint=endpoint)
  63. self.assertTrue(out_tensor.op.name.startswith(
  64. 'InceptionV1/' + endpoint))
  65. self.assertItemsEqual(endpoints[:index+1], end_points)
  66. def testBuildAndCheckAllEndPointsUptoMixed5c(self):
  67. batch_size = 5
  68. height, width = 224, 224
  69. inputs = tf.random_uniform((batch_size, height, width, 3))
  70. _, end_points = inception.inception_v1_base(inputs,
  71. final_endpoint='Mixed_5c')
  72. endpoints_shapes = {'Conv2d_1a_7x7': [5, 112, 112, 64],
  73. 'MaxPool_2a_3x3': [5, 56, 56, 64],
  74. 'Conv2d_2b_1x1': [5, 56, 56, 64],
  75. 'Conv2d_2c_3x3': [5, 56, 56, 192],
  76. 'MaxPool_3a_3x3': [5, 28, 28, 192],
  77. 'Mixed_3b': [5, 28, 28, 256],
  78. 'Mixed_3c': [5, 28, 28, 480],
  79. 'MaxPool_4a_3x3': [5, 14, 14, 480],
  80. 'Mixed_4b': [5, 14, 14, 512],
  81. 'Mixed_4c': [5, 14, 14, 512],
  82. 'Mixed_4d': [5, 14, 14, 512],
  83. 'Mixed_4e': [5, 14, 14, 528],
  84. 'Mixed_4f': [5, 14, 14, 832],
  85. 'MaxPool_5a_2x2': [5, 7, 7, 832],
  86. 'Mixed_5b': [5, 7, 7, 832],
  87. 'Mixed_5c': [5, 7, 7, 1024]}
  88. self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
  89. for endpoint_name in endpoints_shapes:
  90. expected_shape = endpoints_shapes[endpoint_name]
  91. self.assertTrue(endpoint_name in end_points)
  92. self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
  93. expected_shape)
  94. def testModelHasExpectedNumberOfParameters(self):
  95. batch_size = 5
  96. height, width = 224, 224
  97. inputs = tf.random_uniform((batch_size, height, width, 3))
  98. with slim.arg_scope(inception.inception_v1_arg_scope()):
  99. inception.inception_v1_base(inputs)
  100. total_params, _ = slim.model_analyzer.analyze_vars(
  101. slim.get_model_variables())
  102. self.assertAlmostEqual(5607184, total_params)
  103. def testHalfSizeImages(self):
  104. batch_size = 5
  105. height, width = 112, 112
  106. inputs = tf.random_uniform((batch_size, height, width, 3))
  107. mixed_5c, _ = inception.inception_v1_base(inputs)
  108. self.assertTrue(mixed_5c.op.name.startswith('InceptionV1/Mixed_5c'))
  109. self.assertListEqual(mixed_5c.get_shape().as_list(),
  110. [batch_size, 4, 4, 1024])
  111. def testUnknownImageShape(self):
  112. tf.reset_default_graph()
  113. batch_size = 2
  114. height, width = 224, 224
  115. num_classes = 1000
  116. input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
  117. with self.test_session() as sess:
  118. inputs = tf.placeholder(tf.float32, shape=(batch_size, None, None, 3))
  119. logits, end_points = inception.inception_v1(inputs, num_classes)
  120. self.assertTrue(logits.op.name.startswith('InceptionV1/Logits'))
  121. self.assertListEqual(logits.get_shape().as_list(),
  122. [batch_size, num_classes])
  123. pre_pool = end_points['Mixed_5c']
  124. feed_dict = {inputs: input_np}
  125. tf.global_variables_initializer().run()
  126. pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
  127. self.assertListEqual(list(pre_pool_out.shape), [batch_size, 7, 7, 1024])
  128. def testUnknowBatchSize(self):
  129. batch_size = 1
  130. height, width = 224, 224
  131. num_classes = 1000
  132. inputs = tf.placeholder(tf.float32, (None, height, width, 3))
  133. logits, _ = inception.inception_v1(inputs, num_classes)
  134. self.assertTrue(logits.op.name.startswith('InceptionV1/Logits'))
  135. self.assertListEqual(logits.get_shape().as_list(),
  136. [None, num_classes])
  137. images = tf.random_uniform((batch_size, height, width, 3))
  138. with self.test_session() as sess:
  139. sess.run(tf.global_variables_initializer())
  140. output = sess.run(logits, {inputs: images.eval()})
  141. self.assertEquals(output.shape, (batch_size, num_classes))
  142. def testEvaluation(self):
  143. batch_size = 2
  144. height, width = 224, 224
  145. num_classes = 1000
  146. eval_inputs = tf.random_uniform((batch_size, height, width, 3))
  147. logits, _ = inception.inception_v1(eval_inputs, num_classes,
  148. is_training=False)
  149. predictions = tf.argmax(logits, 1)
  150. with self.test_session() as sess:
  151. sess.run(tf.global_variables_initializer())
  152. output = sess.run(predictions)
  153. self.assertEquals(output.shape, (batch_size,))
  154. def testTrainEvalWithReuse(self):
  155. train_batch_size = 5
  156. eval_batch_size = 2
  157. height, width = 224, 224
  158. num_classes = 1000
  159. train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
  160. inception.inception_v1(train_inputs, num_classes)
  161. eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
  162. logits, _ = inception.inception_v1(eval_inputs, num_classes, reuse=True)
  163. predictions = tf.argmax(logits, 1)
  164. with self.test_session() as sess:
  165. sess.run(tf.global_variables_initializer())
  166. output = sess.run(predictions)
  167. self.assertEquals(output.shape, (eval_batch_size,))
  168. def testLogitsNotSqueezed(self):
  169. num_classes = 25
  170. images = tf.random_uniform([1, 224, 224, 3])
  171. logits, _ = inception.inception_v1(images,
  172. num_classes=num_classes,
  173. spatial_squeeze=False)
  174. with self.test_session() as sess:
  175. tf.global_variables_initializer().run()
  176. logits_out = sess.run(logits)
  177. self.assertListEqual(list(logits_out.shape), [1, 1, 1, num_classes])
  178. if __name__ == '__main__':
  179. tf.test.main()