Deep Learning Prediction with ARM Compute Using cnncodegen

This example shows how to use cnncodegen to generate code for a Logo classification application that uses deep learning on ARM® processors. The logo classification application uses the LogoNet series network to perform logo recognition from images. The generated code takes advantage of the ARM Compute library for computer vision and machine learning.

Prerequisites

  • ARM processor that supports the NEON extension

  • Open Source Computer Vision Library (OpenCV) v3.1

  • Environment variables for ARM Compute and OpenCV libraries

  • MATLAB® Coder™ for C++ code generation

  • The support package MATLAB Coder Interface for Deep Learning

  • Deep Learning Toolbox™ for using the SeriesNetwork object

For more information, see Prerequisites for Deep Learning with MATLAB Coder (MATLAB Coder).

This example is supported on Linux® and Windows® platforms and not supported for MATLAB Online.

Get the Pretrained SeriesNetwork

Download the pretrained LogoNet network and save it as logonet.mat, if it does not exist. The network was developed in MATLAB® and its architecture is similar to that of AlexNet. This network can recognize 32 logos under various lighting conditions and camera angles.

net = getLogonet();

The network contains 22 layers including convolution, fully connected, and the classification output layers.

net.Layers
ans = 

  22x1 Layer array with layers:

     1   'imageinput'    Image Input             227x227x3 images with 'zerocenter' normalization and 'randfliplr' augmentations
     2   'conv_1'        Convolution             96 5x5x3 convolutions with stride [1  1] and padding [0  0  0  0]
     3   'relu_1'        ReLU                    ReLU
     4   'maxpool_1'     Max Pooling             3x3 max pooling with stride [2  2] and padding [0  0  0  0]
     5   'conv_2'        Convolution             128 3x3x96 convolutions with stride [1  1] and padding [0  0  0  0]
     6   'relu_2'        ReLU                    ReLU
     7   'maxpool_2'     Max Pooling             3x3 max pooling with stride [2  2] and padding [0  0  0  0]
     8   'conv_3'        Convolution             384 3x3x128 convolutions with stride [1  1] and padding [0  0  0  0]
     9   'relu_3'        ReLU                    ReLU
    10   'maxpool_3'     Max Pooling             3x3 max pooling with stride [2  2] and padding [0  0  0  0]
    11   'conv_4'        Convolution             128 3x3x384 convolutions with stride [2  2] and padding [0  0  0  0]
    12   'relu_4'        ReLU                    ReLU
    13   'maxpool_4'     Max Pooling             3x3 max pooling with stride [2  2] and padding [0  0  0  0]
    14   'fc_1'          Fully Connected         2048 fully connected layer
    15   'relu_5'        ReLU                    ReLU
    16   'dropout_1'     Dropout                 50% dropout
    17   'fc_2'          Fully Connected         2048 fully connected layer
    18   'relu_6'        ReLU                    ReLU
    19   'dropout_2'     Dropout                 50% dropout
    20   'fc_3'          Fully Connected         32 fully connected layer
    21   'softmax'       Softmax                 softmax
    22   'classoutput'   Classification Output   crossentropyex with 'adidas' and 31 other classes

Set Environment Variables

On the ARM target hardware, make sure that ARM_COMPUTELIB is set and that LD_LIBRARY_PATH contains the path to the ARM Compute Library folder.

See Prerequisites for Deep Learning with MATLAB Coder (MATLAB Coder).

Generate Code

For deep learning on ARM targets, you generate code on the host development computer. Then, you move the generated code to the ARM platform where you build and run the executable program. The target platform must support the Neon instruction set architecture (ISA). Raspberry Pi3, Firefly, HiKey are some of the target platforms on which the generated code can be executed.

Call cnncodegen, specifying that the target library is the ARM Compute Library. Specify the version of the library and the architecture of the target ARM processor. The ARMArchitecture parameter is required.

cnncodegen(net,'targetlib','arm-compute','targetparams', struct('ArmComputeVersion','19.05','ARMArchitecture','armv8'));

Copy the Generated Files to the Target

Move the codegen folder and other required files from the host development computer to the target platform by using your preferred scp/ssh client.

For example on the Linux platform, to transfer the files to the Raspberry Pi, use the scp command with the format:

system('sshpass -p [password] scp (sourcefile) [username]@[targetname]:~/');

system('sshpass -p password scp main_arm_logo.cpp username@targetname:~/');
system('sshpass -p password scp coderdemo_google.png username@targetname:~/');
system('sshpass -p password scp makefile_arm_logo.mk username@targetname:~/');
system('sshpass -p password scp synsetWordsLogoDet.txt username@targetname:~/');
system('sshpass -p password scp -r codegen username@targetname:~/');

On the Windows platform, you can use the pscp tool that comes with a PuTTY installation. For example,

system('pscp -pw password -r codegen username@targetname:/home/username');

Note: PSCP utilities must be either on your PATH or in your current directory.

Build and Execute

To build the library on the target platform, use the generated makefile cnnbuild_rtw.mk.

For example, to build the library on the Raspberry Pi from the Linux platform:

system('sshpass -p password ssh username@targetname "make -C /home/username/codegen -f cnnbuild_rtw.mk"');

On the Windows platform, you can use the putty command with -ssh argument to log in and run the make command. For example:

system('putty -ssh username@targetname -pw password');

To build and run the executable on the target platform, use the command with the format: make -C /home/$(username) and ./execfile -f makefile_arm_logo.mk

For example, on the Raspberry Pi:

make -C /home/pi arm_neon -f makefile_arm_logo.mk

Run the executable with an input image file.

./logo_recognition_exe coderdemo_google.png

The top five predictions for the input image file are: