OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
Fix Resize negative axes handling for ONNX import and shape inference (#37279)
Harshal Dharpure183e33df7a6e
diff --git a/src/frontends/onnx/frontend/src/op/resize.cpp b/src/frontends/onnx/frontend/src/op/resize.cppindex 39946d218f7..44480569190 100644--- a/src/frontends/onnx/frontend/src/op/resize.cpp+++ b/src/frontends/onnx/frontend/src/op/resize.cpp@@ -130,6 +130,12 @@ ov::OutputVector resize(const ov::frontend::onnx::Node& node) {if (axes_attr.empty()) {return {std::make_shared<v11::Interpolate>(data, scale_or_sizes, attrs)};}++ const auto data_rank = data.get_partial_shape().rank();+ for (auto& axis : axes_attr) {+ axis = common::normalize_axis(node.get_description(), axis, data_rank);+ }+auto axes = std::make_shared<v0::Constant>(ov::element::i64, ov::Shape{axes_attr.size()}, axes_attr);return {std::make_shared<v11::Interpolate>(data, scale_or_sizes, axes, attrs)};}diff --git a/src/frontends/onnx/tests/models/resize11_negative_axes.prototxt b/src/frontends/onnx/tests/models/resize11_negative_axes.prototxtnew file mode 100644index 00000000000..e17a5eb5a89--- /dev/null+++ b/src/frontends/onnx/tests/models/resize11_negative_axes.prototxt@@ -0,0 +1,88 @@+ir_version: 7+producer_name: "OpenVINO ONNX Frontend"+graph {+ node {+ output: "roi"+ op_type: "Constant"+ attribute {+ name: "value"+ t {+ dims: 0+ data_type: 1+ name: "const_roi"+ }+ type: TENSOR+ }+ }+ node {+ output: "scales"+ op_type: "Constant"+ attribute {+ name: "value"+ t {+ dims: 2+ data_type: 1+ float_data: 2.0+ float_data: 2.0+ name: "const_scales"+ }+ type: TENSOR+ }+ }+ node {+ input: "X"+ input: "roi"+ input: "scales"+ output: "Y"+ op_type: "Resize"+ attribute {+ name: "axes"+ ints: -2+ ints: -1+ type: INTS+ }+ attribute {+ name: "mode"+ s: "nearest"+ type: STRING+ }+ attribute {+ name: "coordinate_transformation_mode"+ s: "asymmetric"+ type: STRING+ }+ }+ name: "test_resize11_negative_axes"+ input {+ name: "X"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_value: 1 }+ dim { dim_value: 1 }+ dim { dim_value: 2 }+ dim { dim_value: 2 }+ }+ }+ }+ }+ output {+ name: "Y"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim { dim_value: 1 }+ dim { dim_value: 1 }+ dim { dim_value: 4 }+ dim { dim_value: 4 }+ }+ }+ }+ }+}+opset_import {+ domain: ""+ version: 11+}diff --git a/src/frontends/onnx/tests/onnx_import.in.cpp b/src/frontends/onnx/tests/onnx_import.in.cppindex 0290481c7ce..c66171e7255 100644--- a/src/frontends/onnx/tests/onnx_import.in.cpp+++ b/src/frontends/onnx/tests/onnx_import.in.cpp@@ -2142,6 +2142,21 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_resize11_down_scales_linear_asymmetric) {test_case.run_with_tolerance_as_fp();}+// Test that Resize correctly handles negative axes (e.g. axes=[-2,-1] equivalent to axes=[2,3] for rank-4 input)+OPENVINO_TEST(${BACKEND_NAME}, onnx_resize11_negative_axes) {+ const auto model = convert_model("resize11_negative_axes.onnx");++ // Input shape [1,1,2,2], scales=[2.0,2.0] on axes [-2,-1] => output [1,1,4,4]+ const Shape expected_output_shape{1, 1, 4, 4};+ auto test_case = ov::test::TestCase(model, s_device);+ test_case.add_input<float>({1.0f, 2.0f, 3.0f, 4.0f});+ test_case.add_expected_output<float>(+ expected_output_shape,+ {1.0f, 1.0f, 2.0f, 2.0f, 1.0f, 1.0f, 2.0f, 2.0f, 3.0f, 3.0f, 4.0f, 4.0f, 3.0f, 3.0f, 4.0f, 4.0f});++ test_case.run();+}+OPENVINO_TEST(${BACKEND_NAME}, onnx_resize11_scales_nearest_asymmetric_floor_dynamic_sizes) {const auto model = convert_model("resize11_scales_nearest_asymmetric_floor_dynamic_scales.onnx");diff --git a/tests/layer_tests/onnx_tests/test_resize.py b/tests/layer_tests/onnx_tests/test_resize.pyindex e6bdd3d2cfd..9a9003d29af 100644--- a/tests/layer_tests/onnx_tests/test_resize.py+++ b/tests/layer_tests/onnx_tests/test_resize.py@@ -209,6 +209,56 @@ class TestResize(OnnxRuntimeLayerTest):self._test(*self.create_resize_net(**params, precision=precision, ir_version=ir_version),ie_device, precision, ir_version, custom_eps=2.0e-4, temp_dir=temp_dir)+ def create_resize_net_negative_axes(self, input_shape, output_shape, scales, axes,+ coordinate_transformation_mode, mode, precision,+ ir_version):+ import onnx+ from onnx import helper, TensorProto, numpy_helper++ roi = helper.make_node('Constant', inputs=[], outputs=['roi'],+ value=numpy_helper.from_array(+ np.array([], dtype=np.float32), name='roi'))+ scales_node = helper.make_node('Constant', inputs=[], outputs=['scales'],+ value=helper.make_tensor(+ 'scales', TensorProto.FLOAT,+ [len(scales)], scales))+ resize_node = helper.make_node(+ 'Resize',+ inputs=['input', 'roi', 'scales'],+ outputs=['output'],+ mode=mode,+ coordinate_transformation_mode=coordinate_transformation_mode,+ axes=axes,+ )+ x = helper.make_tensor_value_info('input', TensorProto.FLOAT, input_shape)+ y = helper.make_tensor_value_info('output', TensorProto.FLOAT, output_shape)+ graph_def = helper.make_graph([roi, scales_node, resize_node], 'test_resize_neg_axes',+ [x], [y])+ onnx_net = onnx_make_model(graph_def, producer_name='test_model',+ opset_imports=[helper.make_opsetid('', 18)])+ onnx.checker.check_model(onnx_net)+ ref_net = None+ return onnx_net, ref_net++ # Test Resize with negative axes (e.g. axes=[-2,-1] equivalent to [2,3] for rank-4 input)+ test_data_negative_axes = [+ dict(input_shape=[1, 1, 2, 2], output_shape=[1, 1, 4, 4],+ scales=[2.0, 2.0], axes=[-2, -1],+ coordinate_transformation_mode='asymmetric', mode='nearest'),+ dict(input_shape=[1, 3, 4, 4], output_shape=[1, 3, 2, 2],+ scales=[0.5, 0.5], axes=[-2, -1],+ coordinate_transformation_mode='asymmetric', mode='nearest'),+ dict(input_shape=[1, 1, 2, 2], output_shape=[1, 1, 4, 4],+ scales=[2.0, 2.0], axes=[-2, -1],+ coordinate_transformation_mode='align_corners', mode='linear'),+ ]++ @pytest.mark.parametrize("params", test_data_negative_axes)+ def test_resize_negative_axes(self, params, ie_device, precision, ir_version, temp_dir):+ self._test(*self.create_resize_net_negative_axes(**params, precision=precision,+ ir_version=ir_version),+ ie_device, precision, ir_version, custom_eps=2.0e-4, temp_dir=temp_dir)+test_data_cubic = [dict(input_shape=[1, 3, 100, 200], output_shape=[1, 3, 350, 150],scales=[1.0, 1.0, 3.5, 150 / 200], sizes=None),