You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_time_distributed_op.py 9.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  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. import numpy as np
  16. import pytest
  17. import mindspore
  18. import mindspore.context as context
  19. import mindspore.nn as nn
  20. import mindspore.ops as ops
  21. from mindspore import Tensor
  22. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  23. class TestTimeDistributed(nn.Cell):
  24. def __init__(self, cell, time_axis, reshape_with_axis=None):
  25. super(TestTimeDistributed, self).__init__()
  26. self.time_distributed = nn.TimeDistributed(cell, time_axis, reshape_with_axis)
  27. def construct(self, inputs):
  28. return self.time_distributed(inputs)
  29. @pytest.mark.level0
  30. @pytest.mark.platform_x86_cpu
  31. @pytest.mark.env_onecard
  32. def test_time_distributed_conv2d():
  33. inputs = np.random.randint(0, 10, [32, 12, 10, 10])
  34. conv2d = nn.Conv2d(12, 24, 4, has_bias=False, weight_init='normal')
  35. output_expect = conv2d(Tensor(inputs, mindspore.float32)).asnumpy()
  36. inputs = inputs.reshape([32, 1, 12, 10, 10]).repeat(6, axis=1)
  37. time_distributed = TestTimeDistributed(conv2d, time_axis=1, reshape_with_axis=0)
  38. output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy()
  39. for i in range(output.shape[1]):
  40. assert np.all(np.abs(output[:, i, :] - output_expect) < 1e-5)
  41. print("Conv2D layer wrapped successful")
  42. @pytest.mark.level0
  43. @pytest.mark.platform_x86_cpu
  44. @pytest.mark.env_onecard
  45. def test_time_distributed_maxpool2d():
  46. inputs = np.random.randint(0, 10, [32, 12, 10, 10])
  47. pool = nn.MaxPool2d(kernel_size=3, stride=1)
  48. output_expect = pool(Tensor(inputs, mindspore.float32)).asnumpy()
  49. inputs = inputs.reshape([32, 1, 12, 10, 10]).repeat(6, axis=1)
  50. time_distributed = TestTimeDistributed(pool, time_axis=1, reshape_with_axis=0)
  51. output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy()
  52. for i in range(output.shape[1]):
  53. assert np.all(output[:, i, :] == output_expect)
  54. print("MaxPooling2D layer wrapped successful")
  55. @pytest.mark.level0
  56. @pytest.mark.platform_x86_cpu
  57. @pytest.mark.env_onecard
  58. def test_time_distributed_dense():
  59. inputs = np.random.randint(0, 10, [32, 10])
  60. dense = nn.Dense(10, 6)
  61. output_expect = dense(Tensor(inputs, mindspore.float32)).asnumpy()
  62. inputs = inputs.reshape([32, 1, 10]).repeat(6, axis=1)
  63. time_distributed = TestTimeDistributed(dense, time_axis=1, reshape_with_axis=0)
  64. output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy()
  65. for i in range(output.shape[1]):
  66. assert np.all(output[:, i, :] == output_expect)
  67. print("Dense layer wrapped successful")
  68. @pytest.mark.level0
  69. @pytest.mark.platform_x86_cpu
  70. @pytest.mark.env_onecard
  71. def test_time_distributed_dense_pynative():
  72. context.set_context(mode=context.PYNATIVE_MODE, device_target='CPU')
  73. inputs = np.random.randint(0, 10, [32, 10])
  74. dense = nn.Dense(10, 6)
  75. output_expect = dense(Tensor(inputs, mindspore.float32)).asnumpy()
  76. inputs = inputs.reshape([32, 1, 10]).repeat(6, axis=1)
  77. time_distributed = TestTimeDistributed(dense, time_axis=1, reshape_with_axis=0)
  78. output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy()
  79. for i in range(output.shape[1]):
  80. assert np.all(output[:, i, :] == output_expect)
  81. print("Dense layer with pynative mode wrapped successful")
  82. @pytest.mark.level0
  83. @pytest.mark.platform_x86_cpu
  84. @pytest.mark.env_onecard
  85. def test_time_distributed_dense_with_reshape_axis_not_first():
  86. inputs = np.random.randint(0, 10, [32, 10])
  87. dense = nn.Dense(10, 6)
  88. output_expect = dense(Tensor(inputs, mindspore.float32)).asnumpy()
  89. inputs = inputs.reshape([1, 32, 10]).repeat(6, axis=0)
  90. time_distributed = TestTimeDistributed(dense, time_axis=0, reshape_with_axis=1)
  91. output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy()
  92. for i in range(output.shape[0]):
  93. assert np.all(output[i, :] == output_expect)
  94. print("Dense layer wrapped successful")
  95. @pytest.mark.level0
  96. @pytest.mark.platform_x86_cpu
  97. @pytest.mark.env_onecard
  98. def test_time_distributed_argmax():
  99. inputs = np.random.randint(0, 10, [3, 4])
  100. argmax = ops.Argmax(output_type=mindspore.int32, axis=1)
  101. output_expect = argmax(Tensor(inputs, mindspore.float32)).asnumpy()
  102. inputs = inputs.reshape([3, 1, 4]).repeat(6, axis=1)
  103. time_distributed = TestTimeDistributed(argmax, time_axis=1, reshape_with_axis=0)
  104. output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy()
  105. for i in range(output.shape[1]):
  106. assert np.all(output[:, i] == output_expect)
  107. print("Argmax op wrapped successful")
  108. @pytest.mark.level0
  109. @pytest.mark.platform_x86_cpu
  110. @pytest.mark.env_onecard
  111. def test_time_distributed_flatten():
  112. inputs = np.random.randint(0, 10, [3, 4, 5])
  113. flatten = nn.Flatten()
  114. output_expect = flatten(Tensor(inputs, mindspore.float32)).asnumpy()
  115. inputs = inputs.reshape([3, 1, 4, 5]).repeat(6, axis=1)
  116. time_distributed = TestTimeDistributed(flatten, time_axis=1, reshape_with_axis=0)
  117. output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy()
  118. for i in range(output.shape[1]):
  119. assert np.all(output[:, i, :] == output_expect)
  120. print("Flatten op wrapped successful")
  121. @pytest.mark.level0
  122. @pytest.mark.platform_x86_cpu
  123. @pytest.mark.env_onecard
  124. def test_time_distributed_conv2d_no_reshape_axis():
  125. inputs = np.random.randint(0, 10, [32, 12, 10, 10])
  126. conv2d = nn.Conv2d(12, 24, 4, has_bias=False, weight_init='normal')
  127. output_expect = conv2d(Tensor(inputs, mindspore.float32)).asnumpy()
  128. inputs = inputs.reshape([32, 1, 12, 10, 10]).repeat(6, axis=1)
  129. time_distributed = TestTimeDistributed(conv2d, time_axis=1)
  130. output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy()
  131. for i in range(output.shape[1]):
  132. assert np.all(output[:, i, :] == output_expect)
  133. print("Conv2D layer with no reshape axis wrapped successful")
  134. @pytest.mark.level0
  135. @pytest.mark.platform_x86_cpu
  136. @pytest.mark.env_onecard
  137. def test_time_distributed_maxpool2d_no_reshape_axis():
  138. inputs = np.random.randint(0, 10, [32, 12, 10, 10])
  139. pool = nn.MaxPool2d(kernel_size=3, stride=1)
  140. output_expect = pool(Tensor(inputs, mindspore.float32)).asnumpy()
  141. inputs = inputs.reshape([32, 1, 12, 10, 10]).repeat(6, axis=1)
  142. time_distributed = TestTimeDistributed(pool, time_axis=1)
  143. output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy()
  144. for i in range(output.shape[1]):
  145. assert np.all(output[:, i, :] == output_expect)
  146. print("MaxPooling2D layer with no reshape axis wrapped successful")
  147. @pytest.mark.level0
  148. @pytest.mark.platform_x86_cpu
  149. @pytest.mark.env_onecard
  150. def test_time_distributed_dense_no_reshape_axis():
  151. inputs = np.random.randint(0, 10, [32, 10])
  152. dense = nn.Dense(10, 6)
  153. output_expect = dense(Tensor(inputs, mindspore.float32)).asnumpy()
  154. inputs = inputs.reshape([32, 1, 10]).repeat(6, axis=1)
  155. time_distributed = TestTimeDistributed(dense, time_axis=1)
  156. output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy()
  157. for i in range(output.shape[1]):
  158. assert np.all(output[:, i, :] == output_expect)
  159. print("Dense layer with no reshape axis wrapped successful")
  160. @pytest.mark.level0
  161. @pytest.mark.platform_x86_cpu
  162. @pytest.mark.env_onecard
  163. def test_time_distributed_argmax_no_reshape_axis():
  164. inputs = np.random.randint(0, 10, [3, 4])
  165. argmax = ops.Argmax(output_type=mindspore.int32, axis=1)
  166. output_expect = argmax(Tensor(inputs, mindspore.float32)).asnumpy()
  167. inputs = inputs.reshape([3, 1, 4]).repeat(6, axis=1)
  168. time_distributed = TestTimeDistributed(argmax, time_axis=1)
  169. output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy()
  170. for i in range(output.shape[1]):
  171. assert np.all(output[:, i] == output_expect)
  172. print("Argmax op with no reshape axis wrapped successful")
  173. @pytest.mark.level0
  174. @pytest.mark.platform_x86_cpu
  175. @pytest.mark.env_onecard
  176. def test_time_distributed_flatten_no_reshape_axis():
  177. inputs = np.random.randint(0, 10, [3, 4, 5])
  178. flatten = nn.Flatten()
  179. output_expect = flatten(Tensor(inputs, mindspore.float32)).asnumpy()
  180. inputs = inputs.reshape([3, 1, 4, 5]).repeat(6, axis=1)
  181. time_distributed = TestTimeDistributed(flatten, time_axis=1)
  182. output = time_distributed(Tensor(inputs, mindspore.float32)).asnumpy()
  183. for i in range(output.shape[1]):
  184. assert np.all(output[:, i, :] == output_expect)
  185. print("Flatten op with no reshape axis wrapped successful")