TensorFlow Lite (TFLite)

支持的芯片

概述

TFLite,或称 LiteRT,是 Google 的设备端框架,可在边缘平台上高效部署高性能 ML 和 GenAI,并使用高效的转换、运行时和设备端机器学习优化。

Ameba-tflite 是针对 Realtek Ameba 系列芯片编译的 TFLite 库。

代码仓库与说明

代码仓库

Ameba-tflite 代码托管于 GitHub:ameba-tflite

需配合 Realtek 的 Linux SDK 使用。

代码结构

代码仓库的目录结构如下:

目录

说明

inc

Ameba-tflite 库的头文件

lib

Ameba-tflite 适配 Ameba SoC 的预编译库

examples

示例应用程序,演示使用方法

bin

用于测试和调试的预编译工具

build

预编译库的编译脚本

patch

补丁文件,包含对 TensorFlow Lite 官方源代码的修改

License

License 信息: Apache License 2.0

Makefile

Ameba Linux SDK 下编译示例程序的 Makefile

README.md

文档和教程

version.txt

版本信息

备注

Ameba-tflite 库是使用 build 目录中的脚本,基于 version.txt 中版本的 TensorFlow Lite 源代码,并应用 patch 目录中的补丁进行编译的。 Ameba-tflite 不包含 TensorFlow Lite 的源代码。有关源代码详情,请参阅 Tensorflow 官方仓库

下载方式

  • 自动拉取:参照 Linux SDK 下载 拉取 Linux SDK,将自动包含 ameba-tflite 子模块。

  • 手动拉取:单独克隆 ameba-tflite,并放置于 SDK 指定路径。

芯片

内核

SDK

TFLite 路径

RTL8730E

CA32

ameba-linux

{SDK}/sources/development/tflite

示例教程

TensorFlow Lite C++ 最简示例

本示例展示如何编译一个简单的 TensorFlow Lite 应用程序 Minimal。

示例代码: {SDK}/sources/development/tflite/examples/minimal

编译示例可执行文件

首先修改 {SDK}/sources/development/tflite/Makefile 中的 EXAMPLE_NAMEminimal

然后执行以下命令进行编译:

cd {SDK}/
. ./envsetup.sh
bitbake rtk-tflite-algo

编译完成后,可执行文件位于 build_rtl8730elh-va7-full/tmp/work/rtl8730elh_va7-rtk-linux-gnueabi/rtk-tflite-algo/1.0/image/bin/rtk_tflite_algo

运行示例可执行文件

使用 ADB 将可执行文件 minimal 和你的 model.tflite 推送到设备上,然后运行:

./minimal model.tflite

TensorFlow Lite C++ 图像分类示例

本示例展示如何使用 TensorFlow Lite 进行图像分类。

示例代码位于 {SDK}/sources/development/tflite/examples/label_image

编译示例可执行文件

首先修改 {SDK}/sources/development/tflite/Makefile 中的 EXAMPLE_NAMElabel_image

然后执行以下命令进行编译:

cd {SDK}/
. ./envsetup.sh
bitbake rtk-tflite-algo

编译完成后,可执行文件位于 build_rtl8730elh-va7-full/tmp/work/rtl8730elh_va7-rtk-linux-gnueabi/rtk-tflite-algo/1.0/image/bin/rtk_tflite_algo

下载示例模型和标签文件

你可以使用任何兼容的模型,但下面的 MobileNet v1 模型是一个经过训练,可以识别 1000 种不同物体的好例子。

# Get model
curl https://storage.googleapis.com/download.tensorflow.org/models/mobilenet_v1_2018_02_22/mobilenet_v1_1.0_224.tgz | tar xzv -C ./tmp

# Get labels
curl https://storage.googleapis.com/download.tensorflow.org/models/mobilenet_v1_1.0_224_frozen.tgz  | tar xzv -C ./tmp  mobilenet_v1_1.0_224/labels.txt

mv ./tmp/mobilenet_v1_1.0_224/labels.txt ./tmp/

运行示例可执行文件

使用 ADB 将可执行文件 label_image、模型文件 mobilenet_v1_1.0_224.tflite、测试图片 grace_hopper.bmp 和标签文件 labels.txt 推送到设备上:

adb push build_rtl8730elh-va7-full/tmp/work/rtl8730elh_va7-rtk-linux-gnueabi/rtk-tflite-algo/1.0/image/bin/rtk_tflite_algo  /data/local/tmp
adb push tmp/mobilenet_v1_1.0_224.tflite  /data/local/tmp
adb push examples/label_image/testdata/grace_hopper.bmp  /data/local/tmp
adb push tmp/labels.txt /data/local/tmp

