ลดขนาดไบนารีของ TensorFlow Lite

ภาพรวม

เมื่อปรับใช้โมเดลสำหรับแอปพลิเคชัน Machine Learning (ODML) บนอุปกรณ์ สิ่งสำคัญคือต้องคำนึงถึงหน่วยความจำที่จำกัดที่มีอยู่ในอุปกรณ์เคลื่อนที่ ขนาดไบนารี่ของโมเดลมีความสัมพันธ์อย่างใกล้ชิดกับจำนวนการดำเนินการที่ใช้ในโมเดล TensorFlow Lite ช่วยให้คุณลดขนาดไบนารีของโมเดลได้โดยใช้บิวด์แบบเลือก บิลด์แบบเลือกข้ามการดำเนินการที่ไม่ได้ใช้ในชุดโมเดลของคุณ และสร้างไลบรารีขนาดกะทัดรัดที่มีเพียงรันไทม์และเคอร์เนล op ที่จำเป็นสำหรับโมเดลในการทำงานบนอุปกรณ์เคลื่อนที่ของคุณ

Selective build ใช้กับไลบรารีการดำเนินการสามไลบรารีต่อไปนี้

  1. ไลบรารี Ops ในตัวของ TensorFlow Lite
  2. ตัวเลือกที่กำหนดเองของ TensorFlow Lite
  3. เลือกไลบรารีปฏิบัติการ TensorFlow

ตารางด้านล่างแสดงให้เห็นถึงผลกระทบของบิวด์แบบเลือกสรรสำหรับกรณีการใช้งานทั่วไปบางกรณี:

ชื่อรุ่น โดเมน สถาปัตยกรรมเป้าหมาย ขนาดไฟล์ AAR
Mobilenet_1.0_224(ลอย) การจำแนกประเภทภาพ อาร์เมอาบี-v7a tensorflow-lite.aar (296,635 ไบต์)
อาร์ม64-v8a tensorflow-lite.aar (382,892 ไบต์)
เครื่องเทศ การสกัดระดับเสียง อาร์เมอาบี-v7a tensorflow-lite.aar (375,813 ไบต์)
tensorflow-lite-select-tf-ops.aar (1,676,380 ไบต์)
อาร์ม64-v8a tensorflow-lite.aar (421,826 ไบต์)
tensorflow-lite-select-tf-ops.aar (2,298,630 ไบต์)
i3d-จลนศาสตร์-400 การจัดหมวดหมู่วิดีโอ อาร์เมอาบี-v7a tensorflow-lite.aar (240,085 ไบต์)
tensorflow-lite-select-tf-ops.aar (1,708,597 ไบต์)
อาร์ม64-v8a tensorflow-lite.aar (273,713 ไบต์)
tensorflow-lite-select-tf-ops.aar (2,339,697 ไบต์)

คัดเลือกสร้าง TensorFlow Lite ด้วย Bazel

ส่วนนี้จะถือว่าคุณได้ดาวน์โหลดซอร์สโค้ด TensorFlow และ ตั้งค่าสภาพแวดล้อมการพัฒนาในเครื่อง เป็น Bazel

สร้างไฟล์ AAR สำหรับโครงการ Android

คุณสามารถสร้าง 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

คำสั่งด้านบนจะสร้างไฟล์ AAR bazel-bin/tmp/tensorflow-lite.aar สำหรับ TensorFlow Lite ในตัวและตัวเลือกที่กำหนดเอง และสร้างไฟล์ aar bazel-bin/tmp/tensorflow-lite-select-tf-ops.aar หากโมเดลของคุณมี Select TensorFlow ops โปรดทราบว่าสิ่งนี้จะสร้าง AAR "อ้วน" ด้วยสถาปัตยกรรมที่แตกต่างกันหลายประการ หากคุณไม่ต้องการทั้งหมด ให้ใช้เซ็ตย่อยที่เหมาะสมสำหรับสภาพแวดล้อมการปรับใช้ของคุณ

สร้างด้วยการดำเนินการที่กำหนดเอง

