#models_test.py# 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 DSN components."""
  16. import numpy as np
  17. import tensorflow as tf
  18. import models
  19. class SharedEncodersTest(tf.test.TestCase):
  20. def _testSharedEncoder(self,
  21. input_shape=[5, 28, 28, 1],
  22. model=models.dann_mnist,
  23. is_training=True):
  24. images = tf.to_float(np.random.rand(*input_shape))
  25. with self.test_session() as sess:
  26. logits, _ = model(images)
  27. sess.run(tf.global_variables_initializer())
  28. logits_np = sess.run(logits)
  29. return logits_np
  30. def testBuildGRLMnistModel(self):
  31. logits = self._testSharedEncoder(model=getattr(models,
  32. 'dann_mnist'))
  33. self.assertEqual(logits.shape, (5, 10))
  34. self.assertTrue(np.any(logits))
  35. def testBuildGRLSvhnModel(self):
  36. logits = self._testSharedEncoder(model=getattr(models,
  37. 'dann_svhn'))
  38. self.assertEqual(logits.shape, (5, 10))
  39. self.assertTrue(np.any(logits))
  40. def testBuildGRLGtsrbModel(self):
  41. logits = self._testSharedEncoder([5, 40, 40, 3],
  42. getattr(models, 'dann_gtsrb'))
  43. self.assertEqual(logits.shape, (5, 43))
  44. self.assertTrue(np.any(logits))
  45. def testBuildPoseModel(self):
  46. logits = self._testSharedEncoder([5, 64, 64, 4],
  47. getattr(models, 'dsn_cropped_linemod'))
  48. self.assertEqual(logits.shape, (5, 11))
  49. self.assertTrue(np.any(logits))
  50. def testBuildPoseModelWithBatchNorm(self):
  51. images = tf.to_float(np.random.rand(10, 64, 64, 4))
  52. with self.test_session() as sess:
  53. logits, _ = getattr(models, 'dsn_cropped_linemod')(
  54. images, batch_norm_params=models.default_batch_norm_params(True))
  55. sess.run(tf.global_variables_initializer())
  56. logits_np = sess.run(logits)
  57. self.assertEqual(logits_np.shape, (10, 11))
  58. self.assertTrue(np.any(logits_np))
  59. class EncoderTest(tf.test.TestCase):
  60. def _testEncoder(self, batch_norm_params=None, channels=1):
  61. images = tf.to_float(np.random.rand(10, 28, 28, channels))
  62. with self.test_session() as sess:
  63. end_points = models.default_encoder(
  64. images, 128, batch_norm_params=batch_norm_params)
  65. sess.run(tf.global_variables_initializer())
  66. private_code = sess.run(end_points['fc3'])
  67. self.assertEqual(private_code.shape, (10, 128))
  68. self.assertTrue(np.any(private_code))
  69. self.assertTrue(np.all(np.isfinite(private_code)))
  70. def testEncoder(self):
  71. self._testEncoder()
  72. def testEncoderMultiChannel(self):
  73. self._testEncoder(None, 4)
  74. def testEncoderIsTrainingBatchNorm(self):
  75. self._testEncoder(models.default_batch_norm_params(True))
  76. def testEncoderBatchNorm(self):
  77. self._testEncoder(models.default_batch_norm_params(False))
  78. class DecoderTest(tf.test.TestCase):
  79. def _testDecoder(self,
  80. height=64,
  81. width=64,
  82. channels=4,
  83. batch_norm_params=None,
  84. decoder=models.small_decoder):
  85. codes = tf.to_float(np.random.rand(32, 100))
  86. with self.test_session() as sess:
  87. output = decoder(
  88. codes,
  89. height=height,
  90. width=width,
  91. channels=channels,
  92. batch_norm_params=batch_norm_params)
  93. sess.run(tf.initialize_all_variables())
  94. output_np = sess.run(output)
  95. self.assertEqual(output_np.shape, (32, height, width, channels))
  96. self.assertTrue(np.any(output_np))
  97. self.assertTrue(np.all(np.isfinite(output_np)))
  98. def testSmallDecoder(self):
  99. self._testDecoder(28, 28, 4, None, getattr(models, 'small_decoder'))
  100. def testSmallDecoderThreeChannels(self):
  101. self._testDecoder(28, 28, 3)
  102. def testSmallDecoderBatchNorm(self):
  103. self._testDecoder(28, 28, 4, models.default_batch_norm_params(False))
  104. def testSmallDecoderIsTrainingBatchNorm(self):
  105. self._testDecoder(28, 28, 4, models.default_batch_norm_params(True))
  106. def testLargeDecoder(self):
  107. self._testDecoder(32, 32, 4, None, getattr(models, 'large_decoder'))
  108. def testLargeDecoderThreeChannels(self):
  109. self._testDecoder(32, 32, 3, None, getattr(models, 'large_decoder'))
  110. def testLargeDecoderBatchNorm(self):
  111. self._testDecoder(32, 32, 4,
  112. models.default_batch_norm_params(False),
  113. getattr(models, 'large_decoder'))
  114. def testLargeDecoderIsTrainingBatchNorm(self):
  115. self._testDecoder(32, 32, 4,
  116. models.default_batch_norm_params(True),
  117. getattr(models, 'large_decoder'))
  118. def testGtsrbDecoder(self):
  119. self._testDecoder(40, 40, 3, None, getattr(models, 'large_decoder'))
  120. def testGtsrbDecoderBatchNorm(self):
  121. self._testDecoder(40, 40, 4,
  122. models.default_batch_norm_params(False),
  123. getattr(models, 'gtsrb_decoder'))
  124. def testGtsrbDecoderIsTrainingBatchNorm(self):
  125. self._testDecoder(40, 40, 4,
  126. models.default_batch_norm_params(True),
  127. getattr(models, 'gtsrb_decoder'))
  128. if __name__ == '__main__':
  129. tf.test.main()