How to fix "Function call failed" issue when trying to call "coder.loa​dDeepLearn​ingNetwork​(network_p​ath);", where network_path= path for trained network.mat file

16 次查看(过去 30 天)
Im trying to generate C++ source files, from a matlab function i wrote, using Matlab coder app. The function is loading a trained SeriesNetwork into the variable "mynet" and then makes a prediction for a specific image file via the "predict" function:
function result = predict_img(net_path,img)
persistent mynet;
if isempty(mynet)
mynet = coder.loadDeepLearningNetwork(net_path);
end
im = imread(img);
result = predict(mynet,im);
end
I use the Matlab Coder app, as shown in the following steps:
Step 1
Step 2
Call of predict_img = predict_img("C:\Users\midan\Desktop\RoboticsVision\Application\Training1.mat","C:\Users\midan\Desktop\RoboticsVision\Application\img1.jpg");
(i change the capacity of the string variables to up to 254)
Step 3
Step 4
The errors:
When I try generating the C++ source file via Coder app, when i reach the step that checks for Run-Time Issues I have the following errors:
Release: MATLAB R2021a
Version: 9.10.0.1602886 (R2021a)
Any Ideas???
Thanks in advance.
  2 个评论
Denis Gurchenkov
Denis Gurchenkov 2022-1-18
This looks like a bug in MATLAB Coder. Can you please attach reproduction steps, all the files required to reproduce this (.mat file, and the command line you used to call predict_img), also specify which release of MATLAB are you using.
If you do not feel comfortable attaching the files and reproduction steps, please contact MathWorks technical support for furhter assistance.

请先登录,再进行评论。

回答(2 个)

Sergio Matiz Romero
First, I would like to thank you for reporting this issue. I was able to reproduce the error you observed on my end, and it does not seem to be the expected/desired behavior from the app. I found two potential workarounds that should allow you to avoid the MATLAB error and generate the code succesfully while we investigate this issue internally:
1) Hardcode the path to the trained network (.MAT file) in the function "predict_img" and use the MATLAB Coder app (as you were doing). Loading the network in the "predict_img" function would look something like:
mynet = coder.loadDeepLearningNetwork('<path_to_network>/Training1.mat');
This should allow you to generate code succesfully using the MATLAB Coder app.
2) You can leave the function "predict_img" as is, and generate the code programatically (not through the MATLAB Coder app). You can do that using a script that would look something like:
%% coder config
cfg = coder.config('mex');
cfg.DeepLearningConfig = coder.DeepLearningConfig('TargetLibrary','none');
cfg.TargetLang = 'C++';
%% Add the paths to both the netwok and the image below
%net_path = '<path_to_network>'
%img_path = '<path_to_image>'
%% codegen
codegen predict_img -config cfg -args {coder.Constant(net_path), img_path}
The path to the network must be a compile time constant as shown above.
A programmatic workflow similar to workaround (2) is described in the link below:
Thanks again for reaching out to us regarding this issue, and sorry for the inconveniences this may have caused. We will continue to investigate this unexpected behavior from the MATLAB Coder app internally. Please let us know if the above workarounds allow you to generate code successfully.

Sergio Matiz Romero
After investigating the issue further, I found that the problem with the initial workflow you were following is the definition of the argument "net_path" in the MATLAB Coder app. It must be defined as a constant and it should work without resorting to the workarounds above.
In the MATLAB Coder app, for "net_path", you must choose the option "Define Constant", similar to what is shown below for the variable "networkPath":
Then, you can type the path as a char. That would look something like:
When the app asks you to select a script (or write a line of code) to test the MEX, you can do as you were doing before and write a line that invokes "predict_img" and pass "net_path" as a char (it must be the same char defined in the previous step). "img_path" iself does not need to be a constant.
I hope this helps you solve the issue you were facing and continue with the initial workflow you were following

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by