หากคุณได้พัฒนาโมเดล Tensorflow Lite ด้วยตัวเลือกแบบกำหนดเอง คุณสามารถสร้างโมเดลเหล่านั้นได้โดยเพิ่มแฟล็กต่อไปนี้ลงในคำสั่ง build:

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 มีไฟล์ต้นฉบับของ ops ที่คุณกำหนดเอง และแฟล็ก tflite_custom_ops_deps มีการพึ่งพาในการสร้างไฟล์ต้นฉบับเหล่านั้น โปรดทราบว่าการขึ้นต่อกันเหล่านี้ต้องมีอยู่ใน repo TensorFlow

การใช้งานขั้นสูง: กฎ Bazel ที่กำหนดเอง

หากโปรเจ็กต์ของคุณใช้ Bazel และคุณต้องการกำหนดการพึ่งพา TFLite แบบกำหนดเองสำหรับชุดรูปแบบที่กำหนด คุณสามารถกำหนดกฎต่อไปนี้ในที่เก็บโปรเจ็กต์ของคุณ:

สำหรับรุ่นที่มี ops ในตัวเท่านั้น:

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 :

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++ ที่คุณกำหนดเองสำหรับโมเดลที่กำหนด คุณสามารถทำตามขั้นตอนด้านล่าง:

สร้างไฟล์ BUILD ชั่วคราวโดยการรันคำสั่งต่อไปนี้ที่ไดเร็กทอรีรากของซอร์สโค้ด TensorFlow:

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

สร้าง TensorFlow Lite แบบคัดเลือกด้วย Docker

ส่วนนี้จะถือว่าคุณได้ติดตั้ง Docker บนเครื่องของคุณแล้ว และดาวน์โหลดไฟล์ TensorFlow Lite Dockerfile ที่นี่

หลังจากดาวน์โหลด Dockerfile ข้างต้นแล้ว คุณสามารถสร้างอิมเมจนักเทียบท่าได้โดยเรียกใช้:

docker build . -t tflite-builder -f tflite-android.Dockerfile

สร้างไฟล์ AAR สำหรับโครงการ Android

ดาวน์โหลดสคริปต์สำหรับการสร้างด้วย 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 คือคอมมิต สาขา หรือแท็กของ repo TensorFlow ที่คุณต้องการชำระเงินก่อนสร้างไลบรารี โดยค่าเริ่มต้นจะเป็นสาขารุ่นล่าสุด คำสั่งข้างต้นจะสร้างไฟล์ AAR tensorflow-lite.aar สำหรับ TensorFlow Lite ในตัวและตัวเลือกที่กำหนดเอง และอาจสร้างไฟล์ AAR tensorflow-lite-select-tf-ops.aar สำหรับ Select TensorFlow ops ในไดเรกทอรีปัจจุบันของคุณ

--cache_dir ระบุไดเร็กทอรีแคช หากไม่ได้ระบุไว้ สคริปต์จะสร้างไดเร็กทอรีชื่อ bazel-build-cache ภายใต้ไดเร็กทอรีการทำงานปัจจุบันสำหรับการแคช

เพิ่มไฟล์ AAR ในโครงการ

เพิ่มไฟล์ AAR โดย การนำเข้า AAR ไปยังโปรเจ็กต์ของคุณ โดยตรง หรือโดย การเผยแพร่ AAR แบบกำหนดเองไปยังที่เก็บ Maven ในเครื่องของคุณ โปรดทราบว่าคุณต้องเพิ่มไฟล์ AAR สำหรับ tensorflow-lite-select-tf-ops.aar ด้วยหากคุณสร้างมันขึ้นมา

บิลด์แบบเลือกสรรสำหรับ iOS

โปรดดู ส่วนการสร้างในเครื่อง เพื่อตั้งค่าสภาพแวดล้อมบิลด์และกำหนดค่าพื้นที่ทำงาน TensorFlow จากนั้นทำตาม คำแนะนำ เพื่อใช้สคริปต์บิลด์แบบเลือกสรรสำหรับ iOS