inception_v4_test.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 slim.inception_v4."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import tensorflow as tf
  20. from nets import inception
  21. class InceptionTest(tf.test.TestCase):
  22. def testBuildLogits(self):
  23. batch_size = 5
  24. height, width = 299, 299
  25. num_classes = 1000
  26. inputs = tf.random_uniform((batch_size, height, width, 3))
  27. logits, end_points = inception.inception_v4(inputs, num_classes)
  28. auxlogits = end_points['AuxLogits']
  29. predictions = end_points['Predictions']
  30. self.assertTrue(auxlogits.op.name.startswith('InceptionV4/AuxLogits'))
  31. self.assertListEqual(auxlogits.get_shape().as_list(),
  32. [batch_size, num_classes])
  33. self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
  34. self.assertListEqual(logits.get_shape().as_list(),
  35. [batch_size, num_classes])
  36. self.assertTrue(predictions.op.name.startswith(
  37. 'InceptionV4/Logits/Predictions'))
  38. self.assertListEqual(predictions.get_shape().as_list(),
  39. [batch_size, num_classes])
  40. def testBuildWithoutAuxLogits(self):
  41. batch_size = 5
  42. height, width = 299, 299
  43. num_classes = 1000
  44. inputs = tf.random_uniform((batch_size, height, width, 3))
  45. logits, endpoints = inception.inception_v4(inputs, num_classes,
  46. create_aux_logits=False)
  47. self.assertFalse('AuxLogits' in endpoints)
  48. self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
  49. self.assertListEqual(logits.get_shape().as_list(),
  50. [batch_size, num_classes])
  51. def testAllEndPointsShapes(self):
  52. batch_size = 5
  53. height, width = 299, 299
  54. num_classes = 1000
  55. inputs = tf.random_uniform((batch_size, height, width, 3))
  56. _, end_points = inception.inception_v4(inputs, num_classes)
  57. endpoints_shapes = {'Conv2d_1a_3x3': [batch_size, 149, 149, 32],
  58. 'Conv2d_2a_3x3': [batch_size, 147, 147, 32],
  59. 'Conv2d_2b_3x3': [batch_size, 147, 147, 64],
  60. 'Mixed_3a': [batch_size, 73, 73, 160],
  61. 'Mixed_4a': [batch_size, 71, 71, 192],
  62. 'Mixed_5a': [batch_size, 35, 35, 384],
  63. # 4 x Inception-A blocks
  64. 'Mixed_5b': [batch_size, 35, 35, 384],
  65. 'Mixed_5c': [batch_size, 35, 35, 384],
  66. 'Mixed_5d': [batch_size, 35, 35, 384],
  67. 'Mixed_5e': [batch_size, 35, 35, 384],
  68. # Reduction-A block
  69. 'Mixed_6a': [batch_size, 17, 17, 1024],
  70. # 7 x Inception-B blocks
  71. 'Mixed_6b': [batch_size, 17, 17, 1024],
  72. 'Mixed_6c': [batch_size, 17, 17, 1024],
  73. 'Mixed_6d': [batch_size, 17, 17, 1024],
  74. 'Mixed_6e': [batch_size, 17, 17, 1024],
  75. 'Mixed_6f': [batch_size, 17, 17, 1024],
  76. 'Mixed_6g': [batch_size, 17, 17, 1024],
  77. 'Mixed_6h': [batch_size, 17, 17, 1024],
  78. # Reduction-A block
  79. 'Mixed_7a': [batch_size, 8, 8, 1536],
  80. # 3 x Inception-C blocks
  81. 'Mixed_7b': [batch_size, 8, 8, 1536],
  82. 'Mixed_7c': [batch_size, 8, 8, 1536],
  83. 'Mixed_7d': [batch_size, 8, 8, 1536],
  84. # Logits and predictions
  85. 'AuxLogits': [batch_size, num_classes],
  86. 'PreLogitsFlatten': [batch_size, 1536],
  87. 'Logits': [batch_size, num_classes],
  88. 'Predictions': [batch_size, num_classes]}
  89. self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
  90. for endpoint_name in endpoints_shapes:
  91. expected_shape = endpoints_shapes[endpoint_name]
  92. self.assertTrue(endpoint_name in end_points)
  93. self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
  94. expected_shape)
  95. def testBuildBaseNetwork(self):
  96. batch_size = 5
  97. height, width = 299, 299
  98. inputs = tf.random_uniform((batch_size, height, width, 3))
  99. net, end_points = inception.inception_v4_base(inputs)
  100. self.assertTrue(net.op.name.startswith(
  101. 'InceptionV4/Mixed_7d'))
  102. self.assertListEqual(net.get_shape().as_list(), [batch_size, 8, 8, 1536])
  103. expected_endpoints = [
  104. 'Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', 'Mixed_3a',
  105. 'Mixed_4a', 'Mixed_5a', 'Mixed_5b', 'Mixed_5c', 'Mixed_5d',
  106. 'Mixed_5e', 'Mixed_6a', 'Mixed_6b', 'Mixed_6c', 'Mixed_6d',
  107. 'Mixed_6e', 'Mixed_6f', 'Mixed_6g', 'Mixed_6h', 'Mixed_7a',
  108. 'Mixed_7b', 'Mixed_7c', 'Mixed_7d']
  109. self.assertItemsEqual(end_points.keys(), expected_endpoints)
  110. for name, op in end_points.iteritems():
  111. self.assertTrue(op.name.startswith('InceptionV4/' + name))
  112. def testBuildOnlyUpToFinalEndpoint(self):
  113. batch_size = 5
  114. height, width = 299, 299
  115. all_endpoints = [
  116. 'Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', 'Mixed_3a',
  117. 'Mixed_4a', 'Mixed_5a', 'Mixed_5b', 'Mixed_5c', 'Mixed_5d',
  118. 'Mixed_5e', 'Mixed_6a', 'Mixed_6b', 'Mixed_6c', 'Mixed_6d',
  119. 'Mixed_6e', 'Mixed_6f', 'Mixed_6g', 'Mixed_6h', 'Mixed_7a',
  120. 'Mixed_7b', 'Mixed_7c', 'Mixed_7d']
  121. for index, endpoint in enumerate(all_endpoints):
  122. with tf.Graph().as_default():
  123. inputs = tf.random_uniform((batch_size, height, width, 3))
  124. out_tensor, end_points = inception.inception_v4_base(
  125. inputs, final_endpoint=endpoint)
  126. self.assertTrue(out_tensor.op.name.startswith(
  127. 'InceptionV4/' + endpoint))
  128. self.assertItemsEqual(all_endpoints[:index+1], end_points)
  129. def testVariablesSetDevice(self):
  130. batch_size = 5
  131. height, width = 299, 299
  132. num_classes = 1000
  133. inputs = tf.random_uniform((batch_size, height, width, 3))
  134. # Force all Variables to reside on the device.
  135. with tf.variable_scope('on_cpu'), tf.device('/cpu:0'):
  136. inception.inception_v4(inputs, num_classes)
  137. with tf.variable_scope('on_gpu'), tf.device('/gpu:0'):
  138. inception.inception_v4(inputs, num_classes)
  139. for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='on_cpu'):
  140. self.assertDeviceEqual(v.device, '/cpu:0')
  141. for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='on_gpu'):
  142. self.assertDeviceEqual(v.device, '/gpu:0')
  143. def testHalfSizeImages(self):
  144. batch_size = 5
  145. height, width = 150, 150
  146. num_classes = 1000
  147. inputs = tf.random_uniform((batch_size, height, width, 3))
  148. logits, end_points = inception.inception_v4(inputs, num_classes)
  149. self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
  150. self.assertListEqual(logits.get_shape().as_list(),
  151. [batch_size, num_classes])
  152. pre_pool = end_points['Mixed_7d']
  153. self.assertListEqual(pre_pool.get_shape().as_list(),
  154. [batch_size, 3, 3, 1536])
  155. def testUnknownBatchSize(self):
  156. batch_size = 1
  157. height, width = 299, 299
  158. num_classes = 1000
  159. with self.test_session() as sess:
  160. inputs = tf.placeholder(tf.float32, (None, height, width, 3))
  161. logits, _ = inception.inception_v4(inputs, num_classes)
  162. self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
  163. self.assertListEqual(logits.get_shape().as_list(),
  164. [None, num_classes])
  165. images = tf.random_uniform((batch_size, height, width, 3))
  166. sess.run(tf.global_variables_initializer())
  167. output = sess.run(logits, {inputs: images.eval()})
  168. self.assertEquals(output.shape, (batch_size, num_classes))
  169. def testEvaluation(self):
  170. batch_size = 2
  171. height, width = 299, 299
  172. num_classes = 1000
  173. with self.test_session() as sess:
  174. eval_inputs = tf.random_uniform((batch_size, height, width, 3))
  175. logits, _ = inception.inception_v4(eval_inputs,
  176. num_classes,
  177. is_training=False)
  178. predictions = tf.argmax(logits, 1)
  179. sess.run(tf.global_variables_initializer())
  180. output = sess.run(predictions)
  181. self.assertEquals(output.shape, (batch_size,))
  182. def testTrainEvalWithReuse(self):
  183. train_batch_size = 5
  184. eval_batch_size = 2
  185. height, width = 150, 150
  186. num_classes = 1000
  187. with self.test_session() as sess:
  188. train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
  189. inception.inception_v4(train_inputs, num_classes)
  190. eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
  191. logits, _ = inception.inception_v4(eval_inputs,
  192. num_classes,
  193. is_training=False,
  194. reuse=True)
  195. predictions = tf.argmax(logits, 1)
  196. sess.run(tf.global_variables_initializer())
  197. output = sess.run(predictions)
  198. self.assertEquals(output.shape, (eval_batch_size,))
  199. if __name__ == '__main__':
  200. tf.test.main()