TensorFlow Lite 工作程式庫

TensorFlow Lite 工作程式庫包含一組功能強大且易於使用 特定任務專屬的程式庫,讓應用程式開發人員能夠透過 TFLite 打造機器學習體驗。 它提供適用於熱門機器學習的立即最佳化模型介面 像是圖片分類、問題與答案等 介面經過特別設計,可滿足各項工作的需求 效能和可用性工作程式庫可跨平台運作,並且支援 Java、C++ 和 Swift。

工作程式庫的內容

  • 非機器學習專家可使用的乾淨且定義明確的 API
    推論可在 5 行程式碼內完成。使用 在工作程式庫中以易於使用的 API 做為建構模塊 在行動裝置上透過 TFLite 開發機器學習

  • 複雜但常見的資料處理
    支援常見的視覺和自然語言處理邏輯,以利轉換 介於資料和模型要求的資料格式之間提供 用於訓練和推論的相同、可共用的處理邏輯。

  • 成效提升
    資料處理所需時間不會超過數毫秒, 快速推論體驗

  • 擴充性與自訂功能
    您可以利用工作程式庫基礎架構提供的所有好處 輕鬆建構自己的 Android/iOS 推論 API

支援的工作

以下是支援的工作類型清單。名單預計將依下列比例增長: 我們會繼續支援更多使用情境

透過委派項目執行工作程式庫

「委派」則可讓硬體加速 TensorFlow Lite 模型,透過使用裝置端的加速器,例如 GPUCoral Edge TPU。善用 讓類神經網路作業在延遲時間方面 以及電池效率舉例來說,GPU 最高可以為 5 倍 速度飛快 行動裝置的延遲時間,以及 Coral Edge TPU 推論得出 10 倍 速度

工作程式庫提供簡易的設定和備用選項 以及使用委派功能Task API 現已支援下列加速器:

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:在工作選項中設定 GPU 委派功能: BaseOptions. 舉例來說,您可以在 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
]

步驟 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:依賴 Terraform 建構目標中的 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.

試用 Task Library CLI 示範 工具 與 Coral Edge TPU 裝置分享資訊進一步瞭解預先訓練的 Edge TPU 模型先進 Edge TPU 設定

在 C++ 中使用 Core ML Delegate 的範例

如需完整範例,請參閱 Image Classifier Core ML Delegate 測試

步驟 1:依附於 Terraform 建構目標中的 Core ML 委派外掛程式,例如 為:

deps = [
  "//tensorflow_lite_support/acceleration/configuration:coreml_plugin", # for Core ML Delegate
]

步驟 2:在工作選項中設定 Core ML 委派。舉例來說,您可以設定 在 ImageClassifier 中進行核心機器學習委派作業,如下所示:

// 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();