主要内容

使用ARM计算的深度学习预测codegen

这个例子展示了如何使用codegen为在ARM®处理器上使用深度学习的徽标分类应用程序生成代码。徽标分类应用程序使用LogoNet系列网络从图像中执行标识识别。生成的代码利用了用于计算机视觉和机器学习的ARM Compute库。

先决条件

  • 支持NEON扩展的ARM处理器金宝app

  • 开源计算机视觉库(OpenCV) v3.1

  • ARM Compute和OpenCV库的环境变量

  • MATLAB®Coder™用于c++代码生成

  • 支持包M金宝appATLAB编码器接口深度学习

  • 深度学习工具箱™用于使用SeriesNetwork对象

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

本示例支持Linux®和Wind金宝appows®平台,不支持MATLAB Online。

获得预训练的SeriesNetwork

下载预训练LogoNet网络并保存为logonet.mat,如果它不存在的话。该网络是在MATLAB®中开发的,其架构与AlexNet相似。该网络可以在不同的光照条件和相机角度下识别32个标志。

net = getLogonet();

该网络包含22层,包括卷积层、完全连接层和分类输出层。

网层
ans = 22×1包含图层的图层数组:1“imageinput”图像输入227×227×3图片zerocenter正常化和randfliplr的扩增2 conv_1卷积96 5×5×3旋转步[1]和填充[0 0 0 0]3‘relu_1 ReLU ReLU 4“maxpool_1”马克斯池3×3马克斯池步(2 - 2)和填充[0 0 0 0]5 conv_2卷积128 3×3×96旋转步[1]和填充[0 0 0 0]6‘relu_2 ReLU ReLU 7“maxpool_2”马克斯池3×3马克斯池步(2 - 2)和填充[0 0 0 0]8“conv_3”卷积384 3×3×128旋转步[1]和填充[0 0 0 0]9 ' relu_3 ReLU ReLU 10“maxpool_3”马克斯池3×3马克斯池步(2 - 2)和填充[0 0 0 0]11 conv_4卷积128 3×3×384旋转步(2 - 2)和填充[0 0 0 0]12的relu_4 ReLU ReLU 13“maxpool_4”马克斯池3×3马克斯池步(2 - 2)和填充[0 0 0 0]14 fc_1完全连接2048完全连接层15 ' relu_5 ReLU ReLU 16“dropout_1”辍学50%辍学17 fc_2完全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

设置环境变量

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

看到与MATLAB编码器深度学习的先决条件(MATLAB编码器)

logonet_predict函数

logonet_predict.m入口点函数接受图像输入,并使用保存在中的深度学习网络对图像进行预测LogoNetMAT-file。函数加载网络对象LogoNet.mat转换为持久的网络变量logonet。在对该函数的后续调用中,将重用持久对象。

类型logonet_predict
版权所有2017-2020 The MathWorks, Inc.持久logonet;if isempty(logonet) logonet = coder.loadDeepLearningNetwork(' logonet .mat','logonet');End out = logonet.predict(in);结束

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

当您生成针对基于arm的设备的代码并且不使用硬件支持包时,请为库创建一个配置对象。金宝app不要为可执行程序创建配置对象。

为c++代码的生成和仅生成代码设置配置对象。

CFG = coder.config(“自由”);cfg。TargetLang =“c++”;cfg。GenCodeOnly = true;

建立一个用于深度学习代码生成的配置对象

创建一个编码器。ARMNEONConfig对象。指定库版本和目标ARM处理器的体系结构。例如,假设目标板是ARMv8架构和ARM计算库版本19.05的HiKey/Rock960板。

DLCFG =编码器。DeepLearningConfig (“arm-compute”);dlcfg。ArmComputeVersion =“19.05”;dlcfg。ArmArchitecture =“armv8”;

将深度学习配置对象附加到代码生成配置对象

设置DeepLearningConfig属性将代码生成配置对象设置为深度学习配置对象。

cfg。DeepLearningConfig = dlcfg;

使用生成c++源代码codegen

codegen配置cfglogonet_predictarg游戏{ones(227,227,3, 'single')}- darm_compute

代码在arm_compute在主机上的当前工作文件夹中的。

生成Zip文件packNGo函数

packNGo函数将所有相关文件打包成压缩的zip文件。

zipFileName = ' arm_computer .zip';bInfo = load(fullfile('arm_compute','buildInfo.mat'));packNGo (bInfo。buildInfo, {'fileName', zipFileName,'minimalHeaders', false, 'ignoreFileMissing',true});

将生成的Zip文件复制到目标硬件

复制Zip文件并解压缩到一个文件夹中。从目标硬件中删除Zip文件。

在以下命令中替换:

  • 密码输入你的密码

  • 用户名输入用户名

  • targetname与您的设备的名称

  • targetloc与文件的目标文件夹

运行这些命令从Linux中复制并解压缩zip文件。

