Main Content

Generate Code and Deploy MobileNet-v2 Network to Raspberry Pi

This example shows how to generate C code that does not depend on any third-party deep learning libraries for pretrained MobileNet-v2 network. After generating the code, you can deploy it to a Raspberry Pi.

When you generate code that uses a hardware support package, the codegen function generates code on the host computer, copies the generated files to the target hardware, and builds the executable on the target hardware.

Third-Party Prerequisites

  • ARM processor that supports the NEON extension

  • Open Source Computer Vision Library (OpenCV) v2.4 (on the target ARM hardware)

  • Environment variables for the compilers and libraries

This example is not supported for MATLAB online.

The Entry-Point Function mobilenet_predict

The mobilenet_predict function loads the pretrained MobileNet-v2 network for MATLAB using imagePretrainedNetwork. Then the entry-point function calls the predict method of the MobileNet-v2 network object on an input image and returns the prediction score output. A dlarray object is created within the entry-point function, input and output to the function are of primitive datatypes. For more information, see Code Generation for dlarray (MATLAB Coder).

type mobilenet_predict
function out = mobilenet_predict(in)
%#codegen
%   Copyright 2019-2024 The MathWorks, Inc.

    dlIn = dlarray(in,'SSC');
    persistent dlnet;
    
    opencv_linkflags = '`pkg-config --cflags --libs opencv`';
    coder.updateBuildInfo('addLinkFlags',opencv_linkflags);
    coder.updateBuildInfo('addCompileFlags',opencv_linkflags);
    
    if isempty(dlnet)
        dlnet = imagePretrainedNetwork('mobilenetv2');
    end

    dlOut = predict(dlnet, dlIn);
    
    out = extractdata(dlOut);
end

Generate Executable for mobilenet_predict

Create a code generation configuration object cfg.

cfg = coder.config('exe');
cfg.CodeReplacementLibrary = 'GCC ARM Cortex-A';

Create a deep learning configuration object and set target library to none. Attach the deep learning configuration object to the code generation configuration object.

dlcfg = coder.DeepLearningConfig(TargetLibrary = 'none');
cfg.DeepLearningConfig = dlcfg;

Create a Connection to the Raspberry Pi

Use the MATLAB Support Package for Raspberry Pi Hardware function raspi to create a connection to the Raspberry Pi.

r = raspi;

If this is your first time connecting to a Raspberry Pi board or you want to connect to a different board, use this line of code and replace

  • raspiname with the host name of your Raspberry Pi

  • username with your username

  • password with your password

r = raspi('raspiname','username','password');

Configure Code Generation Hardware Parameters for Raspberry Pi

Create a 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 = '/home/pi/remoteBuildDir';
cfg.Hardware.BuildDir = buildDir;

Provide a C++ Main File

Specify the main file main_mobilenet.cpp in the code generation configuration object. This file calls the generated C code for the mobilenet_predict function. The file reads the input image, passes the data to the generated function, retrieves the predictions on the image from the function outputs, and prints the prediction scores to a file.

cfg.CustomSource = 'main_mobilenet.cpp';

Generate the Executable Program on the Raspberry Pi

Generate C code. When you use the codegen function with the MATLAB Support Package for Raspberry PI Hardware, the function builds the executable on the Raspberry Pi.

codegen -config cfg mobilenet_predict -args {ones(224, 224, 3,'single')} -report
Code generation successful: View report

Run the Executable Program on the Raspberry Pi

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 the raspi.utils.getRemoteBuildDirectory API. This function lists the folders of the binary files that the codegen function generates. Assuming that the binary is in only one folder, enter:

applicationDirPaths = raspi.utils.getRemoteBuildDirectory('applicationName','mobilenet_predict');
targetDirPath = applicationDirPaths{1}.directory;

Use the putFile function to copy the files required to run the executable program.

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 '; 
command = ['cd ' targetDirPath ';sudo ./' exeName argsforexe];
output = system(r,command);

Get the Prediction Scores for the 1000 Output Classes of the Network.

outputfile = [targetDirPath, '/output.txt'];
r.getFile(outputfile);

Map the Prediction Scores to Labels and Display Output

Map the top five prediction scores to the corresponding labels in the trained network and display the output.

mapPredictedScores_mobilenet

Figure contains an axes object. The axes object contains an object of type image.

See Also

(MATLAB Coder) | (MATLAB Coder) | (MATLAB Coder)

Related Topics