MobileNetV1
本文档讲述如何在 NPU 上运行 MobileNetV1。
参考 Model Zoo 下载获取示例。
MobileNetV1 示例目录结构:
$ tree ./
./
├── class_post.cpp
├── class_pre.cpp
├── CMakeLists.txt
├── convert_model
│ ├── config_yml.py
│ ├── convert_model_env.sh
│ └── inputs_outputs.txt
├── label.h
├── main.cpp
├── model
│ └── space_shuttle_224x224.jpg
└── README.md
模型转换
下载模型
点击下载 mobilenet_v1_1.0_224_frozen.pb 。
然后将模型移动到 convert_model/ 目录下。
创建转换脚本的软链接
cd convert_model/
./convert_model_env.sh
模型导入/量化/转换
需要先进入容器开发环境。可以参考 Model Zoo 下载中创建容器这一部分。
不同平台请使用对应的 Docker 镜像:
- A733:ubuntu-npu:v2.0.10.1
- T527:ubuntu-npu:v1.8.11
docker exec -it model-zoo /bin/bash
进入容器对应目录之后运行脚本。
cd /workspace/examples/mobilenetv1_tensorflow/convert_model/
./pegasus_import.sh mobilenet_v1_1.0_224_frozen
./pegasus_quantize.sh mobilenet_v1_1.0_224_frozen uint8 10
- A733
- T527
./pegasus_export_ovx_nbg.sh mobilenet_v1_1.0_224_frozen uint8 a733
./pegasus_export_ovx_nbg.sh mobilenet_v1_1.0_224_frozen uint8 t527
导出的模型文件存放在../model目录。
编译示例
接下来可以编译示例,先 exit 退出容器,然后执行下面的命令编译示例。
首先需要配置第三方库和交叉编译工具链。
如果你已经在其他示例中配置过第三方库和交叉编译工具链则可以跳过这一步。
cd ../../../3rdparty/opencv/
unzip opencv-4.9.0-aarch64-linux-sunxi-glibc.zip
cd ../../0-toolchains/
需要先手动点击链接下载之后放到 0-toolchains/ 再执行下面的命令:
tar -xvf gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu.tar.xz
cd ../examples/mobilenetv1_tensorflow/
- A733
- T527
../build_linux.sh -t a733 -s debian11
../build_linux.sh -t t527 -s debian11
模型部署
编译示例完成之后,示例会安装到 install 目录,可以使用 scp 传输到板端。
配置 NPU 驱动
如果你已经在其他示例中配置过 NPU 驱动则可以跳过这一步。
将驱动库 scp 传输到板端的 lib 目录。
- A733 对应 common/lib_linux_aarch64/A733 目录
- T527 对应 common/lib_linux_aarch64/T527 目录
然后执行下面的命令导出到环境变量。
echo 'export LD_LIBRARY_PATH=$HOME/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
运行示例
配置好驱动之后就可以运行示例了。
对于 T527 平台,你还需要参考 A5E 的板端启用 NPU文档先启用 NPU ,然后使用下面的命令增加当前用户使用 /dev/vipcore 的权限。
sudo chmod 777 /dev/vipcore
- A733
- T527
cd mobilenetv1_demo_linux_a733/
chmod +x ./mobilenetv1_demo_a733
./mobilenetv1_demo_a733 -nb model/mobilenet_v1_1.0_224_frozen_uint8_a733.nb -i model/space_shuttle_224x224.jpg
运行结果如下:
$ ./mobilenetv1_demo_a733 -nb model/mobilenet_v1_1.0_224_frozen_uint8_a733.nb -i model/space_shuttle_224x224.jpg
model_file=model/mobilenet_v1_1.0_224_frozen_uint8_a733.nb, input=model/space_shuttle_224x224.jpg, loop_count=1, malloc_mbyte=10
VIPLite driver software version 2.0.3.2-AW-2024-08-30
input 0 dim 3 224 224 1, data_format=2, quant_format=0, name=input/output[0], none-quant
output 0 dim 1001 1 0 0, data_format=0, name=uid_20000_sub_uid_1_out_0, none-quant
nbg name=model/mobilenet_v1_1.0_224_frozen_uint8_a733.nb, size: 3182272.
create network 0: 5661 us.
prepare network: 1085 us.
network: 0, loop count: 1
run time for this network 0: 1845 us.
class_postprocess.cpp run.
========== top5 ==========
class id: 813, prob: 0.995117, label: space shuttle
class id: 868, prob: 0.001483, label: trailer truck, tractor trailer, trucking rig, rig, articulated lorry, semi
class id: 405, prob: 0.000979, label: airliner
class id: 406, prob: 0.000563, label: airship, dirigible
class id: 628, prob: 0.000371, label: limousine, limo
destroy npu finished.
~NpuUint.
此性能数据仅计算模型推理的时间消耗。如无特别说明,不包含预处理和后处理的时间消耗。
| SoC | NPU | 模型 | 输入分辨率 | 网络创建耗时 | 网络准备耗时 | 单帧推理耗时 | 后处理耗时 | 总耗时 | 帧率 |
|---|---|---|---|---|---|---|---|---|---|
| 全志 A733 | Vivante VIP9000 | mobilenetv1 | 224×224 | 5.7 ms | 1.1 ms | 1.8 ms | 8.6 ms | 555.6 FPS |
cd mobilenetv1_demo_linux_t527/
chmod +x ./mobilenetv1_demo_t527
./mobilenetv1_demo_t527 -nb model/mobilenet_v1_1.0_224_frozen_uint8_t527.nb -i model/space_shuttle_224x224.jpg
运行结果如下:
$ ./mobilenetv1_demo_t527 -nb model/mobilenet_v1_1.0_224_frozen_uint8_t527.nb -i model/space_shuttle_224x224.jpg
model_file=model/mobilenet_v1_1.0_224_frozen_uint8_t527.nb, input=model/space_shuttle_224x224.jpg, loop_count=1, malloc_mbyte=10
VIPLite driver software version 1.13.0.0-AW-2023-10-19
input 0 dim 3 224 224 1, data_format=2, quant_format=0, name=input[0], none-quant
output 0 dim 1001 1 0 0, data_format=0, name=uid_20000_sub_uid_1_out_0, none-quant
nbg name=model/mobilenet_v1_1.0_224_frozen_uint8_t527.nb, size: 3097728.
create network 0: 5639 us.
prepare network: 980 us.
network: 0, loop count: 1
run time for this network 0: 3809 us.
class_postprocess.cpp run.
========== top5 ==========
class id: 813, prob: 0.993652, label: space shuttle
class id: 868, prob: 0.001479, label: trailer truck, tractor trailer, trucking rig, rig, articulated lorry, semi
class id: 405, prob: 0.000976, label: airliner
class id: 406, prob: 0.000561, label: airship, dirigible
class id: 628, prob: 0.000370, label: limousine, limo
destroy npu finished.
~NpuUint.
此性能数据仅计算模型推理的时间消耗。如无特别说明,不包含预处理和后处理的时间消耗。
| SoC | NPU | 模型 | 输入分辨率 | 网络创建耗时 | 网络准备耗时 | 单帧推理耗时 | 后处理耗时 | 总耗时 | 帧率 |
|---|---|---|---|---|---|---|---|---|---|
| 全志 T527 | Vivante VIP9000 | mobilenetv1 | 224×224 | 5.6 ms | 1.0 ms | 3.8 ms | 10.4 ms | 263.2 FPS |