inception_test.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright 2016 Google Inc. 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."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import tensorflow as tf
  20. from inception.slim import inception_model as 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_v3(inputs, num_classes)
  29. self.assertTrue(logits.op.name.startswith('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_v3(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('aux_logits' in end_points)
  44. aux_logits = end_points['aux_logits']
  45. self.assertListEqual(aux_logits.get_shape().as_list(),
  46. [batch_size, num_classes])
  47. pre_pool = end_points['mixed_8x8x2048b']
  48. self.assertListEqual(pre_pool.get_shape().as_list(),
  49. [batch_size, 8, 8, 2048])
  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_v3(inputs, num_classes)
  59. with tf.variable_scope('on_gpu'), tf.device('/gpu:0'):
  60. inception.inception_v3(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_v3(inputs, num_classes)
  72. self.assertTrue(logits.op.name.startswith('logits'))
  73. self.assertListEqual(logits.get_shape().as_list(),
  74. [batch_size, num_classes])
  75. pre_pool = end_points['mixed_8x8x2048b']
  76. self.assertListEqual(pre_pool.get_shape().as_list(),
  77. [batch_size, 3, 3, 2048])
  78. def testUnknowBatchSize(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_v3(inputs, num_classes)
  85. self.assertTrue(logits.op.name.startswith('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.initialize_all_variables())
  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_v3(eval_inputs, num_classes,
  99. is_training=False)
  100. predictions = tf.argmax(logits, 1)
  101. sess.run(tf.initialize_all_variables())
  102. output = sess.run(predictions)
  103. self.assertEquals(output.shape, (batch_size,))
  104. def testTrainEvalWithReuse(self):
  105. train_batch_size = 5
  106. eval_batch_size = 2
  107. height, width = 150, 150
  108. num_classes = 1000
  109. with self.test_session() as sess:
  110. train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
  111. inception.inception_v3(train_inputs, num_classes)
  112. tf.get_variable_scope().reuse_variables()
  113. eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
  114. logits, _ = inception.inception_v3(eval_inputs, num_classes,
  115. is_training=False)
  116. predictions = tf.argmax(logits, 1)
  117. sess.run(tf.initialize_all_variables())
  118. output = sess.run(predictions)
  119. self.assertEquals(output.shape, (eval_batch_size,))
  120. if __name__ == '__main__':
  121. tf.test.main()