集成图片搜索工具

借助图片搜索,您可以在图片数据库中搜索类似图片。它 通过将搜索查询嵌入到表示 查询的语义含义,然后在 预定义自定义索引 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);

请参阅 源代码和 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 支持 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 演示工具 模型和测试数据。