Main Content

Deep Learning Prediction with ARM Compute Usingcodegen

This example shows how to usecodegen生成代码的标识分类应用tion that uses deep learning on ARM® processors. The logo classification application uses theLogoNet系列网络以从图像执行徽标识别。生成的代码利用ARM计算库进行计算机视觉和机器学习。

Prerequisites

  • 支持霓虹灯扩展的手臂处理器金宝app

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

  • 手臂计算和OPENCV库的环境变量

  • MATLAB® Coder™ for C++ code generation

  • The support package MATLAB Coder Interface for Deep Learning

  • 深度学习工具箱™ for using theSeriesNetworkobject

该示例使用的ARM计算库版本可能不是代码生成支持的最新版本。金宝app有关库的金宝app支持版本以及有关设置环境变量的信息,请参见与MATLAB编码器进行深度学习的先决条件(MATLAB编码器)

该示例在Linux®和Windo金宝appws®平台上得到支持,并且不支持MATLAB在线。

得到Pretrained SeriesNetwork

Download the pretrainedLogoNetnetwork and save it aslogonet.mat,如果不存在。该网络是在Matlab®开发的,其架构与Alexnet相似。该网络可以在各种照明条件和摄像机角度识别32个徽标。

net = getLogonet();

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

net.Layers
ans = 22×1层阵列,带有图层:1'imageInput'图像输入227×227×3图像,带有“ zerecenter”归一化和'randfliplr'增强2'conv_1'卷积96 5×5×3卷积与步行[1 1]和填充[0 0 0 0] 3'relu_1'relu relu 4'maxpool_1'最大池3×3 max boming with stride [2 2]和填充[0 0 0 0] 5'conv_2'conv_2'卷积128 3×3×96大步卷积[1 1]和填充[0 0 0 0] 6'relu_2'relu relu 7'maxpool_2'maxpool_2'最大池3×3 max boming plinide [2 2]和填充[0 0 0 0 0 0] 8'CORV_3''cons_3''卷积384 3×3×128步步卷[1 1]和填充[0 0 0 0] 9'RERU_3'relu relu 10'maxpool_3'最大池池3×3 max poil ting ting plinide [2 2]和填充[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0] 11'CORV_4'卷积128 3×3×384卷积[2 2]和填充[0 0 0 0] 12'RERU_4'relu relu 13'maxpool_4'maxpool_4'max pliming 3×3 max plies plinide [22]和填充[0 0 0 0] 14'FC_1'完全连接2048完全连接的第15'relu_5'relu relu 16'dropout_1'drop out 50%辍学17'fc_2'完全连接的2048完全连接的第18'relu_6'relu relu 19'dropt_2'辍学50%辍学20'fc_3'完全连接32完全连接的层21'softmax'softmax'softmax'softmax 22'callastOutput'分类'分类'分类'带有“ adidas”和其他31个课程的CrossentRopyex

Set Environment Variables

在ARM目标硬件上,请确保设置了ARM_COMPUTELIB,并且LD_LIBRARY_PATH包含ARM Compute库文件夹的路径。

See与MATLAB编码器进行深度学习的先决条件(MATLAB编码器)

logonet_predict函数

Thelogonet_predict.mentry-point function takes an image input and performs prediction on the image using the deep learning network saved in theLogoNetMAT-file. The function loads the network object fromLogoNet.matinto a persistent network variablelogonet。On subsequent calls to the function, the persistent object is reused.

typelogonet_predict
函数输出= logonet_predict(in)%#codegen%版权所有2017-2020 Mathworks,Inc。持续logonet;如果Isempty(Logonet)logonet = coder.loaddeeplearningnetwork('logonet.mat','logonet');结束= logonet.predict(in);结尾

为静态库设置代码生成配置对象

When you generate code targeting an ARM-based device and do not use a hardware support package, create a configuration object for a library. Do not create a configuration object for an executable program.

Set up the configuration object for generation of C++ code and generation of code only.

cfg = coder.config('lib'); cfg.TargetLang ='C++';cfg.GenCodeOnly = true;

Set Up a Configuration Object for Deep Learning Code Generation

创建一个coder.ARMNEONConfigobject. Specify the library version and the architecture of the target ARM processor. For example, suppose that the target board is a HiKey/Rock960 board with ARMv8 architecture and ARM Compute Library version 19.05.

dlcfg = coder.DeepLearningConfig(“手臂计算”); dlcfg.ArmComputeVersion ='19.05';dlcfg.armarchitecture ='armv8';

Attach the Deep Learning Configuration Object to the Code Generation Configuration Object

Set the深度学习property of the code generation configuration object to the deep learning configuration object.

cfg.DeepLearningConfig = dlcfg;

Generate Source C++ Code by Usingcodegen

codegen-configcfglogonet_predict-args{ones(227, 227, 3, 'single')}-darm_compute

The code is generated in thearm_computefolder in the current working folder on the host computer.

使用packNGo功能

The packNGo function packages all relevant files in a compressed zip file.

zipfileName ='arm_compute.zip';binfo = load(fullFile('arm_compute','buildInfo.mat'));packngo(binfo.buildinfo,{'filename',zipfileName,'minimalheaders',false,'nighoreFilemissing',true});

Copy the Generated Zip File to the Target Hardware

Copy the Zip file and extract into a folder. Remove the Zip file from the target hardware.

In the following commands, replace:

  • passwordwith your password

  • usernamewith your user name

  • targetnamewith the name of your device

  • TargetLocwith the destination folder for the files

运行这些命令以从Linux复制和提取zip文件。

