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_pooling.py 1.8 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. """
  16. test pooling api
  17. """
  18. import numpy as np
  19. import pytest
  20. import mindspore.nn as nn
  21. from mindspore import Tensor
  22. def test_avgpool2d():
  23. """ test_avgpool2d """
  24. kernel_size = 3
  25. stride = 2
  26. avg_pool = nn.AvgPool2d(kernel_size, stride)
  27. assert avg_pool.kernel_size == 3
  28. assert avg_pool.stride == 2
  29. input_data = Tensor(np.random.randint(0, 255, [1, 3, 6, 6]) * 0.1)
  30. output = avg_pool(input_data)
  31. output_np = output.asnumpy()
  32. assert isinstance(output_np[0][0][0][0], (np.float32, np.float64))
  33. def test_avgpool2d_error_input():
  34. """ test_avgpool2d_error_input """
  35. kernel_size = 5
  36. stride = 2.3
  37. with pytest.raises(TypeError):
  38. nn.AvgPool2d(kernel_size, stride)
  39. def test_maxpool2d():
  40. """ test_maxpool2d """
  41. kernel_size = 3
  42. stride = 3
  43. max_pool = nn.MaxPool2d(kernel_size, stride, pad_mode='SAME')
  44. assert max_pool.kernel_size == 3
  45. assert max_pool.stride == 3
  46. input_data = Tensor(np.random.randint(0, 255, [1, 3, 6, 6]) * 0.1)
  47. output = max_pool(input_data)
  48. output_np = output.asnumpy()
  49. assert isinstance(output_np[0][0][0][0], (np.float32, np.float64))