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_avgpool_op.py 2.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  21. @pytest.mark.level0
  22. @pytest.mark.platform_x86_cpu
  23. @pytest.mark.env_onecard
  24. def test_avgpool_k2s1pv():
  25. x = np.arange(1 * 1 * 6 * 6).reshape((1, 1, 6, 6)).astype(np.float32)
  26. net = nn.AvgPool2d(kernel_size=2, stride=1, pad_mode='valid')
  27. out = net(Tensor(x))
  28. print(out)
  29. expect_result = np.array(
  30. [[[[3.5, 4.5, 5.5, 6.5, 7.5],
  31. [9.5, 10.5, 11.5, 12.5, 13.5],
  32. [15.5, 16.5, 17.5, 18.5, 19.5],
  33. [21.5, 22.5, 23.5, 24.5, 25.5],
  34. [27.5, 28.5, 29.5, 30.5, 31.5]]]]
  35. )
  36. assert np.allclose(out.asnumpy(), expect_result)
  37. @pytest.mark.level0
  38. @pytest.mark.platform_x86_cpu
  39. @pytest.mark.env_onecard
  40. def test_avgpool_k2s2pv():
  41. x = np.arange(1 * 1 * 6 * 6).reshape((1, 1, 6, 6)).astype(np.float32)
  42. net = nn.AvgPool2d(kernel_size=2, stride=2, pad_mode='valid')
  43. out = net(Tensor(x))
  44. print(out)
  45. expect_result = np.array(
  46. [[[[3.5, 5.5, 7.5],
  47. [15.5, 17.5, 19.5],
  48. [27.5, 29.5, 31.5]]]]
  49. )
  50. assert np.allclose(out.asnumpy(), expect_result)
  51. @pytest.mark.level0
  52. @pytest.mark.platform_x86_cpu
  53. @pytest.mark.env_onecard
  54. def test_avgpool_k3s2pv():
  55. x = np.arange(1 * 1 * 6 * 6).reshape((1, 1, 6, 6)).astype(np.float32)
  56. net = nn.AvgPool2d(kernel_size=3, stride=2, pad_mode='valid')
  57. out = net(Tensor(x))
  58. print(out)
  59. expect_result = np.array(
  60. [[[[7., 9.],
  61. [19., 21.]]]]
  62. )
  63. assert np.allclose(out.asnumpy(), expect_result)
  64. @pytest.mark.level0
  65. @pytest.mark.platform_x86_cpu
  66. @pytest.mark.env_onecard
  67. def test_avgpool_k3s2ps():
  68. x = np.arange(1 * 1 * 6 * 6).reshape((1, 1, 6, 6)).astype(np.float32)
  69. net = nn.AvgPool2d(kernel_size=3, stride=2, pad_mode='same')
  70. out = net(Tensor(x))
  71. print(out)
  72. expect_result = np.array(
  73. [[[[7., 9., 10.5],
  74. [19., 21., 22.5],
  75. [28., 30., 31.5]]]]
  76. )
  77. assert np.allclose(out.asnumpy(), expect_result)
  78. if __name__ == '__main__':
  79. test_avgpool_k2s1pv()
  80. test_avgpool_k2s2pv()
  81. test_avgpool_k3s2pv()
  82. test_avgpool_k3s2ps()