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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. from mindspore import Tensor
  19. from mindspore.ops import operations as P
  20. @pytest.mark.level0
  21. @pytest.mark.platform_x86_gpu_training
  22. @pytest.mark.env_onecard
  23. def test_roi_align():
  24. def roi_align_case(data_type):
  25. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  26. x = Tensor(np.array([[
  27. [[1, 2, 3, 4, 5, 6],
  28. [7, 8, 9, 10, 11, 12],
  29. [13, 14, 15, 16, 17, 18],
  30. [19, 20, 21, 22, 23, 24],
  31. [25, 26, 27, 28, 29, 30],
  32. [31, 32, 33, 34, 35, 36]]
  33. ]], data_type))
  34. # test case 1
  35. rois = Tensor(np.array([[0, -2.0, -2.0, 21.0, 21.0]], data_type))
  36. pooled_height, pooled_width, spatial_scale, sample_num = 3, 3, 0.25, 2
  37. roi_align = P.ROIAlign(pooled_height, pooled_width,
  38. spatial_scale, sample_num, 1)
  39. output = roi_align(x, rois)
  40. print(output)
  41. expect = [[[[4.5, 6.5, 8.5],
  42. [16.5, 18.5, 20.5],
  43. [28.5, 30.5, 32.5]]]]
  44. assert (output.asnumpy() == expect).all()
  45. # test case 2
  46. rois = Tensor(np.array([[0, -2.0, -2.0, 22.0, 22.0]], data_type))
  47. pooled_height, pooled_width, spatial_scale, sample_num = 3, 3, 0.25, 2
  48. roi_align = P.ROIAlign(pooled_height, pooled_width,
  49. spatial_scale, sample_num, 0)
  50. output = roi_align(x, rois)
  51. print(output)
  52. expect = [[[[4.5, 6.5, 8.5],
  53. [16.5, 18.5, 20.5],
  54. [28.5, 30.5, 32.5]]]]
  55. assert (output.asnumpy() == expect).all()
  56. # test case 3
  57. pooled_height, pooled_width, spatial_scale, sample_num = 2, 2, 1.0, -1
  58. rois = Tensor(np.array([[0, -2.0, -2.0, 22.0, 22.0]], data_type))
  59. roi_align = P.ROIAlign(pooled_height, pooled_width,
  60. spatial_scale, sample_num, 0)
  61. output = roi_align(x, rois)
  62. print(output)
  63. expect = [[[[6.295, 0.],
  64. [0., 0.]]]]
  65. np.testing.assert_almost_equal(output.asnumpy(), expect, decimal=2)
  66. roi_align_case(np.float32)
  67. roi_align_case(np.float16)