> 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/serufuhosuto/self-hosted.md).

# セルフホストデプロイ

[推論](https://github.com/roboflow/inference) は、オープンソースのコンピュータビジョン展開ハブです。モデルと Workflow を提供し、動画ストリームを管理し、CPU と GPU 向けに推論を最適化します。ローカル処理、レイテンシーやリソースの制御、またはオフライン展開が必要な場合はセルフホストしてください。Apache 2.0 ライセンスのコアは、Roboflow のホスト型 API も支えています。

{% hint style="info" %}
セルフホスティングとは、自分でインフラを管理することを意味します。サーバーの運用を Roboflow に任せたい場合は、 [専用デプロイメント](/deployment/ja/roboflow-cloud/dedicated-deployments.md) または [Serverless Cloud API](/deployment/ja/roboflow-cloud/serverless-api.md)および [オプションの比較](/deployment/ja/choosing-a-deployment.md).
{% endhint %}

## パスを選ぶ

自分のハードウェアでモデルを実行する方法は 3 つあります。ほとんどのプロジェクトでは Inference Server を使用します。

<table data-view="cards" data-search="false"><thead><tr><th></th><th></th><th data-hidden data-card-cover data-type="image">カバー画像</th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><strong>Inference Server</strong></td><td>モデルと Workflow を HTTP 経由で提供する Docker コンテナ。</td><td><a href="/files/838ae8798b247a391e42d16a05102335bde1d8d6">/files/838ae8798b247a391e42d16a05102335bde1d8d6</a></td><td><a href="/pages/131499957d989b00f99c697baf47b854d3dde050">/pages/131499957d989b00f99c697baf47b854d3dde050</a></td></tr><tr><td><strong>Inference Library</strong></td><td>その <code>inference</code> プロセス内でモデルを実行するための Python パッケージ。</td><td><a href="/files/fb7f1ebe31f1b0e7149c425e917fe5ef31c2ad20">/files/fb7f1ebe31f1b0e7149c425e917fe5ef31c2ad20</a></td><td><a href="/pages/5fa10b455a3ac5694fe82b4d383e7211a543c812">/pages/5fa10b455a3ac5694fe82b4d383e7211a543c812</a></td></tr><tr><td><strong>その他の SDK</strong></td><td>Web ブラウザー、iOS、または組み込みデバイス上でモデルを実行します。</td><td><a href="/files/f288df42833e683d7589fd74ad2657d38e0e9456">/files/f288df42833e683d7589fd74ad2657d38e0e9456</a></td><td><a href="/pages/164a481217e36f9bf504bba5aaf3203ca9818e8f">/pages/164a481217e36f9bf504bba5aaf3203ca9818e8f</a></td></tr></tbody></table>

1 つ以上のクライアントや言語で予測が必要な場合、モデルをアプリケーションの依存関係から分離したい場合、またはエッジデバイスにデプロイする場合はサーバーを使用してください。

<figure><img src="/files/cb0613f17787926c0a1acb2c62649b9d8acda071" alt="Roboflow Inference architecture diagram"><figcaption><p>Inference がアプリケーション、モデル、Roboflow プラットフォームの間で担う役割</p></figcaption></figure>

## ローカルでモデルを実行

ほとんどのプロジェクトでは、Docker で Inference Server を実行し、次を使ってリクエストを送信します: `inference-sdk`。この SDK は、アプリケーションを Inference Server に接続する Python HTTP クライアントです。モデルを Python プロセス内に直接読み込んで実行する必要がある場合は Inference Library を使用してください。どちらの方法でも同じ `model_id` 値を受け入れるため、後から切り替えることができます。

### Model ID

その `model_id` パラメータには次を指定できます:

* A [事前学習済みモデルのエイリアス](/models/ja/pretrained-aliases.md)、たとえば `rfdetr-small` または `rfdetr-large`
* 独自の [Roboflow のファインチューニング済みモデル](https://app.gitbook.com/s/wdr4k0gUcsVnXVoafYcQ/train/model-ids)、たとえば `my-project/1`
* A [Universe モデル](https://docs.roboflow.com/datasets/universe/universe/find-a-model-on-universe)、たとえば `soccer-players-xy9vk/2`

ファインチューニング済みモデルと Universe モデルには [API キー](https://docs.roboflow.com/reference/authentication/authentication/find-your-roboflow-api-key).

{% tabs %}
{% tab title="Inference Server" icon="docker" %}

### インストール

次を使ってサーバーを起動します: [Inference CLI](https://docs.roboflow.com/reference/inference/inference-cli)。ハードウェアを検出し、セキュアなデフォルトで適切な Docker イメージを取得します:

```bash
pip install inference-cli && inference server start
```

次に HTTP クライアントをインストールします:

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

ハードウェア要件、デバイス別ガイド、および手動の `docker run` コマンドについては、 [Inference Server をインストール](/deployment/ja/serufuhosuto/inference-server/install.md)。同じクライアントは次の対象にも使用できます: [Serverless Cloud API](/deployment/ja/roboflow-cloud/serverless-api.md) および [専用デプロイメント](/deployment/ja/roboflow-cloud/dedicated-deployments.md)：変更するのは `api_url` だけです。

### 推論を実行

```python
from inference_sdk import InferenceHTTPClient, InferenceConfiguration

image = "https://media.roboflow.com/inference/people-walking.jpg"
client = InferenceHTTPClient(
    api_url="http://localhost:9001",  # 自己ホストのサーバー
    api_key="YOUR_API_KEY",
).configure(InferenceConfiguration(api_key_transport="header"))
results = client.infer(image, model_id="rfdetr-small")
```

その `api_key_transport="header"` 設定はキーを次の形式でのみ送信します: `Authorization: Bearer` ヘッダー。これにより URL とログからキーが除外されます。これにはリリース 1.5.0 以降の Inference Server が必要です。次を使用してください: `api_key_transport="both"` 古いサーバーをまだ呼び出す間は使用してください。参照: [API キーの送信](https://docs.roboflow.com/reference/inference/inference-sdk/configuration#api-key-transport).

置き換え `api_url` を `https://serverless.roboflow.com` を使用するには [Serverless Cloud API](/deployment/ja/roboflow-cloud/serverless-api.md) 代わりに使用します。他のコード変更は不要です。参照: [Inference SDK リファレンス](https://docs.roboflow.com/reference/inference/inference-sdk) 詳細については。

### 結果を可視化

インストール [Supervision](https://supervision.roboflow.com):

```bash
pip install -U supervision
```

```python
import supervision as sv
from inference_sdk import InferenceHTTPClient, InferenceConfiguration

image = sv.load_image_from_url("https://media.roboflow.com/inference/people-walking.jpg")

client = InferenceHTTPClient(
    api_url="http://localhost:9001",
    api_key="YOUR_API_KEY",
).configure(InferenceConfiguration(api_key_transport="header"))
results = client.infer(image, model_id="rfdetr-medium")

detections = sv.Detections.from_inference(results)

annotated_image = sv.BoxAnnotator().annotate(scene=image, detections=detections)
annotated_image = sv.LabelAnnotator().annotate(scene=annotated_image, detections=detections)

sv.plot_image(annotated_image)
```

{% endtab %}

{% tab title="Inference Library" icon="python" %}

### インストール

次の `inference` パッケージを自分の Python 環境にインストールします:

```bash
pip install inference
```

NVIDIA GPU がある場合は、次をインストールしてください: `inference-gpu` 代わりに、index URL を OS にインストールされている CUDA のバージョンに合わせます:

```bash
pip install --extra-index-url https://download.pytorch.org/whl/cu124 inference-gpu
```

参照 [Inference Library](/deployment/ja/serufuhosuto/inference-library.md) バックエンドの追加機能と GPU セットアップの詳細については

### 推論を実行

```python
from inference import get_model

image = "https://media.roboflow.com/inference/people-walking.jpg"
model = get_model(model_id="rfdetr-small")
results = model.infer(image)
```

`get_model()` 初回使用時にモデルの重みをダウンロードしてキャッシュし、その後ローカルで推論を実行します。参照: [Inference Python Package リファレンス](https://docs.roboflow.com/reference/inference/inference-python) 詳細については。

### 結果を可視化

インストール [Supervision](https://supervision.roboflow.com):

```bash
pip install -U supervision
```

```python
import supervision as sv
from inference import get_model

image = sv.load_image_from_url("https://media.roboflow.com/inference/people-walking.jpg")

model = get_model(model_id="rfdetr-medium")
results = model.infer(image)[0]

detections = sv.Detections.from_inference(results)

annotated_image = sv.BoxAnnotator().annotate(scene=image, detections=detections)
annotated_image = sv.LabelAnnotator().annotate(scene=annotated_image, detections=detections)

sv.plot_image(annotated_image)
```

{% endtab %}
{% endtabs %}

![検出結果で注釈された歩行者](https://storage.googleapis.com/com-roboflow-marketing/inference/people-walking-annotated.jpg)

{% hint style="warning" %}
API キーを外部ユーザーに公開しないよう注意してください。公開フロントエンドアプリに埋め込まず、代わりに自分のバックエンド経由でリクエストを中継してください。
{% endhint %}

次を実行できます: [Workflow](https://docs.roboflow.com/workflows) 同じ方法で、サーバー上または自分のプロセス内で: 参照: [Workflow をデプロイ](https://docs.roboflow.com/workflows/deploy/deploy-a-workflow).

{% hint style="info" %}
プライベートモデル向けの TensorRT 最適化済みモデルパッケージは次でのみ利用できます: [Enterprise プラン](/deployment/ja/serufuhosuto/enterprise.md) Roboflow プラットフォーム外で Inference を実行する場合です。公開モデルには、すべてのプランで TensorRT パッケージが含まれます。
{% endhint %}
