CLIP
CLIPは、画像とテキストの埋め込みを生成できる機械学習モデルです。これらの埋め込みは、ゼロショット分類や意味的画像検索など、さまざまな用途に利用できます。Roboflow Inference ServerでCLIPを利用するためのルートは3つあります:
embed_image:画像の埋め込みを計算するために使用されます
embed_text:テキストの埋め込みを計算するために使用されます
compare:テキストと画像の埋め込みを計算し、比較するために使用されます
画像を埋め込む
画像を埋め込むことは、その画像の情報をより扱いやすいサイズに圧縮するようなものです。画像を埋め込む際、何万ものピクセルで構成された画像を入力として受け取り、数百個の数値(埋め込み)にまで絞り込みます。これらの埋め込みは人間の目には特に意味を持ちませんが、他の埋め込みと比較することで非常に有用になります。
CLIPとRoboflow Inference Serverを使って画像の埋め込みを生成するには:
#リクエストペイロードの定義
infer_clip_payload = {
#画像はURLまたはbase64エンコードされた文字列として提供できます
"image": {
# "type"は"base64"にもできます
"type": "url",
# "value"は画像データのbase64エンコード文字列にもできます
"value": "https://images.freeimages.com/images/large-previews/36c/raccoons-in-the-wild-4-1624830.jpg",
},
}
# 推論サーバーのURLを定義します(localhost:9001, infer.roboflow.comなど)
base_url = "https://infer.roboflow.com"
# Roboflow APIキーを定義します
api_key = <YOUR API KEY HERE>
res = requests.post(
f"{base_url}/clip/embed_image?api_key={api_key}",
json=infer_clip_payload,
)
embeddings = res.json()['embeddings']
print(embeddings)
[[-0.4853120744228363, ... ]]
1つのリクエストで複数の画像を埋め込むことができます:
#リクエストペイロードの定義
infer_clip_payload = {
#画像はURLまたはbase64エンコードされた文字列として提供できます
"image": [
{
"type": "url",
"value": "https://images.freeimages.com/images/large-previews/36c/raccoons-in-the-wild-4-1624830.jpg",
},
{
"type": "url",
"value": "https://images.freeimages.com/images/large-previews/36c/raccoons-in-the-wild-4-1624830.jpg",
}
],
}
res = requests.post(
f"{base_url}/clip/embed_image?api_key={api_key}",
json=infer_clip_payload,
)
テキストを埋め込む
CLIPは画像と同様にテキストの埋め込みも生成できます。
#リクエストペイロードの定義
infer_clip_payload = {
"text": "the quick brown fox jumped over the lazy dog",
}
res = requests.post(
f"{base_url}/clip/embed_text?api_key={api_key}",
json=infer_clip_payload,
)
embeddings = res.json()['embeddings']
print(embeddings)
[[0.56842650744228363, ... ]]
複数のテキストブロックを1つのリクエストにまとめることができます:
#リクエストペイロードの定義
infer_clip_payload = {
"text": [
"the quick brown fox jumped over the lazy dog",
"how vexingly quick daft zebras jump"
]
}
res = requests.post(
f"{base_url}/clip/embed_text?api_key={api_key}",
json=infer_clip_payload,
)
比較
CLIPの真価は埋め込み同士を比較したときに発揮されます。比較は、コサイン類似度を用いて2つの埋め込み間の数学的距離を計算します。この距離は類似度スコアと考えることができます。2つの埋め込みのコサイン類似度が1に近い場合、それらは似ていると言えます。
比較を行う際は、プロンプトと1つまたは複数のサブジェクトを定義します。テキストや画像の任意の組み合わせを比較できるため、プロンプトタイプとサブジェクトタイプも定義する必要があります。
#リクエストペイロードの定義
infer_clip_payload = {
"prompt": {
"type": "url",
"value": "https://images.freeimages.com/images/large-previews/36c/raccoons-in-the-wild-4-1624830.jpg",
},
"prompt_type": "image",
"subject": "とてもかわいいアライグマ",
"subject_type": "text",
}
res = requests.post(
f"{base_url}/clip/compare?api_key={api_key}",
json=infer_clip_payload,
)
similarity = res.json()['similarity']
print(similarity)
[0.30969720949239016]
複数のプロンプト(1リクエストにつき最大8つ)をリストとして渡すことができます:
infer_clip_payload = {
"subject": {
"type": "url",
"value": "https://i.imgur.com/Q6lDy8B.jpg",
},
"subject_type": "image",
"prompt": [
"とてもかわいいアライグマ",
"大きな犬",
"黒い猫",
],
"prompt_type": "text",
}
[0.80559720949239016, 0.20329720949239016, 0.505559720949239016]
Last updated
Was this helpful?