Issue 1:
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:

One is [1, 197, 768], the original shape from a vision transformer; Click to download multi_head_attention.onnx.zip.

The other one is [1, 5, 12], a shrinked shape for debugging and stuff. Click to download multi_head_attention.onnx.zip.

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)

Issue 2:
Currently OpenCV Python bindings "automatically" converts 3D NumPy arrays into 2D cv::Mat with wrapping last dimension onto number of channels (to simulate Image with colors):

a = np.zeros((2,3,4), dtype='f')
print(a.shape)
info = cv.utils.dumpInputArray(a)
print(info)

output:

(2, 3, 4)
InputArray: empty()=false kind=0x00010000 flags=0x01010000 total(-1)=6 dims(-1)=2 size(-1)=3x2 type(-1)=CV_32FC4

Related code is here (previous variant of similar code is here from 2010). By initial design there is support for 2D matrices and 1D arrays only.

So currently there is no way to pass 3D cv::Mat (1-channel) from Python to C++ code.

Just changing of mentioned code is not an option due to breaking of many user applications.

Compatibility should be preserved.

Note: There is such issue in Java because Java's Mat type is used.

Python's bindings relies on np.array type which leads to mentioned reinterpretation issues.

relates #17272

relates #19001

probably relates #18413 #17456