inception_resnet_v2_test.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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, _ = inception.inception_resnet_v2(inputs, num_classes)
  29. self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
  30. self.assertListEqual(logits.get_shape().as_list(),
  31. [batch_size, num_classes])
  32. def testBuildEndPoints(self):
  33. batch_size = 5
  34. height, width = 299, 299
  35. num_classes = 1000
  36. with self.test_session():
  37. inputs = tf.random_uniform((batch_size, height, width, 3))
  38. _, end_points = inception.inception_resnet_v2(inputs, num_classes)
  39. self.assertTrue('Logits' in end_points)
  40. logits = end_points['Logits']
  41. self.assertListEqual(logits.get_shape().as_list(),
  42. [batch_size, num_classes])
  43. self.assertTrue('AuxLogits' in end_points)
  44. aux_logits = end_points['AuxLogits']
  45. self.assertListEqual(aux_logits.get_shape().as_list(),
  46. [batch_size, num_classes])
  47. pre_pool = end_points['PrePool']
  48. self.assertListEqual(pre_pool.get_shape().as_list(),
  49. [batch_size, 8, 8, 1536])
  50. def testVariablesSetDevice(self):
  51. batch_size = 5
  52. height, width = 299, 299
  53. num_classes = 1000
  54. with self.test_session():
  55. inputs = tf.random_uniform((batch_size, height, width, 3))
  56. # Force all Variables to reside on the device.
  57. with tf.variable_scope('on_cpu'), tf.device('/cpu:0'):
  58. inception.inception_resnet_v2(inputs, num_classes)
  59. with tf.variable_scope('on_gpu'), tf.device('/gpu:0'):
  60. inception.inception_resnet_v2(inputs, num_classes)
  61. for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='on_cpu'):
  62. self.assertDeviceEqual(v.device, '/cpu:0')
  63. for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='on_gpu'):
  64. self.assertDeviceEqual(v.device, '/gpu:0')
  65. def testHalfSizeImages(self):
  66. batch_size = 5
  67. height, width = 150, 150
  68. num_classes = 1000
  69. with self.test_session():
  70. inputs = tf.random_uniform((batch_size, height, width, 3))
  71. logits, end_points = inception.inception_resnet_v2(inputs, num_classes)
  72. self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
  73. self.assertListEqual(logits.get_shape().as_list(),
  74. [batch_size, num_classes])
  75. pre_pool = end_points['PrePool']
  76. self.assertListEqual(pre_pool.get_shape().as_list(),
  77. [batch_size, 3, 3, 1536])
  78. def testUnknownBatchSize(self):
  79. batch_size = 1
  80. height, width = 299, 299
  81. num_classes = 1000
  82. with self.test_session() as sess:
  83. inputs = tf.placeholder(tf.float32, (None, height, width, 3))
  84. logits, _ = inception.inception_resnet_v2(inputs, num_classes)
  85. self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
  86. self.assertListEqual(logits.get_shape().as_list(),
  87. [None, num_classes])
  88. images = tf.random_uniform((batch_size, height, width, 3))
  89. sess.run(tf.global_variables_initializer())
  90. output = sess.run(logits, {inputs: images.eval()})
  91. self.assertEquals(output.shape, (batch_size, num_classes))
  92. def testEvaluation(self):
  93. batch_size = 2
  94. height, width = 299, 299
  95. num_classes = 1000
  96. with self.test_session() as sess:
  97. eval_inputs = tf.random_uniform((batch_size, height, width, 3))
  98. logits, _ = inception.inception_resnet_v2(eval_inputs,
  99. num_classes,
  100. is_training=False)
  101. predictions = tf.argmax(logits, 1)
  102. sess.run(tf.global_variables_initializer())
  103. output = sess.run(predictions)
  104. self.assertEquals(output.shape, (batch_size,))
  105. def testTrainEvalWithReuse(self):
  106. train_batch_size = 5
  107. eval_batch_size = 2
  108. height, width = 150, 150
  109. num_classes = 1000
  110. with self.test_session() as sess:
  111. train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
  112. inception.inception_resnet_v2(train_inputs, num_classes)
  113. eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
  114. logits, _ = inception.inception_resnet_v2(eval_inputs,
  115. num_classes,
  116. is_training=False,
  117. reuse=True)
  118. predictions = tf.argmax(logits, 1)
  119. sess.run(tf.global_variables_initializer())
  120. output = sess.run(predictions)
  121. self.assertEquals(output.shape, (eval_batch_size,))
  122. if __name__ == '__main__':
  123. tf.test.main()