opencv / opencv

Open Source Computer Vision Library
https://opencv.org
Apache License 2.0
75.69k stars 55.61k forks source link

opencv dnn failed to call `onnx::ReduceSum` on five-dims. #25456

Closed ausk closed 1 week ago

ausk commented 1 week ago

System Information

// example for python user OpenCV python version: 4.9.0 Operating System / Platform: Ubuntu 20.04; win 10 Python version: 3.8; 3.10

Detailed description

When I try to try to run the onnx file create by converting yolov9-e-converted.pt , it fail to predict on any image. After debug, I find the CBFuse layer output the wrong tensor, then every layer related to those layers will output error tensor.

Then I create a simplest onnx to reproduce it.

y = torch.sum(torch.stack([x, x]), axis=0) so y = 2*x

Steps to reproduce

the onnx file: tmp0.zip

torch

import torch.nn as nn
import torch
import numpy as np
class CBFuse_lite(nn.Module):
    def __init__(self) -> None:
        super().__init__()
    def forward(self, x):
        return torch.sum(torch.stack([x, x]), axis=0)

# create a random matrix/tensor
x = np.random.rand(1,3,1,1)
xx = torch.from_numpy(x)

# export onnx
model = CBFuse_lite()
torch.onnx.export(model,
                    args = xx,
                    f="tmp0.onnx",
                    export_params=True,
                    opset_version=9,
                    input_names=["in"], output_names=["out"],
                    do_constant_folding=True,
                )

print("=== torch ====")
yy = model(xx).detach().cpu().numpy()
res = (x*2 - yy)
print(res.min(), res.max())  # 0.0 0.0
print(res)

opencv

print("=== opencv ====")
import cv2
import numpy as np
net = cv2.dnn.readNet("tmp0.onnx")
x = np.random.rand(1,3,1,1)
net.setInput(x)
y = net.forward()

res = (x*2 - y)
print(res.min(), res.max()) 
print(res)

output

=== torch ====
0.0 0.0
[[[[0.]]

  [[0.]]

  [[0.]]]]
=== opencv ====
-1.0331779876082692e-10 0.10747493578474754
[[[[-1.03317799e-10]]

  [[ 2.33730792e-08]]

  [[ 1.07474936e-01]]]]

Issue submission checklist

ausk commented 1 week ago

x‘s shape: (n,c,h,w);

if c >=3; it failed。

ausk commented 1 week ago

The latest code is ok. The bug has been solve by this commit last month.