🦔

SimianLuo/LCM_Dreamshaper_v7の画像が崩れない最小step数を探す

2023/11/05に公開

はじめに

画像生成周りでは、最近LCM

環境

  • Google Colob(T4 ハイメモリ)

準備

まずは、ライブラリを入れていきます

!pip install torch
!pip install --upgrade diffusers transformers accelerate scipy ftfy safetensors

次にモデルのダウンロードや各種設定を行います

#@title choose model (モデル選択) モデルを変更する場合はこれ以降を実行

import torch
import random
from IPython.display import display
from diffusers import DiffusionPipeline
from PIL import Image
from PIL.PngImagePlugin import PngInfo
import numpy as np
import time

device = "cuda"
# Load the cd_imagenet64_l2 checkpoint.

model_name = "SimianLuo/LCM_Dreamshaper_v7"

pipe = DiffusionPipeline.from_pretrained("SimianLuo/LCM_Dreamshaper_v7", custom_pipeline="latent_consistency_txt2img", custom_revision="main")

# To save GPU memory, torch.float16 can be used, but it may compromise image quality.
pipe.to(torch_device="cuda", torch_dtype=torch.float16)

各ステップでの生成の違い

実行パラメータ

prompt
super fine illustration, an extremely cute and beautiful girl,Fairy, green white wings,a woman dressed in a green dress and green boots and wings on back

guidance_scale (CFG スケール)
guidance_scale = 8.0

生成する画像の大きさ
width = 512、height = 512

lcm_origin_steps
lcm_origin_steps = 50

生成した画像

以下の画像は各ステップで生成したときの画像です
あまり違いが出ていなさそうです

step=1

step=2

step=3

step=4

step=5

step = 8

step=20

step=50

まとめ

step数だけだとstep=1でも50でも大きくわからないため、通常時はstep1で実行するのがよさそうです!
LoRAの適応はまだ試せていないので、次回以降の検証項目になりそうです

備考

生成時に実行したコードは以下です。

#@title  画像生成

#@markdown Prompt
prompt = "super fine illustration, an extremely cute and beautiful girl,Fairy, green white wings,a woman dressed in a green dress and green boots and wings on back" #@param{type:"string"}

# Can be set to 1~50 steps. LCM support fast inference even <= 4 steps. Recommend: 1~8 steps.

# guidance_scale (CFG スケール)
guidance_scale = 8.0

width = 512

height = 512

#lcm_origin_steps(いじらなくていい)
lcm_origin_steps = 50


#生成枚数
num_gen_img = 4

image_list = []

def image_grid(imgs):
  rows = int(np.sqrt(num_gen_img))
  cols = int(np.sqrt(num_gen_img))
  assert len(imgs) == rows*cols

  w, h = imgs[0].size
  w,h=w//2,h//2
  grid = Image.new('RGB', size=(cols*w, rows*h))
  grid_w, grid_h = grid.size

  for i, img in enumerate(imgs):
      img = img.resize((w, h))
      grid.paste(img, box=(i%cols*w, i//cols*h))
  return grid


def generate_image(step):
  for i in range(num_gen_img):
    gen_image = pipe(prompt=prompt,width = width , height = height , num_inference_steps=step, guidance_scale=guidance_scale, lcm_origin_steps=lcm_origin_steps, output_type="pil" ,).images[0]

    #save
    gen_image.save("LCM_{:04}.png".format(i))
    image_list.append(gen_image)

# generate_image(1)
# generate_image(2)
# generate_image(3)
# generate_image(4)
# generate_image(5)
# generate_image(6)
# generate_image(7)
generate_image(50)

grid
MidraLab(ミドラボ)

Discussion