This example shows how to generate and deploy C++ code that uses the MobileNet-v2 pretrained network for object prediction.
ARM processor that supports the NEON extension
ARM Compute Library (on the target ARM hardware)
Open Source Computer Vision Library(OpenCV) v2.4 (on the target ARM hardware)
Environment variables for the compilers and libraries
MATLAB® Coder™
MATLAB Coder Interface for Deep Learning Libraries support package
Deep Learning Toolbox™
Deep Learning Toolbox Model for MobileNet-v2 Network support package
Image Processing Toolbox™
MATLAB Support Package for Raspberry Pi Hardware
The ARM Compute library version that this example uses might not be the latest version that code generation supports. For supported versions of libraries and for information about setting up environment variables, seePrerequisites for Deep Learning with MATLAB Coder(MATLAB Coder).
This example is not supported for MATLAB online.
This example uses the DAG network MobileNet-v2 to perform image classification with the ARM® Compute Library. The pretrained MobileNet-v2 network for MATLAB is available in the Deep Learning Toolbox Model for MobileNet-v2 Network support package.
When you generate code that uses the ARM Compute Library and a hardware support package,codegen
generates code on the host computer, copies the generated files to the target hardware, and builds the executable on the target hardware.
mobilenet_predict
FunctionThemobilenet_predict
function calls the predict method of the MobileNet-v2 network object on an input image and returns the prediction score output. The function callscoder.updateBuildInfo
指定链接options for the generated makefile.
typemobilenet_predict
function out = mobilenet_predict(in) persistent net; opencv_linkflags = '`pkg-config --cflags --libs opencv`'; coder.updateBuildInfo('addLinkFlags',opencv_linkflags); if isempty(net) net = coder.loadDeepLearningNetwork('mobilenetv2', 'mobilenet'); end out = net.predict(in); end
创建一个C++ code generation configuration object.
cfg = coder.config('exe'); cfg.TargetLang ='C++';
Specify Use of the ARM Compute Library. The ARM Compute Library provides optimized functionality for the Raspberry Pi hardware. To generate code that uses the ARM Compute Library, create acoder.ARMNEONConfig
object. Specify the version of the ARM Compute Library installed on your Raspberry Pi and the architecture of the Raspberry Pi. Attach the deep learning configuration object to the code generation configuration object.
dlcfg = coder.DeepLearningConfig('arm-compute'); supportedVersions = dlcfg.getARMComputeSupportedVersions; dlcfg.ArmArchitecture ='armv7'; dlcfg.ArmComputeVersion ='19.05'; cfg.DeepLearningConfig = dlcfg;
Use the MATLAB Support Package for Raspberry Pi Hardware functionraspi
to create a connection to the Raspberry Pi. In this code, replace:
raspiname
with the host name of your Raspberry Pi
username
with your user name
password
with your password
r = raspi('raspiname','username','password');
创建一个coder.Hardware
object for the Raspberry Pi and attach it to the code generation configuration object.
hw = coder.hardware('Raspberry Pi'); cfg.Hardware = hw;
Specify a build folder on the Raspberry Pi:
buildDir ='~/remoteBuildDir'; cfg.Hardware.BuildDir = buildDir;
Specify the main filemain_mobilenet.cpp
in the code generation configuration object. The file calls the generated C++ code for themobilenet_predict
function. The file reads the input image, passes the data to the generated function calls, retrieves the predictions on the image, and prints the prediction scores to a file.
cfg.CustomSource ='main_mobilenet.cpp';
Generate C++ code. When you usecodegen
with the MATLAB Support Package for Raspberry PI Hardware, the executable is built on the Raspberry Pi.
For code generation, you must set theEnvironment Variables(MATLAB Coder)ARM_COMPUTELIB
andLD_LIBRARY_PATH
on the Raspberry Pi.
codegen-configcfgmobilenet_predict-args{ones(224, 224, 3,'single')}-report
To test the generated code on the Raspberry Pi, copy the input image to the generated code folder. You can find this folder manually or by using theraspi.utils.getRemoteBuildDirectory
API. This function lists the folders of the binary files that are generated by usingcodegen
. Assuming that the binary is found in only one folder, enter:
applicationDirPaths = raspi.utils.getRemoteBuildDirectory('applicationName','mobilenet_predict'); targetDirPath = applicationDirPaths{1}.directory;
To copy files required to run the executable program, useputFile
.
r.putFile('peppers_raspi_mobilenet.png',targetDirPath);
Run the executable program on the Raspberry Pi from MATLAB and direct the output back to MATLAB.
exeName ='mobilenet_predict.elf'; argsforexe =' peppers_raspi_mobilenet.png ';% Provide the input image;command = ['cd 'targetDirPath';sudo ./'exeName argsforexe]; output = system(r,command);
outputfile = [targetDirPath,'/output.txt']; r.getFile(outputfile);
Map the top five prediction scores to the corresponding labels in the trained network, and display the output.
typemapPredictedScores_mobilenet
%% Map the Prediction Scores to Labels and Display Output net = mobilenetv2; ClassNames = net.Layers(end).ClassNames; %% Read the classification fid = fopen('output.txt') ; S = textscan(fid,'%s'); fclose(fid) ; S = S{1} ; predict_scores = cellfun(@(x)str2double(x), S); %% Remove NaN values that were strings predict_scores(isnan(predict_scores))=[]; [val,indx] = sort(predict_scores, 'descend'); scores = val(1:5)*100; top5labels = ClassNames(indx(1:5)); %% Display classification labels on the image im = imread('peppers_raspi_mobilenet.png'); im = imresize(im, [224 224]); outputImage = zeros(224,400,3, 'uint8'); for k = 1:3 outputImage(:,177:end,k) = im(:,:,k); end scol = 1; srow = 1; outputImage = insertText(outputImage, [scol, srow], 'Classification with MobileNetv2', 'TextColor', 'w','FontSize',20, 'BoxColor', 'black'); srow = srow + 30; for k = 1:5 outputImage = insertText(outputImage, [scol, srow], [top5labels{k},' ',num2str(scores(k), '%2.2f'),'%'], 'TextColor', 'w','FontSize',15, 'BoxColor', 'black'); srow = srow + 25; end imshow(outputImage);
coder.ARMNEONConfig
(MATLAB Coder)|coder.DeepLearningConfig
(MATLAB Coder)|coder.hardware
(MATLAB Coder)