0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

diffusersでスケジューラを読み込んで画像生成する方法

Posted at

はじめに

これはhuggingface/diffusersモジュールを利用して画像生成する際に、スケジューラ(Euler等)をを読み込む方法です。

Diffusersではすでに何種類か用意されているので、それを使ってパイプラインを生成する

今回はそれの備忘録

コード

import datetime
import torch
from diffusers import (
    DiffusionPipeline,
    DDIMScheduler,
    DDPMScheduler,
    DEISMultistepScheduler,
    DPMSolverMultistepScheduler,
    DPMSolverSinglestepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    HeunDiscreteScheduler,
    KDPM2AncestralDiscreteScheduler,
    KDPM2DiscreteScheduler,
    UniPCMultistepScheduler,
)

MODEL_ID=''
SCHEDULER = 'EulerAncestralDiscrete'
CACHE_DIR = './cache'
DEVICE = 'cuda'

SCHEDULERS_LIST = {
    'DDIM' : DDIMScheduler,
    'DDPM' : DDPMScheduler,
    'DEISMultistep' : DEISMultistepScheduler,
    'DPMSolverMultistep' : DPMSolverMultistepScheduler,
    'DPMSolverSinglestep' : DPMSolverSinglestepScheduler,
    'EulerAncestralDiscrete' : EulerAncestralDiscreteScheduler,
    'EulerDiscrete' : EulerDiscreteScheduler,
    'HeunDiscrete' : HeunDiscreteScheduler,
    'KDPM2AncestralDiscrete' : KDPM2AncestralDiscreteScheduler,
    'KDPM2Discrete' : KDPM2DiscreteScheduler,
    'UniPCMultistep' : UniPCMultistepScheduler
}

# Create Pipeline
pipe = DiffusionPipeline.from_pretrained(
    pretrained_model_name_or_path=MODEL_ID,
    torch_dtype=torch.float16,
    cache_dir=CACHE_DIR,
)

# Add Scheduler
pipe.scheduler = SCHEDULERS_LIST[SCHEDULER].from_pretrained(
    pretrained_model_name_or_path=MODEL_ID,
    torch_dtype=torch.float16,
    cache_dir=CACHE_DIR,
    subfolder='scheduler'
)

pipe = pipe.to(DEVICE)

# Generate Image
image = pipe(
    prompt='',
    height=000,
    width=000,
    num_inference_steps=000,
    guidance_scale=000,
    negative_prompt='',
).images[0]

# Save Image
image.save("images/" + str(datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')) + ".png")

上記の下記がスケジューラ読み込み部分です

pipe.scheduler = SCHEDULERS_LIST[SCHEDULER].from_pretrained(
    pretrained_model_name_or_path=MODEL_ID,
    torch_dtype=torch.float16,
    cache_dir=CACHE_DIR,
    subfolder='scheduler'
)

SCHEDULERS_LIST[SCHEDULER] の部分がクラス名の選択になります。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?