Skip to content

dnn: getting wrong input shape if using python interfaces #23242

@fengyuentau

Description

@fengyuentau

System Information

OpenCV version: 4.7 release
OpenCV Python version: 4.7.0.68
OS: macOS 13.0.1
Python version: 3.9.6
Compiler: Apple clang version 14.0.0 (clang-1400.0.29.202)

Detailed description

I made two attention models of ONNX format. They have the same structure and the only difference is input shape:

I found if using Python interfaces with the correct shape set, the one of input shape [1, 197, 768] can run successfully, but the other one of input shape [1, 5, 12] fails:

[ERROR:0@110.035] global net_impl.cpp:1164 getLayerShapesRecursively OPENCV/DNN: [Permute]:(onnx_node_output_0!Transpose.0.out): getMemoryShapes() throws exception. inputs=1 outputs=0/1 blobs=0
[ERROR:0@110.035] global net_impl.cpp:1167 getLayerShapesRecursively     input[0] = [ 1 5 ]
[ERROR:0@110.035] global net_impl.cpp:1177 getLayerShapesRecursively Exception message: OpenCV(4.7.0) /Users/opencv-cn/GHA-OCV-1/_work/opencv-python/opencv-python/opencv/modules/dnn/src/layers/permute_layer.cpp:163: error: (-215:Assertion failed) (int)_numAxes == inputs[0].size() in function 'getMemoryShapes'

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.7.0) /Users/opencv-cn/GHA-OCV-1/_work/opencv-python/opencv-python/opencv/modules/dnn/src/layers/permute_layer.cpp:163: error: (-215:Assertion failed) (int)_numAxes == inputs[0].size() in function 'getMemoryShapes'

But if I try to debug in C++, for example adding test cases in test_onnx_importer.cpp, they all pass. So I guess there is something wrong with the python interface. Reproducers are attached below.

Steps to reproduce

Python:

import numpy as np
import cv2 as cv

# This passes!
net = cv.dnn.readNet("original_scale/models/multi_head_attention.onnx")
x = np.random.rand(1, 197, 768).astype(np.float32)
net.setInput(x)
net.forward()

# This fails!
net = cv.dnn.readNet("scale_1_5_12/models/multi_head_attention.onnx")
x = np.random.rand(1, 5, 12).astype(np.float32)
net.setInput(x)
net.forward()

C++: Add the following test cases in test_onnx_importer.cpp

TEST_P(Test_ONNX_layers, MHA_ORIG)
{
    Net net = readNet(findDataFile("original_scale/models/multi_head_attention.onnx", true));

    std::vector<int> blob_shape{1, 197, 768};
    Mat blob(blob_shape, CV_32FC1);
    randn(blob, 1.f, 0.f);

    net.setInput(blob);
    net.forward();
}

TEST_P(Test_ONNX_layers, MHA_SMALL_SCALE)
{
    Net net = readNet(findDataFile("scale_1_5_12/models/multi_head_attention.onnx", true));

    std::vector<int> blob_shape{1, 5, 12};
    Mat blob(blob_shape, CV_32FC1);
    randn(blob, 1.f, 0.f);

    net.setInput(blob);
    net.forward();
}

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions