How to add a trained deep neural network into a MATLAB function for C++ code generation using MATLAB Coder?

2 次查看(过去 30 天)
As part of my project, I have to include a trained deep neural network into a MATLAB function in order to classify different types of arrows using a webcam. The code is given below. And when i try to convert the MATLAB code using MATLAB Coder it shows an error of Maximum recursion limit of 500 reached. What is the reason this error occuring and how can i avoid this error.
function [Vx,Vy,Omega] = Landmarkerkennung(Image)
net = coder.loadDeepLearningNetwork('convnetwork1'); %Is this how to include a trained network?
%convnetwork1 is the name of my trained Network which is saved in MATLAB
% Vx, Vy,Omega are the linear and angular velocities which has to be fed to the robot. And input is the image from the camera of a robot.
%Workspace.
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
% Continous loop for live video from thr Robotino Camera
while true
img = Image; % Capturing new Image in 'img'
picture = imresize(img,[227,227]); % Resizing the picture (227 227). Pixel required for Alexnet Algorithm
label = classify(net, picture); % Classify the picture. Labels of the pictures are stored in 'label'
% Calling the respective function according to the label that appears on
% the camera frame
if label == 'straight'
Omega = 0;
Vx = 75;
Vy = 0;
% [Vx,Vy,Omega] = gostraight();
elseif label == 'left'
disp('Left');
Omega = 50;
Vx = 0;
Vy = 0;
pause(2); % Pause the execution of the Programm for 2 second
% [Vx,Vy,Omega] = gosharpleft();
elseif label == 'right'
disp('Right');
Omega = -50;
Vx = 0;
Vy = 0;
pause(2); % Pause the execution of the Programm for 2 second
% [Vx,Vy,Omega] = gosharpright();
elseif label == 'right'
disp('Right');
Omega = -50;
Vx = 0;
Vy = 0;
pause(2); % Pause the execution of the Programm for 2 second
end
end
end
When i convert the MATLAB function i get this error.
Error Matlab coder.JPG

采纳的回答

Mukund Sankaran
Mukund Sankaran 2019-8-14
You are seeing that error message because you've named your function 'convnetwork1' and in line #5 of that function, you are calling coder.loadDeepLearningNetwork('convnetwork1' ).
The call to coder.loadDeepLearningNetwork can take either the name of a MAT-file or the name of a function existing in the MATLAB path as the first argument.
When codegen runs, it hits line #5, which invokes the function 'convnetwork1' again, and this continues in a recursive loop until the call stack overflows, which is why you see the error 'Maximum recursion limit of 500 reached'
You have a couple of options to load your trained network as I see it. Hope this helps!
1. Call coder.loadDeepLearningNetwork with the name of a MAT-file that contains your network. Here's an example
% foo.m
function out = foo()
out = coder.loadDeepLearningNetwork('convnetwork.mat') % Contains your trained network
end
% Now you can codegen foo.m
2. Write a function that returns your trained network and provide the name of that function while calling coder.loadDeepLearningNetwork. Here's an example
% foo.m
function out = foo()
out = coder.loadDeepLearningNetwork('convnetwork');
end
% convnetwork.m
function out = convnetwork()
out = alexnet; % Return your network
end
% Now you can codegen foo.m
  3 个评论
Mukund Sankaran
Mukund Sankaran 2019-8-14
The error says that the input passed to the "predict" function must be fixed size for codegen to work.
In your example, you pass in a variable that contains the image to the Landmarkerkennung function, which is later resized and passed to the predict function.
In the "Define Input Types" step, you must make sure that the input named "Image" is fixed size i.e. no dimension can be variable size ('up to' or 'Inf'). It must be fixed size: eg: uint8(227 x 227 x 3)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by