ขอขอบคุณที่เข้าร่วม Google I/O ดูเซสชั่นทั้งหมดตามความต้องการ ดูตามความต้องการ

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

ภาพรวม

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

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

  1. ห้องสมุด ops ในตัว TensorFlow Lite
  2. การดำเนินการที่กำหนดเองของ TensorFlow Lite
  3. เลือกไลบรารี TensorFlow ops

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

ชื่อรุ่น โดเมน สถาปัตยกรรมเป้าหมาย ขนาดไฟล์ AAR
Mobilenet_1.0_224(โฟลต) การจำแนกภาพ อาร์มีบี-v7a tensorflow-lite.aar (296,635 ไบต์)
arm64-v8a tensorflow-lite.aar (382,892 ไบต์)
เครื่องเทศ การแยกระดับเสียง อาร์มีบี-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-จลนพลศาสตร์-400 การจัดประเภทวิดีโอ อาร์มีบี-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 ไบต์)

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

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

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

คุณสามารถสร้าง TensorFlow Lite AARs ที่กำหนดเองได้โดยระบุเส้นทางไฟล์โมเดลของคุณดังต่อไปนี้

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

สร้างด้วย ops แบบกำหนดเอง

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

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

สร้างไฟล์ 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 ops คุณต้องสร้างไลบรารีที่ใช้ร่วมกันต่อไปนี้ด้วย:

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 บนเครื่องของคุณ และดาวน์โหลดไฟล์ Docker ของ TensorFlow Lite ที่นี่

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

Selective Build สำหรับ iOS

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