Ultralytics Explorer API

社区提示 ⚠️

ultralytics>=8.3.12 起,Ultralytics Explorer 已被移除。若要使用 Explorer,请安装 pip install ultralytics==8.3.11。类似的(以及扩展后的)数据集探索功能可在 Ultralytics Platform 中找到。

介绍

Open In Colab Explorer API 是一个用于探索数据集的 Python API。它支持使用 SQL 查询、向量相似度搜索和语义搜索来过滤和搜索你的数据集。



Watch: Ultralytics Explorer API Overview

安装

Explorer 的部分功能依赖于外部库。当你使用 Explorer 时,这些库会自动安装。若要手动安装这些依赖项,请使用以下命令:

pip install ultralytics[explorer]

使用方法

from ultralytics import Explorer

# Create an Explorer object
explorer = Explorer(data="coco128.yaml", model="yolo26n.pt")

# Create embeddings for your dataset
explorer.create_embeddings_table()

# Search for similar images to a given image/images
df = explorer.get_similar(img="path/to/image.jpg")

# Or search for similar images to a given index/indices
df = explorer.get_similar(idx=0)
注意

针对特定数据集和模型对的 Embeddings 表仅会创建一次并重复使用。它们在底层使用了 LanceDB,该工具支持磁盘扩展,因此你可以为 COCO 等大型数据集创建和重复使用 embeddings,而无需担心内存不足。

如果你想强制更新 embeddings 表,可以将 force=True 传递给 create_embeddings_table 方法。

你可以直接访问 LanceDB 表对象以进行高级分析。在 Working with Embeddings Table 部分了解更多信息。

相似度搜索

相似度搜索是一种用于查找与给定图像相似图像的技术。其基本思想是相似的图像将具有相似的 embeddings。一旦建立了 embeddings 表,你可以通过以下任一方式运行语义搜索:

  • 针对数据集中的给定索引或索引列表:exp.get_similar(idx=[1,10], limit=10)
  • 针对数据集之外的任何图像或图像列表:exp.get_similar(img=["path/to/img1", "path/to/img2"], limit=10)

如果有多个输入,则使用它们 embeddings 的聚合值。

你将获得一个 pandas DataFrame,其中包含与输入最相似的 limit 个数据点,以及它们在 embedding 空间中的距离。你可以使用此数据集进行进一步过滤。

语义搜索
from ultralytics import Explorer

# create an Explorer object
exp = Explorer(data="coco128.yaml", model="yolo26n.pt")
exp.create_embeddings_table()

similar = exp.get_similar(img="https://ultralytics.com/images/bus.jpg", limit=10)
print(similar.head())

# Search using multiple indices
similar = exp.get_similar(
    img=["https://ultralytics.com/images/bus.jpg", "https://ultralytics.com/images/bus.jpg"],
    limit=10,
)
print(similar.head())

绘制相似图像

你还可以使用 plot_similar 方法绘制相似图像。此方法采用与 get_similar 相同的参数,并将相似图像以网格形式绘制出来。

绘制相似图像
from ultralytics import Explorer

# create an Explorer object
exp = Explorer(data="coco128.yaml", model="yolo26n.pt")
exp.create_embeddings_table()

plt = exp.plot_similar(img="https://ultralytics.com/images/bus.jpg", limit=10)
plt.show()

Ask AI (自然语言查询)

此功能允许你使用自然语言过滤数据集,而无需编写 SQL。AI 驱动的查询生成器会将你的提示转换为查询并返回匹配结果。例如,你可以问:“给我展示 100 张恰好包含一个人和 2 只狗的图像。也可以有其他物体”,它会生成查询并为你显示这些结果。 注意:此功能使用 LLM,因此结果是概率性的,可能不准确。

Ask AI
from ultralytics.data.explorer import plot_query_result

from ultralytics import Explorer

# create an Explorer object
exp = Explorer(data="coco128.yaml", model="yolo26n.pt")
exp.create_embeddings_table()

df = exp.ask_ai("show me 100 images with exactly one person and 2 dogs. There can be other objects too")
print(df.head())

# plot the results
plt = plot_query_result(df)
plt.show()

SQL 查询

你可以使用 sql_query 方法在数据集上运行 SQL 查询。此方法接受 SQL 查询作为输入,并返回包含结果的 pandas DataFrame。

SQL 查询
from ultralytics import Explorer

# create an Explorer object
exp = Explorer(data="coco128.yaml", model="yolo26n.pt")
exp.create_embeddings_table()

df = exp.sql_query("WHERE labels LIKE '%person%' AND labels LIKE '%dog%'")
print(df.head())

绘制 SQL 查询结果

你还可以使用 plot_sql_query 方法绘制 SQL 查询的结果。此方法采用与 sql_query 相同的参数,并将结果以网格形式绘制出来。

绘制 SQL 查询结果
from ultralytics import Explorer

# create an Explorer object
exp = Explorer(data="coco128.yaml", model="yolo26n.pt")
exp.create_embeddings_table()

# plot the SQL Query
exp.plot_sql_query("WHERE labels LIKE '%person%' AND labels LIKE '%dog%' LIMIT 10")

使用 Embeddings 表

你也可以直接使用 embeddings 表。一旦创建了 embeddings 表,就可以使用 Explorer.table 访问它。

提示

Explorer 在内部使用 LanceDB 表。你可以使用 Explorer.table 对象直接访问此表,并运行原始查询、下推预过滤和后过滤等。

from ultralytics import Explorer

