Skip to content

dnn onnx importer parseGemm issue #22096

@TE-ShiyuYang

Description

@TE-ShiyuYang

I tried to import a small onnx model, for logistic_regression, with Gemm operator. And I encountered assertion error as following:

>>> cv2.dnn.readNetFromONNX('logistic_regression.onnx')
[ERROR:0@152.260] global D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\onnx\onnx_importer.cpp (1021) cv::dnn::dnn4_v20220524::ONNXImporter::handleNode DNN/ONNX: ERROR during processing node with 3 inputs and 1 outputs: [Gemm]:(onnx_node!Gemm_0003) from domain='ai.onnx'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\onnx\onnx_importer.cpp:1040: error: (-2:Unspecified error) in function 'cv::dnn::dnn4_v20220524::ONNXImporter::handleNode'
> Node [Gemm@ai.onnx]:(onnx_node!Gemm_0003) parse error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\layers\fully_connected_layer.cpp:92: error: (-215:Assertion failed) !bias || (blobs.size() == 2 && (size_t)numOutput == blobs[1].total()) in function 'cv::dnn::FullyConnectedLayerImpl::FullyConnectedLayerImpl'

After I checked the source code:

if (layerParams.has("transB") && !layerParams.get<int>("transB")) {

I found when Gemm operator has attr transB=0,
if origin Gemm performs A(M,K) * B(K,N), after
transpose(weights, weights);

it becomes A(M,K) * B'(N,K), but shouldn't ind_num_out still be 0,
layerParams.set("num_output", layerParams.blobs[0].size[ind_num_out]);

so num_output would still be N?

Also, I found if attr transB is not set(I mean missing, which would be treated using default mode), the procedure would not handle such scenario.

Here is my onnx model:
logistic_regression.zip

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions