# OpenVINO

🤗 [Optimum](https://github.com/huggingface/optimum-intel) は、OpenVINO 互換の Stable Diffusion パイプラインを提供し、さまざまな Intel プロセッサ上で推論を実行できます（サポート対象デバイスの[完全なリスト](https://docs.openvino.ai/latest/openvino_docs_OV_UG_supported_plugins_Supported_Devices.html)を参照）。

`--upgrade-strategy eager` オプションを指定して🤗 Optimum Intel をインストールし、[`optimum-intel`](https://github.com/huggingface/optimum-intel) が最新バージョンを使用していることを確認する必要があります。

```bash
pip install --upgrade-strategy eager optimum["openvino"]
```

このガイドでは、OpenVINOでStable DiffusionおよびStable Diffusion XL (SDXL)のパイプラインを使用する方法を示します。

## Stable Diffusion

読み込みと推論の実行には、`OVStableDiffusionPipeline`を使用します。PyTorchモデルを読み込んでOpenVINO形式にオンザフライで変換したい場合は、`export=True`を設定します:

```python
from optimum.intel import OVStableDiffusionPipeline

model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5"
pipeline = OVStableDiffusionPipeline.from_pretrained(model_id, export=True)
prompt = "sailing ship in storm by Rembrandt"
image = pipeline(prompt).images[0]

# Don't forget to save the exported model
pipeline.save_pretrained("openvino-sd-v1-5")
```

推論をさらに高速化するために、モデルを静的にリシェイプします。出力の高さや幅などのパラメータを変更した場合は、モデルを再度静的にリシェイプする必要があります。

```python
# Define the shapes related to the inputs and desired outputs
batch_size, num_images, height, width = 1, 1, 512, 512

# Statically reshape the model
pipeline.reshape(batch_size, height, width, num_images)
# Compile the model before inference
pipeline.compile()

image = pipeline(
    prompt,
    height=height,
    width=width,
    num_images_per_prompt=num_images,
).images[0]
```

    

🤗 Optimumの[ドキュメント](https://huggingface.co/docs/optimum/intel/inference#stable-diffusion)にさらに多くの例があります。Stable Diffusionは、テキストから画像への変換、画像から画像への変換、インペインティングに対応しています。

## Stable Diffusion XL

SDXLで読み込みと推論を実行するには、`OVStableDiffusionXLPipeline`を使用します:

```python
from optimum.intel import OVStableDiffusionXLPipeline

model_id = "stabilityai/stable-diffusion-xl-base-1.0"
pipeline = OVStableDiffusionXLPipeline.from_pretrained(model_id)
prompt = "sailing ship in storm by Rembrandt"
image = pipeline(prompt).images[0]
```

推論をさらに高速化するには、Stable Diffusion のセクションに示されているように、モデルを[静的にリシェイプ](#stable-diffusion)します。

🤗 Optimumの[ドキュメント](https://huggingface.co/docs/optimum/intel/inference#stable-diffusion-xl)にさらに多くの例があります。OpenVINOでのSDXLの実行は、テキストから画像への変換と画像から画像への変換の両方をサポートしています。

