From 3a6f1fcf70ff1de21122bce82e9b87eb32ab4ade Mon Sep 17 00:00:00 2001 From: hub-bla Date: Fri, 26 Jul 2024 14:47:54 +0200 Subject: [PATCH 1/2] Extend Python API with ROIAlignRotated --- .../src/openvino/runtime/opset15/__init__.py | 1 + .../src/openvino/runtime/opset15/ops.py | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/bindings/python/src/openvino/runtime/opset15/__init__.py b/src/bindings/python/src/openvino/runtime/opset15/__init__.py index d7040d974c1ab9..13087ef74a78a4 100644 --- a/src/bindings/python/src/openvino/runtime/opset15/__init__.py +++ b/src/bindings/python/src/openvino/runtime/opset15/__init__.py @@ -10,3 +10,4 @@ from openvino.runtime.opset15.ops import embedding_bag_offsets from openvino.runtime.opset15.ops import embedding_bag_packed from openvino.runtime.opset15.ops import scatter_nd_update +from openvino.runtime.opset15.ops import roi_align_rotated diff --git a/src/bindings/python/src/openvino/runtime/opset15/ops.py b/src/bindings/python/src/openvino/runtime/opset15/ops.py index 5613de6bd8267a..8ed73361db19f8 100644 --- a/src/bindings/python/src/openvino/runtime/opset15/ops.py +++ b/src/bindings/python/src/openvino/runtime/opset15/ops.py @@ -144,3 +144,45 @@ def embedding_bag_packed( inputs.append(per_sample_weights) return _get_node_factory_opset15().create("EmbeddingBagPacked", as_nodes(*inputs, name=name), {"reduction": reduction}) + + +@nameable_op +def roi_align_rotated( + data: NodeInput, + rois: NodeInput, + batch_indices: NodeInput, + pooled_h: int, + pooled_w: int, + sampling_ratio: int, + spatial_scale: float, + clockwise_mode: bool, + name: Optional[str] = None, +) -> Node: + """Return a node which performs ROIAlignRotated operation. + + :param data: Input data. + :param rois: RoIs (Regions of Interest) to pool over. + :param batch_indices: Tensor with each element denoting the index of + the corresponding image in the batch. + :param pooled_h: Height of the ROI output feature map. + :param pooled_w: Width of the ROI output feature map. + :param sampling_ratio: Number of bins over height and width to use to calculate + each output feature map element. + :param spatial_scale: Multiplicative spatial scale factor to translate ROI coordinates. + :param clockwise_mode: If true, rotation angle is interpreted as clockwise, + otherwise as counterclockwise + :param name: The optional name for the output node + + :return: The new node which performs ROIAlignRotated + """ + return _get_node_factory_opset15().create( + "ROIAlignRotated", + as_nodes(data, rois, batch_indices, name=name), + { + "pooled_h": pooled_h, + "pooled_w": pooled_w, + "sampling_ratio": sampling_ratio, + "spatial_scale": spatial_scale, + "clockwise_mode": clockwise_mode, + }, + ) From 8a4abd901de059d1e648022338fa5e86783a02a7 Mon Sep 17 00:00:00 2001 From: hub-bla Date: Fri, 26 Jul 2024 14:49:51 +0200 Subject: [PATCH 2/2] test roi_align_rotated --- .../python/tests/test_graph/test_create_op.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/bindings/python/tests/test_graph/test_create_op.py b/src/bindings/python/tests/test_graph/test_create_op.py index 006a0b12d3211e..6a6d3cea2ba233 100644 --- a/src/bindings/python/tests/test_graph/test_create_op.py +++ b/src/bindings/python/tests/test_graph/test_create_op.py @@ -12,6 +12,7 @@ import openvino.runtime.opset1 as ov_opset1 import openvino.runtime.opset5 as ov_opset5 import openvino.runtime.opset10 as ov_opset10 +import openvino.runtime.opset15 as ov_opset15 import openvino.runtime.opset11 as ov from openvino.runtime.op.util import VariableInfo, Variable @@ -885,6 +886,37 @@ def test_roi_align(data_shape, rois, batch_indices, pooled_h, pooled_w, sampling assert list(node.get_output_shape(0)) == expected_shape +@pytest.mark.parametrize( + ("data_shape", "rois", "batch_indices", "pooled_h", "pooled_w", "sampling_ratio", "spatial_scale", "clockwise_mode", "expected_shape"), + [ + ([2, 3, 5, 6], [7, 5], [7], 2, 2, 1, 1.0, True, [7, 3, 2, 2]), + ([10, 3, 5, 5], [7, 5], [7], 3, 4, 1, 1.0, True, [7, 3, 3, 4]), + ([10, 3, 5, 5], [3, 5], [3], 3, 4, 1, 1.0, False, [3, 3, 3, 4]), + ([10, 3, 5, 5], [3, 5], [3], 3, 4, 1, float(1), False, [3, 3, 3, 4]), + ], +) +def test_roi_align_rotated(data_shape, rois, batch_indices, pooled_h, pooled_w, sampling_ratio, spatial_scale, clockwise_mode, expected_shape): + data_parameter = ov.parameter(data_shape, name="Data", dtype=np.float32) + rois_parameter = ov.parameter(rois, name="Rois", dtype=np.float32) + batch_indices_parameter = ov.parameter(batch_indices, name="Batch_indices", dtype=np.int32) + + node = ov_opset15.roi_align_rotated( + data_parameter, + rois_parameter, + batch_indices_parameter, + pooled_h, + pooled_w, + sampling_ratio, + spatial_scale, + clockwise_mode, + ) + + assert node.get_type_name() == "ROIAlignRotated" + assert node.get_output_size() == 1 + assert node.get_output_element_type(0) == Type.f32 + assert list(node.get_output_shape(0)) == expected_shape + + @pytest.mark.parametrize("op_name", ["psroipooling", "psroiPoolingOpset1"]) def test_psroi_pooling(op_name): inputs = ov.parameter([1, 72, 4, 5], dtype=np.float32)