Code Generation for Networks

4 views (last 30 days)
Hallo everyone,
when we want to generate a Code, we should choose a pretrained net like mobilenetv2() and have an entry-point function for this net, type("mobilenetv2 _predict.m") :
% Copyright 2017-2019 The MathWorks, Inc.
function out = mobilenetv2_predict(in)
persistent mynet;
if isempty(mynet)
mynet = coder.loadDeepLearningNetwork('mobilenetv2','mobilenetv2');
end
% pass in input
out = mynet.predict(in);
My question is: What if I train a standalone network for my project? How can I put it in this function to deploy it on Jetson nano?
Thank you very much

Accepted Answer

Hariprasad Ravishankar
Hariprasad Ravishankar on 27 Sep 2022
Hello,
If you have a standlone network, you can save the network to a MAT file and specify the name of the MAT file as the first argument to coder.loadDeepLearningNetwork function as follows.
net = squeezenet; % net could be any custom SeriesNetwork, DAGNetwork or dlnetwork object
save mynet.mat net
function out = mpredict(in)
%#codegen
persistent net;
if isempty(net)
net = coder.loadDeepLearningNetwork('mynet.mat');
end
out = predict(net, in);
end
You can refer to the example link below to deploy your application to NVIDIA Jetson boards:
Here is an example video:

More Answers (1)

yazan doha
yazan doha on 28 Sep 2022
Thank you for your Answer @Hariprasad Ravishankar
now when i use this function in GPU Coder as an Entry-Point Functions, so the next step is to provide a test file that calls the project entry-point functions.
should i convert my Matlab Code (train the Network) in a test file or??
Can you help me to convert my Matlab Code in a test file to define the Input??
  1 Comment
Hariprasad Ravishankar
Hariprasad Ravishankar on 30 Sep 2022
Hi Yazan,
You can write an entry point function that passes a single input or a batch of inputs to classfiy function. For example:
function out = mclassify(in)
%#codegen
persistent net;
if isempty(net)
net = coder.loadDeepLearningNetwork('mynet.mat');
end
out = classify(net, in);
You can then generate code and interface with it using MEX using GPU Coder as follows:
cfg = coder.gpuConfig('mex');
cfg.DeepLearningConfig = coder.DeepLearningConfig(TargetLibrary = 'cudnn');
codegen -config cfg -args {testInput} mclassify
This will generate a MEX file named mclassify_mex which you can invoke from your test file as follows:
idx2=randi([5,50]);
aug_idx2=augmentedImageDatastore([224 224], idx2);
o2= readimage(imds_test,idx2);
aug_o2=augmentedImageDatastore([224 224], o2);
result2=mclassify_mex(convnet,aug_o2);
Hari

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!