importONNXFunction can't import 'Conv' Operator which is not supported with its current settings or in this context?

14 次查看(过去 30 天)
When I tried to use the built-in function importONNXFunction to import my ONNX model into the MATLAB environment, I encountered a 'Conv' operator not supported error. The warning was:
Warning: Unable to import some ONNX operators or attributes. They may have been replaced by 'PLACEHOLDER' functions in the imported model function.
60 operator(s) : Operator 'Conv' is not supported with its current settings or in this context.
This 'Conv' operator should be supported, as it has been specified in the documentation as a supported operator. my ONNX model link here. The code used in MATLAB is:
modelfile = "xfeat.onnx";
params = importONNXFunction(modelfile,"xfeatFcn")
img1 = imresize(imread("viprectification_deskLeft.png"),2);
img2 = imresize(imread("viprectification_deskRight.png"),2);
%% model inference
[mkpts1,mkpts2] = xfeatFcn(img1,img2,params);
Warning: Attribute(s) [training_mode] in layer 'x_fine_matcher_fine_matcher_1_BatchNorma' with operator type 'BatchNormalization' are unknown. It is possible that the imported model will not output the same
predictions as the source model.
In
In
In
In
In
In
In
In
Warning: Attribute(s) [training_mode] in layer 'x_fine_matcher_fine_matcher_4_BatchNorma' with operator type 'BatchNormalization' are unknown. It is possible that the imported model will not output the same
predictions as the source model.
In
In
In
In
In
In
In
In
Warning: Attribute(s) [training_mode] in layer 'x_fine_matcher_fine_matcher_7_BatchNorma' with operator type 'BatchNormalization' are unknown. It is possible that the imported model will not output the same
predictions as the source model.
In
In
In
In
In
In
In
In
Warning: Attribute(s) [training_mode] in layer 'x_fine_matcher_fine_matcher_10_BatchNorm' with operator type 'BatchNormalization' are unknown. It is possible that the imported model will not output the same
predictions as the source model.
In
In
In
In
In
In
In
In
Function containing the imported ONNX network architecture was saved to the file xfeatFcn.m.
To learn how to use this function, type: help xfeatFcn.
Warning: Unable to import some ONNX operators or attributes. They may have been replaced by 'PLACEHOLDER' functions in the imported model function.
60 operator(s) : Operator 'Conv' is not supported with its current settings or in this context.
Unrecognized function or variable 'PLACEHOLDER'.
[Vars.x_net_block1_block1_0_layer_layer_0_1__1, NumDims.x_net_block1_block1_0_layer_layer_0_1__1] = PLACEHOLDER(Vars.x_net_norm_1_InstanceNormalization_outpu, Vars.onnx__Conv_1586, Vars.onnx__Conv_1587, NumDims.x_net_norm_1_InstanceNormalization_outpu, NumDims.onnx__Conv_1586, NumDims.onnx__Conv_1587, Vars.Convdilations1085, Vars.Convgroup1086, Vars.Convkernel_shape1087, Vars.Convpads1088, Vars.Convstrides1089, NumDims.Convdilations1085, NumDims.Convgroup1086, NumDims.Convkernel_shape1087, NumDims.Convpads1088, NumDims.Convstrides1089);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in
[matched_points1, matched_points2, matched_points1NumDims, matched_points2NumDims, state] = main_graphGraph1000(input1, input2, NumDims.input1, NumDims.input2, Vars, NumDims, Training, params.State);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in
[mkpts1,mkpts2] = xfeatFcn(img1,img2,params);
------------------------------------------------------
my env:
  • '25.1.0.2897550 (R2025a) Prerelease Update 5'
  • "Deep Learning Toolbox Converter for ONNX Model Format" "25.1.0" true "ONNXCONVERTER"
Note: I can correctly perform inference with this ONNX model using ONNXRuntime in Python and C++.
  2 个评论
Megh Singh
Megh Singh 2025-4-14
@cui,xingxing I tried loading the ONNX model in Python ONNXRuntime and sent 2 dummy inputs of size:
1 x 3 x 400 x 300 (N x C x H x W) since the model does not specify the expected height and width of its inputs, and these are the sizes of "viprectification_deskLeft.png" and "viprectification_deskRight.png". Following is my python scirpt:
import onnxruntime as ort
import numpy as np
model_path = 'xfeat.onnx'
session = ort.InferenceSession(model_path)
input1_name = session.get_inputs()[0].name
input2_name = session.get_inputs()[1].name
# Create a dummy input tensor with the same shape as the model's input
# Replace this with your actual input data
dummy_input1 = np.random.rand(1,3,400,300).astype(np.float32)
dummy_input2 = np.random.rand(1,3,400,300).astype(np.float32)
# Run inference
outputs = session.run(None, {input1_name: dummy_input1, input2_name: dummy_input2})
Unfortunately, I run into the following error in ONNX Runtime, upon running the above script:
---------------------------------------------------------------------------Fail Traceback (most recent call last) Cell In[7], line 14 11 dummy_input2 = np.random.rand(1,3,400,300).astype(np.float32) 13 # Run inference---> 14 outputs = session.run(None, {input1_name: dummy_input1, input2_name: dummy_input2}) 16 # Print the output 17 print("Model output:", outputs[0]) File ~\AppData\Roaming\Python\Python311\site-packages\onnxruntime\capi\onnxruntime_inference_collection.py:220, in Session.run(self, output_names, input_feed, run_options) 218 output_names = [output.name for output in self._outputs_meta] 219 try: --> 220 return self._sess.run(output_names, input_feed, run_options) 221 except C.EPFail as err: 222 if self._enable_fallback: Fail: [ONNXRuntimeError] : 1 : FAIL : Non-zero status code returned while running TopK node. Name:'/TopK_3' Status Message: k argument [6400] should not be greater than specified axis dim value [3072]
Please let me know the correct input sizes that work for this model, in ONNX Runtime?
Since you already reached out to us through MathWorks Technical Support, please directly reply to them with the requested information.
cui,xingxing
cui,xingxing 2025-4-15
编辑:cui,xingxing 2025-4-15
Thank you for your reply. The ONNX model accepts dynamic-sized input [1, 3, height, width], where the height and width are generally dynamic. Currently, I am using images "viprectification_deskLeft.png" and "viprectification_deskRight.png" with a size of 400x300(width x height), which may be an exception. I apologize for this. Please try using the size [1, 3, 600, 800](N x C x H x W) ,which should work.
I will seek support from MathWorks Technical Support going forward and will try to avoid posting information here. Thank you again for pointing out the issue.

