圖像搜尋可讓您在圖像資料庫中搜尋相似的圖像。其運作方式是將搜尋查詢嵌入高維向量,代表查詢的語意,然後使用 ScaNN (可擴充最近鄰演算法) 在預先定義的自訂索引中進行相似度搜尋。
與 圖像分類 相反,擴充可辨識項目的數量不需要重新訓練整個模型。只需重建索引即可新增項目。這也讓您能夠處理較大型 (10 萬個以上項目) 的圖像資料庫。
使用工作程式庫 ImageSearcher
API,將您的自訂圖像搜尋器部署到您的行動應用程式中。
ImageSearcher API 的主要功能
以單一圖像作為輸入,在索引中執行嵌入擷取和最近鄰搜尋。
輸入圖像處理,包括旋轉、調整大小和色彩空間轉換。
輸入圖像的感興趣區域。
先決條件
使用 ImageSearcher
API 之前,需要根據要搜尋的自訂圖像語料庫建立索引。這可以使用 Model Maker Searcher API,並依照和調整 教學課程 來達成。
為此,您需要
- TFLite 圖像嵌入器模型,例如 mobilenet v3。請參閱 TensorFlow Hub 上的 Google 圖像模組集合,以取得更多預先訓練的嵌入器模型 (亦稱為特徵向量模型)。
- 您的圖像語料庫。
在此步驟之後,您應該會有一個獨立的 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 (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);
請參閱 原始碼和 javadoc,以取得設定 ImageSearcher
的更多選項。
在 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 示範工具。