> 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 Hosted API では利用できません。次で実行してください。 [専用デプロイメント](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) または [セルフホスト型 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` - 画像とテキストプロンプトのリスト間の類似度を計算する

## コード例

{% 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` Dedicated Deployment 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",
    json={
        "api_key": os.environ["ROBOFLOW_API_KEY"],
        "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 %}

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

```
Embedding length: 1024
First values: [0.0545, -0.0338, -0.0355, -0.0062, 0.0154]
```

## 推論速度

レイテンシの測定条件： [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>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) サーバー用。
* あなたの [専用デプロイメント](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) プライベートエンドポイント用の URL。
  {% endhint %}

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

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 %}

### Workflows での使用

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