> 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/perception-encoder.md).

# Perception Encoder

Perception Encoder は Meta の視覚言語埋め込みモデルです。画像とテキストを、類似検索、ゼロショット分類、検索のための共有埋め込み空間にマッピングします。

{% hint style="info" %}
Perception Encoder は Serverless Cloud API では利用できません。実行するには [Dedicated Deployment](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) または [self-hosted Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted).
{% endhint %}

Perception Encoder のエンドポイントは 3 つサポートしています:

* `/perception_encoder/embed_image` - 画像を埋め込む
* `/perception_encoder/embed_text` - 文字列を埋め込む
* `/perception_encoder/compare` - 画像とテキストプロンプトのリスト間の類似度を計算する

## Perception Encoder API

{% stepper %}
{% step %}

### APIキーを取得する

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

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

{% endstep %}

{% step %}

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

これらのパッケージは画像を取得して API を呼び出します:

```bash
pip install -U requests opencv-python supervision
```

{% endstep %}

{% step %}

### モデルを実行する

以下のサンプルは画像を次へ送信します `/perception_encoder/embed_image` そして埋め込みの形状を出力します。次を設定してください: `URL` 専用デプロイURLまたはローカル推論サーバーに対して。

```python
import base64
import os
import cv2
import requests
import supervision as sv

URL = "https://your-deployment.roboflow.cloud"

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

_, buffer = cv2.imencode(".jpg", image)
image_base64 = base64.b64encode(buffer).decode("utf-8")

response = requests.post(
    f"{URL}/perception_encoder/embed_image",
    headers={"Authorization": f"Bearer {os.environ['ROBOFLOW_API_KEY']}"},
    json={
        "image": {"type": "base64", "value": image_base64},
    },
)
result = response.json()
embedding = result["embeddings"][0]
print(f"Embedding length: {len(embedding)}")
print(f"First values: {embedding[:5]}")
```

{% endstep %}
{% endstepper %}

上のコードは埋め込みの形状をターミナルに出力します:

```
埋め込み長: 1024
最初の値: [0.0545, -0.0338, -0.0355, -0.0062, 0.0154]
```

## Perception Encoder の推論速度

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

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

以下で測定: `embed_image` 上の `PE-Core-L14-336` チェックポイント（画像埋め込みのみ）。

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

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

## セルフホスト型 Inference で Perception Encoder を実行する

Perception Encoder は次のものを使って直接読み込めます: [`inference`](https://docs.roboflow.com/deployment/self-hosted/self-hosted) パッケージです。ローカルで多数の画像や動画フレームを埋め込む場合、これが最も高速な方法です。

{% stepper %}
{% step %}

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

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

使用する `inference-gpu[transformers]` GPUマシンでは。
{% endstep %}

{% step %}

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

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

pe = PerceptionEncoder(model_id="perception_encoder/PE-Core-B16-224")

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

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

{% endstep %}
{% endstepper %}

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

`model_id` バックボーンを選択します:

* `perception_encoder/PE-Core-B16-224`
* `perception_encoder/PE-Core-L14-336`
* `perception_encoder/PE-Core-G14-448`

サポートされているのは CLIP 形式のインターフェースのみです。言語整合型および空間整合型の Perception Encoder 変種はまだ利用できません。

{% hint style="info" %}
Perception Encoder は次と同じ API 形式を使用します: [CLIP](/models/ja/supported-models/clip.md): `embed_image`, `embed_text`、 `compare` 同じ引数を取り、同じレスポンス形式を返すため、CLIP 向けに書かれたコードはモデルを変更するだけで Perception Encoder でも動作します。
{% endhint %}

### ワークフローで使用

Perception Encoder は次で利用できます: [Workflows](https://docs.roboflow.com/workflows) 次を通じて **Perception Encoder 埋め込みモデル** ブロックです。コードを書かずに画像またはテキストの埋め込みを生成できます。
