Code Generation for Networks
4 views (last 30 days)
Show older comments
yazan doha
on 27 Sep 2022
Commented: Hariprasad Ravishankar
on 30 Sep 2022
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
0 Comments
Accepted Answer
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:
0 Comments
More Answers (1)
yazan doha
on 28 Sep 2022
1 Comment
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
See Also
Categories
Find more on Get Started with GPU Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!