Thư viện tác vụ TensorFlow Lite

Thư viện tác vụ TensorFlow Lite chứa một tập hợp các thư viện dành riêng cho tác vụ mạnh mẽ và dễ sử dụng dành cho các nhà phát triển ứng dụng để tạo trải nghiệm ML với TFLite. Nó cung cấp các giao diện mô hình sẵn dùng được tối ưu hóa cho các tác vụ máy học phổ biến, chẳng hạn như phân loại hình ảnh, câu hỏi và câu trả lời, v.v. Các giao diện mô hình được thiết kế riêng cho từng tác vụ để đạt được hiệu suất và khả năng sử dụng tốt nhất. Thư viện tác vụ hoạt động đa nền tảng và được hỗ trợ trên Java, C++ và Swift.

Những gì mong đợi từ Thư viện tác vụ

  • Các API rõ ràng và được xác định rõ ràng có thể được sử dụng bởi những người không phải chuyên gia ML
    Suy luận có thể được thực hiện chỉ trong 5 dòng mã. Sử dụng các API mạnh mẽ và dễ sử dụng trong thư viện Tác vụ làm khối xây dựng để giúp bạn dễ dàng phát triển ML bằng TFLite trên thiết bị di động.

  • Xử lý dữ liệu phức tạp nhưng phổ biến
    Hỗ trợ tầm nhìn chung và logic xử lý ngôn ngữ tự nhiên để chuyển đổi giữa dữ liệu của bạn và định dạng dữ liệu mà mô hình yêu cầu. Cung cấp logic xử lý tương tự, có thể chia sẻ cho đào tạo và suy luận.

  • Đạt được hiệu suất cao
    Quá trình xử lý dữ liệu sẽ mất không quá vài mili giây, đảm bảo trải nghiệm suy luận nhanh khi sử dụng TensorFlow Lite.

  • Khả năng mở rộng và tùy biến
    Bạn có thể tận dụng tất cả lợi ích mà cơ sở hạ tầng Thư viện tác vụ mang lại và dễ dàng xây dựng API suy luận Android/iOS của riêng mình.

Nhiệm vụ được hỗ trợ

Dưới đây là danh sách các loại nhiệm vụ được hỗ trợ. Danh sách này dự kiến ​​sẽ tăng lên khi chúng tôi tiếp tục hỗ trợ ngày càng nhiều trường hợp sử dụng.

Chạy thư viện tác vụ với các đại biểu

Các đại biểu cho phép tăng tốc phần cứng của các mẫu TensorFlow Lite bằng cách tận dụng các bộ tăng tốc trên thiết bị như GPUCoral Edge TPU . Việc sử dụng chúng cho các hoạt động của mạng thần kinh mang lại lợi ích to lớn về độ trễ và hiệu quả sử dụng năng lượng. Ví dụ: GPU có thể tăng tốc độ trễ lên tới 5 lần trên thiết bị di động và suy luận của Coral Edge TPU nhanh hơn 10 lần so với CPU máy tính để bàn.

Thư viện tác vụ cung cấp các tùy chọn cấu hình và dự phòng dễ dàng để bạn thiết lập và sử dụng đại biểu. Các trình tăng tốc sau hiện được hỗ trợ trong API tác vụ:

Sắp có hỗ trợ tăng tốc trong Task Swift / API Web.

Ví dụ sử dụng GPU trên Android bằng Java

Bước 1. Thêm thư viện plugin ủy nhiệm GPU vào tệp build.gradle của mô-đun của bạn:

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'
}

Bước 2. Cấu hình GPU delegate trong task options thông qua BaseOptions . Ví dụ: bạn có thể thiết lập GPU trong ObjectDetector như sau:

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

Ví dụ sử dụng GPU trên Android trong C++

Bước 1. Phụ thuộc vào plugin ủy nhiệm GPU trong mục tiêu xây dựng bazel của bạn, chẳng hạn như:

deps = [
  "//tensorflow_lite_support/acceleration/configuration:gpu_plugin", # for GPU
]

Các tùy chọn đại biểu khác bao gồm:

"//tensorflow_lite_support/acceleration/configuration:nnapi_plugin", # for NNAPI
"//tensorflow_lite_support/acceleration/configuration:hexagon_plugin", # for Hexagon

Bước 2. Định cấu hình đại biểu GPU trong tùy chọn tác vụ. Ví dụ: bạn có thể thiết lập GPU trong BertQuestionAnswerer như sau:

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

Khám phá các cài đặt tăng tốc nâng cao hơn tại đây .

Ví dụ sử dụng Coral Edge TPU trong Python

Định cấu hình Coral Edge TPU trong các tùy chọn cơ bản của tác vụ. Ví dụ: bạn có thể thiết lập Coral Edge TPU trong ImageClassifier như sau:

# 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)

Ví dụ sử dụng Coral Edge TPU trong C++

Bước 1. Phụ thuộc vào plugin ủy nhiệm Coral Edge TPU trong mục tiêu xây dựng bazel của bạn, chẳng hạn như:

deps = [
  "//tensorflow_lite_support/acceleration/configuration:edgetpu_coral_plugin", # for Coral Edge TPU
]

Bước 2. Định cấu hình Coral Edge TPU trong tùy chọn tác vụ. Ví dụ: bạn có thể thiết lập Coral Edge TPU trong ImageClassifier như sau:

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

Bước 3. Cài đặt gói libusb-1.0-0-dev như bên dưới. Nếu nó đã được cài đặt, hãy chuyển sang bước tiếp theo.

# On the Linux
sudo apt-get install libusb-1.0-0-dev

# On the macOS
port install libusb
# or
brew install libusb

Bước 4. Biên dịch các cấu hình sau trong lệnh bazel của bạn:

# 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.

Hãy dùng thử công cụ demo CLI của Thư viện tác vụ với các thiết bị Coral Edge TPU của bạn. Khám phá thêm về các mẫu Edge TPU đã được huấn luyện trướccài đặt Edge TPU nâng cao .

Ví dụ sử dụng Core ML Delegate trong C++

Bạn có thể tìm thấy một ví dụ hoàn chỉnh tại Image Classifier Core ML Delegate Test .

Bước 1. Phụ thuộc vào plugin ủy nhiệm Core ML trong mục tiêu xây dựng bazel của bạn, chẳng hạn như:

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

Bước 2. Định cấu hình Core ML Delegate trong các tùy chọn tác vụ. Ví dụ: bạn có thể thiết lập Core ML Delegate trong ImageClassifier như sau:

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