[備忘] Stable Diffusion × Google Colab

AI

「Stable Diffusion 」を「Google Colab」上で試す方法について記載をしています。

Stable Diffusion とは

Stable Diffusionとは、テキストから画像を生成することができるAIです。

Google Colab とは

Google Colabとは、Googleが提供する無料でPython(Jupyter Notebook)をブラウザから扱うことができるサービスです。

Google Colaboratory

HuggingFace トークンの取得

下記リンクにアクセスし、「Access repository」を押下します。(アカウントを保有していないばあは作成をしてください。)

CompVis/stable-diffusion-v1-4 · Hugging Face
We’re on a journey to advance and democratize artificial intelligence through open source and open science.

その後、「Settings → Access Token」よりトークンを取得します。

Colab 上で実行!

Google Colaboratory

Google ColabでのStable Diffusion 実行方法は以下の通りです。

1.ノートブックを新規で作成します。

2. メニュー「編集→ノートブックの設定」で、「ハードウェアアクセラレータ」に「GPU」を選択します。

3.「Stable Diffusion」をインストールします。

# パッケージをインストールする
!pip install diffusers==0.3.0 transformers scipy ftfy

4.トークン変数の設定します。(HuggingFaceで発行したトークンを設定します)

# トークン変数の設定
TOKEN="---HugginFace のトークン---"

5.Stable Diffusion Diffusersをインポートします。

# Diffusersをインポート
from diffusers import StableDiffusionPipeline
GitHub - huggingface/diffusers: 🤗 Diffusers: State-of-the-art diffusion models for image and audio generation in PyTorch
🤗 Diffusers: State-of-the-art diffusion models for image and audio generation in PyTorch - GitHub - huggingface/diffusers: 🤗 Diffusers: State-of-the-art diffusi...

6.「Stable Diffusion」のパイプラインを設定します。

from diffusers import StableDiffusionPipeline

# StableDiffusionパイプラインの準備
pipe = StableDiffusionPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4", 
    use_auth_token=TOKEN
).to("cuda")

”{‘trained_betas’} was not found in config. Values will be initialized to default values.”と表示されますが、そのまま画像生成に進んでOKです。

7.画像を生成します。
  ”best project manager” を自由に変更してください。

from torch import autocast

# テキストからの画像生成
inputText = "best project manager"
with autocast("cuda"):
    images = pipe(inputText, guidance_scale=7.5).images
images[0].save("outputImg.png")

8.出力結果を確認する
  ファイルから、outputImg.pngをダブルクリックしてください。

おわりに

ここでは、Stable Diffusion をGoogle Colab上で実行する方法について記載をしました。

コメント

タイトルとURLをコピーしました