> For the complete documentation index, see [llms.txt](https://docs.roboflow.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.roboflow.com/deployment/ja/regash/legacy-serverless/foundation-models/clip.md).

# CLIP（レガシー）

Roboflow Inference Server を使用して、CLIP の画像埋め込みとテキスト埋め込みを生成・比較します。

CLIPは、画像とテキストの埋め込みを生成できる機械学習モデルです。これらの埋め込みは、ゼロショット分類やセマンティック画像検索など、さまざまな用途に使用できます。Roboflow Inference ServerでCLIPを利用するには、次の3つのルートがあります。

* embed\_image: 画像の埋め込みの計算に使用します
* embed\_text: テキストの埋め込みの計算に使用します
* compare: テキストと画像の埋め込みを計算し、比較するために使用します

## 画像の埋め込み

画像の埋め込みは、その画像に含まれる情報を扱いやすいサイズに圧縮するようなものです。画像を埋め込むときは、何万ものピクセルで構成された画像を入力として受け取り、埋め込みと呼ばれる数百個の数値にまで情報を絞り込みます。これらの埋め込みは人間の目には特に意味のあるものではありませんが、ほかの埋め込みと比較すると非常に役立ちます。

CLIPとRoboflow Inference Serverを使用して画像の埋め込みを生成するには、次のようにします。

{% code overflow="wrap" %}

```python
#リクエストペイロードを定義
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",
    headers={"Authorization": f"Bearer {api_key}"},
    json=infer_clip_payload,
)

embeddings = res.json()['embeddings']

print(embeddings)
```

{% endcode %}

```bash
[[-0.4853120744228363, ... ]]
```

1回のリクエストで複数の画像を埋め込むこともできます。

```python
#リクエストペイロードを定義
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",
    headers={"Authorization": f"Bearer {api_key}"},
    json=infer_clip_payload,
)
```

## テキストの埋め込み

CLIPは画像と同様に、テキストの埋め込みも生成できます。

```python
#リクエストペイロードを定義
infer_clip_payload = {
    "text": "the quick brown fox jumped over the lazy dog",
}

res = requests.post(
    f"{base_url}/clip/embed_text",
    headers={"Authorization": f"Bearer {api_key}"},
    json=infer_clip_payload,
)

embeddings = res.json()['embeddings']

print(embeddings)
```

```
[[0.56842650744228363, ... ]]
```

複数のテキストブロックを1つのリクエストにまとめて送信できます。

```python
#リクエストペイロードを定義
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",
    headers={"Authorization": f"Bearer {api_key}"},
    json=infer_clip_payload,
)
```

## 比較

CLIPの真価は、埋め込みを比較することで発揮されます。比較では、コサイン類似度を使って2つの埋め込み間の数学的な距離を計算します。この距離は類似度スコアと考えることができます。2つの埋め込みのコサイン類似度が1に近い場合、それらは類似しています。

比較を行うときは、プロンプトと1つ以上の対象を指定します。テキストと画像を自由に組み合わせて比較できるため、プロンプトの種類と対象の種類も指定する必要があります。

```python
#リクエストペイロードを定義
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",
    headers={"Authorization": f"Bearer {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]
```
