【AutoGluon】【Zero-Shot Image Classification】OpenAIのCLIP(Contrastive Language-Image Pre-Training)がAutoGluonから簡単に使えるようです

公開日:2022年7月14日
最終更新日:2023年6月21日

はじめに

CLIP(Contrastive Language-Image Pre-Training)は自然言語処理の技術を用いて画像とテキストの組み合わせを学習したモデルです。

「ゼロショット画像分類(Zero-Shot Image Classification)」にも使用可能です。

AutoGluonで、「ゼロショット画像分類(Zero-Shot Image Classification)」が簡単に実行できるようになっています。

Pythonスクリプト

from autogluon.multimodal import download, MultiModalPredictor

url = "https://live.staticflickr.com/7236/7114602897_9cf00b2820_b.jpg"
segway_image = download(url)

predictor = MultiModalPredictor(problem_type="zero_shot_image_classification")

text_list = ['segway', 'bicycle', 'wheel', 'car']
prob = predictor.predict_proba({"image": [segway_image]}, {"text": text_list})

for i, text in enumerate(text_list):
    print(f"{text}: {prob[0][i]:.6f}")
    
'''
maxIndex = prob.argmax()
print(f"{text_list[maxIndex]}:{prob[0][maxIndex]:.6f}")
'''



候補となるクラス名(または文章)を与えてあげる必要があります。


上記スクリプトのこの部分です。

text_list = ['segway', 'bicycle', 'wheel', 'car']

結果

以下の画像に対して実行してみました。

segway: 0.999972
bicycle: 0.000006
wheel: 0.000020
car: 0.000002

動作環境

Windows 11
CUDA 11.7
Python 3.10

インストールしたのはPyTorchとAutoGluonのみです。
AutoGluonは2023年6月に公開されたv0.8.0を使用しています。

pip install -U setuptools wheel
pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 --extra-index-url https://download.pytorch.org/whl/cu117
pip install autogluon

MultiModalPredictor関連記事

touch-sp.hatenablog.com