exp = Explorer()
exp.create_embeddings_table()
table = exp.table

以下是你可以使用该表执行的操作的一些示例:

获取原始 Embeddings

示例
from ultralytics import Explorer

exp = Explorer()
exp.create_embeddings_table()
table = exp.table

embeddings = table.to_pandas()["vector"]
print(embeddings)

具有预过滤和后过滤的高级查询

示例
from ultralytics import Explorer

exp = Explorer(model="yolo26n.pt")
exp.create_embeddings_table()
table = exp.table

# Dummy embedding
embedding = [i for i in range(256)]
rs = table.search(embedding).metric("cosine").where("").limit(10)

创建向量索引

使用大型数据集时,你还可以创建专用的向量索引以加快查询速度。这是通过在 LanceDB 表上使用 create_index 方法完成的。

table.create_index(num_partitions=..., num_sub_vectors=...)

Embeddings 应用

你可以使用 embeddings 表进行各种探索性分析。以下是一些示例:

相似度索引

Explorer 附带了一个 similarity_index 操作:

  • 它试图估算每个数据点与数据集中其余部分的相似程度。
  • 它通过计算在生成的 embedding 空间中有多少图像 embeddings 比当前图像更接近 max_dist 来实现这一点,同时每次考虑 top_k 个相似图像。

它返回一个包含以下列的 pandas DataFrame:

  • idx: 图像在数据集中的索引
  • im_file: 图像文件路径
  • count: 数据集中比当前图像更接近 max_dist 的图像数量
  • sim_im_files: count 个相似图像的路径列表
提示

对于给定的数据集、模型、max_disttop_k,生成的相似度索引将被重复使用。如果你的数据集发生了变化,或者你只是需要重新生成相似度索引,可以传递 force=True

相似度索引
from ultralytics import Explorer

exp = Explorer()
exp.create_embeddings_table()

sim_idx = exp.similarity_index()

你可以使用相似度索引来构建自定义条件以过滤数据集。例如,你可以使用以下代码过滤掉与数据集中任何其他图像都不相似的图像:

import numpy as np

sim_count = np.array(sim_idx["count"])
sim_idx["im_file"][sim_count > 30]

可视化 Embedding 空间

你还可以使用你选择的绘图工具来可视化 embedding 空间。例如,这是一个使用 Matplotlib 的简单示例:

import matplotlib.pyplot as plt
from sklearn.decomposition import PCA

# Reduce dimensions using PCA to 3 components for visualization in 3D
pca = PCA(n_components=3)
reduced_data = pca.fit_transform(embeddings)

# Create a 3D scatter plot using Matplotlib Axes3D
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection="3d")

# Scatter plot
ax.scatter(reduced_data[:, 0], reduced_data[:, 1], reduced_data[:, 2], alpha=0.5)
ax.set_title("3D Scatter Plot of Reduced 256-Dimensional Data (PCA)")
ax.set_xlabel("Component 1")
ax.set_ylabel("Component 2")
ax.set_zlabel("Component 3")

plt.show()

开始使用 Explorer API 创建你自己的 CV 数据集探索报告。如需灵感,请查看 VOC Exploration Example

使用 Ultralytics Explorer 构建的应用

尝试我们基于 Explorer API 的 GUI Demo

常见问题 (FAQ)

Ultralytics Explorer API 有什么用途?

Ultralytics Explorer API 专为全面的数据集探索而设计。它允许用户使用 SQL 查询、向量相似度搜索和语义搜索来过滤和搜索数据集。这个强大的 Python API 可以处理大型数据集,非常适合使用 Ultralytics 模型执行各种 computer vision 任务。

如何安装 Ultralytics Explorer API?

要安装 Ultralytics Explorer API 及其依赖项,请使用以下命令:

pip install ultralytics[explorer]

这将自动安装 Explorer API 功能所需的所有外部库。有关更多设置详细信息,请参阅我们文档的 安装部分

如何使用 Ultralytics Explorer API 进行相似度搜索?

你可以通过创建 embeddings 表并查询相似图像来使用 Ultralytics Explorer API 执行相似度搜索。这是一个基本示例:

from ultralytics import Explorer

# Create an Explorer object
explorer = Explorer(data="coco128.yaml", model="yolo26n.pt")
explorer.create_embeddings_table()

# Search for similar images to a given image
similar_images_df = explorer.get_similar(img="path/to/image.jpg")
print(similar_images_df.head())

有关更多详细信息,请访问 Similarity Search 部分

将 LanceDB 与 Ultralytics Explorer 结合使用有什么好处?

Ultralytics Explorer 底层使用的 LanceDB 提供了可扩展的、基于磁盘的 embeddings 表。这确保了你可以为 COCO 等大型数据集创建和重复使用 embeddings,而不会耗尽内存。这些表仅创建一次并可重复使用,从而提高了数据处理的效率。

Ultralytics Explorer API 中的 Ask AI 功能是如何工作的?

Ask AI 功能允许用户使用自然语言查询来过滤数据集。此功能利用 LLM 在后台将这些查询转换为 SQL 查询。这是一个示例:

from ultralytics import Explorer

# Create an Explorer object
explorer = Explorer(data="coco128.yaml", model="yolo26n.pt")
explorer.create_embeddings_table()

# Query with natural language
query_result = explorer.ask_ai("show me 100 images with exactly one person and 2 dogs. There can be other objects too")
print(query_result.head())

有关更多示例,请查看 Ask AI 部分

评论