如果isunix、系统(['sshpass -p password SCP -r 'zipFileName fullfile (pwd)“username@targetname: targetloc /”]),结束如果isunix系统('sshpass -p password SSH username@targetname "if [-d targetloc/arm_compute];然后rm -rf targetloc/arm_compute;fi”),结束如果isunix、系统(['sshpass -p password SSH username@targetname "unzip targetloc/ "zipFileName' -d targetloc/arm_compute ' ']),结束如果isunix、系统(['sshpass -p password SSH username@targetname "rm -rf targetloc "zipFileName“””]),结束

运行这些命令从Windows中复制并解压缩zip文件。

如果ispc、系统(['pscp.exe -pw password -r 'zipFileName fullfile (pwd)“username@targetname: targetloc /”]),结束如果ispc系统('plink.exe -l username -pw password targetname "if [-d targetloc/arm_compute];然后rm -rf targetloc/arm_compute;fi”),结束如果ispc、系统(['plink.exe -l username -pw password targetname "unzip targetloc/'zipFileName' -d targetloc/arm_compute ' ']),结束如果ispc、系统(['plink.exe -l username -pw password targetname "rm -rf targetloc "zipFileName“””]),结束

将示例文件复制到目标硬件

将这些支持文件从主机复金宝app制到目标硬件:

  • 输入图像,coderdemo_google.png

  • 生成库的Makefile;logonet_predict_rtw.mk

  • 用于构建可执行程序的Makefile;makefile_arm_logo.mk

  • 同义词集词典,synsetWordsLogoDet.txt

在以下命令中替换:

  • 密码输入你的密码

  • 用户名输入用户名

  • targetname与您的设备的名称

  • targetloc与文件的目标文件夹

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

如果isunix系统('sshpass -p password SCP logonet_predict_rtw. '可username@targetname: targetloc / arm_compute /),结束如果isunix系统('sshpass -p password SCP coderdemo_google.png username@targetname:targetloc/arm_compute/'),结束如果isunix系统(’sshpass -p password SCP makefile_arm_logo。可username@targetname: targetloc / arm_compute /),结束如果isunix系统('sshpass -p password scp synsetwordslogode .txt username@targetname:targetloc/arm_compute/'),结束

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

如果ispc系统(pscp.exe -pw password logonet_predict_rtw. exe可username@targetname: targetloc / arm_compute /),结束如果ispc系统('pscp.exe -pw password coderdemo_google.png username@targetname:targetloc/arm_compute/'),结束如果ispc系统(pscp.exe -pw password makefile_arm_logo. exe可username@targetname: targetloc / arm_compute /),结束如果ispc系统('pscp.exe -pw password synsetwordslogode .txt username@targetname:targetloc/arm_compute/'),结束

在目标硬件上构建库

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

确保在目标硬件上设置了环境变量ARM_COMPUTELIB和LD_LIBRARY_PATH。看到与MATLAB编码器深度学习的先决条件(MATLAB编码器)。在Makefile中使用ARM_ARCH变量来传递基于Arm架构的编译器标志。在Makefile中使用ARM_VER变量来编译基于Arm Compute Version的代码。与前一节类似,替换这些命令中的硬件凭据和路径。

执行以下步骤从Linux构建库。

如果isunix系统('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. '可ARM_ARCH = 'dlcfg。ArmArchitecture“ARM_VER = 'dlcfg。ArmComputeVersion“””]),结束

执行以下步骤从窗口构建库。

如果ispc系统('pscp.exe -pw密码main_arm_logo.cpp username@targetname:targetloc/arm_compute/'),结束如果ispc、系统(['plink.exe -l username -pw password targetname "make -C targetloc/arm_compute/ -f logonet_predict_rtw. exe可ARM_ARCH = 'dlcfg。ArmArchitecture“ARM_VER = 'dlcfg。ArmComputeVersion“””]),结束

从目标硬件上的库创建可执行文件

使用源主包装器文件构建库以创建可执行文件。main_arm_logo.cpp是c++主包装器文件,它调用logonet_predict函数。

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

如果isunix系统('sshpass -p password ssh username@targetname "make -C targetloc/arm_compute/ -f makefile_arm_logo。可targetDirName = targetloc / arm_compute”),结束

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

如果ispc系统('plink.exe -l username -pw password targetname "make -C targetloc/arm_compute/ -f makefile_arm_logo. exe可targetDirName = targetloc / arm_compute”),结束

在目标硬件上运行可执行文件

使用下面的命令从Linux运行可执行文件。
如果isunix系统('sshpass -p password SSH username@targetname "cd targetloc/arm_compute/;。/ logonet coderdemo_google.png”),结束
使用下面的命令从Windows运行可执行文件。
如果ispc系统('plink.exe -l username -pw password targetname ";。/ logonet coderdemo_google.png”),结束
5大预言 : ----------------------------- 谷歌99.992% 0.003%电晕0.003%啤酒0.001%埃索0.000%的联邦快递