TensorFlow Lite Task 库

TensorFlow Lite Task 库包含一系列强大且易用的 特定于任务的库,供应用开发者使用 TFLite 打造机器学习体验。 它为热门机器学习提供经过优化的开箱即用模型界面 例如图片分类、问题和答案等。 系统专门针对每项任务而设计了界面, 性能和易用性Task 库可跨平台运行,且支持 Java、C++ 和 Swift。

Task 库的用途

  • 可供非机器学习专家使用的干净且定义明确的 API
    只需 5 行代码即可完成推断。使用功能强大的 一些简单易用的 API 作为构建块,可帮助您轻松 在移动设备上使用 TFLite 开发机器学习。

  • 复杂但常见的数据处理
    支持通用视觉和自然语言处理逻辑进行转换 模型所需的数据格式之间。提供 用于训练和推理的相同、可共享的处理逻辑。

  • 高性能增益
    数据处理只需几毫秒,确保 使用 TensorFlow Lite 实现快速推理。

  • 可扩展性和自定义
    您可以利用 Task 库基础架构提供的所有优势, 轻松构建自己的 Android/iOS 推理 API。

支持的任务

下面列出了支持的任务类型。随着 我们会继续支持越来越多的使用场景。

使用委托运行 Task 库

代理用于为以下对象启用硬件加速: TensorFlow Lite 模型,利用设备端加速器(如 GPUCoral Edge TPU。利用 因此能为神经网络操作带来极大的延迟, 和能源效率。例如,GPU 最多可提供高达 5 倍的 加速器 移动设备上的延迟时间,以及 Coral Edge TPU 推断 10x 比桌面设备 CPU 快多。

Task 库提供简单的配置和回退选项供您设置 以及如何使用委托Task API 现在支持以下加速器:

  • Android 设备 <ph type="x-smartling-placeholder">
      </ph>
    • GPU:Java / C++
  • Linux / Mac <ph type="x-smartling-placeholder">
  • iOS <ph type="x-smartling-placeholder">

我们即将在 Task Swift / Web API 中提供加速支持。

Android 上 GPU 的 Java 用法示例

第 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. 例如,您可以在 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);

Android 上的 GPU 使用示例 (C++)

第 1 步:依赖于 bazel 构建目标中的 GPU delegate 插件,例如:

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)

Coral Edge TPU 的 C++ 用法示例

第 1 步:依赖于您的 bazel 构建目标中的 Coral Edge TPU delegate 插件, 例如:

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 设置

Core ML Delegate 的 C++ 用法示例

您可以在 Image Classifier Core ML Delegate 中找到完整示例 测试

第 1 步:依赖于 bazel 构建目标中的 Core ML delegate 插件,例如 以:

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