Code Generation for Networks
4 次查看(过去 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
0 个评论
采纳的回答
Hariprasad Ravishankar
2022-9-27
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 个评论
更多回答(1 个)
yazan doha
2022-9-28
1 个评论
Hariprasad Ravishankar
2022-9-30
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 Center 和 File Exchange 中查找有关 Get Started with GPU Coder 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!