Error importing onnx model using opencv c++

Hi, did you find any solution for this, I am also facing the same error.

without model opencv, version, langauge nobody can help you

I have exported a Swin tranformer model in onnx format:

Code to convert model to ONNX

input_size = (1, 3, 224, 224)  # Example input size
dummy_input = torch.randn(*input_size)
# Let's create a dummy input tensor  
#dummy_input = torch.randn(1, dummy_input, requires_grad=True)  

# Export the model   
torch.onnx.export(model,         # model being run 
     dummy_input,       # model input (or a tuple for multiple inputs) 
     "ImageClassifier2.onnx",       # where to save the model  
     export_params=True,  # store the trained parameter weights inside the model file 
     opset_version=12,    # the ONNX version to export the model to 
     do_constant_folding=True,  # whether to execute constant folding for optimization 
     input_names = ['modelInput'],   # the model's input names 
     output_names = ['modelOutput'], # the model's output names 
     dynamic_axes={'modelInput' : {0 : 'batch_size'},    # variable length axes 
                            'modelOutput' : {0 : 'batch_size'}}) 
print(" ") 
print('Model has been converted to ONNX')

When i use cv::dnn::readNetFromONNX(), I get the error:
***global onnx_importer.cpp (1060) handleNode DNN/ONNX: ERROR during processing node with 3 inputs and 1 outputs: [Split]:(onnx_node!/Where) from domain='ai.onnx'***

pytorch version 2.0.1 opencv version 4.7.0

I have created new post:

Update your code to opencv 4.8.0
If error is still here make an issue

please give full output in issue and a link to onnx model:

[ INFO:0@8.918] global onnx_importer.cpp:835 cv::dnn::dnn4_v20230620::ONNXImporter::populateNet DNN/ONNX: loading ONNX v7 model produced by 'pytorch':2.1.0. Number of nodes = 1147, initializers = 152, inputs = 1, outputs = 1
[ INFO:0@8.918] global onnx_importer.cpp:728 cv::dnn::dnn4_v20230620::ONNXImporter::parseOperatorSet DNN/ONNX: ONNX opset version = 14
[ INFO:0@9.271] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 3 inputs and 1 outputs: [Conv]:(onnx_node!/patch_embed/proj/Conv) from domain='ai.onnx'
[ INFO:0@9.272] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 1 inputs and 1 outputs: [Shape]:(onnx_node!/patch_embed/Shape) from domain='ai.onnx'
[ INFO:0@9.273] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 0 inputs and 1 outputs: [Constant]:(onnx_node!/patch_embed/Constant) from domain='ai.onnx'
[ INFO:0@9.274] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 0 inputs and 1 outputs: [Constant]:(onnx_node!/patch_embed/Constant_1) from domain='ai.onnx'
[ INFO:0@9.274] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 0 inputs and 1 outputs: [Constant]:(onnx_node!/patch_embed/Constant_2) from domain='ai.onnx'
[ INFO:0@9.275] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 4 inputs and 1 outputs: [Slice]:(onnx_node!/patch_embed/Slice) from domain='ai.onnx'
[ INFO:0@9.279] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 0 inputs and 1 outputs: [Constant]:(onnx_node!/patch_embed/Constant_3) from domain='ai.onnx'
[ INFO:0@9.280] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 2 inputs and 1 outputs: [Concat]:(onnx_node!/patch_embed/Concat) from domain='ai.onnx'
[ INFO:0@9.281] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 2 inputs and 1 outputs: [Reshape]:(onnx_node!/patch_embed/Reshape) from domain='ai.onnx'
[ INFO:0@9.284] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 1 inputs and 1 outputs: [Transpose]:(onnx_node!/patch_embed/Transpose) from domain='ai.onnx'
[ INFO:0@9.285] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 0 inputs and 1 outputs: [Constant]:(onnx_node!/Constant) from domain='ai.onnx'
[ INFO:0@9.285] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 1 inputs and 1 outputs: [Shape]:(onnx_node!/Shape) from domain='ai.onnx'
[ INFO:0@9.286] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 0 inputs and 1 outputs: [Constant]:(onnx_node!/Constant_1) from domain='ai.onnx'
[ INFO:0@9.287] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 2 inputs and 1 outputs: [Gather]:(onnx_node!/Gather) from domain='ai.onnx'
[ INFO:0@9.288] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 0 inputs and 1 outputs: [Constant]:(onnx_node!/Constant_2) from domain='ai.onnx'
[ INFO:0@9.288] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 2 inputs and 1 outputs: [Unsqueeze]:(onnx_node!/Unsqueeze) from domain='ai.onnx'
[ INFO:0@9.290] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 0 inputs and 1 outputs: [Constant]:(onnx_node!/Constant_3) from domain='ai.onnx'
[ INFO:0@9.290] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 0 inputs and 1 outputs: [Constant]:(onnx_node!/Constant_4) from domain='ai.onnx'
[ INFO:0@9.291] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 3 inputs and 1 outputs: [Concat]:(onnx_node!/Concat) from domain='ai.onnx'
[ INFO:0@9.291] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 0 inputs and 1 outputs: [Constant]:(onnx_node!/Constant_5) from domain='ai.onnx'
[ INFO:0@9.292] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 2 inputs and 1 outputs: [Reshape]:(onnx_node!/Reshape) from domain='ai.onnx'
[ INFO:0@9.293] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 1 inputs and 1 outputs: [Shape]:(onnx_node!/Shape_1) from domain='ai.onnx'
[ INFO:0@9.293] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 1 inputs and 1 outputs: [ConstantOfShape]:(onnx_node!/ConstantOfShape) from domain='ai.onnx'
[ INFO:0@9.295] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 0 inputs and 1 outputs: [Constant]:(onnx_node!/Constant_6) from domain='ai.onnx'
[ INFO:0@9.295] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 2 inputs and 1 outputs: [Mul]:(onnx_node!/Mul) from domain='ai.onnx'
[ INFO:0@9.304] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 2 inputs and 1 outputs: [Equal]:(onnx_node!/Equal) from domain='ai.onnx'
[ INFO:0@9.307] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 3 inputs and 1 outputs: [Where]:(onnx_node!/Where) from domain='ai.onnx'
[ INFO:0@9.308] global onnx_importer.cpp:1006 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: processing node with 2 inputs and 1 outputs: [Expand]:(onnx_node!/Expand) from domain='ai.onnx'
OpenCV(4.8.0-dev) Error: Sizes of input arguments do not match (Requested and source matrices have different count of elements) in cv::Mat::reshape, file C:\lib\opencv\modules\core\src\matrix.cpp, line 1240
[ERROR:0@9.361] global onnx_importer.cpp:1064 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode DNN/ONNX: ERROR during processing node with 2 inputs and 1 outputs: [Expand]:(onnx_node!/Expand) from domain='ai.onnx'
[ INFO:0@9.361] global onnx_importer.cpp:1068 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode     Input[0] = 'cls_token'
[ INFO:0@9.362] global onnx_importer.cpp:1068 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode     Input[1] = '/Where_output_0'
[ INFO:0@9.362] global onnx_importer.cpp:1072 cv::dnn::dnn4_v20230620::ONNXImporter::handleNode     Output[0] = '/Expand_output_0'
OpenCV(4.8.0-dev) Error: Unspecified error (> Node [Expand@ai.onnx]:(onnx_node!/Expand) parse error: OpenCV(4.8.0-dev) C:\lib\opencv\modules\core\src\matrix.cpp:1240: error: (-209:Sizes of input arguments do not match) Requested and source matrices have different count of elements in function 'cv::Mat::reshape'
> ) in cv::dnn::dnn4_v20230620::ONNXImporter::handleNode, file C:\lib\opencv\modules\dnn\src\onnx\onnx_importer.cpp, line 1083
1 Like

Did you solve your problem?

I think you have to fixed input shape size too

No, I did not.
Here is the link to the pth and onnx model.

https://drive.google.com/drive/folders/1MePM7WZEzNqXFxMPPQ2Rm2f1DJ2en8ws?usp=sharing

1 Like

Can you make an issue?

if you want to fixe input size you have to enter this command

python -m onnxruntime.tools.make_dynamic_shape_fixed --dim_param batch_size --dim_value 1 "ViT.onnx" model.fixed.onnx

1 Like