Skip to content

Commit

Permalink
[PyOV] Extend Python API with ROIAlignRotated-15 (openvinotoolkit#25755)
Browse files Browse the repository at this point in the history
### Details:
 - Add Python API for `ROIAlignRotated-15`

### Tickets:
 - 141569

---------

Co-authored-by: Katarzyna Mitrus <[email protected]>
Co-authored-by: Michal Lukaszewski <[email protected]>
  • Loading branch information
3 people authored and mory91 committed Aug 13, 2024
1 parent 84bcc77 commit 64ffd54
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 42 additions & 0 deletions src/bindings/python/src/openvino/runtime/opset15/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
)
32 changes: 32 additions & 0 deletions src/bindings/python/tests/test_graph/test_create_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 64ffd54

Please sign in to comment.