> 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/cosmos-3-edge.md).

# Cosmos 3 Edge

NVIDIA の Cosmos 3 Edge 視覚言語ワールドモデルを Serverless Cloud API 経由で使用します

Cosmos 3 Edgeは、NVIDIAのビジョン・ランゲージ「ワールドモデル」です。物理的なシーンの理解に特化しており、物体間の空間的な関係を推論したり、シーンが安全条件を満たしているか確認したり、次に起こりそうなことを予測したりできます。画像とテキストプロンプトを受け取り、テキストで応答します。また、動作を調整するためのシステムプロンプトを任意で指定できます。Cosmos 3 Edgeは、次の方法で利用できます。 [Serverless Cloud API](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api), [Dedicated Deployments](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments)および [self-hosted Inference](https://inference.roboflow.com/).

{% hint style="info" %}
セルフホスト型のCosmos 3 Edgeを実行するには、CUDA対応のGPUが必要です。CPUでは実行できません。次のいずれかで実行してください。 [Serverless Cloud API](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api)、GPUを搭載した [Dedicated Deployment](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) 、またはGPUを利用する [self-hosted Inference](https://inference.roboflow.com/) サーバー。
{% endhint %}

## Cosmos 3 Edge API

{% stepper %}
{% step %}

### APIキーを取得する

Roboflowアカウントを作成し、 [Roboflow API設定ページ](https://app.roboflow.com/settings/api) でキーを確認して、シェルから使えるようにします。

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

{% endstep %}

{% step %}

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

をインストールします。 [Inference SDK](https://inference.roboflow.com/):

```bash
pip install inference-sdk
```

{% endstep %}

{% step %}

### モデルを実行する

このサンプルでは、 `nvidia/cosmos-3-edge` モデルに画像についての物理的推論の質問をし、応答を出力します。

```python
import os
import cv2
from inference_sdk import InferenceHTTPClient, InferenceConfiguration

image = cv2.imread("my-image.jpg")
client = InferenceHTTPClient(
    api_url="https://serverless.roboflow.com",
    api_key=os.environ["ROBOFLOW_API_KEY"],
).configure(InferenceConfiguration(api_key_transport="header"))
result = client.infer_lmm(
    image,
    model_id="nvidia/cosmos-3-edge",
    prompt="What is likely going to happen next in this scene?",
    max_new_tokens=128,
)
print(result["response"])
```

{% endstep %}
{% endstepper %}

上記のコードは、モデルの応答をターミナルに出力します。サンプル画像での結果は次のとおりです。

<figure><img src="https://1194881119-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fcmo9mhfIjYrvFFy1U7dk%2Fuploads%2Fgit-blob-cb5b18a7741195d6a2a5be4d730ddea029cc0ce7%2Fcosmos-3-edge-reasoning.webp?alt=media" alt="Cosmos 3 Edge predicting what will happen next in a street scene with a red SUV"><figcaption><p>Cosmos 3 Edgeは、シーンで次に起こりそうなことを予測します。</p></figcaption></figure>

## Cosmos 3 Edgeをトレーニングする

マルチモーダルプロジェクトでCosmos 3 Edgeをファインチューニングできます。プロジェクトで「Train」を開き、「Custom Training」をクリックして、モデルアーキテクチャの一覧から「Cosmos 3」を選択します。Cosmos 3はベータ版で、有料プランが必要です。手順の続きは [モデルをトレーニングする](https://docs.roboflow.com/models/ja/xue-xi/train-a-model.md) を、データセットの準備方法は [マルチモーダルデータにアノテーションを付ける](https://docs.roboflow.com/datasets/annotate/annotate/annotate-multimodal-data) をご覧ください。

## ワークフローでCosmos 3 Edgeを使用する

Cosmos 3 Edgeは、次の場所で利用できます。 [ワークフロー](https://docs.roboflow.com/workflows) 「Cosmos 3」ブロックとして使用します。このブロックは画像と省略可能なテキスト `prompt` （デフォルトは `"Describe what's in this image."`）、さらに省略可能なシステムプロンプトを受け取り、モデルのテキスト応答を出力します。この出力を後続のブロックにつなげて、解析、フィルタリング、通知に利用できます。

{% hint style="info" %}
設定します `api_url` デプロイ先に合わせて次のように設定します。

* `https://serverless.roboflow.com` は、Serverless Cloud API 用です。
* `http://localhost:9001` は、ローカルの [Inference](https://inference.roboflow.com/) サーバー用です。
* 非公開エンドポイントの場合は、 [専用デプロイメントの](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) URLを使用します。
  {% endhint %}

## セルフホスト型の Inference で Cosmos 3 Edge を実行する

Cosmos 3 Edge は、自分でホストする [Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) サーバーでも実行できます。

{% hint style="warning" %}
Cosmos 3 Edge をセルフホストするには、CUDA 対応 GPU と、GPU Inference サーバーの Docker イメージの Cosmos ビルド（ `-cosmos3` が末尾に付いたタグ。例： `roboflow/roboflow-inference-server-gpu:1.3.9-cosmos3`）が必要です。このモデルは、プレリリース版の `transformers` ビルドに依存しており、これらは該当イメージ内にのみ含まれています。標準の `latest` イメージでは実行できません。また、 `inference` Python パッケージを pip でインストールするだけでは不十分です。
{% endhint %}

Cosmos イメージを使用してローカルサーバーを起動します。

```bash
pip install inference-cli
inference server start --image roboflow/roboflow-inference-server-gpu:1.3.9-cosmos3  # serves http://localhost:9001
```

次に、同じ SDK コードからそのサーバーを指定します。

```python
import os
from inference_sdk import InferenceHTTPClient

client = InferenceHTTPClient(
    api_url="http://127.0.0.1:9001",
    api_key=os.environ["ROBOFLOW_API_KEY"],
)

result = client.infer_lmm(
    inference_input="./my-image.jpg",
    model_id="nvidia/cosmos-3-edge",
    prompt="What is likely going to happen next in this scene?",
)
print(result["response"])
```

## Inference Python パッケージで Cosmos 3 Edge を実行する

HTTP サーバーを使わずに、 [Inference](https://inference.roboflow.com/) Python パッケージを使って、プロセス内でモデルを実行することもできます。Cosmos の依存関係は `-cosmos3` Docker イメージ内にのみ含まれているため、そのイメージ内でスクリプトを実行してください。 `app.py`:

```python
from inference import get_model

model = get_model("nvidia/cosmos-3-edge", api_key="YOUR_ROBOFLOW_API_KEY")

result = model.infer(
    "https://media.roboflow.com/dog.jpeg",
    prompt="What is likely going to happen next in this scene?",
)

print(result[0].response)
```

次に、Cosmos イメージ内で実行します。

```bash
docker run --rm --gpus all \
  -v $(pwd):/workspace -w /workspace \
  -v /tmp/model-cache:/tmp/model-cache -e MODEL_CACHE_DIR=/tmp/model-cache \
  --entrypoint python3 \
  roboflow/roboflow-inference-server-gpu:1.3.9-cosmos3 app.py
```

この `/tmp/model-cache` マウントにより、ダウンロードした重みが実行のたびに保持されます。これは、 `inference server start` が使用するキャッシュディレクトリと同じです。