然后在设备上运行以下命令:

/data/local/tmp/label_image \
 -m /data/local/tmp/mobilenet_v1_1.0_224.tflite \
 -i /data/local/tmp/grace_hopper.bmp \
 -l /data/local/tmp/labels.txt

你将会看到类似如下的输出结果:

Loaded model /data/local/tmp/mobilenet_v1_1.0_224.tflite
resolved reporter
INFO: Initialized TensorFlow Lite runtime.
invoked
average time: 25.03 ms
0.907071: 653 military uniform
0.0372416: 907 Windsor tie
0.00733753: 466 bulletproof vest
0.00592852: 458 bow tie
0.00414091: 514 cornet

使用 -h 或其他不支持的标志,label_image 将列出受支持的选项:

./label_image -h

label_image
--accelerated, -a: [0|1], use Android NNAPI or not
--old_accelerated, -d: [0|1], use old Android NNAPI delegate or not
--allow_fp16, -f: [0|1], allow running fp32 models with fp16 or not
--count, -c: loop interpreter->Invoke() for certain times
--gl_backend, -g: [0|1]: use GPU Delegate on Android
--hexagon_delegate, -j: [0|1]: use Hexagon Delegate on Android
--input_mean, -b: input mean
--input_std, -s: input standard deviation
--image, -i: image_name.bmp
--labels, -l: labels for the model
--tflite_model, -m: model_name.tflite
--profiling, -p: [0|1], profiling or not
--num_results, -r: number of results to show
--threads, -t: number of threads
--verbose, -v: [0|1] print more information
--warmup_runs, -w: number of warmup runs
--xnnpack_delegate, -x [0:1]: xnnpack delegate

label_image.cc 源代码中可以查看其他命令行选项。

备注

此示例不支持使用 XNNPACK delegate 或 Hexagon delegate 或 GPU delegate,因为它们未内置于预编译的 Ameba-tflite 库中。

工具

TFLite 模型基准测试工具

一个预编译的 C++ 二进制文件,用于在设备上对 TFLite 模型及其各个算子进行基准测试。 该二进制文件接受一个 TFLite 模型,生成随机输入,然后重复运行该模型指定的次数。 运行基准测试后,将报告汇总的延迟统计信息。

使用方法

使用 ADB 直接将可执行文件 benchmark_model 和你的模型文件推送到设备上。 然后在设备上运行以下命令:

./benchmark_model --graph=<your_model.tflite>

你将会看到类似如下的输出结果:

./benchmark_model --graph=conv_1024x1x1x1024_input1x1_int8.tflite

INFO: STARTING!
INFO: Log parameter values verbosely: [0]
INFO: Graph: [conv_1024x1x1x1024_input1x1_int8.tflite]
INFO: Signature to run: []
INFO: Loaded model conv_1024x1x1x1024_input1x1_int8.tflite
INFO: The input model file size (MB): 1.07843
INFO: Initialized session in 2.962ms.
INFO: Running benchmark for at least 1 iterations and at least 0.5 seconds but terminate if exceeding 150 seconds.
INFO: count=270 first=2147 curr=1839 min=1832 max=2147 avg=1849.66 std=27

INFO: Running benchmark for at least 50 iterations and at least 1 seconds but terminate if exceeding 150 seconds.
INFO: count=539 first=1861 curr=1835 min=1830 max=2137 avg=1848.98 std=25

INFO: Inference timings in us: Init: 2962, First inference: 2147, Warmup (avg): 1849.66, Inference (avg): 1848.98
INFO: Note: as the benchmark tool itself affects memory footprint, the following is only APPROXIMATE to the actual memory footprint of the model at runtime. Take the information at your discretion.
INFO: Memory footprint delta from the start of the tool (MB): init=2.875 overall=3.875

使用 --helpbenchmark 将列出受支持的选项:

Flags:
--num_runs=50                                        int32   optional        expected number of runs, see also min_secs, max_secs
--graph=                                             string  optional        graph file name
--input_layer=                                       string  optional        input layer names
--enable_op_profiling=false                          bool    optional        enable op profiling
...(更多选项)

备注

此示例不支持使用 XNNPACK delegate 或 Hexagon delegate 或 GPU delegate,因为它们未内置于预编译的 Ameba-tflite 库中。