主要内容

基于ARM目标的深度学习代码生成

本示例展示了如何在不使用硬件支持包的情况下,在基于ARM®的设备上生成和部署用于预测的代码。金宝app

当您使用ARM计算库和硬件支持包生成预测代码时,金宝appcodegen在主机上生成代码,将生成的文件复制到目标硬件,并在目标硬件上构建可执行文件。如果没有硬件支持包,金宝appcodegen在主机上生成代码。您必须运行命令来复制文件并在目标硬件上构建可执行程序。

本例使用packNGo函数将所有相关文件打包到压缩的zip文件中。通过本例了解如何在没有硬件支持包的ARM Neon目标上部署生成的代码金宝apppackNGo

先决条件

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

  • ARM计算库(在目标ARM硬件上)

  • 开源计算机视觉库(Open CV)

  • 编译器和库的环境变量

  • MATLAB®编码器™

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

  • 深度学习工具箱™

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

此示例不支持MATLAB Online。金宝app

squeezenet_predict函数

本例使用DAG网络SqueezeNet来显示ARM计算库中的图像分类。一个预先训练的SqueezeNet的MATLAB可在深度学习工具箱。的squeezenet_predict函数将SqueezeNet网络加载到一个持久化网络对象中。在对函数的后续调用中,将重用持久对象。

类型squeezenet_predict
MathWorks, Inc.函数out = squeezenet_predict(in) %#codegen %一个持久对象mynet用于加载DAG网络对象。在第一次调用此函数时,将构造持久对象并设置。在以后多次调用该函数时,将重用相同的对象%,以对输入调用predict,从而避免重构和重新加载% network对象。持久mynet;if isempty(mynet) mynet = code . loaddeeplearningnetwork ('squeezenet','squeezenet');End out = mynet.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配置cfgsqueezenet_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文件并解压到文件夹中,并删除硬件中的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制到目标硬件:

  • 输入图像,coffeemug.png

  • 生成库的Makefile,squeezenet_predict_rtw.mk

  • 构建可执行程序的Makefile,makefile_squeezenet_arm_generic.mk

  • 同义词集词典,synsetWords.txt

在以下命令中替换:

  • 密码用你的密码

  • 用户名用你的用户名

  • targetname您的设备名称

  • targetloc文件的目标文件夹

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

如果isunix系统('sshpass -p password SCP squeezenet_predict_rtw. '可username@targetname: targetloc / arm_compute /),结束如果isunix系统('sshpass -p password SCP coffemug .png username@targetname:targetloc/arm_compute/'),结束如果isunix系统('sshpass -p password SCP makefile_squeezenet_arm_generic. '可username@targetname: targetloc / arm_compute /),结束如果isunix系统(scp synsetWords.txt username@targetname:targetloc/arm_compute/),结束

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

如果ispc系统('pscp.exe -pw密码squeezenet_predict_rtw。可username@targetname: targetloc / arm_compute /),结束如果ispc系统('pscp.exe -pw密码coffeemug.png username@targetname:targetloc/arm_compute/'),结束如果ispc系统('pscp.exe -pw密码makefile_squeezenet_arm_generic。可username@targetname: targetloc / arm_compute /),结束如果ispc系统('pscp.exe -pw密码synsetWords.txt username@targetname:targetloc/arm_compute/'),结束

在目标硬件上构建库

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

确保在目标硬件上设置了环境变量ARM_COMPUTELIB和LD_LIBRARY_PATH。看到用MATLAB编码器进行深度学习的先决条件(MATLAB编码器).ARM_ARCH变量在Makefile中用于传递基于Arm架构的编译器标志。在Makefile中使用ARM_VER变量编译基于Arm计算版本的代码。按照类似上述步骤的方法替换硬件凭证和路径。

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

如果isunix系统(SCP main_squeezenet_arm_generic.cpp username@targetname:targetloc/arm_compute/'),结束如果isunix、系统(['sshpass -p password ssh username@targetname "make -C targetloc/arm_compute/ -f squeezenet_predict_rtw。可ARM_ARCH = 'dlcfg。ArmArchitecture“ARM_VER = 'dlcfg。ArmComputeVersion“””]),结束

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

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

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

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

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

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

运行下面的命令从Windows中创建可执行文件。

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

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

使用以下命令从Linux运行可执行文件。
如果isunix系统('sshpass -p password SSH username@targetname "cd targetloc/arm_compute/;。/ squeezenet coffeemug.png”),结束
使用以下命令从Windows运行可执行文件。
如果ispc系统('plink.exe -l username -pw password targetname "cd targetloc/arm_compute/;。/ squeezenet coffeemug.png”),结束
5大预言 : ----------------------------- 纸巾蜡烛杯咖啡杯7.309% 1.098% 88.299% 0.634% 0.591%水壶