HuggingFace Diffusers 0.4 : ノートブック : Stable Diffusion の画像-to-画像パイプライン

HuggingFace Diffusers 0.4 : ノートブック : Stable Diffusion の画像-to-画像パイプライン (翻訳/解説)

翻訳 : (株)クラスキャット セールスインフォメーション
作成日時 : 10/11/2022 (v0.4.1)

* 本ページは、HuggingFace Diffusers の以下のドキュメントを翻訳した上で適宜、補足説明したものです:

* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。

 

 

HuggingFace Diffusers 0.4 : ノートブック : Stable Diffusion の画像-to-画像パイプライン

このノートブックは、🤗 Hugging Face 🧨 Diffusers ライブラリ を使用して Stable Diffusion モデルによるテキスト-guided 画像-to-画像生成に対するカスタム diffusers パイプラインを作成する方法を示します。

Stable Diffusion モデルへの一般的なイントロダクションについては こちら を参照してください。

!nvidia-smi
Thu Sep  8 18:58:28 2022       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 460.32.03    Driver Version: 460.32.03    CUDA Version: 11.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  Tesla V100-SXM2...  Off  | 00000000:00:04.0 Off |                    0 |
| N/A   42C    P0    27W / 300W |      0MiB / 16160MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+
!pip install diffusers==0.4.1 transformers ftfy
!pip install -qq "ipywidgets>=7,<8"

重みをダウンロードまたは使用する前に、モデルライセンスを承認する必要もあります。この記事ではモデルのバージョン v1-4 を使用しますので、そのカード にアクセスし、ライセンスを読んで、同意するならばチェックボックスをチェックする必要があります。

貴方は 🤗 Hugging Face ハブで登録ユーザである必要があり、コードを動作させるにはアクセストークンを使用する必要もあります。アクセストークンの詳細は、ドキュメントのこのセクション を参照してください。

from huggingface_hub import notebook_login

notebook_login()

 

Image2Image パイプライン

import inspect
import warnings
from typing import List, Optional, Union

import torch
from torch import autocast
from tqdm.auto import tqdm

from diffusers import StableDiffusionImg2ImgPipeline

Load the pipeline

device = "cuda"
model_path = "CompVis/stable-diffusion-v1-4"

pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
    model_path,
    revision="fp16", 
    torch_dtype=torch.float16,
    use_auth_token=True
)
pipe = pipe.to(device)

初期画像をダウンロードしてそれを前処理し、それをパイプラインに渡すことができます。

import requests
from io import BytesIO
from PIL import Image

url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"

response = requests.get(url)
init_img = Image.open(BytesIO(response.content)).convert("RGB")
init_img = init_img.resize((768, 512))
init_img

プロンプトを定義してパイプラインを実行します。

prompt = "A fantasy landscape, trending on artstation"

ここで、strength は 0.0 と 1.0 間の値で、入力画像に追加されるノイズの総量を制御します。1.0 に近づく値は多くのバリエーションを可能にしますが、意味的に入力と一貫していない画像も生成します。

generator = torch.Generator(device=device).manual_seed(1024)
with autocast("cuda"):
    image = pipe(prompt=prompt, init_image=init_img, strength=0.75, guidance_scale=7.5, generator=generator).images[0]
image

with autocast("cuda"):
    image = pipe(prompt=prompt, init_image=init_img, strength=0.5, guidance_scale=7.5, generator=generator).images[0]
image

ご覧のように、strength に対して低い値を使用するとき、生成画像は元の init_image に近くなります。

Now using LMSDiscreteScheduler

from diffusers import LMSDiscreteScheduler

lms = LMSDiscreteScheduler(beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear")
pipe.scheduler = lms
generator = torch.Generator(device=device).manual_seed(1024)
with autocast("cuda"):
    image = pipe(prompt=prompt, init_image=init_img, strength=0.75, guidance_scale=7.5, generator=generator).images[0]
image

 

以上