YOLO Vision 2026:

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="yolo11n.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 表,可以将 force=True 传递给 create_embeddings_table 方法。

你可以直接访问 LanceDB 表对象以执行高级分析。在 Working with Embeddings Table section 中了解更多关于它的信息

1. 相似度搜索#

相似度搜索是一种用于查找与给定图片相似的图片的 tehnique。它的基本原理是相似的图片会具有相似的 embeddings。构建好 embeddings table 后,你可以通过以下任一方式运行语义搜索:

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

如果有多个输入,则使用其 embeddings 的聚合结果。

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

语义搜索
from ultralytics import Explorer

# create an Explorer object
exp = Explorer(data="coco128.yaml", model="yolo11n.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="yolo11n.pt")
exp.create_embeddings_table()

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

2. Ask AI(自然语言查询)#

此功能允许你使用自然语言过滤数据集,而无需编写 SQL。AI 驱动的查询生成器会将你的提示词转换为查询并返回匹配结果。例如,你可以问:“显示 100 张刚好有 1 个人和 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="yolo11n.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()

3. SQL 查询#

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

SQL 查询
from ultralytics import Explorer

# create an Explorer object
exp = Explorer(data="coco128.yaml", model="yolo11n.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="yolo11n.pt")
exp.create_embeddings_table()

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

4. 操作 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="yolo11n.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 表上创建专用的向量索引以加快查询速度。这是通过在 LanceDB 表上使用 create_index 方法来完成的。

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

5. Embeddings 应用#

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

相似度索引#

Explorer 附带一个 similarity_index 操作:

  • 它试图评估每个数据点与数据集其余部分的相似程度。
  • 它是通过计算在生成的嵌入空间中比当前图像距离更近(小于 max_dist)的图像嵌入数量来做到这一点的,每次考虑 top_k 个相似图像。

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

  • idx:图像在数据集中的索引
  • im_file:图像文件的路径
  • count:数据集中比当前图像距离更近(小于 max_dist)的图像数量
  • sim_im_filescount 个相似图像的路径列表
提示

对于给定的数据集、模型、max_disttop_k,相似度索引一旦生成就会被重复使用。如果你的数据集已更改,或者你 simplesmente 需要重新生成相似度索引,你可以传递 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

常见问题解答#

Ultralytics Explorer API 的用途是什么?#

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

如何安装 Ultralytics Explorer API?#

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

pip install ultralytics[explorer]

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

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

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

from ultralytics import Explorer

# Create an Explorer object
explorer = Explorer(data="coco128.yaml", model="yolo11n.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 section

将 LanceDB 与 Ultralytics Explorer 配合使用有哪些好处?#

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

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

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

from ultralytics import Explorer

# Create an Explorer object
explorer = Explorer(data="coco128.yaml", model="yolo11n.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 section

评论