画像検索では、画像のデータベースで類似した画像を検索できます。これは、検索クエリをクエリのセマンティックな意味を表す高次元ベクトルにエンベディングし、次に ScaNN(Scalable Nearest Neighbors)を使用して事前定義されたカスタム インデックスで類似性検索を行うことで機能します。
画像分類とは異なり、認識できるアイテムの数を増やすためにモデル全体を再トレーニングする必要はありません。新しいアイテムは、インデックスを再構築するだけで追加できます。これにより、画像の大規模なデータベース(10 万件以上のアイテム)を操作することもできます。
Task Library ImageSearcher API を使用して、カスタム画像検索ツールをモバイルアプリにデプロイします。
ImageSearcher API の主な機能
単一の画像を入力として受け取り、エンベディング抽出とインデックス内の最近傍検索を実行します。
回転、サイズ変更、色空間変換などの入力画像処理。
入力画像の対象領域。
前提条件
ImageSearcher API を使用する前に、検索対象となる画像のカスタム コーパスに基づいてインデックスを構築する必要があります。これは、チュートリアルに沿って Model Maker Searcher API を使用して実現できます。
これには、次のものが必要です。
- mobilenet v3 などの TFLite 画像エンベッダー モデル。Kaggle Models の Google Image Modules コレクションで、事前トレーニング済みのエンベッダー モデル(特徴ベクトル モデルとも呼ばれます)をご覧ください。
- 画像コーパス。
この手順を完了すると、スタンドアロンの TFLite サーチャー モデル(mobilenet_v3_searcher.tflite など)が作成されます。これは、インデックスが TFLite モデル メタデータに付加された元の画像エンベッダー モデルです。
Java で推論を実行する
ステップ 1: Gradle の依存関係とその他の設定をインポートする
.tflite サーチャー モデルファイルを、モデルが実行される Android モジュールの assets ディレクトリにコピーします。ファイルを圧縮しないように指定し、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
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 デモツールを試してください。