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 2.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. 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. class Net_Pool(nn.Cell):
  22. def __init__(self):
  23. super(Net_Pool, self).__init__()
  24. self.maxpool_fun = nn.MaxPool2d(kernel_size=2, stride=2, pad_mode="VALID")
  25. def construct(self, x):
  26. return self.maxpool_fun(x)
  27. class Net_Pool2(nn.Cell):
  28. def __init__(self):
  29. super(Net_Pool2, self).__init__()
  30. self.maxpool_fun2 = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode="SAME")
  31. def construct(self, x):
  32. return self.maxpool_fun2(x)
  33. @pytest.mark.level0
  34. @pytest.mark.platform_x86_cpu
  35. @pytest.mark.env_onecard
  36. def test_maxpool2d():
  37. x = Tensor(np.array([[[
  38. [0, 1, 2, 3, -4, -5],
  39. [6, 7, 8, 9, -10, -11],
  40. [12, 13, 14, -15, -16, -17],
  41. [18, 19, 20, 21, 22, 23],
  42. [24, 25, 26, 27, 28, 29],
  43. [30, 31, 32, 33, 34, 35]
  44. ]]]).astype(np.float32))
  45. maxpool2d = Net_Pool()
  46. maxpool2d2 = Net_Pool2()
  47. output2 = maxpool2d2(x)
  48. output = maxpool2d(x)
  49. expect_result = (np.array([[[
  50. [7, 9, -4],
  51. [19, 21, 23],
  52. [31, 33, 35]
  53. ]]]))
  54. expect_result2 = (np.array([[[
  55. [14, 14, -4],
  56. [26, 28, 29],
  57. [32, 34, 35]
  58. ]]]))
  59. print(output.asnumpy())
  60. assert (output.asnumpy() == expect_result).all()
  61. print(output2.asnumpy())
  62. assert (output2.asnumpy() == expect_result2).all()
  63. @pytest.mark.level0
  64. @pytest.mark.platform_x86_cpu
  65. @pytest.mark.env_onecard
  66. def test_maxpool():
  67. x = Tensor(np.array([[[
  68. [0, 1, 2, 3, -4, -5],
  69. [6, 7, 8, 9, -10, -11],
  70. [12, 13, 14, -15, -16, -17],
  71. [18, 19, 20, 21, 22, 23],
  72. [24, 25, 26, 27, 28, 29],
  73. [30, 31, 32, 33, 34, 35]
  74. ]]]).astype(np.int16))
  75. maxpool2d = Net_Pool()
  76. with pytest.raises(Exception):
  77. maxpool2d(x)