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_bounding_box_encode.py 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright 2022 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. import numpy as np
  15. import pytest
  16. import mindspore as ms
  17. from mindspore import context, Tensor
  18. from mindspore.common.api import _cell_graph_executor
  19. from mindspore.nn import Cell
  20. from mindspore.ops import operations as P
  21. _anchor_box = Tensor(np.ones([32, 4]), ms.float32)
  22. _gt_boxes = Tensor(np.ones([32, 4]), ms.float32)
  23. class Net(Cell):
  24. """
  25. Create the test net.
  26. """
  27. def __init__(self, strategy=None):
  28. super(Net, self).__init__()
  29. self.bbox_encode = P.BoundingBoxEncode().shard(strategy)
  30. def construct(self, anchor_boxes, gt_boxes):
  31. x = self.bbox_encode(anchor_boxes, gt_boxes)
  32. return x
  33. def compile_net(net: Cell, *inputs):
  34. net.set_auto_parallel()
  35. net.set_train()
  36. _cell_graph_executor.compile(net, *inputs)
  37. context.reset_auto_parallel_context()
  38. def test_bounding_box_encode_data_parallel():
  39. """
  40. Feature: test BoundingBoxEncode data parallel strategy
  41. Description: only shard the batch dimension
  42. Expectation: compile success
  43. """
  44. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  45. strategy = ((8, 1), (8, 1))
  46. net = Net(strategy)
  47. compile_net(net, _anchor_box, _gt_boxes)
  48. def test_bounding_box_encode_auto_parallel():
  49. """
  50. Feature: test BoundingBoxEncode auto parallel
  51. Description: auto parallel
  52. Expectation: compile success
  53. """
  54. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  55. net = Net()
  56. compile_net(net, _anchor_box, _gt_boxes)
  57. def test_bounding_box_encode_strategy_error():
  58. """
  59. Feature: test BoundingBoxEncode with illegal strategy
  60. Description: illegal strategy
  61. Expectation: raise RuntimeError
  62. """
  63. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  64. strategy = ((8, 1), (4, 1))
  65. net = Net(strategy)
  66. with pytest.raises(RuntimeError):
  67. compile_net(net, _anchor_box, _gt_boxes)
  68. context.reset_auto_parallel_context()