图片搜索功能可用于在图片数据库中搜索类似的图片。其工作原理是将搜索查询嵌入到表示查询语义的高维向量中,然后使用 ScaNN(可扩容最近邻)在预定义的自定义索引中进行相似度搜索。
与图片分类不同,扩大可识别的商品数量不需要重新训练整个模型。只需重新构建索引即可添加新商品。这还支持处理更大的(10 万项以上)图片数据库。
使用 Task Library ImageSearcher API 将自定义图片搜索器部署到移动应用中。
ImageSearcher API 的主要功能
以单张图片作为输入,在索引中执行嵌入提取和最近邻搜索。
输入图像处理,包括旋转、调整大小和颜色空间转换。
输入图片的感兴趣区域。
前提条件
在使用 ImageSearcher API 之前,需要根据要搜索的自定义图片语料库构建索引。您可以按照并调整教程,使用 Model Maker Searcher API 实现此目的。
为此,您需要:
- TFLite 图片嵌入器模型,例如 MobileNetV3。
如需查看更多预训练的嵌入器模型(也称为特征向量模型),请访问 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 支持 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 演示工具。