inception_v4_test.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 testBuildPreLogitsNetwork(self):
  41. batch_size = 5
  42. height, width = 299, 299
  43. num_classes = None
  44. inputs = tf.random_uniform((batch_size, height, width, 3))
  45. net, end_points = inception.inception_v4(inputs, num_classes)
  46. self.assertTrue(net.op.name.startswith('InceptionV4/Logits/AvgPool'))
  47. self.assertListEqual(net.get_shape().as_list(), [batch_size, 1, 1, 1536])
  48. self.assertFalse('Logits' in end_points)
  49. self.assertFalse('Predictions' in end_points)
  50. def testBuildWithoutAuxLogits(self):
  51. batch_size = 5
  52. height, width = 299, 299
  53. num_classes = 1000
  54. inputs = tf.random_uniform((batch_size, height, width, 3))
  55. logits, endpoints = inception.inception_v4(inputs, num_classes,
  56. create_aux_logits=False)
  57. self.assertFalse('AuxLogits' in endpoints)
  58. self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
  59. self.assertListEqual(logits.get_shape().as_list(),
  60. [batch_size, num_classes])
  61. def testAllEndPointsShapes(self):
  62. batch_size = 5
  63. height, width = 299, 299
  64. num_classes = 1000
  65. inputs = tf.random_uniform((batch_size, height, width, 3))
  66. _, end_points = inception.inception_v4(inputs, num_classes)
  67. endpoints_shapes = {'Conv2d_1a_3x3': [batch_size, 149, 149, 32],
  68. 'Conv2d_2a_3x3': [batch_size, 147, 147, 32],
  69. 'Conv2d_2b_3x3': [batch_size, 147, 147, 64],
  70. 'Mixed_3a': [batch_size, 73, 73, 160],
  71. 'Mixed_4a': [batch_size, 71, 71, 192],
  72. 'Mixed_5a': [batch_size, 35, 35, 384],
  73. # 4 x Inception-A blocks
  74. 'Mixed_5b': [batch_size, 35, 35, 384],
  75. 'Mixed_5c': [batch_size, 35, 35, 384],
  76. 'Mixed_5d': [batch_size, 35, 35, 384],
  77. 'Mixed_5e': [batch_size, 35, 35, 384],
  78. # Reduction-A block
  79. 'Mixed_6a': [batch_size, 17, 17, 1024],
  80. # 7 x Inception-B blocks
  81. 'Mixed_6b': [batch_size, 17, 17, 1024],
  82. 'Mixed_6c': [batch_size, 17, 17, 1024],
  83. 'Mixed_6d': [batch_size, 17, 17, 1024],
  84. 'Mixed_6e': [batch_size, 17, 17, 1024],
  85. 'Mixed_6f': [batch_size, 17, 17, 1024],
  86. 'Mixed_6g': [batch_size, 17, 17, 1024],
  87. 'Mixed_6h': [batch_size, 17, 17, 1024],
  88. # Reduction-A block
  89. 'Mixed_7a': [batch_size, 8, 8, 1536],
  90. # 3 x Inception-C blocks
  91. 'Mixed_7b': [batch_size, 8, 8, 1536],
  92. 'Mixed_7c': [batch_size, 8, 8, 1536],
  93. 'Mixed_7d': [batch_size, 8, 8, 1536],
  94. # Logits and predictions
  95. 'AuxLogits': [batch_size, num_classes],
  96. 'global_pool': [batch_size, 1, 1, 1536],
  97. 'PreLogitsFlatten': [batch_size, 1536],
  98. 'Logits': [batch_size, num_classes],
  99. 'Predictions': [batch_size, num_classes]}
  100. self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
  101. for endpoint_name in endpoints_shapes:
  102. expected_shape = endpoints_shapes[endpoint_name]
  103. self.assertTrue(endpoint_name in end_points)
  104. self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
  105. expected_shape)
  106. def testBuildBaseNetwork(self):
  107. batch_size = 5
  108. height, width = 299, 299
  109. inputs = tf.random_uniform((batch_size, height, width, 3))
  110. net, end_points = inception.inception_v4_base(inputs)
  111. self.assertTrue(net.op.name.startswith(
  112. 'InceptionV4/Mixed_7d'))
  113. self.assertListEqual(net.get_shape().as_list(), [batch_size, 8, 8, 1536])
  114. expected_endpoints = [
  115. 'Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', 'Mixed_3a',
  116. 'Mixed_4a', 'Mixed_5a', 'Mixed_5b', 'Mixed_5c', 'Mixed_5d',
  117. 'Mixed_5e', 'Mixed_6a', 'Mixed_6b', 'Mixed_6c', 'Mixed_6d',
  118. 'Mixed_6e', 'Mixed_6f', 'Mixed_6g', 'Mixed_6h', 'Mixed_7a',
  119. 'Mixed_7b', 'Mixed_7c', 'Mixed_7d']
  120. self.assertItemsEqual(end_points.keys(), expected_endpoints)
  121. for name, op in end_points.iteritems():
  122. self.assertTrue(op.name.startswith('InceptionV4/' + name))
  123. def testBuildOnlyUpToFinalEndpoint(self):
  124. batch_size = 5
  125. height, width = 299, 299
  126. all_endpoints = [
  127. 'Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', 'Mixed_3a',
  128. 'Mixed_4a', 'Mixed_5a', 'Mixed_5b', 'Mixed_5c', 'Mixed_5d',
  129. 'Mixed_5e', 'Mixed_6a', 'Mixed_6b', 'Mixed_6c', 'Mixed_6d',
  130. 'Mixed_6e', 'Mixed_6f', 'Mixed_6g', 'Mixed_6h', 'Mixed_7a',
  131. 'Mixed_7b', 'Mixed_7c', 'Mixed_7d']
  132. for index, endpoint in enumerate(all_endpoints):
  133. with tf.Graph().as_default():
  134. inputs = tf.random_uniform((batch_size, height, width, 3))
  135. out_tensor, end_points = inception.inception_v4_base(
  136. inputs, final_endpoint=endpoint)
  137. self.assertTrue(out_tensor.op.name.startswith(
  138. 'InceptionV4/' + endpoint))
  139. self.assertItemsEqual(all_endpoints[:index+1], end_points)
  140. def testVariablesSetDevice(self):
  141. batch_size = 5
  142. height, width = 299, 299
  143. num_classes = 1000
  144. inputs = tf.random_uniform((batch_size, height, width, 3))
  145. # Force all Variables to reside on the device.
  146. with tf.variable_scope('on_cpu'), tf.device('/cpu:0'):
  147. inception.inception_v4(inputs, num_classes)
  148. with tf.variable_scope('on_gpu'), tf.device('/gpu:0'):
  149. inception.inception_v4(inputs, num_classes)
  150. for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='on_cpu'):
  151. self.assertDeviceEqual(v.device, '/cpu:0')
  152. for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='on_gpu'):
  153. self.assertDeviceEqual(v.device, '/gpu:0')
  154. def testHalfSizeImages(self):
  155. batch_size = 5
  156. height, width = 150, 150
  157. num_classes = 1000
  158. inputs = tf.random_uniform((batch_size, height, width, 3))
  159. logits, end_points = inception.inception_v4(inputs, num_classes)
  160. self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
  161. self.assertListEqual(logits.get_shape().as_list(),
  162. [batch_size, num_classes])
  163. pre_pool = end_points['Mixed_7d']
  164. self.assertListEqual(pre_pool.get_shape().as_list(),
  165. [batch_size, 3, 3, 1536])
  166. def testGlobalPool(self):
  167. batch_size = 2
  168. height, width = 400, 600
  169. num_classes = 1000
  170. inputs = tf.random_uniform((batch_size, height, width, 3))
  171. logits, end_points = inception.inception_v4(inputs, num_classes)
  172. self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
  173. self.assertListEqual(logits.get_shape().as_list(),
  174. [batch_size, num_classes])
  175. pre_pool = end_points['Mixed_7d']
  176. self.assertListEqual(pre_pool.get_shape().as_list(),
  177. [batch_size, 11, 17, 1536])
  178. def testGlobalPoolUnknownImageShape(self):
  179. batch_size = 2
  180. height, width = 400, 600
  181. num_classes = 1000
  182. with self.test_session() as sess:
  183. inputs = tf.placeholder(tf.float32, (batch_size, None, None, 3))
  184. logits, end_points = inception.inception_v4(
  185. inputs, num_classes, create_aux_logits=False)
  186. self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
  187. self.assertListEqual(logits.get_shape().as_list(),
  188. [batch_size, num_classes])
  189. pre_pool = end_points['Mixed_7d']
  190. images = tf.random_uniform((batch_size, height, width, 3))
  191. sess.run(tf.global_variables_initializer())
  192. logits_out, pre_pool_out = sess.run([logits, pre_pool],
  193. {inputs: images.eval()})
  194. self.assertTupleEqual(logits_out.shape, (batch_size, num_classes))
  195. self.assertTupleEqual(pre_pool_out.shape, (batch_size, 11, 17, 1536))
  196. def testUnknownBatchSize(self):
  197. batch_size = 1
  198. height, width = 299, 299
  199. num_classes = 1000
  200. with self.test_session() as sess:
  201. inputs = tf.placeholder(tf.float32, (None, height, width, 3))
  202. logits, _ = inception.inception_v4(inputs, num_classes)
  203. self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
  204. self.assertListEqual(logits.get_shape().as_list(),
  205. [None, num_classes])
  206. images = tf.random_uniform((batch_size, height, width, 3))
  207. sess.run(tf.global_variables_initializer())
  208. output = sess.run(logits, {inputs: images.eval()})
  209. self.assertEquals(output.shape, (batch_size, num_classes))
  210. def testEvaluation(self):
  211. batch_size = 2
  212. height, width = 299, 299
  213. num_classes = 1000
  214. with self.test_session() as sess:
  215. eval_inputs = tf.random_uniform((batch_size, height, width, 3))
  216. logits, _ = inception.inception_v4(eval_inputs,
  217. num_classes,
  218. is_training=False)
  219. predictions = tf.argmax(logits, 1)
  220. sess.run(tf.global_variables_initializer())
  221. output = sess.run(predictions)
  222. self.assertEquals(output.shape, (batch_size,))
  223. def testTrainEvalWithReuse(self):
  224. train_batch_size = 5
  225. eval_batch_size = 2
  226. height, width = 150, 150
  227. num_classes = 1000
  228. with self.test_session() as sess:
  229. train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
  230. inception.inception_v4(train_inputs, num_classes)
  231. eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
  232. logits, _ = inception.inception_v4(eval_inputs,
  233. num_classes,
  234. is_training=False,
  235. reuse=True)
  236. predictions = tf.argmax(logits, 1)
  237. sess.run(tf.global_variables_initializer())
  238. output = sess.run(predictions)
  239. self.assertEquals(output.shape, (eval_batch_size,))
  240. if __name__ == '__main__':
  241. tf.test.main()