HuggingFace Diffusers 0.3 : 使用方法 : 推論のためのパイプライン (翻訳/解説)
翻訳 : (株)クラスキャット セールスインフォメーション
作成日時 : 09/29/2022 (v0.3.0)
* 本ページは、HuggingFace Diffusers の以下のドキュメントを翻訳した上で適宜、補足説明したものです:
- Using Diffusers : Unonditional Image Generation
- Using Diffusers : Conditional Image Generation
- Using Diffusers : Text-Guided Image-to-Image Generation
- Using Diffusers : Text-Guided Image-Inpainting
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
- 人工知能研究開発支援
- 人工知能研修サービス(経営者層向けオンサイト研修)
- テクニカルコンサルティングサービス
- 実証実験(プロトタイプ構築)
- アプリケーションへの実装
- 人工知能研修サービス
- PoC(概念実証)を失敗させないための支援
- お住まいの地域に関係なく Web ブラウザからご参加頂けます。事前登録 が必要ですのでご注意ください。
◆ お問合せ : 本件に関するお問い合わせ先は下記までお願いいたします。
- 株式会社クラスキャット セールス・マーケティング本部 セールス・インフォメーション
- sales-info@classcat.com ; Web: www.classcat.com ; ClassCatJP
HuggingFace Diffusers 0.3 : 使用方法 : 推論のためのパイプライン
公式にサポートされている Diffusers パイプラインはタスクベースで各種用意されています。その各々についてその利用方法を簡単に説明します。
条件なし画像生成
DiffusionPipeline は推論のために事前訓練済み拡散システムを使用する最も簡単な方法です。
DiffusionPipeline のインスタンスを作成することから始めてどのパイプライン・チェックポイントをダウンロードしたいかを指定します。任意の Diffusers のチェックポイント に対して DiffusionPipeline を使用できます。このガイドではしかし、DDPM による条件なし画像生成に対して DiffusionPipeline を使用します :
from diffusers import DiffusionPipeline
generator = DiffusionPipeline.from_pretrained("google/ddpm-celebahq-256")
DiffusionPipeline はモデリング、トークン化とスケジューリング・コンポーネントのすべてをダウンロードしてキャッシュします。モデルはおよそ 14 億のパラメータから構成されていますので、それを GPU で実行することを強く勧めます。ちょうど PyTorch でのように、generator オブジェクトを GPU に移動できます。
generator.to("cuda")
そして generator を使用できます :
image = generator().images[0]
出力はデフォルトでは PIL Image オブジェクト にラップされます。
単純に次を呼び出すことで画像をセーブできます :
image.save("generated_image.png")
条件付き画像生成
DiffusionPipeline は推論のために事前訓練済み拡散システムを使用する最も簡単な方法です。
DiffusionPipeline のインスタンスを作成することから始めてどのパイプライン・チェックポイントをダウンロードしたいかを指定します。任意の Diffusers のチェックポイント に対して DiffusionPipeline を使用できます。このガイドではしかし、Latent Diffusion によるテキスト-to-画像生成に対して DiffusionPipeline を使用します :
from diffusers import DiffusionPipeline
generator = DiffusionPipeline.from_pretrained("CompVis/ldm-text2im-large-256")
DiffusionPipeline はモデリング、トークン化とスケジューリング・コンポーネントのすべてをダウンロードしてキャッシュします。モデルはおよそ 14 億のパラメータから構成されていますので、それを GPU で実行することを強く勧めます。ちょうど PyTorch でのように、generator オブジェクトを GPU に移動できます。
generator.to("cuda")
そして貴方のテキストプロンプトで generator を使用できます :
image = generator("An image of a squirrel in Picasso style").images[0]
出力はデフォルトでは PIL Image オブジェクト にラップされます。
単純に次を呼び出すことで画像をセーブできます :
image.save("image_of_squirrel_painting.png")
テキスト-Guided 画像-to-画像生成
StableDiffusionImg2ImgPipeline はテキストプロンプトと初期画像を渡して新しい画像の生成を条件付けます。
from torch import autocast
import requests
from PIL import Image
from io import BytesIO
from diffusers import StableDiffusionImg2ImgPipeline
# load the pipeline
device = "cuda"
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4", revision="fp16", torch_dtype=torch.float16, use_auth_token=True
).to(device)
# let's download an initial image
url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
response = requests.get(url)
init_image = Image.open(BytesIO(response.content)).convert("RGB")
init_image = init_image.resize((768, 512))
prompt = "A fantasy landscape, trending on artstation"
with autocast("cuda"):
images = pipe(prompt=prompt, init_image=init_image, strength=0.75, guidance_scale=7.5).images
images[0].save("fantasy_landscape.png")
テキスト-Guided 画像インペインティング
StableDiffusionInpaintPipeline はマスクとテキストプロンプトを提供することにより画像の特定の一部を編集できます。
from io import BytesIO
from torch import autocast
import requests
import PIL
from diffusers import StableDiffusionInpaintPipeline
def download_image(url):
response = requests.get(url)
return PIL.Image.open(BytesIO(response.content)).convert("RGB")
img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
init_image = download_image(img_url).resize((512, 512))
mask_image = download_image(mask_url).resize((512, 512))
device = "cuda"
pipe = StableDiffusionInpaintPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4", revision="fp16", torch_dtype=torch.float16, use_auth_token=True
).to(device)
prompt = "a cat sitting on a bench"
with autocast("cuda"):
images = pipe(prompt=prompt, init_image=init_image, mask_image=mask_image, strength=0.75).images
images[0].save("cat_on_bench.png")
以上