コンテンツへスキップ

VOC探査の例

Welcome to the Ultralytics Explorer API notebook! This notebook serves as the starting point for exploring the various resources available to help you get started with using Ultralytics to explore your datasets using with the power of semantic search. You can utilities out of the box that allow you to examine specific types of labels using vector search or even SQL queries.

試す yolo explorer powered by Explorer API

単に pip install ultralytics そして yolo explorer を使えば、ブラウザ上でデータセットのカスタムクエリやセマンティック検索を実行することができます!

コミュニティ・ノート ⚠️

現在 ultralytics>=8.3.10Ultralytics エクスプローラーのサポートは廃止されました。しかしご心配なく!現在では、同様の機能、さらに強化された機能に Ultralytics ハブHUBは、ワークフローを合理化するために設計された、直感的なコード不要のプラットフォームです。Ultralytics HUBを使えば、コードを一行も書くことなく、データの探索、視覚化、管理を楽に続けることができます。ぜひチェックして、その強力な機能をご活用ください🚀。

セットアップ

ピップインストール ultralytics そして 依存関係 そしてソフトウェアとハードウェアをチェックする。

%pip install ultralytics[explorer] openai
yolo checks

Utilize the power of vector similarity search to find the similar data points in your dataset along with their distance in the embedding space. Simply create an embeddings table for the given dataset-model pair. It is only needed once, and it is reused automatically.

exp = Explorer("VOC.yaml", model="yolo11n.pt")
exp.create_embeddings_table()

埋め込みテーブルが構築されると、以下のいずれかの方法でセマンティック検索を実行することができます:

  • On a given index / list of indices in the dataset like - exp.get_similar(idx=[1,10], limit=10)
  • On any image/ list of images not in the dataset - exp.get_similar(img=["path/to/img1", "path/to/img2"], limit=10) In case of multiple inputs, the aggregate of their embeddings is used.

You get a pandas dataframe with the limit number of most similar data points to the input, along with their distance in the embedding space. You can use this dataset to perform further filtering

Similarity search table

# Search dataset by index
similar = exp.get_similar(idx=1, limit=10)
similar.head()

を使用して、類似サンプルを直接プロットすることもできます。 plot_similar 利用

Similarity search image 1

exp.plot_similar(idx=6500, limit=20)
exp.plot_similar(idx=[100, 101], limit=10)  # Can also pass list of idxs or imgs

exp.plot_similar(img="https://ultralytics.com/images/bus.jpg", limit=10, labels=False)  # Can also pass external images

Similarity search image 2

Ask AI: Search or filter with Natural Language

You can prompt the Explorer object with the kind of data points you want to see, and it'll try to return a dataframe with those. Because it is powered by LLMs, it doesn't always get it right. In that case, it'll return None.

Ask ai table

df = exp.ask_ai("show me images containing more than 10 objects with at least 2 persons")
df.head(5)

これらの結果をプロットするには plot_query_result util Example:

plt = plot_query_result(exp.ask_ai("show me 10 images containing exactly 2 persons"))
Image.fromarray(plt)

Ask ai image 1

# plot
from PIL import Image

from ultralytics.data.explorer import plot_query_result

plt = plot_query_result(exp.ask_ai("show me 10 images containing exactly 2 persons"))
Image.fromarray(plt)

Run SQL queries on your Dataset

Sometimes you might want to investigate a certain type of entries in your dataset. For this Explorer allows you to execute SQL queries. It accepts either of the formats:

  • Queries beginning with "WHERE" will automatically select all columns. This can be thought of as a shorthand query
  • どのカラムを選択するかを指定できる完全なクエリを書くこともできます。

これは、モデルのパフォーマンスや特定のデータポイントを調査するために使用することができます。例えば

  • 例えば、あなたのモデルが人間と犬のいる画像で格闘しているとしよう。このようなクエリを書いて、少なくとも2人の人間と少なくとも1匹の犬がいるポイントを選択することができます。

