主要内容

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

这个例子展示了如何在不使用硬件支持包的情况下在基于ARM®的设备上生成和部署用于预测的代码。万博1manbetx

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

此示例使用packNGo函数将所有相关文件打包到压缩的zip文件中。使用此示例学习如何将生成的代码部署到没有硬件支持包的ARM Neon目标上万博1manbetxpackNGo

先决条件

  • 支持NEON扩展的ARM处理器万博1manbetx

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

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

  • 编译器和库的环境变量

  • MATLAB®编码器™

  • 深度学习万博1manbetx的支持包MATLAB编码器接口

  • 深度学习工具箱™

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

MATLAB Online不支持此示例。万博1manbetx

squeezenet_predict函数

本例使用DAG网络SqueezeNet与ARM计算库一起显示图像分类。在深度学习工具箱中提供了一个预训练的MATLAB SqueezeNet。的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的设备而不使用硬件支持包的代码时,请为库创建一个配置对象。万博1manbetx不要为可执行程序创建配置对象。

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

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

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

创建一个编码器。ARMNEONConfig对象。指定目标ARM处理器的库版本和体系结构。例如,假设目标板是HiKey/Rock960板,具有ARMv8架构,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_computer .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、系统(['sshpass -p密码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密码-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 '解压缩targetloc/'zipFileName' -d targetloc/arm_compute ' ']),结束如果ispc、系统(['plink.exe -l username -pw password targetname ' rm -rf targetloc'zipFileName“””]),结束

复制示例文件到目标硬件

将这些支持文件从主机复万博1manbetx制到目标硬件:

  • 输入图像,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 coffeemug.png username@targetname:targetloc/arm_compute/'),结束如果isunix系统('sshpass -p password SCP makefile_squeezenet_arm_generic. '可username@targetname: targetloc / arm_compute /),结束如果isunix系统('sshpass -p password scp synsetWords.txt username@targetname:targetloc/arm_compute/'),结束

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

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

在目标硬件上构建库

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

确保在目标硬件上设置了环境变量ARM_COMPUTELIB和LD_LIBRARY_PATH。看到用MATLAB编码器进行深度学习的先决条件.ARM_ARCH变量在Makefile中用于传递基于Arm架构的编译器标志。在Makefile中使用ARM_VER变量编译基于Arm Compute Version的代码。按照上面的步骤替换硬件凭据和路径。

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

如果isunix系统('sshpass -p密码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. '可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. '可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%水壶

另请参阅

|||

相关的话题