TensorFlow Lite タスク ライブラリ

TensorFlow Lite タスク ライブラリには、 アプリ デベロッパーが TFLite で ML エクスペリエンスを作成するためのタスク固有のライブラリ。 一般的な ML 用に最適化されたすぐに使えるモデル インターフェースが用意されている 質問と回答などのタスクを実行できます。モデルは、 各タスクに最適なインターフェースを 向上しますタスク ライブラリはクロス プラットフォームで動作し、 C++、Swift アプリケーションです。

タスク ライブラリに期待されること

  • ML エキスパート以外も使用できる、クリーンで明確に定義された API
    推論は、わずか 5 行のコードで実行できます。パワフルな Task ライブラリ内の使いやすい API を構成要素として活用することで、 モバイル デバイスでの TFLite による ML の開発

  • 複雑だが一般的なデータ処理
    一般的なビジョンと自然言語処理のロジックをサポートし、 モデルで必要とされるデータ形式との間にあるかを評価します。機能 同じ共有可能な処理ロジックを使用して トレーニングと推論を実行できます

  • 高パフォーマンス ゲイン
    データ処理にかかる時間は数ミリ秒以内で、 TensorFlow Lite を使った高速推論処理を 実現しました

  • 拡張性とカスタマイズ
    タスク ライブラリ インフラストラクチャのすべてのメリットを 独自の Android/iOS 推論 API を簡単に構築できます。

サポートされているタスク

サポートされているタスクの種類は次のとおりです。このリストは今後も増加するものと予想されます。 より多くのユースケースに対応し続けています

デリゲートを使用してタスク ライブラリを実行する

デリゲートは、次のハードウェア アクセラレーションを有効にします。 などのオンデバイス アクセラレータを活用して、TensorFlow Lite モデルを作成できます。 GPUCoral Edge TPU。活用 ニューラル ネットワーク操作に利用できると、レイテンシの点で大きなメリットを得られます。 向上させることですたとえば、GPU では最大 5 倍の スピードアップ モバイル デバイス上のレイテンシ、Coral Edge TPU 推論が 10 倍 デスクトップの CPU より高速です

タスク ライブラリには、設定するための簡単な構成オプションとフォールバック オプションが用意されています。 代理人を使用できます。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. タスク オプションで 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 デリゲートを構成します。たとえば、CPU 使用率、 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.

タスク ライブラリ CLI のデモを試す ツール Coral Edge TPU デバイスと連携します事前トレーニング済み Edge TPU の詳細 モデルおよび高度な Edge TPU 設定をご覧ください。

C++ での Core ML Delegate の使用例

完全な例については、Image Classifier Core ML Delegate をご覧ください。 テストをご覧ください。

ステップ 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();