SQLクエリーとセマンティック検索を組み合わせることで、特定のタイプの結果を絞り込むことができます。

table = exp.sql_query("WHERE labels LIKE '%person, person%' AND labels LIKE '%dog%' LIMIT 10")
exp.plot_sql_query("WHERE labels LIKE '%person, person%' AND labels LIKE '%dog%' LIMIT 10", labels=True)

SQL queries table

table = exp.sql_query("WHERE labels LIKE '%person, person%' AND labels LIKE '%dog%' LIMIT 10")
print(table)

類似性検索と同様に、次のようなSQLクエリを直接プロットするユーティリティもある。 exp.plot_sql_query

SQL queries image 1

exp.plot_sql_query("WHERE labels LIKE '%person, person%' AND labels LIKE '%dog%' LIMIT 10", labels=True)

Working with embeddings Table (Advanced)

エクスプローラーは ランスDB テーブルを内部的に使用します。このテーブルには Explorer.table オブジェクトを作成し、生のクエリーを実行したり、プリ・フィルターやポスト・フィルターをプッシュダウンしたりすることができます。

table = exp.table
print(table.schema)

Run raw queries¶

ベクトル検索は、データベースから最も近いベクトルを検索します。推薦システムや検索エンジンでは、検索した商品の類似商品を見つけることができる。LLMや他のAIアプリケーションでは、各データポイントは、いくつかのモデルから生成された埋め込みによって提示することができ、それは最も関連性の高い特徴を返します。

高次元ベクトル空間での検索は、クエリベクトルのK-最近傍(KNN)を見つけることである。

Metric In LanceDB, a Metric is the way to describe the distance between a pair of vectors. Currently, it supports the following metrics:

  • L2
  • コサイン
  • Dot Explorer's similarity search uses L2 by default. You can run queries on tables directly, or use the lance format to build custom utilities to manage datasets. More details on available LanceDB table ops in the docs

Raw-queries-table

dummy_img_embedding = [i for i in range(256)]
table.search(dummy_img_embedding).limit(5).to_pandas()
df = table.to_pandas()
pa_table = table.to_arrow()

エンベッディングを使う

lancedbテーブルから生の埋め込みにアクセスし、解析することができます。画像埋め込みは vector

import numpy as np

embeddings = table.to_pandas()["vector"].tolist()
embeddings = np.array(embeddings)

散布図

埋め込みを分析する前段階の1つは、次元削減によって埋め込みを2次元空間にプロットすることです。例として

Scatterplot Example

import matplotlib.pyplot as plt
from sklearn.decomposition import PCA  # pip install scikit-learn

# 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's 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()

類似性指数

エンベッディングテーブルを使った簡単な操作の例を示します。エクスプローラには similarity_index 操作

  • これは、各データポイントが残りのデータセットとどの程度類似しているかを推定しようとするものである。
  • It does that by counting how many image embeddings lie closer than max_dist to the current image in the generated embedding space, considering top_k similar images at a time.

与えられたデータセットに対して、モデル、 max_dist & top_k を渡すと、一度生成された類似度インデックスが再利用される。データセットが変更された場合、あるいは単に類似度インデックスを再生成する必要がある場合、次のように渡すことができます。 force=True. Similar to vector and SQL search, this also comes with a util to directly plot it. Let's look

sim_idx = exp.similarity_index(max_dist=0.2, top_k=0.01)
exp.plot_similarity_index(max_dist=0.2, top_k=0.01)

類似性指数

at the plot first

exp.plot_similarity_index(max_dist=0.2, top_k=0.01)

では、操作の出力を見てみよう。

sim_idx = exp.similarity_index(max_dist=0.2, top_k=0.01, force=False)

sim_idx

類似度が30を超えるデータポイントを確認するクエリを作成し、類似する画像をプロットしてみよう。

import numpy as np

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

次のように表示されるはずです。

similarity-index-image

exp.plot_similar(idx=[7146, 14035])  # Using avg embeddings of 2 images
📅 Created 16 days ago ✏️ Updated 1 day ago

コメント