主要内容

基于ARM计算的深度学习预测codegen

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

先决条件

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

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

  • ARM Compute和OpenCV库的环境变量

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

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

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

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

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

获得预训练的系列网络

下载预培训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计算库文件夹的路径。

看到用MATLAB编码器进行深度学习的先决条件

logonet_predict函数

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

类型logonet_predict
function out = logonet_predict(in) %#codegen % Copyright 2017-2020 The MathWorks, Inc. persistent logonet;if isempty(logonet) logonet = code . loaddeeplearningnetwork (' logonet .mat','logonet');End out = logonet.predict(in);结束

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

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

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

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

设置深度学习代码生成配置对象

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

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_compute.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、系统([密码:SCP -rzipFileName fullfile (pwd)“username@targetname: targetloc /”]),结束如果isunix系统('sshpass -p password SSH username@targetname "if [-d targetloc/arm_compute];rm -rf targetloc/arm_compute;fi”),结束如果isunix、系统([' SSH username@targetname "unzip targetloc/'zipFileName-d targetloc/arm_compute"']),结束如果isunix、系统([' SSH username@targetname "rm -rf targetloc'zipFileName“””]),结束

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

如果ispc、系统(['pscp.exe -pw密码-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 synsetwordslogodt .txt username@targetname:targetloc/arm_compute/'),结束

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

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

在目标硬件上构建库

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

确保在目标硬件上设置了环境变量ARM_COMPUTELIB和LD_LIBRARY_PATH。看到用MATLAB编码器进行深度学习的先决条件.ARM_ARCH变量在Makefile中用于传递基于Arm架构的编译器标志。ARM_VER变量在Makefile中用于编译基于Arm计算版本的代码。替换这些命令中的硬件凭证和路径,类似于前一节。

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

如果isunix系统(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 -l username -pw password targetname "可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 -l username -pw password可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 "cd targetloc/arm_compute/;。/ logonet coderdemo_google.png”),结束
5大预言 : ----------------------------- 谷歌99.992% 0.003%电晕0.003%啤酒0.001%埃索0.000%的联邦快递

另请参阅

||

相关的话题