ジャコ Lab

プログラミング関連のメモ帳的ブログです

ControlNet の Tile を使ってみる

huggingface.co

ようやく diffusers の ControlNet の最後、14個目に到達しました。

Tile というやつを使ってみます

Tile

恐らく、高解像度にするときに使える ControlNet です

投入画像の準備

from diffusers.utils import load_image
from PIL import Image

# 投入画像の準備
init_image_url = "https://huggingface.co/lllyasviel/control_v11f1e_sd15_tile/resolve/main/images/original.png"
init_image = load_image(init_image_url)

def resize_for_condition_image(input_image: Image, resolution: int):
    input_image = input_image.convert("RGB")
    W, H = input_image.size
    k = float(resolution) / min(H, W)
    H *= k
    W *= k
    H = int(round(H / 64.0)) * 64
    W = int(round(W / 64.0)) * 64
    img = input_image.resize((W, H), resample=Image.LANCZOS)
    return img

condition_image = resize_for_condition_image(init_image, 1024)

(左) 投入画像 (64x64) | (右) 変換した画像 (1024x1024)

ControlNet, Pipeline の準備

import torch
from diffusers import ControlNetModel, DiffusionPipeline

# ControlNet の準備
controlnet = ControlNetModel.from_pretrained(
    "lllyasviel/control_v11f1e_sd15_tile",
    torch_dtype=torch.float16
)

# Pipeline の準備
pipe = DiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    custom_pipeline="stable_diffusion_controlnet_img2img",
    torch_dtype=torch.float16,
    controlnet=controlnet
).to('cuda')
pipe.enable_model_cpu_offload()
lpw_stable_diffusionのときに使用した Community pipeline が出てきた!
stable_diffusion_controlnet_img2imgというものを使ってますね!

パイプライン実行

prompt = "best quality"
negative_prompt = "blur, lowres, bad anatomy, bad hands, cropped, worst quality"
image = pipe(
    prompt, 
    negative_prompt=negative_prompt, 
    image=condition_image, 
    controlnet_conditioning_image=condition_image, 
    width=condition_image.size[0],
    height=condition_image.size[1],
    strength=1.0,
    generator=torch.manual_seed(0),
    num_inference_steps=32,
).images[0]
image

ControlNet の Tile で生成された画像

高画質なワンコ!

このワンコを更に大きくできるのか?

64x64 を 2048にした場合

2048x2048 にしてみた画像

逆にぼやけるのね

64x64 を 1024 にしたやつを 2048 にした場合

64x64 を 1024x1024 経由で 2048x2048 にしてみた画像

こっちは綺麗!

まとめ

一気にやり過ぎるとダメなのかもしれない