縮減 TensorFlow Lite 二進位檔大小

總覽

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

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

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

下表示範了選擇性版本對部分常見用途的影響:

模型名稱 網域 目標架構 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。請注意,這樣會建構具有多種不同架構的「笨重」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 依附元件,您可以在專案存放區中定義下列規則:

僅適用於含有內建作業的模型:

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",
    ],
)

針對包含 Select TF Ops 的模型:

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 版本 (將 android_arm 替換為 android_arm64,代表 64 位元):

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 版本 (將 android_arm 替換為 android_arm64,代表 64 位元):

bazel build -c opt --cxxopt=--std=c++17 --config=android_arm \
  //tmp:tensorflowlite

對於具有「Select 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 版本 (將 android_arm 替換為 android_arm64,代表 64 位元):

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 發布至本機 Maven 存放區,即可新增 AAR 檔案。請注意,如果您產生了 tensorflow-lite-select-tf-ops.aar 的 AAR 檔案,也必須新增該檔案。

iOS 適用的選擇性版本

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