LoginSignup
0
1

More than 1 year has passed since last update.

Stable DiffusionをColab上でCPUで動かす

Last updated at Posted at 2022-09-06

概要

GPUで高速に生成するぜ!って使ってたら、

GPU バックエンドに接続できません
Colab での使用量上限に達したため、現在 GPU に接続できません。

とか言われて、利用制限にかかって何もできなくなって悲しくなります。復活までに12〜24時間くらいかかるっぽい。
CPUで低速でもいいから作りたいぜ!ということで動かしました。20分/枚くらい時間かかるけど、5並行まで動かしたら、数分/枚くらいの速度は出せます。

コード

GPUで動作するとこまでは到達している前提(https://note.com/npaka/n/ndd549d2ce556 を参考にして設定しました)で記載します。

!pip install transformers scipy ftfy
!pip install git+https://github.com/huggingface/diffusers.git
# for cpu
!pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cpu

YOUR_TOKEN="ここにトークンを入れる"
# 準備 (img2img)
from PIL import Image
from torch import autocast
from diffusers import StableDiffusionImg2ImgPipeline

pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4",
    use_auth_token=YOUR_TOKEN
).to("cpu")
# img2img
prompt = "perfect pupil of the eyes, anime style eyes, hyper detailed, cute girl, detail of anime"
init_image = Image.open("/content/drive/MyDrive/Colab Data/moenai.png").convert("RGB")
init_image = init_image.resize((512, 512))
# 200枚生成する
for num in range(200):
  images = pipe(
      prompt=prompt,          
      init_image=init_image, 
              strength=float(0.75), # 0.0〜1.0
              guidance_scale=10,   # 0〜20
              num_inference_steps=50,
              generator=None,
      )["sample"]
  images[0].save("/content/drive/MyDrive/Colab Data/output"+str(num)+".png")

input
moenai.png

output
output2_.png
とても惜しかった部分を少しだけ手で直してます。Promptあまり凝ってないので10〜20枚くらいに一個はこれくらいのが出てきそう。ただ、この構図は苦手っぽいので足が変になることが多いです。

txt2imgの場合も同様の感じでできます。

Enjoy.

0
1
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
0
1