opencv
opencv copied to clipboard
ONNX broken for identity function
System Information
OpenCV python version: 4.10.0.82 PyTorch version: 2.0.0+cu117 Operating System / Platform: Ubuntu 22.04 Python version: 3.10.6
Detailed description
I have been having some issues with ONNX files lately. Decided to check that the simplest function of them all works: the identity ...
Here is a script that shows for what input shapes ONNX is broken (0, 1 and 3 dimensional inputs). I redirect stdout when exporting the ONNX file to make the output more readable.
I suspect that this is the underlying error to https://github.com/opencv/opencv/issues/25762
Steps to reproduce
import torch
import cv2 as cv
from contextlib import redirect_stdout
class Identity(torch.nn.Module):
def __init__(self):
super().__init__()
def forward(self, X):
return X
model = Identity()
shape = (2,3,5,7,11,13,17,19)
for i in range(1 + len(shape)):
X = torch.zeros(shape[:i])
with open('/dev/null', 'w') as f:
with redirect_stdout(f):
torch.onnx.export(model, X, '/tmp/model.onnx', output_names=['Y'])
Y = model(X)
ok = X.shape == Y.shape
print('OK ' if ok else 'ERROR', 'PyTorch', tuple(X.shape), '->', tuple(Y.shape))
net = cv.dnn.readNetFromONNX('/tmp/model.onnx')
X = X.numpy()
net.setInput(X)
Y = net.forward(['Y'])[0]
ok = X.shape == Y.shape
print('OK ' if ok else 'ERROR', 'OpenCV ', X.shape, '->', Y.shape)
which gives me the following output (format: Dimension, OK/ERROR, PyTorch/OpenCV, SourceShape, TargetShape)
0 OK PyTorch () -> ()
0 ERROR OpenCV () -> (1, 1)
1 OK PyTorch (2,) -> (2,)
1 ERROR OpenCV (2,) -> (2, 1)
2 OK PyTorch (2, 3) -> (2, 3)
2 OK OpenCV (2, 3) -> (2, 3)
3 OK PyTorch (2, 3, 5) -> (2, 3, 5)
3 ERROR OpenCV (2, 3, 5) -> (2, 3)
4 OK PyTorch (2, 3, 5, 7) -> (2, 3, 5, 7)
4 OK OpenCV (2, 3, 5, 7) -> (2, 3, 5, 7)
5 OK PyTorch (2, 3, 5, 7, 11) -> (2, 3, 5, 7, 11)
5 OK OpenCV (2, 3, 5, 7, 11) -> (2, 3, 5, 7, 11)
6 OK PyTorch (2, 3, 5, 7, 11, 13) -> (2, 3, 5, 7, 11, 13)
6 OK OpenCV (2, 3, 5, 7, 11, 13) -> (2, 3, 5, 7, 11, 13)
7 OK PyTorch (2, 3, 5, 7, 11, 13, 17) -> (2, 3, 5, 7, 11, 13, 17)
7 OK OpenCV (2, 3, 5, 7, 11, 13, 17) -> (2, 3, 5, 7, 11, 13, 17)
8 OK PyTorch (2, 3, 5, 7, 11, 13, 17, 19) -> (2, 3, 5, 7, 11, 13, 17, 19)
8 OK OpenCV (2, 3, 5, 7, 11, 13, 17, 19) -> (2, 3, 5, 7, 11, 13, 17, 19)
As you can see, it's broken for 0, 1 and 3 dimensional inputs.
Issue submission checklist
- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [X] I updated to the latest OpenCV version and the issue is still there
- [X] There is reproducer code and related data files (videos, images, onnx, etc)