ภาพรวม
เมื่อทำให้โมเดลสำหรับแอปพลิเคชันแมชชีนเลิร์นนิง (ODML) ในอุปกรณ์ใช้งานได้ คุณควรทราบว่าจะมีหน่วยความจำจำกัดที่ใช้ได้ในอุปกรณ์เคลื่อนที่ ขนาดไบนารีของโมเดลสัมพันธ์กับจำนวนการดำเนินการที่ใช้ในโมเดลอย่างมาก TensorFlow Lite ช่วยให้คุณลดขนาดไบนารีของโมเดลโดยใช้บิลด์เฉพาะจุด บิลด์เฉพาะจุดจะข้ามการดำเนินการที่ไม่ได้ใช้ในชุดโมเดลของคุณ และสร้างไลบรารีขนาดกะทัดรัดที่มีเฉพาะรันไทม์และเคอร์เนลของการดำเนินการที่จำเป็นเพื่อให้โมเดลทำงานบนอุปกรณ์เคลื่อนที่
บิลด์ที่เลือกมีผลกับไลบรารีการดำเนินการ 3 รายการต่อไปนี้
- ไลบรารี Ops ในตัวของ TensorFlow Lite
- การดำเนินการที่กำหนดเองของ TensorFlow Lite
- เลือกไลบรารี TensorFlow ของ TensorFlow
ตารางด้านล่างแสดงผลกระทบของบิลด์เฉพาะจุดสำหรับกรณีการใช้งานทั่วไปบางกรณี
ชื่อโมเดล | โดเมน | สถาปัตยกรรมเป้าหมาย | ขนาดไฟล์ AAR |
---|---|---|---|
Mobilenet_1.0_224(float) | การจัดประเภทรูปภาพ | Armeabi-V7a | tensorflow-lite.aar (296,635 ไบต์) |
ARM64-V8a | tensorflow-lite.aar (382,892 ไบต์) | ||
เครื่องเทศ | การแยกระดับเสียงสูงต่ำ | 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 ไบต์) |
เลือกสร้าง 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
ด้วยหากโมเดลของคุณมีการดำเนินการเลือก TensorFlow โปรดทราบว่าการดำเนินการนี้จะสร้าง AAR "Ft" ที่มีสถาปัตยกรรมที่แตกต่างกันจำนวนมาก หากคุณไม่ต้องการให้มีทั้งหมด ให้ใช้ชุดย่อยที่เหมาะกับสภาพแวดล้อมในการทำให้ใช้งานได้ของคุณ
สร้างด้วย Ops ที่กำหนดเอง
หากคุณพัฒนาโมเดล 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
มีทรัพยากร Dependency สำหรับสร้างไฟล์ต้นทางเหล่านั้น โปรดทราบว่าทรัพยากร Dependency เหล่านี้ต้องอยู่ในที่เก็บของ 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",
],
)
สำหรับรุ่นที่มีการเลือกการดำเนินการของ 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
สำหรับโมเดลที่มีการปฏิบัติการ เลือก 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 ในเครื่องของคุณและดาวน์โหลด Dockerfile ของ TensorFlow Lite ที่นี่
หลังจากดาวน์โหลดไฟล์ Dockerfile ข้างต้นแล้ว คุณสามารถสร้างอิมเมจ Docker ได้โดยการเรียกใช้
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>]
Flag checkpoint
คือคอมมิต สาขา หรือแท็กของที่เก็บ TensorFlow ที่คุณต้องการชำระเงินก่อนสร้างไลบรารี และโดยค่าเริ่มต้นจะเป็น Branch ที่เผยแพร่ล่าสุด คำสั่งด้านบนจะสร้างไฟล์ AAR
tensorflow-lite.aar
สำหรับ TensorFlow Lite ในตัวและปฏิบัติการที่กำหนดเองและ
เลือกไฟล์ AAR tensorflow-lite-select-tf-ops.aar
สำหรับการดำเนินการเลือก TensorFlow ในไดเรกทอรีปัจจุบันของคุณ
--cache_dir จะระบุไดเรกทอรีของแคช หากไม่ระบุ สคริปต์จะสร้างไดเรกทอรีชื่อ bazel-build-cache
ภายใต้ไดเรกทอรีการทำงานปัจจุบันสำหรับการแคช
เพิ่มไฟล์ AAR ลงในโปรเจ็กต์
เพิ่มไฟล์ AAR โดยนำเข้า AAR ลงในโปรเจ็กต์โดยตรง หรือเผยแพร่ AAR ที่กำหนดเองไปยังที่เก็บ Maven ในเครื่อง โปรดทราบว่าคุณต้องเพิ่มไฟล์ AAR สำหรับ tensorflow-lite-select-tf-ops.aar
ด้วยหากคุณสร้างไฟล์
บิลด์ที่เลือกสำหรับ iOS
โปรดดูส่วนการสร้างภายในเพื่อตั้งค่าสภาพแวดล้อมของบิลด์และกำหนดค่าพื้นที่ทำงาน TensorFlow จากนั้นทำตามคำแนะนำเพื่อใช้สคริปต์บิลด์เฉพาะสำหรับ iOS