inception_resnet_v2_test.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_resnet_v2."""
  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. with self.test_session():
  27. inputs = tf.random_uniform((batch_size, height, width, 3))
  28. logits, endpoints = inception.inception_resnet_v2(inputs, num_classes)
  29. self.assertTrue('AuxLogits' in endpoints)
  30. auxlogits = endpoints['AuxLogits']
  31. self.assertTrue(
  32. auxlogits.op.name.startswith('InceptionResnetV2/AuxLogits'))
  33. self.assertListEqual(auxlogits.get_shape().as_list(),
  34. [batch_size, num_classes])
  35. self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
  36. self.assertListEqual(logits.get_shape().as_list(),
  37. [batch_size, num_classes])
  38. def testBuildWithoutAuxLogits(self):
  39. batch_size = 5
  40. height, width = 299, 299
  41. num_classes = 1000
  42. with self.test_session():
  43. inputs = tf.random_uniform((batch_size, height, width, 3))
  44. logits, endpoints = inception.inception_resnet_v2(inputs, num_classes,
  45. create_aux_logits=False)
  46. self.assertTrue('AuxLogits' not in endpoints)
  47. self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
  48. self.assertListEqual(logits.get_shape().as_list(),
  49. [batch_size, num_classes])
  50. def testBuildNoClasses(self):
  51. batch_size = 5
  52. height, width = 299, 299
  53. num_classes = None
  54. with self.test_session():
  55. inputs = tf.random_uniform((batch_size, height, width, 3))
  56. net, endpoints = inception.inception_resnet_v2(inputs, num_classes)
  57. self.assertTrue('AuxLogits' not in endpoints)
  58. self.assertTrue('Logits' not in endpoints)
  59. self.assertTrue(
  60. net.op.name.startswith('InceptionResnetV2/Logits/AvgPool'))
  61. self.assertListEqual(net.get_shape().as_list(), [batch_size, 1, 1, 1536])
  62. def testBuildEndPoints(self):
  63. batch_size = 5
  64. height, width = 299, 299
  65. num_classes = 1000
  66. with self.test_session():
  67. inputs = tf.random_uniform((batch_size, height, width, 3))
  68. _, end_points = inception.inception_resnet_v2(inputs, num_classes)
  69. self.assertTrue('Logits' in end_points)
  70. logits = end_points['Logits']
  71. self.assertListEqual(logits.get_shape().as_list(),
  72. [batch_size, num_classes])
  73. self.assertTrue('AuxLogits' in end_points)
  74. aux_logits = end_points['AuxLogits']
  75. self.assertListEqual(aux_logits.get_shape().as_list(),
  76. [batch_size, num_classes])
  77. pre_pool = end_points['Conv2d_7b_1x1']
  78. self.assertListEqual(pre_pool.get_shape().as_list(),
  79. [batch_size, 8, 8, 1536])
  80. def testBuildBaseNetwork(self):
  81. batch_size = 5
  82. height, width = 299, 299
  83. inputs = tf.random_uniform((batch_size, height, width, 3))
  84. net, end_points = inception.inception_resnet_v2_base(inputs)
  85. self.assertTrue(net.op.name.startswith('InceptionResnetV2/Conv2d_7b_1x1'))
  86. self.assertListEqual(net.get_shape().as_list(),
  87. [batch_size, 8, 8, 1536])
  88. expected_endpoints = ['Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3',
  89. 'MaxPool_3a_3x3', 'Conv2d_3b_1x1', 'Conv2d_4a_3x3',
  90. 'MaxPool_5a_3x3', 'Mixed_5b', 'Mixed_6a',
  91. 'PreAuxLogits', 'Mixed_7a', 'Conv2d_7b_1x1']
  92. self.assertItemsEqual(end_points.keys(), expected_endpoints)
  93. def testBuildOnlyUptoFinalEndpoint(self):
  94. batch_size = 5
  95. height, width = 299, 299
  96. endpoints = ['Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3',
  97. 'MaxPool_3a_3x3', 'Conv2d_3b_1x1', 'Conv2d_4a_3x3',
  98. 'MaxPool_5a_3x3', 'Mixed_5b', 'Mixed_6a',
  99. 'PreAuxLogits', 'Mixed_7a', 'Conv2d_7b_1x1']
  100. for index, endpoint in enumerate(endpoints):
  101. with tf.Graph().as_default():
  102. inputs = tf.random_uniform((batch_size, height, width, 3))
  103. out_tensor, end_points = inception.inception_resnet_v2_base(
  104. inputs, final_endpoint=endpoint)
  105. if endpoint != 'PreAuxLogits':
  106. self.assertTrue(out_tensor.op.name.startswith(
  107. 'InceptionResnetV2/' + endpoint))
  108. self.assertItemsEqual(endpoints[:index+1], end_points)
  109. def testBuildAndCheckAllEndPointsUptoPreAuxLogits(self):
  110. batch_size = 5
  111. height, width = 299, 299
  112. inputs = tf.random_uniform((batch_size, height, width, 3))
  113. _, end_points = inception.inception_resnet_v2_base(
  114. inputs, final_endpoint='PreAuxLogits')
  115. endpoints_shapes = {'Conv2d_1a_3x3': [5, 149, 149, 32],
  116. 'Conv2d_2a_3x3': [5, 147, 147, 32],
  117. 'Conv2d_2b_3x3': [5, 147, 147, 64],
  118. 'MaxPool_3a_3x3': [5, 73, 73, 64],
  119. 'Conv2d_3b_1x1': [5, 73, 73, 80],
  120. 'Conv2d_4a_3x3': [5, 71, 71, 192],
  121. 'MaxPool_5a_3x3': [5, 35, 35, 192],
  122. 'Mixed_5b': [5, 35, 35, 320],
  123. 'Mixed_6a': [5, 17, 17, 1088],
  124. 'PreAuxLogits': [5, 17, 17, 1088]
  125. }
  126. self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
  127. for endpoint_name in endpoints_shapes:
  128. expected_shape = endpoints_shapes[endpoint_name]
  129. self.assertTrue(endpoint_name in end_points)
  130. self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
  131. expected_shape)
  132. def testBuildAndCheckAllEndPointsUptoPreAuxLogitsWithAlignedFeatureMaps(self):
  133. batch_size = 5
  134. height, width = 299, 299
  135. inputs = tf.random_uniform((batch_size, height, width, 3))
  136. _, end_points = inception.inception_resnet_v2_base(
  137. inputs, final_endpoint='PreAuxLogits', align_feature_maps=True)
  138. endpoints_shapes = {'Conv2d_1a_3x3': [5, 150, 150, 32],
  139. 'Conv2d_2a_3x3': [5, 150, 150, 32],
  140. 'Conv2d_2b_3x3': [5, 150, 150, 64],
  141. 'MaxPool_3a_3x3': [5, 75, 75, 64],
  142. 'Conv2d_3b_1x1': [5, 75, 75, 80],
  143. 'Conv2d_4a_3x3': [5, 75, 75, 192],
  144. 'MaxPool_5a_3x3': [5, 38, 38, 192],
  145. 'Mixed_5b': [5, 38, 38, 320],
  146. 'Mixed_6a': [5, 19, 19, 1088],
  147. 'PreAuxLogits': [5, 19, 19, 1088]
  148. }
  149. self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
  150. for endpoint_name in endpoints_shapes:
  151. expected_shape = endpoints_shapes[endpoint_name]
  152. self.assertTrue(endpoint_name in end_points)
  153. self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
  154. expected_shape)
  155. def testBuildAndCheckAllEndPointsUptoPreAuxLogitsWithOutputStrideEight(self):
  156. batch_size = 5
  157. height, width = 299, 299
  158. inputs = tf.random_uniform((batch_size, height, width, 3))
  159. _, end_points = inception.inception_resnet_v2_base(
  160. inputs, final_endpoint='PreAuxLogits', output_stride=8)
  161. endpoints_shapes = {'Conv2d_1a_3x3': [5, 149, 149, 32],
  162. 'Conv2d_2a_3x3': [5, 147, 147, 32],
  163. 'Conv2d_2b_3x3': [5, 147, 147, 64],
  164. 'MaxPool_3a_3x3': [5, 73, 73, 64],
  165. 'Conv2d_3b_1x1': [5, 73, 73, 80],
  166. 'Conv2d_4a_3x3': [5, 71, 71, 192],
  167. 'MaxPool_5a_3x3': [5, 35, 35, 192],
  168. 'Mixed_5b': [5, 35, 35, 320],
  169. 'Mixed_6a': [5, 33, 33, 1088],
  170. 'PreAuxLogits': [5, 33, 33, 1088]
  171. }
  172. self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
  173. for endpoint_name in endpoints_shapes:
  174. expected_shape = endpoints_shapes[endpoint_name]
  175. self.assertTrue(endpoint_name in end_points)
  176. self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
  177. expected_shape)
  178. def testVariablesSetDevice(self):
  179. batch_size = 5
  180. height, width = 299, 299
  181. num_classes = 1000
  182. with self.test_session():
  183. inputs = tf.random_uniform((batch_size, height, width, 3))
  184. # Force all Variables to reside on the device.
  185. with tf.variable_scope('on_cpu'), tf.device('/cpu:0'):
  186. inception.inception_resnet_v2(inputs, num_classes)
  187. with tf.variable_scope('on_gpu'), tf.device('/gpu:0'):
  188. inception.inception_resnet_v2(inputs, num_classes)
  189. for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='on_cpu'):
  190. self.assertDeviceEqual(v.device, '/cpu:0')
  191. for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='on_gpu'):
  192. self.assertDeviceEqual(v.device, '/gpu:0')
  193. def testHalfSizeImages(self):
  194. batch_size = 5
  195. height, width = 150, 150
  196. num_classes = 1000
  197. with self.test_session():
  198. inputs = tf.random_uniform((batch_size, height, width, 3))
  199. logits, end_points = inception.inception_resnet_v2(inputs, num_classes)
  200. self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
  201. self.assertListEqual(logits.get_shape().as_list(),
  202. [batch_size, num_classes])
  203. pre_pool = end_points['Conv2d_7b_1x1']
  204. self.assertListEqual(pre_pool.get_shape().as_list(),
  205. [batch_size, 3, 3, 1536])
  206. def testGlobalPool(self):
  207. batch_size = 2
  208. height, width = 400, 600
  209. num_classes = 1000
  210. with self.test_session():
  211. inputs = tf.random_uniform((batch_size, height, width, 3))
  212. logits, end_points = inception.inception_resnet_v2(inputs, num_classes)
  213. self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
  214. self.assertListEqual(logits.get_shape().as_list(),
  215. [batch_size, num_classes])
  216. pre_pool = end_points['Conv2d_7b_1x1']
  217. self.assertListEqual(pre_pool.get_shape().as_list(),
  218. [batch_size, 11, 17, 1536])
  219. def testGlobalPoolUnknownImageShape(self):
  220. batch_size = 2
  221. height, width = 400, 600
  222. num_classes = 1000
  223. with self.test_session() as sess:
  224. inputs = tf.placeholder(tf.float32, (batch_size, None, None, 3))
  225. logits, end_points = inception.inception_resnet_v2(
  226. inputs, num_classes, create_aux_logits=False)
  227. self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
  228. self.assertListEqual(logits.get_shape().as_list(),
  229. [batch_size, num_classes])
  230. pre_pool = end_points['Conv2d_7b_1x1']
  231. images = tf.random_uniform((batch_size, height, width, 3))
  232. sess.run(tf.global_variables_initializer())
  233. logits_out, pre_pool_out = sess.run([logits, pre_pool],
  234. {inputs: images.eval()})
  235. self.assertTupleEqual(logits_out.shape, (batch_size, num_classes))
  236. self.assertTupleEqual(pre_pool_out.shape, (batch_size, 11, 17, 1536))
  237. def testUnknownBatchSize(self):
  238. batch_size = 1
  239. height, width = 299, 299
  240. num_classes = 1000
  241. with self.test_session() as sess:
  242. inputs = tf.placeholder(tf.float32, (None, height, width, 3))
  243. logits, _ = inception.inception_resnet_v2(inputs, num_classes)
  244. self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
  245. self.assertListEqual(logits.get_shape().as_list(),
  246. [None, num_classes])
  247. images = tf.random_uniform((batch_size, height, width, 3))
  248. sess.run(tf.global_variables_initializer())
  249. output = sess.run(logits, {inputs: images.eval()})
  250. self.assertEquals(output.shape, (batch_size, num_classes))
  251. def testEvaluation(self):
  252. batch_size = 2
  253. height, width = 299, 299
  254. num_classes = 1000
  255. with self.test_session() as sess:
  256. eval_inputs = tf.random_uniform((batch_size, height, width, 3))
  257. logits, _ = inception.inception_resnet_v2(eval_inputs,
  258. num_classes,
  259. is_training=False)
  260. predictions = tf.argmax(logits, 1)
  261. sess.run(tf.global_variables_initializer())
  262. output = sess.run(predictions)
  263. self.assertEquals(output.shape, (batch_size,))
  264. def testTrainEvalWithReuse(self):
  265. train_batch_size = 5
  266. eval_batch_size = 2
  267. height, width = 150, 150
  268. num_classes = 1000
  269. with self.test_session() as sess:
  270. train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
  271. inception.inception_resnet_v2(train_inputs, num_classes)
  272. eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
  273. logits, _ = inception.inception_resnet_v2(eval_inputs,
  274. num_classes,
  275. is_training=False,
  276. reuse=True)
  277. predictions = tf.argmax(logits, 1)
  278. sess.run(tf.global_variables_initializer())
  279. output = sess.run(predictions)
  280. self.assertEquals(output.shape, (eval_batch_size,))
  281. if __name__ == '__main__':
  282. tf.test.main()