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 ConvTranspose with output_shape containing batch and channel dimensions (#32854)
Evgeny Kotov2e27a0d05ce7
diff --git a/src/frontends/onnx/frontend/src/op/conv_transpose.cpp b/src/frontends/onnx/frontend/src/op/conv_transpose.cppindex c3f2c56d255..550021a7659 100644--- a/src/frontends/onnx/frontend/src/op/conv_transpose.cpp+++ b/src/frontends/onnx/frontend/src/op/conv_transpose.cpp@@ -170,14 +170,67 @@ ov::OutputVector conv_transpose(const ov::frontend::onnx::Node& node) {std::vector<std::int64_t> output_shape{node.get_attribute_value<std::vector<std::int64_t>>("output_shape", {})};+ // ONNX spec states that output_shape should contain only spatial dimensions.+ // However, some models incorrectly include batch and channel dimensions.+ // To maintain compatibility with other runtimes (e.g., ONNXRuntime),+ // extract only the spatial dimensions if the shape includes batch and channels.+ if (!output_shape.empty() && output_shape.size() > num_spatial_dims) {+ CHECK_VALID_NODE(node,+ output_shape.size() == num_spatial_dims + 2,+ "ConvTranspose output_shape attribute has unexpected number of dimensions. "+ "Expected ",+ num_spatial_dims,+ " spatial dimensions or ",+ num_spatial_dims + 2,+ " dimensions (with batch and channels), but got ",+ output_shape.size());++ // Extract last num_spatial_dims elements (the spatial dimensions)+ // For example: [1, 16, 32, 32] -> [32, 32] for 2D convolution+ output_shape = std::vector<std::int64_t>(output_shape.end() - num_spatial_dims, output_shape.end());+ }+std::vector<std::int64_t> output_padding{node.get_attribute_value<std::vector<std::int64_t>>("output_padding",std::vector<std::int64_t>(num_spatial_dims, 0))};+ // ONNX Runtime behavior: When output_shape is explicitly provided, recalculate padding+ // to achieve the desired output shape. This is necessary because ONNX models may specify+ // output_shape without corresponding padding values.+ // Note: When both output_shape and auto_pad are specified, output_shape takes precedence+ // and auto_pad is effectively ignored (padding is computed to match output_shape).+ // This matches ONNX Runtime behavior.+ if (!output_shape.empty() && data_pshape.is_static() && filters_pshape.is_static()) {+ const ov::Shape data_static_shape = data_pshape.to_shape();+ const ov::Shape filters_static_shape = filters_pshape.to_shape();++ // Recalculate padding for each spatial dimension to achieve output_shape+ for (size_t i = 0; i < num_spatial_dims; ++i) {+ const int64_t in_size = data_static_shape[2 + i];+ const int64_t kernel_size = filters_static_shape[2 + i];+ const int64_t stride = strides[i];+ const int64_t dilation = dilations[i];+ const int64_t adj = output_padding[i];+ const int64_t out_size = output_shape[i];++ // Formula from ONNX Runtime: ComputeTotalPad+ // total_pad = max(0, (in_size - 1) * stride + adj + (kernel - 1) * dilation + 1 - out_size)+ const int64_t total_pad =+ std::max<int64_t>(0, (in_size - 1) * stride + adj + (kernel_size - 1) * dilation + 1 - out_size);++ // Distribute padding: for NOTSET/EXPLICIT, put more on head when odd+ // (ONNX Runtime default for non-SAME modes)+ pads_begin[i] = total_pad - total_pad / 2;+ pads_end[i] = total_pad / 2;+ }++ // Override auto_pad_type to EXPLICIT since we've calculated explicit pads+ auto_pad_type = ov::op::PadType::EXPLICIT;+ }// Calculate padding for auto_pad modes when output_shape is not provided// Per ONNX spec, for ConvTranspose with SAME_UPPER/SAME_LOWER: output = input * stride- if (output_shape.empty() &&- (auto_pad_type == ov::op::PadType::SAME_UPPER || auto_pad_type == ov::op::PadType::SAME_LOWER)) {+ else if (output_shape.empty() &&+ (auto_pad_type == ov::op::PadType::SAME_UPPER || auto_pad_type == ov::op::PadType::SAME_LOWER)) {// Only calculate if shapes are static (required for padding calculation)if (data_pshape.is_static() && filters_pshape.is_static()) {const ov::Shape data_static_shape = data_pshape.to_shape();diff --git a/src/frontends/onnx/tests/models/convtranspose_output_shape_with_batch_channels.prototxt b/src/frontends/onnx/tests/models/convtranspose_output_shape_with_batch_channels.prototxtnew file mode 100644index 00000000000..641605ed5ca--- /dev/null+++ b/src/frontends/onnx/tests/models/convtranspose_output_shape_with_batch_channels.prototxt@@ -0,0 +1,101 @@+ir_version: 7+producer_name: "OpenVINO ONNX Frontend Test"+producer_version: "1.0"+graph {+ node {+ input: "data"+ input: "filters"+ output: "output"+ op_type: "ConvTranspose"+ attribute {+ name: "kernel_shape"+ ints: 3+ ints: 3+ type: INTS+ }+ attribute {+ name: "output_shape"+ ints: 1+ ints: 2+ ints: 10+ ints: 8+ type: INTS+ }+ attribute {+ name: "strides"+ ints: 3+ ints: 2+ type: INTS+ }+ }+ name: "test_convtranspose_output_shape_with_batch_channels"+ input {+ name: "data"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 3+ }+ dim {+ dim_value: 3+ }+ }+ }+ }+ }+ input {+ name: "filters"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 3+ }+ dim {+ dim_value: 3+ }+ }+ }+ }+ }+ output {+ name: "output"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 10+ }+ dim {+ dim_value: 8+ }+ }+ }+ }+ }+}+opset_import {+ version: 11+}diff --git a/src/frontends/onnx/tests/models/convtranspose_output_shape_with_batch_channels_stride2.prototxt b/src/frontends/onnx/tests/models/convtranspose_output_shape_with_batch_channels_stride2.prototxtnew file mode 100644index 00000000000..b569e20dbb7--- /dev/null+++ b/src/frontends/onnx/tests/models/convtranspose_output_shape_with_batch_channels_stride2.prototxt@@ -0,0 +1,115 @@+ir_version: 7+producer_name: "OpenVINO ONNX Frontend Test"+producer_version: "1.0"+graph {+ node {+ input: "data"+ input: "filters"+ input: "bias"+ output: "output"+ op_type: "ConvTranspose"+ attribute {+ name: "kernel_shape"+ ints: 3+ ints: 3+ type: INTS+ }+ attribute {+ name: "output_shape"+ ints: 1+ ints: 2+ ints: 6+ ints: 6+ type: INTS+ }+ attribute {+ name: "strides"+ ints: 2+ ints: 2+ type: INTS+ }+ }+ name: "test_convtranspose_output_shape_with_batch_channels_stride2"+ input {+ name: "data"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 4+ }+ dim {+ dim_value: 4+ }+ }+ }+ }+ }+ input {+ name: "filters"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 3+ }+ dim {+ dim_value: 3+ }+ }+ }+ }+ }+ input {+ name: "bias"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 2+ }+ }+ }+ }+ }+ output {+ name: "output"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ dim {+ dim_value: 2+ }+ dim {+ dim_value: 6+ }+ dim {+ dim_value: 6+ }+ }+ }+ }+ }+}+opset_import {+ version: 11+}diff --git a/src/frontends/onnx/tests/onnx_import_convpool.in.cpp b/src/frontends/onnx/tests/onnx_import_convpool.in.cppindex a6c61ea625b..604e9aef69c 100644--- a/src/frontends/onnx/tests/onnx_import_convpool.in.cpp+++ b/src/frontends/onnx/tests/onnx_import_convpool.in.cpp@@ -1518,3 +1518,68 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_conv_transpose_autopad) {test_case.run_with_tolerance_as_fp(0.00001f);}++OPENVINO_TEST(${BACKEND_NAME}, onnx_model_convtranspose_output_shape_with_batch_channels) {+ // Test ConvTranspose with output_shape containing batch and channel dimensions+ // Per ONNX spec, output_shape should only contain spatial dimensions [H, W]+ // However, some models incorrectly include all dimensions [N, C, H, W]+ // This test verifies that OpenVINO gracefully handles this by extracting spatial dims+ auto model = convert_model("convtranspose_output_shape_with_batch_channels.onnx");++ // Verify model loaded correctly and output shape is correct+ // Expected output: [1, 2, 10, 8] from input [1, 1, 3, 3]+ ASSERT_EQ(model->get_output_shape(0), (ov::Shape{1, 2, 10, 8}));++ auto test_case = ov::test::TestCase(model, s_device);++ // Input data: [1,1,3,3]+ test_case.add_input<float>({0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f});++ // Filters: [1,2,3,3]+ test_case.add_input<float>(+ {0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 1.0f, 1.1f, 1.2f, 1.3f, 1.4f, 1.5f, 1.6f, 1.7f, 1.8f});++ // Expected output shape: [1, 2, 10, 8]+ // This test verifies that the conversion succeeded despite invalid output_shape format+ test_case.run();+}++OPENVINO_TEST(${BACKEND_NAME}, onnx_model_convtranspose_output_shape_with_batch_channels_stride2) {+ // Test ConvTranspose with output_shape=[1,2,6,6] (includes batch/channels)+ // Input: [1,1,4,4], output_shape attribute: [1,2,6,6]+ // Should extract spatial dims: [6,6] and produce output [1,2,6,6]+ auto model = convert_model("convtranspose_output_shape_with_batch_channels_stride2.onnx");++ // Verify output shape matches expected: [1, 2, 6, 6]+ ASSERT_EQ(model->get_output_shape(0), (ov::Shape{1, 2, 6, 6}));++ auto test_case = ov::test::TestCase(model, s_device);++ // Input data: [1,1,4,4] - values 0 to 15+ std::vector<float> input_data(16);+ std::iota(input_data.begin(), input_data.end(), 0.0f);+ test_case.add_input<float>(input_data);++ // Filters: [1,2,3,3] - all 0.1f+ std::vector<float> filter_data(18, 0.1f);+ test_case.add_input<float>(filter_data);++ // Bias: [2] - all 0.0f+ std::vector<float> bias_data(2, 0.0f);+ test_case.add_input<float>(bias_data);++ // Expected output from ONNX Runtime: [1, 2, 6, 6]+ test_case.add_expected_output<float>(+ ov::Shape{1, 2, 6, 6},+ {1.00000000f, 0.60000002f, 1.40000010f, 0.80000001f, 1.79999995f, 1.00000000f, 0.89999998f, 0.50000000f,+ 1.10000002f, 0.60000002f, 1.29999995f, 0.69999999f, 2.60000014f, 1.40000010f, 3.00000000f, 1.60000002f,+ 3.40000010f, 1.79999995f, 1.70000005f, 0.90000004f, 1.90000010f, 1.00000000f, 2.09999990f, 1.10000002f,+ 4.20000029f, 2.20000005f, 4.59999990f, 2.40000010f, 5.00000000f, 2.59999990f, 2.50000000f, 1.30000007f,+ 2.70000005f, 1.39999998f, 2.90000010f, 1.50000000f, 1.00000000f, 0.60000002f, 1.40000010f, 0.80000001f,+ 1.79999995f, 1.00000000f, 0.89999998f, 0.50000000f, 1.10000002f, 0.60000002f, 1.29999995f, 0.69999999f,+ 2.60000014f, 1.40000010f, 3.00000000f, 1.60000002f, 3.40000010f, 1.79999995f, 1.70000005f, 0.90000004f,+ 1.90000010f, 1.00000000f, 2.09999990f, 1.10000002f, 4.20000029f, 2.20000005f, 4.59999990f, 2.40000010f,+ 5.00000000f, 2.59999990f, 2.50000000f, 1.30000007f, 2.70000005f, 1.39999998f, 2.90000010f, 1.50000000f});++ test_case.run();+}