如果isunix,系统(['sshpass -p password scp -r 'fullfile(pwd,zipFileName)'用户名@targetName:targetLoc/']),结尾如果isunix, system('sshpass -p password ssh username@targetname "if [ -d targetloc/arm_compute ]; then rm -rf targetloc/arm_compute; fi"'),结尾如果isunix,系统(['sshpass -p密码ssh用户名@targetName“ unzip targetloc/'ZipfileName'-d targetloc/arm_compute“'']),结尾如果isunix,系统(['sshpass -p password ssh username@targetname "rm -rf targetloc'ZipfileName'"']),结尾

Run these commands to copy and extract zip file from Windows.

如果ispc, system(['pscp.exe -pw password -r 'fullfile(pwd,zipFileName)'用户名@targetName:targetLoc/']),结尾如果ispc, system('plink.exe -l用户名-PW密码targetName“如果[-d targetloc/arm_compute];然后rm -rf targetloc/arm_compute; fi''''),结尾如果ispc, system(['plink.exe -l username -pw password targetname "unzip targetloc/'ZipfileName'-d targetloc/arm_compute“'']),结尾如果ispc, system(['plink.exe -l username -pw password targetname "rm -rf targetloc'ZipfileName'"']),结尾

Copy Example Files to the Target Hardware

Copy these supporting files from the host computer to the target hardware:

  • Input image,coderdemo_google.png

  • Makefile for generating the library,logonet_predict_rtw.mk

  • Makefile for building the executable program,makefile_arm_logo.mk

  • 同义词集词典,Synsetwordslogodet.txt

In the following commands, replace:

  • passwordwith your password

  • usernamewith your user name

  • targetnamewith the name of your device

  • TargetLocwith the destination folder for the files

Perform the steps below to copy all the required files when running from Linux

如果isunix, system('sshpass -p password scp logonet_predict_rtw.mk username@targetname:targetloc/arm_compute/'),结尾如果isunix, system('sshpass -p password scp coderdemo_google.png username@targetname:targetloc/arm_compute/'),结尾如果isunix, system('sshpass -p password scp makefile_arm_logo.mk username@targetname:targetloc/arm_compute/'),结尾如果isunix, system('sshpass -p密码scp synsetwordslogodet.txt用户名@targetName:targetloc/arm_compute/'),结尾

执行以下步骤以复制从Windows运行时所有必需的文件

如果ispc, system('pscp.exe -pw password logonet_predict_rtw.mk username@targetname:targetloc/arm_compute/'),结尾如果ispc, system('pscp.exe -pw password coderdemo_google.png username@targetname:targetloc/arm_compute/'),结尾如果ispc, system('pscp.exe -pw密码makefile_arm_logo.mk用户名@targetName:targetloc/arm_compute/'),结尾如果ispc, system('pscp.exe -pw password synsetWordsLogoDet.txt username@targetname:targetloc/arm_compute/'),结尾

Build the Library on the Target Hardware

要在目标硬件上构建库,请在ARM硬件上执行生成的Makefile。

Make sure that you set the environment variables ARM_COMPUTELIB and LD_LIBRARY_PATH on the target hardware. See与MATLAB编码器进行深度学习的先决条件(MATLAB编码器)。ARM_ARCH变量在MakeFile中用于基于ARM架构的编译器标志。ARM_VER变量在MakeFile中用于基于ARM COMPUTE版本编译代码。在这些命令中替换硬件凭据和路径,类似于上一节。

Perform the below steps to build the library from Linux.

如果isunix, system('sshpass -p password scp main_arm_logo.cpp username@targetname:targetloc/arm_compute/'),结尾如果isunix,系统(['sshpass -p password ssh username@targetname "make -C targetloc/arm_compute/ -f logonet_predict_rtw.mk ARM_ARCH='dlcfg.ArmArchitecture' ARM_VER='dlcfg.ArmComputeVersion' "']),结尾

Perform the below steps to build the library from windows.

如果ispc, system('pscp.exe -pw password main_arm_logo.cpp username@targetname:targetloc/arm_compute/'),结尾如果ispc, system(['plink.exe -l username -pw password targetname "make -C targetloc/arm_compute/ -f logonet_predict_rtw.mk ARM_ARCH='dlcfg.ArmArchitecture' ARM_VER='dlcfg.ArmComputeVersion' "']),结尾

Create Executable from the Library on the Target Hardware

Build the library with the source main wrapper file to create the executable.main_arm_logo.cpp是调用的C ++主包装文件logonet_predict功能。

运行以下命令以从Linux创建可执行文件。

如果isunix, system('sshpass -p password ssh username@targetname "make -C targetloc/arm_compute/ -f makefile_arm_logo.mk targetDirName=targetloc/arm_compute"'),结尾

Run the below command to create the executable from Windows.

如果ispc, system('plink.exe -l username -pw password targetname "make -C targetloc/arm_compute/ -f makefile_arm_logo.mk targetDirName=targetloc/arm_compute"'),结尾

Run the Executable on the Target Hardware

使用以下命令从Linux运行可执行文件。
如果isunix, system('sshpass -p password ssh username@targetname "cd targetloc/arm_compute/; ./logonet coderdemo_google.png"'),结尾
Run the executable from Windows using below command.
如果ispc, system('plink.exe -l用户名-PW密码targetName“ cd targetloc/arm_compute/; ./logonet coderdemo_google.png”'''),结尾
Top 5 Predictions: ----------------------------- 99.992% google 0.003% corona 0.003% singha 0.001% esso 0.000% fedex