TensorFlow Lite 任務函式庫包含一組功能強大且易於使用的任務專用函式庫,供應用程式開發人員使用 TFLite 建立 ML 體驗。它為熱門機器學習任務 (例如影像分類、問答等) 提供最佳化的開箱即用模型介面。模型介面是專為每個任務設計的,以達到最佳效能和可用性。Task Library 具備跨平台功能,並支援 Java、C++ 和 Swift。
Task Library 的預期功能
非 ML 專家也能使用的清晰且定義完善的 API
只需 5 行程式碼即可完成推論。Task Library 中功能強大且易於使用的 API 可作為建構區塊,協助您在行動裝置上輕鬆開發使用 TFLite 的 ML。複雜但常見的資料處理
支援常見的視覺和自然語言處理邏輯,以便在您的資料和模型所需的資料格式之間轉換。為訓練和推論提供相同、可共用的處理邏輯。高效能增益
資料處理耗時不超過幾毫秒,確保使用 TensorFlow Lite 的快速推論體驗。擴充性和自訂性
您可以充分利用 Task Library 基礎架構提供的所有優勢,並輕鬆建構自己的 Android/iOS 推論 API。
支援的工作類型
以下是支援的工作類型清單。隨著我們持續啟用越來越多的使用情境,此清單預計會持續擴增。
視覺 API
自然語言 (NL) API
音訊 API
自訂 API
- 擴充 Task API 基礎架構並建構自訂 API。
使用委派執行 Task Library
委派透過利用裝置端加速器 (例如 GPU 和 Coral Edge TPU) 來啟用 TensorFlow Lite 模型的硬體加速。將其用於神經網路運算在延遲和電源效率方面帶來巨大優勢。例如,GPU 可在行動裝置上提供高達 5 倍的延遲加速,而 Coral Edge TPU 的推論速度比桌上型電腦 CPU 快 10 倍。
Task Library 提供簡單的設定和備援選項,供您設定和使用委派。Task API 現在支援以下加速器
- Android
- Linux / Mac
- Coral Edge TPU:C++
- iOS
- Core ML delegate:C++
Task Swift / Web API 的加速支援即將推出。
在 Java 中於 Android 上使用 GPU 的範例
步驟 1. 將 GPU 委派外掛程式庫新增至您模組的 build.gradle
檔案
dependencies {
// Import Task Library dependency for vision, text, or audio.
// Import the GPU delegate plugin Library for GPU inference
implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin'
}
步驟 2. 透過 BaseOptions 在工作選項中設定 GPU 委派。例如,您可以如下所示在 ObjectDetector
中設定 GPU
// Turn on GPU delegation.
BaseOptions baseOptions = BaseOptions.builder().useGpu().build();
// Configure other options in ObjectDetector
ObjectDetectorOptions options =
ObjectDetectorOptions.builder()
.setBaseOptions(baseOptions)
.setMaxResults(1)
.build();
// Create ObjectDetector from options.
ObjectDetector objectDetector =
ObjectDetector.createFromFileAndOptions(context, modelFile, options);
// Run inference
List<Detection> results = objectDetector.detect(image);
在 C++ 中於 Android 上使用 GPU 的範例
步驟 1. 依賴您 bazel 建構目標中的 GPU 委派外掛程式,例如
deps = [
"//tensorflow_lite_support/acceleration/configuration:gpu_plugin", # for GPU
]
其他委派選項包括
"//tensorflow_lite_support/acceleration/configuration:nnapi_plugin", # for NNAPI
"//tensorflow_lite_support/acceleration/configuration:hexagon_plugin", # for Hexagon
步驟 2. 在工作選項中設定 GPU 委派。例如,您可以如下所示在 BertQuestionAnswerer
中設定 GPU
// Initialization
BertQuestionAnswererOptions options;
// Load the TFLite model.
auto base_options = options.mutable_base_options();
base_options->mutable_model_file()->set_file_name(model_file);
// Turn on GPU delegation.
auto tflite_settings = base_options->mutable_compute_settings()->mutable_tflite_settings();
tflite_settings->set_delegate(Delegate::GPU);
// (optional) Turn on automatical fallback to TFLite CPU path on delegation errors.
tflite_settings->mutable_fallback_settings()->set_allow_automatic_fallback_on_execution_error(true);
// Create QuestionAnswerer from options.
std::unique_ptr<QuestionAnswerer> answerer = BertQuestionAnswerer::CreateFromOptions(options).value();
// Run inference on GPU.
std::vector<QaAnswer> results = answerer->Answer(context_of_question, question_to_ask);
在此處探索更多進階加速器設定:這裡。
在 Python 中使用 Coral Edge TPU 的範例
在工作的基本選項中設定 Coral Edge TPU。例如,您可以如下所示在 ImageClassifier
中設定 Coral Edge TPU
# Imports
from tflite_support.task import vision
from tflite_support.task import core
# Initialize options and turn on Coral Edge TPU delegation.
base_options = core.BaseOptions(file_name=model_path, use_coral=True)
options = vision.ImageClassifierOptions(base_options=base_options)
# Create ImageClassifier from options.
classifier = vision.ImageClassifier.create_from_options(options)
# Run inference on Coral Edge TPU.
image = vision.TensorImage.create_from_file(image_path)
classification_result = classifier.classify(image)
在 C++ 中使用 Coral Edge TPU 的範例
步驟 1. 依賴您 bazel 建構目標中的 Coral Edge TPU 委派外掛程式,例如
deps = [
"//tensorflow_lite_support/acceleration/configuration:edgetpu_coral_plugin", # for Coral Edge TPU
]
步驟 2. 在工作選項中設定 Coral Edge TPU。例如,您可以如下所示在 ImageClassifier
中設定 Coral Edge TPU
// Initialization
ImageClassifierOptions options;
// Load the TFLite model.
options.mutable_base_options()->mutable_model_file()->set_file_name(model_file);
// Turn on Coral Edge TPU delegation.
options.mutable_base_options()->mutable_compute_settings()->mutable_tflite_settings()->set_delegate(Delegate::EDGETPU_CORAL);
// Create ImageClassifier from options.
std::unique_ptr<ImageClassifier> image_classifier = ImageClassifier::CreateFromOptions(options).value();
// Run inference on Coral Edge TPU.
const ClassificationResult result = image_classifier->Classify(*frame_buffer).value();
步驟 3. 如下所示安裝 libusb-1.0-0-dev
套件。如果已安裝,請跳至下一步。
# On the Linux
sudo apt-get install libusb-1.0-0-dev
# On the macOS
port install libusb
# or
brew install libusb
步驟 4. 在您的 bazel 命令中使用以下組態進行編譯
# On the Linux
--define darwinn_portable=1 --linkopt=-lusb-1.0
# On the macOS, add '--linkopt=-lusb-1.0 --linkopt=-L/opt/local/lib/' if you are
# using MacPorts or '--linkopt=-lusb-1.0 --linkopt=-L/opt/homebrew/lib' if you
# are using Homebrew.
--define darwinn_portable=1 --linkopt=-L/opt/local/lib/ --linkopt=-lusb-1.0
# Windows is not supported yet.
使用您的 Coral Edge TPU 裝置試用 Task Library CLI 示範工具。探索更多關於 預先訓練的 Edge TPU 模型和進階 Edge TPU 設定。
在 C++ 中使用 Core ML Delegate 的範例
完整的範例可在 Image Classifier Core ML Delegate Test 中找到。
步驟 1. 依賴您 bazel 建構目標中的 Core ML 委派外掛程式,例如
deps = [
"//tensorflow_lite_support/acceleration/configuration:coreml_plugin", # for Core ML Delegate
]
步驟 2. 在工作選項中設定 Core ML Delegate。例如,您可以如下所示在 ImageClassifier
中設定 Core ML Delegate
// Initialization
ImageClassifierOptions options;
// Load the TFLite model.
options.mutable_base_options()->mutable_model_file()->set_file_name(model_file);
// Turn on Core ML delegation.
options.mutable_base_options()->mutable_compute_settings()->mutable_tflite_settings()->set_delegate(::tflite::proto::Delegate::CORE_ML);
// Set DEVICES_ALL to enable Core ML delegation on any device (in contrast to
// DEVICES_WITH_NEURAL_ENGINE which creates Core ML delegate only on devices
// with Apple Neural Engine).
options.mutable_base_options()->mutable_compute_settings()->mutable_tflite_settings()->mutable_coreml_settings()->set_enabled_devices(::tflite::proto::CoreMLSettings::DEVICES_ALL);
// Create ImageClassifier from options.
std::unique_ptr<ImageClassifier> image_classifier = ImageClassifier::CreateFromOptions(options).value();
// Run inference on Core ML.
const ClassificationResult result = image_classifier->Classify(*frame_buffer).value();