Generation of C++ Code from DAGNetwork fails

8 views (last 30 days)
Dear cummunity,
I try to generate C/C++ code from an DAGNetwork, which is generated imported from an ONNX model. When I try to run this model in MATLAB itself, no errrors occour. The model is saved as *.mat file. Loading and running the model again is working well.
If I want to generate C/C++ code from the model via the following code (in two separate files gencode.m and myNet_predict.m), I get the error message: ??? Layer hyper-parameters for custom layer 'PadLayer1183' must be numeric scalar, scalar logical, character or string array, or a matrix of type double or single.
% codegen.m
cfg = coder.config('mex');
cfg.TargetLang = 'C++';
cfg.DeepLearningConfig = coder.DeepLearningConfig('mkldnn');
args = {zeros(1024,1024,3,'single')};
codegen -config cfg -args args myNet_predict -report
% myNet_predict.m
function out = myNet_predict(img)
persistent mynet;
if isempty(mynet)
mynet = coder.loadDeepLearningNetwork('DeepLabV3plus.mat');
end
pred = predict(mynet, img);
end
Unfortunately the *.mat files of the neural network and PadLayer1183 are to big for uploading here, I uploaded them under https://cloudstore.zih.tu-dresden.de/index.php/s/ajSCte9G9HN2pqP for save downloading. These are all files which are neccessary to reproduce the discribed behaviour.
I tried to debug the code so far, but I do not got an idea why this error occours. As I followed the compilation process, the error appears only when the prediction is called.
The input is every time an image with the dimension 1024x1024x3. I know its very big, but thats a requirement of the neural network. I guess that the problem is the attribute ONNXParams from the type ONNXParameters. Because it would be a very huge workaround to replace this variable with single parameters, I do not think that MATLAB would handle code generations in this way.
Hopefully anyone can help me to figure out whats wrong in the model code or my code.
Thanks in advance.
Best regards

Accepted Answer

Sivylla Paraskevopoulou
Sivylla Paraskevopoulou on 6 Jun 2022
I am assuming that you using the importONNXNetwork function to import the ONNX model, and importONNXNetwork automatically generates custom layers. You can see the automatically generated custom layers in the created package in your current folder. Automatically generated custom layers do not support code generation.
To deploy the imported network, you must replace the autogenerated custom layer with built-in MATLAB layer(s). For more information, see Generate Code for Imported Network. Check also the functionLayer documentation. The functionLayer also supports code generation with some limitations (see, Code Generation for functionLayer).
  2 Comments
Renato
Renato on 11 Jan 2025
I'm currently working with a yolov5 neural network, and I'm having the same problem reported above. In a few words, around 7 layers from the original yolov5s.onnx model have been replaced by a custom layer script in matlab (when using importNetworkFromONNX). Below there is an image showing the original ONNX layers.
Everything below the top Mul layer have been replaced by a matlab script which is not supported for code generation.
Is there any way I can pass a flag to a importing function in order to tell deep learning toolbox not to replace these layers by the auto generated custom layer script?
Following (in gray) is an image of the replaced layers.
thank you!
Sivylla Paraskevopoulou
Sivylla Paraskevopoulou on 16 Jan 2025
@Renato, at the moment the importNetworkFromONNX function automatically generates custom layers when converting the model from ONNX to MATLAB and there is no way to manually choose which layers to not replace. But you do have the option to replace/edit layers after the model has been converted into a MATLAB network. You might find this example helpful.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!