骁龙神经处理引擎SDK参考指南(18)
6.4.3 运行 Word-RNN 模型
概述
本教程中的示例 C++ 应用程序称为snpe-net-run。它是一个命令行可执行文件,使用 SNPE SDK API 执行神经网络。
snpe-net-run 所需的参数是:
-
DLC文件格式的神经网络模型
-
包含输入数据路径的输入列表文件。
snpe-net-run 的可选参数是: -
选择 GPU 或 DSP 运行时(默认为 CPU)
-
输出目录(默认为./output)
-
显示帮助说明
snpe-net-run 使用对输入数据执行神经网络的结果创建并填充输出目录。
SNPE SDK 提供snpe-net-run的Linux 和 Android 二进制文件
- $SNPE_ROOT/bin/x86_64-linux-clang
- $SNPE_ROOT/bin/arm-android-clang6.0
- $SNPE_ROOT/bin/aarch64-android-clang6.0
- $SNPE_ROOT/bin/aarch64-oe-linux-gcc6.4
- $SNPE_ROOT/bin/arm-oe-linux-gcc6.4hf
介绍
递归神经网络 (RNN) 架构广泛用于机器学习应用程序中,用于处理顺序输入数据。本章将展示一个简单的 Word-RNN 示例,用于使用长短期记忆 (LSTM) 预测嵌入中的下一个单词。分步示例将使用 SNPE 创建、训练、转换和执行 Word-RNN 模型。
本示例需要的外部 python3 包是:
- numpy
- pandas
- sklearn
- tensorflow (1.6 or 1.11)
$SNPE_ROOT/models/word_rnn 文件夹下有六个文件
- inference.py
- input_list.txt
- belling_the_cat.txt
- word_rnn.py
- word_rnn_adb.sh
- NOTICE.txt
word_rnn.py python3 脚本创建并训练具有一个 LSTM 层的 RNN 模型。RNN训练完成后,会生成对应的frozen protobuf文件。
inference.py提示用户输入几个单词,此时会循环调用snpe-net-run生成后续单词。
先决条件
- SNPE SDK 已按照SNPE 设置章节进行设置。
- 教程设置已完成。
- TensorFlow 已安装(请参阅TensorFlow 设置)
创建、训练和转换 Word-RNN 模型
运行 word_rnn.py 以创建和训练 Word-RNN 模型。
cd $SNPE_ROOT/models/word_rnn
python3 word_rnn.py
终端将显示以下消息。
Training will be logged in word_rnn_log.
Load training file belling_the_cat.txt.
Embedding created.
Iter= 1000
Iter= 2000
Iter= 3000
Iter= 4000
Iter= 5000
Optimization done.
Save frozen graph in word_rnn.pb.
然后,使用 snpe-tensorflow-to-dlc 转换冻结图模型。
snpe-tensorflow-to-dlc --input_network word_rnn.pb \
--input_dim Placeholder "1, 4, 1" \
--out_node "rnn/basic_lstm_cell/Mul_11" \
--output_path word_rnn.dlc
dlc转换后,我们可以通过snpe-dlc-info和snpe-dlc-viewer查看转换后的dlc架构,如下:
snpe-dlc-info -i word_rnn.dlc
输出将是:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Id | Name | Type | Inputs | Outputs | Out Dims | Runtimes | Parameters |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 0 | Placeholder:0 | data | Placeholder:0 | Placeholder:0 | 1x4x1 | A D G C | input_preprocessing: passthrough |
| | | | | | | | input_type: default |
| 1 | Reshape:0 | reshape | Placeholder:0 | Reshape:0 | 1x4 | A D G C | |
| 2 | rnn/basic_lstm_cell/Mul_2_reshape | reshape | Reshape:0 | rnn/basic_lstm_cell/Mul_2_reshape | 1x4x1 | A D G C | |
| 3 | rnn/basic_lstm_cell/Mul_11:0 | lstm | rnn/basic_lstm_cell/Mul_2_reshape | rnn/basic_lstm_cell/Mul_11:0_all_time_steps | 1x4x112 | A C | x weights: (1, 448) |
| | | | | | | | h weights: (112, 448) |
| | | | | | | | biases: (448,) |
| | | | | | | | backward: False |
| | | | | | | | param count: 51k (100%) |
| | | | | | | | MACs per inference: 203k (100%) |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
snpe-dlc-viewer -i word_rnn.dlc
输出网络模型 HTML 文件将保存在 /tmp/word_rnn.html。
在 Linux 主机上运行
转到模型的基本位置并运行 python3 脚本,包括snpe-net-run
python3 inference.py
运行inference.py后,您将看到词嵌入键列表和用户输入提示,如下所示:
Load training file belling_the_cat.txt.
Embedding created.
Use host cpu.
Display word embedding keys:
dict_keys(['long', 'is', 'up', 'it', 'i', 'chief', 'our', 'procured', 'her', 'in', 'mouse', 'council', 'treacherous', 'meet', 'manner', 'approaches', 'with', 'propose', 'which', 'consider', 'thought', 'know', 'bell', 'signal', 'always', 'by', 'small', 'old', 'could', 'about', 'neck', 'of', 'approach', 'well', 'easy', 'take', 'all', 'outwit', 'met', 'they', 'this', 'who', 'cat', 'what', '.', 'will', 'attached', 'their', 'when', 'receive', 'agree', 'applause', 'and', 'if', 'now', 'to', 'a', 'round', 'enemy', 'was', 'ribbon', 'us', 'had', 'general', 'ago', 'means', 'last', 'venture', 'got', 'sly', 'measures', 'young', 'she', 'very', 'impossible', 'therefore', 'we', 'should', 'one', 'mice', 'case', '?', 'make', 'nobody', 'he', 'that', 'consists', 'spoke', 'from', 'easily', 'at', 'neighbourhood', 'the', 'looked', 'then', 'until', 'an', 'common', 'but', 'be', 'would', 'danger', 'retire', 'proposal', 'another', 'you', ',', 'while', 'escape', 'some', 'remedies', 'said'])
Please input 4 words:
用户可以输入嵌入的单词并查看结果。例如:long ago , the
...
...
-------------------------------------------------------------------------------
Model String: N/A
SNPE vX.Y.Z.dev
-------------------------------------------------------------------------------
Processing DNN input(s):
./input.raw
-------------------------------------------------------------------------------
Model String: N/A
SNPE vX.Y.Z.dev
-------------------------------------------------------------------------------
Processing DNN input(s):
./input.raw
Inference result: long ago , the said she a , , and the could could , , and the could could , , and the could could , , and the could could , , and the could
inference.py将多次调用snpe-net-run以使用经过训练的 LSTM模型生成后续单词。
二进制数据输入
请注意,Word-RNN 模型不接受纯文本文件作为输入。该模型期望其输入张量维度为1x4x1作为浮点数组。
inference.py中的create_embedding函数将解析、收集、编码和构建词嵌入。
然后,用户输入将被转换为1x4x1向量并发送到 LSTM 模型。之后 LSTM 输出也将转换为相应的嵌入词。
在 Android 目标上运行
选择目标架构
SNPE 为 armeabi-v7a 和 arm64-v8a 架构提供 Android 二进制文件。对于每个体系结构,二进制文件都是使用 clang6.0 使用 libc++ STL 实现编译的。下面显示了选择所需二进制文件的命令。
# architecture: armeabi-v7a - compiler: clang - STL: libc++
export SNPE_TARGET_ARCH=arm-android-clang6.0
export SNPE_TARGET_STL=libc++_shared.so
# architecture: arm64-v8a - compiler: clang - STL: libc++
export SNPE_TARGET_ARCH=aarch64-android-clang6.0
export SNPE_TARGET_STL=libc++_shared.so
为简单起见,本教程将目标二进制文件设置为 arm-android-clang6.0,它使用 libc++_shared.so 作为主机和目标上的命令。
将二进制文件推送到目标
将 SNPE 库和预构建的 snpe-net-run 可执行文件推送到 Android 目标上的 /data/local/tmp/snpeexample。
export SNPE_TARGET_ARCH=arm-android-clang6.0
export SNPE_TARGET_STL=libc++_shared.so
adb shell "mkdir -p /data/local/tmp/snpeexample/$SNPE_TARGET_ARCH/bin"
adb shell "mkdir -p /data/local/tmp/snpeexample/$SNPE_TARGET_ARCH/lib"
adb shell "mkdir -p /data/local/tmp/snpeexample/dsp/lib"
adb push $SNPE_ROOT/lib/$SNPE_TARGET_ARCH/$SNPE_TARGET_STL \
/data/local/tmp/snpeexample/$SNPE_TARGET_ARCH/lib
adb push $SNPE_ROOT/lib/$SNPE_TARGET_ARCH/*.so \
/data/local/tmp/snpeexample/$SNPE_TARGET_ARCH/lib
adb push $SNPE_ROOT/lib/dsp/*.so \
/data/local/tmp/snpeexample/dsp/lib
adb push $SNPE_ROOT/bin/$SNPE_TARGET_ARCH/snpe-net-run \
/data/local/tmp/snpeexample/$SNPE_TARGET_ARCH/bin
设置环境变量
在 adb shell 中设置库路径、路径变量和目标体系结构,以使用 -h 参数运行可执行文件以查看其描述。
adb shell
export SNPE_TARGET_ARCH=arm-android-clang6.0
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/data/local/tmp/snpeexample/$SNPE_TARGET_ARCH/lib
export PATH=$PATH:/data/local/tmp/snpeexample/$SNPE_TARGET_ARCH/bin
snpe-net-run -h
exit
将模型数据推送到 Android 目标
要在您的 Android 目标上执行 Word-RNN 模型,请按照以下步骤操作:
adb shell "mkdir -p /data/local/tmp/word_rnn"
adb push input_list.txt /data/local/tmp/word_rnn
adb push word_rnn.dlc /data/local/tmp/word_rnn
adb push word_rnn_adb.sh /data/local /tmp/word_rnn
注意:将 word_rnn dlc 文件推送到您的目标可能需要一些时间。
使用 CPU 运行时在 Android 上运行
使用以下命令运行 Android C++ 可执行文件:
python3 inference.py --use_cpu
我们将得到与在 Linux 主机上运行时相同的结果。