Skip to content

ONNX Identity produces incorrect output shape and values for 1D tensor #92

Description

@lstahlman

Mirrored from upstream: opencv#28339
Original author: @shahkarnav115-beep · created 2025-12-30T20:58:39Z
Upstream labels: bug
Selection: mirrored by mirror-issues.py


System Information

  • OpenCV version: latest 4.x (tested via Python cv2.dnn)
  • OS: Windows
  • Python version: 3.12
  • Backend: default (CPU)
  • ONNX opset: 13
  • ONNX IR version: 7

Detailed description

A minimal ONNX model containing a single Identity operator with a 1D input
tensor produces incorrect results when run with OpenCV DNN.

ONNX Runtime (reference implementation) correctly preserves the 1D tensor
shape and values. However, OpenCV outputs a 2D tensor and incorrect values.

This indicates that OpenCV DNN does not correctly handle 1D tensors even
for the Identity operator, leading to incorrect shape inference and data
corruption.

Steps to reproduce

import numpy as np
import onnx
import onnx.helper as oh
import onnxruntime as ort
import cv2 as cv

# 1. Create input data (fixed shape)
data = np.random.randn(5).astype(np.float32)

# 2. Define ONNX input/output
inp = oh.make_tensor_value_info("inp", onnx.TensorProto.FLOAT, [5])
out = oh.make_tensor_value_info("out", onnx.TensorProto.FLOAT, [5])

# 3. Identity node
node = oh.make_node(
    "Identity",
    inputs=["inp"],
    outputs=["out"]
)

# 4. Build graph
graph = oh.make_graph(
    [node],
    "identity_test",
    [inp],
    [out]
)

# 5. Build model (opset 13)
model = oh.make_model(
    graph,
    opset_imports=[oh.make_opsetid("", 13)],
    ir_version=7
)

# 6. Save model
onnx.save(model, "identity.onnx")

# 7. Run with ONNX Runtime
sess = ort.InferenceSession("identity.onnx", providers=["CPUExecutionProvider"])
ort_out = sess.run(None, {"inp": data})

# 8. Run with OpenCV
net = cv.dnn.readNetFromONNX("identity.onnx")
net.setInput(data, "inp")
cv_out = net.forward()

# 9. Compare
print("ONNX shape:", ort_out[0].shape)
print("OpenCV shape:", cv_out.shape)
print("Max diff:", np.max(np.abs(ort_out[0] - cv_out)))

Observed output

ONNX shape: (5,)
OpenCV shape: (5, 1)
Max diff: 2.1548517

Expected output

  • Output shape: (5,)
  • Output values identical to input (Identity behavior)

Actual Behaviour

  • OpenCV output shape: (5, 1)
  • Output values differ from reference (non-zero max difference)

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions