MATLAB Answers

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

3 views (last 30 days)
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

  0 Comments

Sign in to comment.

Accepted Answer

Mukund Sankaran
Mukund Sankaran on 14 Aug 2019
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 Comments

Philip Kanjirathingal Kurian
Thanks for the fast reply. I tried your solution.
function [Vx,Vy,Omega] = Landmarkerkennung(Image)
function out = foo()%#codegen
out = coder.loadDeepLearningNetwork('looprun1.mat');
end
%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=predict(foo,picture);
% 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;
elseif label == 'left'
disp('Left');
Omega = 50;
Vx = 0;
Vy = 0;
pause(2); % Pause the execution of the Programm for 2 second
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
But there are some other errors poping up when i generate the code.
When i checked, to classify the input image using the trained network, there are two options.
  1. Using the function classify() and
  2. Using the function predict()
I tried the classify() function first, but the function was not recognised in MATLAB Coder. So now i used the predict() function in order to classify the input image. But then i get the first error in the image. What should i change in my code in order to avoid the error?
Error Matlab coder 2.JPG
Mukund Sankaran
Mukund Sankaran on 14 Aug 2019
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)

Sign in to comment.

More Answers (0)

Sign in to answer this question.