Code Generation for Networks

2 次查看(过去 30 天)
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

采纳的回答

Hariprasad Ravishankar
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:

更多回答(1 个)

yazan doha
yazan doha 2022-9-28
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 个评论
Hariprasad Ravishankar
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Deep Learning Code Generation Fundamentals 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by