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_maxpool_op.py 11 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. # Copyright 2019 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. from functools import reduce
  16. import numpy as np
  17. import pytest
  18. import mindspore.context as context
  19. import mindspore.nn as nn
  20. import mindspore.ops.operations as P
  21. from mindspore import Tensor
  22. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  23. class Net_Pool(nn.Cell):
  24. def __init__(self):
  25. super(Net_Pool, self).__init__()
  26. self.maxpool_fun = nn.MaxPool2d(kernel_size=2, stride=2, pad_mode="VALID")
  27. def construct(self, x):
  28. return self.maxpool_fun(x)
  29. class Net_Pool2(nn.Cell):
  30. def __init__(self):
  31. super(Net_Pool2, self).__init__()
  32. self.maxpool_fun2 = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode="SAME")
  33. def construct(self, x):
  34. return self.maxpool_fun2(x)
  35. @pytest.mark.level0
  36. @pytest.mark.platform_x86_cpu
  37. @pytest.mark.env_onecard
  38. def test_maxpool2d():
  39. x = Tensor(np.array([[[
  40. [0, 1, 2, 3, -4, -5],
  41. [6, 7, 8, 9, -10, -11],
  42. [12, 13, 14, -15, -16, -17],
  43. [18, 19, 20, 21, 22, 23],
  44. [24, 25, 26, 27, 28, 29],
  45. [30, 31, 32, 33, 34, 35]
  46. ]]]).astype(np.float32))
  47. maxpool2d = Net_Pool()
  48. maxpool2d2 = Net_Pool2()
  49. output2 = maxpool2d2(x)
  50. output = maxpool2d(x)
  51. expect_result = (np.array([[[
  52. [7, 9, -4],
  53. [19, 21, 23],
  54. [31, 33, 35]
  55. ]]]))
  56. expect_result2 = (np.array([[[
  57. [14, 14, -4],
  58. [26, 28, 29],
  59. [32, 34, 35]
  60. ]]]))
  61. print(output.asnumpy())
  62. assert (output.asnumpy() == expect_result).all()
  63. print(output2.asnumpy())
  64. assert (output2.asnumpy() == expect_result2).all()
  65. @pytest.mark.level0
  66. @pytest.mark.platform_x86_cpu
  67. @pytest.mark.env_onecard
  68. def test_maxpool():
  69. x = Tensor(np.array([[[
  70. [0, 1, 2, 3, -4, -5],
  71. [6, 7, 8, 9, -10, -11],
  72. [12, 13, 14, -15, -16, -17],
  73. [18, 19, 20, 21, 22, 23],
  74. [24, 25, 26, 27, 28, 29],
  75. [30, 31, 32, 33, 34, 35]
  76. ]]]).astype(np.int16))
  77. maxpool2d = Net_Pool()
  78. with pytest.raises(Exception):
  79. maxpool2d(x)
  80. @pytest.mark.level0
  81. @pytest.mark.platform_x86_cpu
  82. @pytest.mark.env_onecard
  83. def test_max_pool3d_1():
  84. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  85. x_shape = (2, 3, 2, 3, 4)
  86. kernel_size = (2, 2, 3)
  87. strides = 1
  88. pad_mode = 'VALID'
  89. x_val = np.arange(reduce(lambda x, y: x * y, x_shape))
  90. x_ms = Tensor(x_val).reshape(x_shape).astype(np.float32)
  91. output_ms = P.MaxPool3D(kernel_size=kernel_size, strides=strides, pad_mode=pad_mode)(x_ms)
  92. expert_result = (np.array([[[[[18, 19],
  93. [22, 23]]],
  94. [[[42, 43],
  95. [46, 47]]],
  96. [[[66, 67],
  97. [70, 71]]]],
  98. [[[[90, 91],
  99. [94, 95]]],
  100. [[[114, 115],
  101. [118, 119]]],
  102. [[[138, 139],
  103. [142, 143]]]]]))
  104. assert (output_ms.asnumpy() == expert_result).all()
  105. @pytest.mark.level0
  106. @pytest.mark.platform_x86_cpu
  107. @pytest.mark.env_onecard
  108. def test_max_pool3d_2():
  109. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  110. x_shape = (2, 3, 2, 3, 4)
  111. kernel_size = 2
  112. strides = 1
  113. pad_mode = 'VALID'
  114. x_val = np.arange(reduce(lambda x, y: x * y, x_shape))
  115. x_ms = Tensor(x_val).reshape(x_shape).astype(np.float32)
  116. output_ms = P.MaxPool3D(kernel_size=kernel_size, strides=strides, pad_mode=pad_mode)(x_ms)
  117. expert_result = (np.array([[[[[17, 18, 19],
  118. [21, 22, 23]]],
  119. [[[41, 42, 43],
  120. [45, 46, 47]]],
  121. [[[65, 66, 67],
  122. [69, 70, 71]]]],
  123. [[[[89, 90, 91],
  124. [93, 94, 95]]],
  125. [[[113, 114, 115],
  126. [117, 118, 119]]],
  127. [[[137, 138, 139],
  128. [141, 142, 143]]]]]))
  129. assert (output_ms.asnumpy() == expert_result).all()
  130. @pytest.mark.level0
  131. @pytest.mark.platform_x86_cpu
  132. @pytest.mark.env_onecard
  133. def test_max_pool3d_3():
  134. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  135. x_shape = (2, 3, 2, 3, 4)
  136. kernel_size = 2
  137. strides = 3
  138. pad_mode = 'VALID'
  139. x_val = np.arange(reduce(lambda x, y: x * y, x_shape))
  140. x_ms = Tensor(x_val).reshape(x_shape).astype(np.float32)
  141. output_ms = P.MaxPool3D(kernel_size=kernel_size, strides=strides, pad_mode=pad_mode)(x_ms)
  142. expert_result = (np.array([[[[[17]]],
  143. [[[41]]],
  144. [[[65]]]],
  145. [[[[89]]],
  146. [[[113]]],
  147. [[[137]]]]]))
  148. assert (output_ms.asnumpy() == expert_result).all()
  149. @pytest.mark.level0
  150. @pytest.mark.platform_x86_cpu
  151. @pytest.mark.env_onecard
  152. def test_max_pool3d_4():
  153. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  154. x_shape = (2, 3, 2, 3, 4)
  155. kernel_size = (2, 2, 3)
  156. strides = 1
  157. pad_mode = 'SAME'
  158. x_val = np.arange(reduce(lambda x, y: x * y, x_shape))
  159. x_ms = Tensor(x_val).reshape(x_shape).astype(np.float32)
  160. output_ms = P.MaxPool3D(kernel_size=kernel_size, strides=strides, pad_mode=pad_mode)(x_ms)
  161. expert_result = (np.array([[[[[17, 18, 19, 19],
  162. [21, 22, 23, 23],
  163. [21, 22, 23, 23]],
  164. [[17, 18, 19, 19],
  165. [21, 22, 23, 23],
  166. [21, 22, 23, 23]]],
  167. [[[41, 42, 43, 43],
  168. [45, 46, 47, 47],
  169. [45, 46, 47, 47]],
  170. [[41, 42, 43, 43],
  171. [45, 46, 47, 47],
  172. [45, 46, 47, 47]]],
  173. [[[65, 66, 67, 67],
  174. [69, 70, 71, 71],
  175. [69, 70, 71, 71]],
  176. [[65, 66, 67, 67],
  177. [69, 70, 71, 71],
  178. [69, 70, 71, 71]]]],
  179. [[[[89, 90, 91, 91],
  180. [93, 94, 95, 95],
  181. [93, 94, 95, 95]],
  182. [[89, 90, 91, 91],
  183. [93, 94, 95, 95],
  184. [93, 94, 95, 95]]],
  185. [[[113, 114, 115, 115],
  186. [117, 118, 119, 119],
  187. [117, 118, 119, 119]],
  188. [[113, 114, 115, 115],
  189. [117, 118, 119, 119],
  190. [117, 118, 119, 119]]],
  191. [[[137, 138, 139, 139],
  192. [141, 142, 143, 143],
  193. [141, 142, 143, 143]],
  194. [[137, 138, 139, 139],
  195. [141, 142, 143, 143],
  196. [141, 142, 143, 143]]]]]))
  197. assert (output_ms.asnumpy() == expert_result).all()
  198. @pytest.mark.level0
  199. @pytest.mark.platform_x86_cpu
  200. @pytest.mark.env_onecard
  201. def test_max_pool3d_5():
  202. context.set_context(mode=context.PYNATIVE_MODE, device_target="CPU")
  203. x_shape = (2, 3, 2, 3, 4)
  204. kernel_size = (2, 2, 3)
  205. strides = 1
  206. pad_mode = 'SAME'
  207. x_val = np.arange(reduce(lambda x, y: x * y, x_shape))
  208. x_ms = Tensor(x_val).reshape(x_shape).astype(np.float32)
  209. output_ms = P.MaxPool3D(kernel_size=kernel_size, strides=strides, pad_mode=pad_mode)(x_ms)
  210. expert_result = (np.array([[[[[17, 18, 19, 19],
  211. [21, 22, 23, 23],
  212. [21, 22, 23, 23]],
  213. [[17, 18, 19, 19],
  214. [21, 22, 23, 23],
  215. [21, 22, 23, 23]]],
  216. [[[41, 42, 43, 43],
  217. [45, 46, 47, 47],
  218. [45, 46, 47, 47]],
  219. [[41, 42, 43, 43],
  220. [45, 46, 47, 47],
  221. [45, 46, 47, 47]]],
  222. [[[65, 66, 67, 67],
  223. [69, 70, 71, 71],
  224. [69, 70, 71, 71]],
  225. [[65, 66, 67, 67],
  226. [69, 70, 71, 71],
  227. [69, 70, 71, 71]]]],
  228. [[[[89, 90, 91, 91],
  229. [93, 94, 95, 95],
  230. [93, 94, 95, 95]],
  231. [[89, 90, 91, 91],
  232. [93, 94, 95, 95],
  233. [93, 94, 95, 95]]],
  234. [[[113, 114, 115, 115],
  235. [117, 118, 119, 119],
  236. [117, 118, 119, 119]],
  237. [[113, 114, 115, 115],
  238. [117, 118, 119, 119],
  239. [117, 118, 119, 119]]],
  240. [[[137, 138, 139, 139],
  241. [141, 142, 143, 143],
  242. [141, 142, 143, 143]],
  243. [[137, 138, 139, 139],
  244. [141, 142, 143, 143],
  245. [141, 142, 143, 143]]]]]))
  246. assert (output_ms.asnumpy() == expert_result).all()