请先登录,再进行评论。

采纳的回答

cui,xingxing
cui,xingxing 2025-4-13
编辑:cui,xingxing 2025-4-13
Through my investigation of above ONNX model, I discovered the underlying issue.
Currently, the importONNXFunction is still not robust enough to handle these problems. The diagram below shows the support status for Conv layers; the red arrows indicate supported imports, while the green arrows indicate unsupported imports.
The solutions I am working hard to implement are:
  • 'PLACEHOLDER' functions implementation
I try rewritten the implementation of the 'PLACEHOLDER' functions in the imported model to support this non-standard Conv operator. Below is my replacement custom function.
function [output, numdims_output] = conv_costom(input, weights, bias, numdims_input, numdims_w, numdims_b,...
Convdilations, Convgroup, Convkernel_shape, Convpads, Convstrides, numdims_Convdilations, ...
numdims_Convgroup, numdims_Convkernel_shape, numdims_Convpads, numdims_Convstrides)
dataFormat = "SSCB";
if isdlarray(Convstrides)
stride = extractdata(Convstrides);
stride = stride(:)';
end
if isdlarray(Convdilations)
dilationFactor = extractdata(Convdilations);
dilationFactor = dilationFactor(:)';
end
if isdlarray(Convpads)
padding = extractdata(Convpads);
padding = padding(:)';
end
output = dlconv(input, weights, bias, 'Stride', stride, 'DilationFactor', dilationFactor, 'Padding', padding, 'DataFormat', dataFormat);
numdims_output = 4;
end
The above function can be executed, but then another issue arises: the array dimensions do not match!
Arrays have incompatible sizes for this operation.
Error in == (line 39)
zdata = matlab.lang.internal.move(xdata) == matlab.lang.internal.move(ydata);
^
Error in
Vars.x_Equal_12_output_0 = Vars.x_Gather_30_output_0 == Vars.x_Constant_187_output_0;
^
Error in
[matched_points1, matched_points2, matched_points1NumDims, matched_points2NumDims, state] = main_graphGraph1000(input1, input2, NumDims.input1, NumDims.input2, Vars, NumDims, Training, params.State);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in
[mkpts1,mkpts2] = xfeatFcn(img1,img2,params);
^^^^^^^^^^^^^^^^^^^^^^^^^^
This solution ended in failure.
  • Adjusting and adapting the ONNX structure
In addition,I also used the onnx-sim 0.4.36 tool to simplify and merge Identify into Conv, meaning the weights and biases were filled in.
!onnxsim xfeat.onnx xfeat_onnxsim.onnx
Now the diagram looks like this:
However, another problem has arisen: importONNXFunction does not effectively handle the issue of duplicate node names coming from ONNX.
modelfile = "xfeat_onnxsim.onnx";
params = importONNXFunction(modelfile,"xfeatFcn");
Parameter name 'onnx__Unsqueeze_154' already exists. Choose a different name.
Error in
checkAddArgs(params, paramName, paramValue, paramKind, numDimensions, nargout);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in
params = params.addParameter(names{i}, val.Array, 'learnable', val.Rank);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in
this.Params = fillONNXParameters(this);
^^^^^^^^^^^^^^^^^^^^^^^^
Error in
modelTranslation = nnet.internal.cnn.onnx.fcn.ModelTranslation(modelProto, outputFcnPath, outputFcnName, maxNameLength, '', false);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in
params = nnet.internal.cnn.onnx.importONNXFunction(modelfile, outputFunctionName);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in
params = importONNXFunction(modelfile,"xfeatFcn");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This solution ended in failure too.
I hope this issue receives timely attention and fixing from MathWorks!
my "xfeat_onnxsim.onnx" is here:
baidu cloud: https://pan.baidu.com/s/1qlLLUA3C9W0zcxkh-Bb8NA
code: b3h5

更多回答(1 个)

Matt J
Matt J 2025-4-12
编辑:Matt J 2025-4-12
This 'Conv' operator should be supported, as it has been specified in the documentation as a supported operator.
I haven't looked at your ONNX model, but the appearance of Conv in the table is not reason enough to think it will be supported in all possible configurations. As the documentation you linked to says,
"importONNXFunction supports the following ONNX operators for conversion into built-in MATLAB layers or custom layers, with some limitations."
As for what specific limitations you might have encountered, CHAT-GPT suggests the following possibilities:

产品


版本

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by