概览
为设备端机器学习 (ODML) 应用部署模型时,请务必注意移动设备上的可用内存有限。模型二进制文件大小与模型中使用的操作数量密切相关。借助 TensorFlow Lite,您可以使用选择性 build 来缩减模型二进制文件的大小。选择性构建会跳过模型集中未使用的操作,并生成一个紧凑的库,其中仅包含模型在移动设备上运行所需的运行时和操作内核。
选择性构建适用于以下三个操作库。
下表显示了选择性 build 对一些常见用例的影响:
模型名称 | 网域 | 目标架构 | AAR 文件大小 |
---|---|---|---|
Mobilenet_1.0_224(float) | 图像分类 | armeabi-v7a | tensorflow-lite.aar(296635 字节) |
arm64-v8a | tensorflow-lite.aar(382892 字节) | ||
SPICE | 音高提取 | armeabi-v7a | tensorflow-lite.aar(375813 字节) tensorflow-lite-select-tf-ops.aar(1676380 字节) |
arm64-v8a | tensorflow-lite.aar(421826 字节) tensorflow-lite-select-tf-ops.aar(2298630 字节) |
||
i3d-kinetics-400 | 视频分类 | armeabi-v7a | tensorflow-lite.aar(240085 字节) tensorflow-lite-select-tf-ops.aar(1708597 字节) |
arm64-v8a | tensorflow-lite.aar(273713 字节) tensorflow-lite-select-tf-ops.aar(2339697 字节) |
有选择地使用 Bazel 构建 TensorFlow Lite
本部分假定您已下载 TensorFlow 源代码,并已针对 Bazel 设置本地开发环境。
为 Android 项目构建 AAR 文件
您可以通过提供模型文件路径来构建自定义 TensorFlow Lite AAR,如下所示。
sh tensorflow/lite/tools/build_aar.sh \
--input_models=/a/b/model_one.tflite,/c/d/model_two.tflite \
--target_archs=x86,x86_64,arm64-v8a,armeabi-v7a
上述命令将为 TensorFlow Lite 内置操作和自定义操作生成 AAR 文件 bazel-bin/tmp/tensorflow-lite.aar
;如果您的模型包含“选择 TensorFlow 操作”,则该命令还可选择生成 aar 文件 bazel-bin/tmp/tensorflow-lite-select-tf-ops.aar
。请注意,这会构建一个具有几种不同架构的“胖”AAR;如果您不需要所有架构,请使用适合部署环境的子集。
使用自定义操作构建
如果您开发了包含自定义操作的 Tensorflow Lite 模型,则可以通过在构建命令中添加以下标志来构建这些模型:
sh tensorflow/lite/tools/build_aar.sh \
--input_models=/a/b/model_one.tflite,/c/d/model_two.tflite \
--target_archs=x86,x86_64,arm64-v8a,armeabi-v7a \
--tflite_custom_ops_srcs=/e/f/file1.cc,/g/h/file2.h \
--tflite_custom_ops_deps=dep1,dep2
tflite_custom_ops_srcs
标志包含自定义操作的源文件,tflite_custom_ops_deps
标志包含用于构建这些源文件的依赖项。请注意,这些依赖项必须存在于 TensorFlow 代码库中。
高级用法:自定义 Bazel 规则
如果您的项目使用的是 Bazel,并且您希望为一组给定的模型定义自定义 TFLite 依赖项,则可以在项目代码库中定义以下规则:
仅适用于具有内置 op 的模型:
load(
"@org_tensorflow//tensorflow/lite:build_def.bzl",
"tflite_custom_android_library",
"tflite_custom_c_library",
"tflite_custom_cc_library",
)
# A selectively built TFLite Android library.
tflite_custom_android_library(
name = "selectively_built_android_lib",
models = [
":model_one.tflite",
":model_two.tflite",
],
)
# A selectively built TFLite C library.
tflite_custom_c_library(
name = "selectively_built_c_lib",
models = [
":model_one.tflite",
":model_two.tflite",
],
)
# A selectively built TFLite C++ library.
tflite_custom_cc_library(
name = "selectively_built_cc_lib",
models = [
":model_one.tflite",
":model_two.tflite",
],
)
对于具有选择 TF 操作的模型:
load(
"@org_tensorflow//tensorflow/lite/delegates/flex:build_def.bzl",
"tflite_flex_android_library",
"tflite_flex_cc_library",
)
# A Select TF ops enabled selectively built TFLite Android library.
tflite_flex_android_library(
name = "selective_built_tflite_flex_android_lib",
models = [
":model_one.tflite",
":model_two.tflite",
],
)
# A Select TF ops enabled selectively built TFLite C++ library.
tflite_flex_cc_library(
name = "selective_built_tflite_flex_cc_lib",
models = [
":model_one.tflite",
":model_two.tflite",
],
)
高级用途:构建自定义 C/C++ 共享库
如果您希望针对给定模型构建自己的自定义 TFLite C/C++ 共享对象,可以按照以下步骤操作:
在 TensorFlow 源代码的根目录下运行以下命令,创建临时 BUILD 文件:
mkdir -p tmp && touch tmp/BUILD
构建自定义 C 共享对象
如果您想构建自定义 TFLite C 共享对象,请将以下内容添加到 tmp/BUILD
文件中:
load(
"//tensorflow/lite:build_def.bzl",
"tflite_custom_c_library",
"tflite_cc_shared_object",
)
tflite_custom_c_library(
name = "selectively_built_c_lib",
models = [
":model_one.tflite",
":model_two.tflite",
],
)
# Generates a platform-specific shared library containing the TensorFlow Lite C
# API implementation as define in `c_api.h`. The exact output library name
# is platform dependent:
# - Linux/Android: `libtensorflowlite_c.so`
# - Mac: `libtensorflowlite_c.dylib`
# - Windows: `tensorflowlite_c.dll`
tflite_cc_shared_object(
name = "tensorflowlite_c",
linkopts = select({
"//tensorflow:ios": [
"-Wl,-exported_symbols_list,$(location //tensorflow/lite/c:exported_symbols.lds)",
],
"//tensorflow:macos": [
"-Wl,-exported_symbols_list,$(location //tensorflow/lite/c:exported_symbols.lds)",
],
"//tensorflow:windows": [],
"//conditions:default": [
"-z defs",
"-Wl,--version-script,$(location //tensorflow/lite/c:version_script.lds)",
],
}),
per_os_targets = True,
deps = [
":selectively_built_c_lib",
"//tensorflow/lite/c:exported_symbols.lds",
"//tensorflow/lite/c:version_script.lds",
],
)
新添加的目标可以按如下方式构建:
bazel build -c opt --cxxopt=--std=c++17 \
//tmp:tensorflowlite_c
对于 Android(针对 64 位,请将 android_arm
替换为 android_arm64
):
bazel build -c opt --cxxopt=--std=c++17 --config=android_arm \
//tmp:tensorflowlite_c
构建自定义 C++ 共享对象
如果您想构建自定义 TFLite C++ 共享对象,请将以下内容添加到 tmp/BUILD
文件中:
load(
"//tensorflow/lite:build_def.bzl",
"tflite_custom_cc_library",
"tflite_cc_shared_object",
)
tflite_custom_cc_library(
name = "selectively_built_cc_lib",
models = [
":model_one.tflite",
":model_two.tflite",
],
)
# Shared lib target for convenience, pulls in the core runtime and builtin ops.
# Note: This target is not yet finalized, and the exact set of exported (C/C++)
# APIs is subject to change. The output library name is platform dependent:
# - Linux/Android: `libtensorflowlite.so`
# - Mac: `libtensorflowlite.dylib`
# - Windows: `tensorflowlite.dll`
tflite_cc_shared_object(
name = "tensorflowlite",
# Until we have more granular symbol export for the C++ API on Windows,
# export all symbols.
features = ["windows_export_all_symbols"],
linkopts = select({
"//tensorflow:macos": [
"-Wl,-exported_symbols_list,$(location //tensorflow/lite:tflite_exported_symbols.lds)",
],
"//tensorflow:windows": [],
"//conditions:default": [
"-Wl,-z,defs",
"-Wl,--version-script,$(location //tensorflow/lite:tflite_version_script.lds)",
],
}),
per_os_targets = True,
deps = [
":selectively_built_cc_lib",
"//tensorflow/lite:tflite_exported_symbols.lds",
"//tensorflow/lite:tflite_version_script.lds",
],
)
新添加的目标可以按如下方式构建:
bazel build -c opt --cxxopt=--std=c++17 \
//tmp:tensorflowlite
对于 Android(针对 64 位,请将 android_arm
替换为 android_arm64
):
bazel build -c opt --cxxopt=--std=c++17 --config=android_arm \
//tmp:tensorflowlite
对于具有选择 TF 操作的模型,您还需要构建以下共享库:
load(
"@org_tensorflow//tensorflow/lite/delegates/flex:build_def.bzl",
"tflite_flex_shared_library"
)
# Shared lib target for convenience, pulls in the standard set of TensorFlow
# ops and kernels. The output library name is platform dependent:
# - Linux/Android: `libtensorflowlite_flex.so`
# - Mac: `libtensorflowlite_flex.dylib`
# - Windows: `libtensorflowlite_flex.dll`
tflite_flex_shared_library(
name = "tensorflowlite_flex",
models = [
":model_one.tflite",
":model_two.tflite",
],
)
新添加的目标可以按如下方式构建:
bazel build -c opt --cxxopt='--std=c++17' \
--config=monolithic \
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
//tmp:tensorflowlite_flex
对于 Android(针对 64 位,请将 android_arm
替换为 android_arm64
):
bazel build -c opt --cxxopt='--std=c++17' \
--config=android_arm \
--config=monolithic \
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
//tmp:tensorflowlite_flex
选择性地构建带有 Docker 的 TensorFlow Lite
本部分假定您已在本地机器上安装 Docker,并在此处下载了 TensorFlow Lite Dockerfile。
下载上述 Dockerfile 后,您可以通过运行以下命令来构建 Docker 映像:
docker build . -t tflite-builder -f tflite-android.Dockerfile
为 Android 项目构建 AAR 文件
运行以下命令,下载用于使用 Docker 进行构建的脚本:
curl -o build_aar_with_docker.sh \
https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/lite/tools/build_aar_with_docker.sh &&
chmod +x build_aar_with_docker.sh
然后,您可以通过按如下方式提供模型文件路径来构建自定义 TensorFlow Lite AAR。
sh build_aar_with_docker.sh \
--input_models=/a/b/model_one.tflite,/c/d/model_two.tflite \
--target_archs=x86,x86_64,arm64-v8a,armeabi-v7a \
--checkpoint=master \
[--cache_dir=<path to cache directory>]
checkpoint
标志是您在构建库之前要检出的 TensorFlow 代码库的提交内容、分支或标记;默认情况下,它是最新版本分支。上述命令将为 TensorFlow Lite 的内置和自定义操作生成 AAR 文件 tensorflow-lite.aar
,并(可选)为当前目录中的 Select TensorFlow 操作生成 AAR 文件 tensorflow-lite-select-tf-ops.aar
。
--cache_dir 用于指定缓存目录。如果未提供,该脚本将在当前工作目录下创建一个名为 bazel-build-cache
的目录以进行缓存。
将 AAR 文件添加到项目
您可以通过以下方法添加 AAR 文件:直接将 AAR 导入项目,或将自定义 AAR 发布到本地 Maven 制品库。请注意,如果您要生成 tensorflow-lite-select-tf-ops.aar
,也必须为其添加 AAR 文件。
适用于 iOS 的选择性 build
请参阅“本地构建”部分以设置构建环境和配置 TensorFlow 工作区,然后按照指南针对 iOS 使用选择性构建脚本。