画像検索機能を統合する

画像検索を使用すると、画像データベース内で類似した画像を検索できます。これは、クエリの意味論的な意味を表す高次元ベクトルに検索クエリを埋め込み、その後、 ScaNN (Scalable Nearest Neighbors) を使用して事前定義されたカスタム インデックスで類似性検索を行うことによって機能します。

画像分類とは対照的に、認識できる項目の数を増やす場合、モデル全体を再トレーニングする必要はありません。新しい項目はインデックスを再構築するだけで追加できます。これにより、より大きな (100,000 アイテム以上) の画像データベースを操作することも可能になります。

タスク ライブラリImageSearcher API を使用して、カスタム イメージ サーチャーをモバイル アプリにデプロイします。

ImageSearcher API の主な機能

  • 単一の画像を入力として受け取り、インデックス内の埋め込み抽出と最近傍検索を実行します。

  • 回転、サイズ変更、色空間変換などの入力画像処理。

  • 入力画像の関心領域。

前提条件

ImageSearcher API を使用する前に、検索対象の画像のカスタム コーパスに基づいてインデックスを構築する必要があります。これは、 Model Maker Searcher APIを使用してチュートリアルに従い、適応させることで実現できます。

このためには以下が必要になります。

この手順を完了すると、スタンドアロンの TFLite サーチャー モデル ( mobilenet_v3_searcher.tfliteなど) が作成されます。これは、 TFLite モデル メタデータにインデックスが付加された元の画像エンベッダー モデルです。

Javaで推論を実行する

ステップ 1: Gradle の依存関係とその他の設定をインポートする

.tfliteサーチャー モデル ファイルを、モデルが実行される Android モジュールのアセット ディレクトリにコピーします。ファイルを圧縮しないように指定し、TensorFlow Lite ライブラリをモジュールのbuild.gradleファイルに追加します。

android {
    // Other settings

    // Specify tflite index file should not be compressed for the app apk
    aaptOptions {
        noCompress "tflite"
    }

}

dependencies {
    // Other dependencies

    // Import the Task Vision Library dependency (NNAPI is included)
    implementation 'org.tensorflow:tensorflow-lite-task-vision:0.4.4'
    // Import the GPU delegate plugin Library for GPU inference
    implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin:0.4.4'
}

ステップ 2: モデルの使用

// Initialization
ImageSearcherOptions options =
    ImageSearcherOptions.builder()
        .setBaseOptions(BaseOptions.builder().useGpu().build())
        .setSearcherOptions(
            SearcherOptions.builder().setL2Normalize(true).build())
        .build();
ImageSearcher imageSearcher =
    ImageSearcher.createFromFileAndOptions(context, modelFile, options);

// Run inference
List<NearestNeighbor> results = imageSearcher.search(image);

ImageSearcher構成するためのその他のオプションについては、ソース コードと javadoc を参照してください。

C++ で推論を実行する

// Initialization
ImageSearcherOptions options;
options.mutable_base_options()->mutable_model_file()->set_file_name(model_path);
options.mutable_embedding_options()->set_l2_normalize(true);
std::unique_ptr<ImageSearcher> image_searcher = ImageSearcher::CreateFromOptions(options).value();

// Create input frame_buffer from your inputs, `image_data` and `image_dimension`.
// See more information here: tensorflow_lite_support/cc/task/vision/utils/frame_buffer_common_utils.h
std::unique_ptr<FrameBuffer> frame_buffer = CreateFromRgbRawBuffer(
      image_data, image_dimension);

// Run inference
const SearchResult result = image_searcher->Search(*frame_buffer).value();

ImageSearcherを構成するその他のオプションについては、ソース コードを参照してください。

Python で推論を実行する

ステップ 1: TensorFlow Lite Support Pypi パッケージをインストールします。

次のコマンドを使用して、TensorFlow Lite Support Pypi パッケージをインストールできます。

pip install tflite-support

ステップ 2: モデルの使用

from tflite_support.task import vision

# Initialization
image_searcher = vision.ImageSearcher.create_from_file(model_path)

# Run inference
image = vision.TensorImage.create_from_file(image_file)
result = image_searcher.search(image)

ImageSearcherを構成するその他のオプションについては、ソース コードを参照してください。

結果の例

Results:
 Rank#0:
  metadata: burger
  distance: 0.13452
 Rank#1:
  metadata: car
  distance: 1.81935
 Rank#2:
  metadata: bird
  distance: 1.96617
 Rank#3:
  metadata: dog
  distance: 2.05610
 Rank#4:
  metadata: cat
  distance: 2.06347

独自のモデルとテスト データを使用して、ImageSearcher のシンプルな CLI デモ ツールを試してください。