> 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/models/ja/supported-models/clip.md).

# CLIP

OpenAI の [CLIP](https://github.com/openai/CLIP) 画像とテキストの埋め込みを生成し、それら間のゼロショット類似度比較を行うためのモデルを、私たちの [サーバーレスホスト型 API](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api)。以下の3つのエンドポイントを公開しています：

* `/clip/embed_image`、画像の埋め込みベクトルを返します
* `/clip/embed_text`、文字列または文字列のリストの埋め込みベクトルを返します
* `/clip/compare`、subject と prompts のリスト間の類似度スコアを返します

埋め込みはキャッシュして、分類、検索、クラスタリング、セマンティック検索などのタスクに再利用できます。より広い用途の詳細については、 [推論ドキュメント](https://docs.roboflow.com/deployment/self-hosted/self-hosted).

## コード例

以下は、画像をテキストラベルのリストと比較するコードサンプルです。HTTP エンドポイントを直接呼び出すには `curl`、または [`inference-sdk`](https://docs.roboflow.com/reference/inference/inference-sdk) ラッパー。

{% tabs %}
{% tab title="HTTP（curl）" icon="webhook" %}
{% stepper %}
{% step %}

### API キーを取得する

Roboflow アカウントを作成し、以下でキーを見つけます： [Roboflow API 設定ページ](https://app.roboflow.com/settings/api) その後、シェルで利用できるようにします：

```bash
export ROBOFLOW_API_KEY="your-key-here"
```

{% endstep %}

{% step %}

### モデルを実行する

以下を呼び出します: `/clip/compare` エンドポイントを `curl`:

```bash
curl --location 'https://serverless.roboflow.com/clip/compare' \\
  --header 'Content-Type: application/json' \
  --data '{
    "api_key": "'"$ROBOFLOW_API_KEY"'",
    "subject": {"type": "url", "value": "https://media.roboflow.com/notebooks/examples/dog.jpeg"},
    "subject_type": "image",
    "prompt": ["犬の写真", "猫の写真", "車の写真"],
    "prompt_type": "text"
  }'
```

{% endstep %}
{% endstepper %}
{% endtab %}

{% tab title="SDK（Python）" icon="python" %}
{% stepper %}
{% step %}

### API キーを取得する

Roboflow アカウントを作成し、以下でキーを見つけます： [Roboflow API 設定ページ](https://app.roboflow.com/settings/api) その後、シェルで利用できるようにします：

```bash
export ROBOFLOW_API_KEY="your-key-here"
```

{% endstep %}

{% step %}

### 依存関係をインストールする

このパッケージは次のモデルを呼び出します：

```bash
pip install -U inference-sdk supervision
```

{% endstep %}

{% step %}

### モデルを実行する

比較を実行する（画像は [こちら](https://media.roboflow.com/notebooks/examples/dog.jpeg)):

```python
import os
import supervision as sv
from inference_sdk import InferenceHTTPClient

image = sv.load_image_from_url("https://media.roboflow.com/notebooks/examples/dog.jpeg")

client = InferenceHTTPClient(
    api_url="https://serverless.roboflow.com",
    api_key=os.environ["ROBOFLOW_API_KEY"],
)

result = client.clip_compare(
    subject=image,
    prompt=[
        "犬の写真",
        "猫の写真",
        "車の写真",
    ],
    subject_type="image",
    prompt_type="text",
)

# similarity はプロンプトごとのコサイン類似度スコアのリストです
print(result["similarity"])
```

上のコードは推論結果をターミナルに出力します：

```
[0.2726989686489105, 0.19865083694458008, 0.20997387170791626]
```

{% endstep %}
{% endstepper %}
{% endtab %}
{% endtabs %}

## 推論速度

レイテンシの測定条件： [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) NVIDIA L4 1基、バッチサイズ1でのウォームアップ後の平均。

<table data-search="false"><thead><tr><th>モデル</th><th>レイテンシ（ms）</th></tr></thead><tbody><tr><td><code>clip</code></td><td>3.9</td></tr></tbody></table>

測定対象： `embed_image` における `ViT-B-16` チェックポイント（画像埋め込みのみ）。

{% hint style="info" %}
設定する `api_url` をデプロイ先に合わせます：

* `https://serverless.roboflow.com` Serverless Hosted API 用。
* `http://localhost:9001` ローカルの [Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) サーバー用。
* あなたの [専用デプロイメント](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) プライベートエンドポイント用の URL。
  {% endhint %}

## Inference（セルフホスト型）で使用する

CLIP は、次のものを使って完全に自分のハードウェア上でも実行できます： [`inference`](https://docs.roboflow.com/deployment/self-hosted/self-hosted) Python パッケージ。重みをプロセス内で読み込むことで、呼び出しごとのネットワーク往復を避けられます。これは、検索、クラスタリング、データセットのクリーニングのために大規模な画像セットを埋め込む場合に重要です。

{% stepper %}
{% step %}

### パッケージをインストールする

```bash
pip install "inference[clip]"
```

{% endstep %}

{% step %}

### ローカルで埋め込みと比較を行う

以下の `Clip` クラスは次を公開しています： `embed_image`, `embed_text`、および `compare`。このサンプルでは、画像とプロンプトを埋め込み、そのコサイン類似度をスコア化します：

```python
from inference.models import Clip
from inference.core.utils.postprocess import cosine_similarity

clip = Clip(model_id="clip/ViT-B-16")

image_embedding = clip.embed_image("https://media.roboflow.com/inference/people-walking.jpg")
text_embedding = clip.embed_text("a crowd of people walking")

print(cosine_similarity(image_embedding[0], text_embedding[0]))
```

結果は 0 から 1 の間です。数値が高いほど、画像とテキストの類似度が高くなります。
{% endstep %}
{% endstepper %}

### 利用可能なチェックポイント

`model_id` CLIP のバックボーンを選択します：

`clip/RN50`, `clip/RN101`, `clip/RN50x4`, `clip/RN50x16`, `clip/RN50x64`, `clip/ViT-B-32`, `clip/ViT-B-16`, `clip/ViT-L-14`, `clip/ViT-L-14-336px`.

SDK のメソッド `clip_compare`, `get_clip_image_embeddings`、および `get_clip_text_embeddings` 〜を受け取ります `clip_version` 引数を指定して、サーバー呼び出し時に同じチェックポイントを選択できます。

## さらに読む

* [CLIP とは？](https://blog.roboflow.com/openai-clip/)
* [CLIP と Faiss で画像検索エンジンを構築する](https://blog.roboflow.com/clip-image-search-faiss/)
* [CLIP で動画を解析・分類する](https://blog.roboflow.com/how-to-analyze-and-classify-video-with-clip/)
