🚀 モデルカード: OWL-ViT
OWL-ViT(Vision Transformer for Open-World Localizationの略)は、ゼロショットのテキスト条件付き物体検出モデルです。このモデルを使用することで、1つまたは複数のテキストクエリを使って画像を検索することができます。
🚀 クイックスタート
ライブラリのインポート
import requests
from PIL import Image
import torch
from transformers import OwlViTProcessor, OwlViTForObjectDetection
モデルの読み込み
processor = OwlViTProcessor.from_pretrained("google/owlvit-base-patch32")
model = OwlViTForObjectDetection.from_pretrained("google/owlvit-base-patch32")
画像の取得と処理
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
texts = [["a photo of a cat", "a photo of a dog"]]
inputs = processor(text=texts, images=image, return_tensors="pt")
outputs = model(**inputs)
結果の処理と表示
# ターゲット画像のサイズ(高さ、幅)を取得してボックス予測をリスケール
target_sizes = torch.Tensor([image.size[::-1]])
# 出力(バウンディングボックスとクラスロジット)をCOCO API形式に変換
results = processor.post_process_object_detection(outputs=outputs, threshold=0.1, target_sizes=target_sizes)
i = 0 # 最初の画像に対する予測結果を取得
text = texts[i]
boxes, scores, labels = results[i]["boxes"], results[i]["scores"], results[i]["labels"]
# 検出された物体とリスケールされたボックス座標を表示
for box, score, label in zip(boxes, scores, labels):
box = [round(i, 2) for i in box.tolist()]
print(f"Detected {text[label]} with confidence {round(score.item(), 3)} at location {box}")
✨ 主な機能
OWL-ViTは、CLIPをマルチモーダルバックボーンとして使用し、ViTのようなTransformerで視覚特徴を取得し、因果言語モデルでテキスト特徴を取得します。検出にCLIPを使用するために、ビジョンモデルの最終トークンプーリング層を削除し、軽量な分類とボックスヘッドを各Transformer出力トークンに取り付けます。固定分類層の重みを、テキストモデルから取得したクラス名埋め込みで置き換えることで、オープンボキャブラリ分類が可能になります。
📦 インストール
このモデルを使用するには、transformers
ライブラリをインストールする必要があります。以下のコマンドでインストールできます。
pip install transformers
💻 使用例
基本的な使用法
import requests
from PIL import Image
import torch
from transformers import OwlViTProcessor, OwlViTForObjectDetection
processor = OwlViTProcessor.from_pretrained("google/owlvit-base-patch32")
model = OwlViTForObjectDetection.from_pretrained("google/owlvit-base-patch32")
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
texts = [["a photo of a cat", "a photo of a dog"]]
inputs = processor(text=texts, images=image, return_tensors="pt")
outputs = model(**inputs)
target_sizes = torch.Tensor([image.size[::-1]])
results = processor.post_process_object_detection(outputs=outputs, threshold=0.1, target_sizes=target_sizes)
i = 0
text = texts[i]
boxes, scores, labels = results[i]["boxes"], results[i]["scores"], results[i]["labels"]
for box, score, label in zip(boxes, scores, labels):
box = [round(i, 2) for i in box.tolist()]
print(f"Detected {text[label]} with confidence {round(score.item(), 3)} at location {box}")
📚 ドキュメント
🔧 技術詳細
モデルの日付
2022年5月
モデルのタイプ
このモデルは、ViT-B/32 Transformerアーキテクチャを持つCLIPバックボーンを画像エンコーダとして使用し、マスクされた自己注意Transformerをテキストエンコーダとして使用します。これらのエンコーダは、対照損失を通じて(画像、テキスト)ペアの類似度を最大化するように訓練されています。CLIPバックボーンは最初から訓練され、物体検出の目的でボックスとクラス予測ヘッドと一緒に微調整されます。
データ
モデルのCLIPバックボーンは、公開されている画像キャプションデータで訓練されています。これは、いくつかのウェブサイトをクローリングし、YFCC100Mなどの一般的に使用される既存の画像データセットを組み合わせて行われました。データの大部分はインターネットのクローリングから得られています。これは、データがインターネットに最も接続されている人々や社会をより代表していることを意味します。OWL-ViTの予測ヘッドは、CLIPバックボーンとともに、COCOやOpenImagesなどの公開されている物体検出データセットで微調整されています。
BibTeXエントリと引用情報
@article{minderer2022simple,
title={Simple Open-Vocabulary Object Detection with Vision Transformers},
author={Matthias Minderer, Alexey Gritsenko, Austin Stone, Maxim Neumann, Dirk Weissenborn, Alexey Dosovitskiy, Aravindh Mahendran, Anurag Arnab, Mostafa Dehghani, Zhuoran Shen, Xiao Wang, Xiaohua Zhai, Thomas Kipf, Neil Houlsby},
journal={arXiv preprint arXiv:2205.06230},
year={2022},
}
📄 ライセンス
このモデルはApache-2.0ライセンスの下で提供されています。