整合圖片搜尋工具

圖片搜尋功能可讓您在圖片資料庫中搜尋相似圖片。這項功能會將搜尋查詢嵌入高維度向量,代表查詢的語意,然後使用 ScaNN (可擴充最鄰近項目) 在預先定義的自訂索引中執行相似度搜尋。

圖片分類不同,擴充可辨識的項目數量不需要重新訓練整個模型。只要重新建立索引,即可新增項目。這也讓您能夠處理較大的圖片資料庫 (超過 10 萬個項目)。

使用 Task Library 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
    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 支援 Pypi 套件。

您可以使用下列指令安裝 TensorFlow Lite 支援 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 示範工具