shapes_test.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 shapes."""
  16. import numpy as np
  17. import tensorflow as tf
  18. import shapes
  19. def _rand(*size):
  20. return np.random.uniform(size=size).astype('f')
  21. class ShapesTest(tf.test.TestCase):
  22. """Tests just the shapes from a call to transposing_reshape."""
  23. def __init__(self, other):
  24. super(ShapesTest, self).__init__(other)
  25. self.batch_size = 4
  26. self.im_height = 24
  27. self.im_width = 36
  28. self.depth = 20
  29. def testReshapeTile(self):
  30. """Tests that a tiled input can be reshaped to the batch dimension."""
  31. fake = tf.placeholder(
  32. tf.float32, shape=(None, None, None, self.depth), name='inputs')
  33. real = _rand(self.batch_size, self.im_height, self.im_width, self.depth)
  34. with self.test_session() as sess:
  35. outputs = shapes.transposing_reshape(
  36. fake, src_dim=2, part_a=3, part_b=-1, dest_dim_a=0, dest_dim_b=2)
  37. res_image = sess.run([outputs], feed_dict={fake: real})
  38. self.assertEqual(
  39. tuple(res_image[0].shape),
  40. (self.batch_size * 3, self.im_height, self.im_width / 3, self.depth))
  41. def testReshapeDepth(self):
  42. """Tests that depth can be reshaped to the x dimension."""
  43. fake = tf.placeholder(
  44. tf.float32, shape=(None, None, None, self.depth), name='inputs')
  45. real = _rand(self.batch_size, self.im_height, self.im_width, self.depth)
  46. with self.test_session() as sess:
  47. outputs = shapes.transposing_reshape(
  48. fake, src_dim=3, part_a=4, part_b=-1, dest_dim_a=2, dest_dim_b=3)
  49. res_image = sess.run([outputs], feed_dict={fake: real})
  50. self.assertEqual(
  51. tuple(res_image[0].shape),
  52. (self.batch_size, self.im_height, self.im_width * 4, self.depth / 4))
  53. class DataTest(tf.test.TestCase):
  54. """Tests that the data is moved correctly in a call to transposing_reshape.
  55. """
  56. def testTransposingReshape_2_2_3_2_1(self):
  57. """Case: dest_a == src, dest_b < src: Split with Least sig part going left.
  58. """
  59. with self.test_session() as sess:
  60. fake = tf.placeholder(
  61. tf.float32, shape=(None, None, None, 2), name='inputs')
  62. outputs = shapes.transposing_reshape(
  63. fake, src_dim=2, part_a=2, part_b=3, dest_dim_a=2, dest_dim_b=1)
  64. # Make real inputs. The tensor looks like this:
  65. # tensor=[[[[0, 1][2, 3][4, 5][6, 7][8, 9][10, 11]]
  66. # [[12, 13][14, 15][16, 17][18, 19][20, 21][22, 23]]
  67. # [[[24, 25]...
  68. real = np.arange(120).reshape((5, 2, 6, 2))
  69. np_array = sess.run([outputs], feed_dict={fake: real})[0]
  70. self.assertEqual(tuple(np_array.shape), (5, 6, 2, 2))
  71. self.assertAllEqual(np_array[0, :, :, :],
  72. [[[0, 1], [6, 7]], [[12, 13], [18, 19]],
  73. [[2, 3], [8, 9]], [[14, 15], [20, 21]],
  74. [[4, 5], [10, 11]], [[16, 17], [22, 23]]])
  75. def testTransposingReshape_2_2_3_2_3(self):
  76. """Case: dest_a == src, dest_b > src: Split with Least sig part going right.
  77. """
  78. with self.test_session() as sess:
  79. fake = tf.placeholder(
  80. tf.float32, shape=(None, None, None, 2), name='inputs')
  81. outputs = shapes.transposing_reshape(
  82. fake, src_dim=2, part_a=2, part_b=3, dest_dim_a=2, dest_dim_b=3)
  83. # Make real inputs. The tensor looks like this:
  84. # tensor=[[[[0, 1][2, 3][4, 5][6, 7][8, 9][10, 11]]
  85. # [[12, 13][14, 15][16, 17][18, 19][20, 21][22, 23]]
  86. # [[[24, 25]...
  87. real = np.arange(120).reshape((5, 2, 6, 2))
  88. np_array = sess.run([outputs], feed_dict={fake: real})[0]
  89. self.assertEqual(tuple(np_array.shape), (5, 2, 2, 6))
  90. self.assertAllEqual(
  91. np_array[0, :, :, :],
  92. [[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]],
  93. [[12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]]])
  94. def testTransposingReshape_2_2_3_2_2(self):
  95. """Case: dest_a == src, dest_b == src. Transpose within dimension 2.
  96. """
  97. with self.test_session() as sess:
  98. fake = tf.placeholder(
  99. tf.float32, shape=(None, None, None, 2), name='inputs')
  100. outputs = shapes.transposing_reshape(
  101. fake, src_dim=2, part_a=2, part_b=3, dest_dim_a=2, dest_dim_b=2)
  102. # Make real inputs. The tensor looks like this:
  103. # tensor=[[[[0, 1][2, 3][4, 5][6, 7][8, 9][10, 11]]
  104. # [[12, 13][14, 15][16, 17][18, 19][20, 21][22, 23]]
  105. # [[[24, 25]...
  106. real = np.arange(120).reshape((5, 2, 6, 2))
  107. np_array = sess.run([outputs], feed_dict={fake: real})[0]
  108. self.assertEqual(tuple(np_array.shape), (5, 2, 6, 2))
  109. self.assertAllEqual(
  110. np_array[0, :, :, :],
  111. [[[0, 1], [6, 7], [2, 3], [8, 9], [4, 5], [10, 11]],
  112. [[12, 13], [18, 19], [14, 15], [20, 21], [16, 17], [22, 23]]])
  113. def testTransposingReshape_2_2_3_1_2(self):
  114. """Case: dest_a < src, dest_b == src. Split with Most sig part going left.
  115. """
  116. with self.test_session() as sess:
  117. fake = tf.placeholder(
  118. tf.float32, shape=(None, None, None, 2), name='inputs')
  119. outputs = shapes.transposing_reshape(
  120. fake, src_dim=2, part_a=2, part_b=3, dest_dim_a=1, dest_dim_b=2)
  121. # Make real inputs. The tensor looks like this:
  122. # tensor=[[[[0, 1][2, 3][4, 5][6, 7][8, 9][10, 11]]
  123. # [[12, 13][14, 15][16, 17][18, 19][20, 21][22, 23]]
  124. # [[[24, 25]...
  125. real = np.arange(120).reshape((5, 2, 6, 2))
  126. np_array = sess.run([outputs], feed_dict={fake: real})[0]
  127. self.assertEqual(tuple(np_array.shape), (5, 4, 3, 2))
  128. self.assertAllEqual(np_array[0, :, :, :],
  129. [[[0, 1], [2, 3], [4, 5]],
  130. [[12, 13], [14, 15], [16, 17]],
  131. [[6, 7], [8, 9], [10, 11]],
  132. [[18, 19], [20, 21], [22, 23]]])
  133. def testTransposingReshape_2_2_3_3_2(self):
  134. """Case: dest_a < src, dest_b == src. Split with Most sig part going right.
  135. """
  136. with self.test_session() as sess:
  137. fake = tf.placeholder(
  138. tf.float32, shape=(None, None, None, 2), name='inputs')
  139. outputs = shapes.transposing_reshape(
  140. fake, src_dim=2, part_a=2, part_b=3, dest_dim_a=3, dest_dim_b=2)
  141. # Make real inputs. The tensor looks like this:
  142. # tensor=[[[[0, 1][2, 3][4, 5][6, 7][8, 9][10, 11]]
  143. # [[12, 13][14, 15][16, 17][18, 19][20, 21][22, 23]]
  144. # [[[24, 25]...
  145. real = np.arange(120).reshape((5, 2, 6, 2))
  146. np_array = sess.run([outputs], feed_dict={fake: real})[0]
  147. self.assertEqual(tuple(np_array.shape), (5, 2, 3, 4))
  148. self.assertAllEqual(
  149. np_array[0, :, :, :],
  150. [[[0, 1, 6, 7], [2, 3, 8, 9], [4, 5, 10, 11]],
  151. [[12, 13, 18, 19], [14, 15, 20, 21], [16, 17, 22, 23]]])
  152. if __name__ == '__main__':
  153. tf.test.main()