LoginSignup
6
4

More than 1 year has passed since last update.

話題の Stable DiffusionをColabで作ってみた!

Last updated at Posted at 2022-08-23

今話題のAI画像生成の簡易的なソースを作りましたので、試してみたい方は是非使ってみてください!

実行環境

Google colab
Python 3.7.13
diffusers 0.2.4

前提条件

Googleのアカウントがある
huggingfaceのアカウントがある

環境構築手順

  1. ライセンスの確認
    モデルカードにアクセスしてライセンスを確認します。
    https://huggingface.co/CompVis/stable-diffusion-v1-4
    CompVis_stable-diffusion-v1-4 · Hugging Face - Google Chrome 2022_08_23 23_01_41.png

  2. アクセストークン発行
    https://huggingface.co/settings/tokens
    CompVis_stable-diffusion-v1-4 · Hugging Face - Google Chrome 2022_08_23 23_05_23.jpg

  3. Colabハードウェアアクセラレータの変更
    通常NoneになっているハードウェアアクセラレータをGPUに変更
    Colab上部タブ「編集」押下

    ノートブックの編集を押下

    ハードウェアアクセラレータのプルダウンでGPUを選択

Colabコマンド

!pip install diffusers==0.2.4 transformers scipy ftfy
import torch
from torch import autocast
from diffusers import StableDiffusionPipeline

model_id = "CompVis/stable-diffusion-v1-4"
device = "cuda"
YOUR_TOKEN="<HugginFace Hubのトークン>"

pipe = StableDiffusionPipeline.from_pretrained(model_id, use_auth_token=YOUR_TOKEN)

# お題をpromptに記入
prompt = "日本 城 合戦"
image = pipe(prompt)["sample"][0]

# 保存名を記載
image.save(f"japanese_castle.png")

実行すると下記の写真のように処理が始まり、完了するまで放置する。

StableDiffusion.ipynb - Colaboratory - Google Chrome 2022_08_23 23_14_44 (2).png

上記のキーワードでできた画像がこちら
japanese_castle.png

お城というかお寺ができましたね!

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4