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_check_valid_op.py 3.1 kB

5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. from mindspore.ops import operations as P
  21. class NetCheckValid(nn.Cell):
  22. def __init__(self):
  23. super(NetCheckValid, self).__init__()
  24. self.valid = P.CheckValid()
  25. def construct(self, anchor, image_metas):
  26. return self.valid(anchor, image_metas)
  27. def check_valid(nptype):
  28. anchor = np.array([[50, 0, 100, 700], [-2, 2, 8, 100], [10, 20, 300, 2000]], nptype)
  29. image_metas = np.array([768, 1280, 1], nptype)
  30. anchor_box = Tensor(anchor)
  31. image_metas_box = Tensor(image_metas)
  32. expect = np.array([True, False, False], np.bool)
  33. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  34. boundingbox_decode = NetCheckValid()
  35. output = boundingbox_decode(anchor_box, image_metas_box)
  36. assert np.array_equal(output.asnumpy(), expect)
  37. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  38. boundingbox_decode = NetCheckValid()
  39. output = boundingbox_decode(anchor_box, image_metas_box)
  40. assert np.array_equal(output.asnumpy(), expect)
  41. @pytest.mark.level0
  42. @pytest.mark.platform_x86_gpu_training
  43. @pytest.mark.env_onecard
  44. def test_check_valid_float32():
  45. check_valid(np.float32)
  46. @pytest.mark.level0
  47. @pytest.mark.platform_x86_gpu_training
  48. @pytest.mark.env_onecard
  49. def test_check_valid_float16():
  50. check_valid(np.float16)
  51. @pytest.mark.level0
  52. @pytest.mark.platform_x86_gpu_training
  53. @pytest.mark.env_onecard
  54. def test_check_valid_int16():
  55. check_valid(np.int16)
  56. @pytest.mark.level0
  57. @pytest.mark.platform_x86_gpu_training
  58. @pytest.mark.env_onecard
  59. def test_check_valid_uint8():
  60. anchor = np.array([[5, 0, 10, 70], [2, 2, 8, 10], [1, 2, 30, 200]], np.uint8)
  61. image_metas = np.array([76, 128, 1], np.uint8)
  62. anchor_box = Tensor(anchor)
  63. image_metas_box = Tensor(image_metas)
  64. expect = np.array([True, True, False], np.bool)
  65. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  66. boundingbox_decode = NetCheckValid()
  67. output = boundingbox_decode(anchor_box, image_metas_box)
  68. assert np.array_equal(output.asnumpy(), expect)
  69. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  70. boundingbox_decode = NetCheckValid()
  71. output = boundingbox_decode(anchor_box, image_metas_box)
  72. assert np.array_equal(output.asnumpy(), expect)