縮減 TensorFlow Lite 二進位檔案大小

總覽

當部署用於裝置端機器學習 (ODML) 應用程式的模型時,務必注意行動裝置上可用的記憶體有限。模型二進位檔案大小與模型中使用的運算數量密切相關。TensorFlow Lite 可讓您透過選擇性建構來縮減模型二進位檔案大小。選擇性建構會略過模型集中未使用的運算,並產生精簡的程式庫,其中僅包含在行動裝置上執行模型所需的執行階段和運算核心。

選擇性建構適用於下列三個運算程式庫。

  1. TensorFlow Lite 內建運算程式庫
  2. TensorFlow Lite 自訂運算
  3. 選取 TensorFlow 運算程式庫

下表說明選擇性建構對一些常見用例的影響

模型名稱 領域 目標架構 AAR 檔案大小
Mobilenet_1.0_224(float) 圖片分類 armeabi-v7a tensorflow-lite.aar (296,635 位元組)
arm64-v8a tensorflow-lite.aar (382,892 位元組)
SPICE 聲音音調擷取 armeabi-v7a tensorflow-lite.aar (375,813 位元組)
tensorflow-lite-select-tf-ops.aar (1,676,380 位元組)
arm64-v8a tensorflow-lite.aar (421,826 位元組)
tensorflow-lite-select-tf-ops.aar (2,298,630 位元組)
i3d-kinetics-400 影片分類 armeabi-v7a tensorflow-lite.aar (240,085 位元組)
tensorflow-lite-select-tf-ops.aar (1,708,597 位元組)
arm64-v8a tensorflow-lite.aar (273,713 位元組)
tensorflow-lite-select-tf-ops.aar (2,339,697 位元組)

使用 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。請注意,這會建構包含多種不同架構的「fat」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 依附元件,您可以在專案存放區中定義下列規則

僅適用於具有內建運算的模型

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,並選擇性地為您目前目錄中的選取 TensorFlow 運算產生 AAR 檔案 tensorflow-lite-select-tf-ops.aar

--cache_dir 指定快取目錄。如果未提供,指令碼會在目前工作目錄下建立名為 bazel-build-cache 的目錄以進行快取。

將 AAR 檔案新增至專案

透過將 AAR 直接匯入您的專案,或透過將自訂 AAR 發布至您的本機 Maven 存放區來新增 AAR 檔案。請注意,如果您產生了 tensorflow-lite-select-tf-ops.aar,您也必須新增此檔案的 AAR 檔案。

iOS 的選擇性建構

請參閱「在本機建構」章節以設定建構環境並設定 TensorFlow 工作區,然後按照指南使用 iOS 的選擇性建構指令碼。