Upamanyu098's picture
End of training
ef4d689 verified
|
raw
history blame
4.04 kB

ํ…์ŠคํŠธ ๊ธฐ๋ฐ˜ image-to-image ์ƒ์„ฑ

[[open-in-colab]]

[StableDiffusionImg2ImgPipeline]์„ ์‚ฌ์šฉํ•˜๋ฉด ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ์™€ ์‹œ์ž‘ ์ด๋ฏธ์ง€๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์ƒˆ ์ด๋ฏธ์ง€ ์ƒ์„ฑ์˜ ์กฐ๊ฑด์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋ชจ๋‘ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”:

!pip install diffusers transformers ftfy accelerate

nitrosocke/Ghibli-Diffusion๊ณผ ๊ฐ™์€ ์‚ฌ์ „ํ•™์Šต๋œ stable diffusion ๋ชจ๋ธ๋กœ [StableDiffusionImg2ImgPipeline]์„ ์ƒ์„ฑํ•˜์—ฌ ์‹œ์ž‘ํ•˜์„ธ์š”.

import torch
import requests
from PIL import Image
from io import BytesIO
from diffusers import StableDiffusionImg2ImgPipeline

device = "cuda"
pipe = StableDiffusionImg2ImgPipeline.from_pretrained("nitrosocke/Ghibli-Diffusion", torch_dtype=torch.float16).to(
    device
)

์ดˆ๊ธฐ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์‚ฌ์ „ ์ฒ˜๋ฆฌํ•˜์—ฌ ํŒŒ์ดํ”„๋ผ์ธ์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"

response = requests.get(url)
init_image = Image.open(BytesIO(response.content)).convert("RGB")
init_image.thumbnail((768, 768))
init_image

๐Ÿ’ก strength๋Š” ์ž…๋ ฅ ์ด๋ฏธ์ง€์— ์ถ”๊ฐ€๋˜๋Š” ๋…ธ์ด์ฆˆ์˜ ์–‘์„ ์ œ์–ดํ•˜๋Š” 0.0์—์„œ 1.0 ์‚ฌ์ด์˜ ๊ฐ’์ž…๋‹ˆ๋‹ค. 1.0์— ๊ฐ€๊นŒ์šด ๊ฐ’์€ ๋‹ค์–‘ํ•œ ๋ณ€ํ˜•์„ ํ—ˆ์šฉํ•˜์ง€๋งŒ ์ž…๋ ฅ ์ด๋ฏธ์ง€์™€ ์˜๋ฏธ์ ์œผ๋กœ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ •์˜ํ•˜๊ณ (์ง€๋ธŒ๋ฆฌ ์Šคํƒ€์ผ(Ghibli-style)์— ๋งž๊ฒŒ ์กฐ์ •๋œ ์ด ์ฒดํฌํฌ์ธํŠธ์˜ ๊ฒฝ์šฐ ํ”„๋กฌํ”„ํŠธ ์•ž์— ghibli style ํ† ํฐ์„ ๋ถ™์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค) ํŒŒ์ดํ”„๋ผ์ธ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค:

prompt = "ghibli style, a fantasy landscape with castles"
generator = torch.Generator(device=device).manual_seed(1024)
image = pipe(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5, generator=generator).images[0]
image

๋‹ค๋ฅธ ์Šค์ผ€์ค„๋Ÿฌ๋กœ ์‹คํ—˜ํ•˜์—ฌ ์ถœ๋ ฅ์— ์–ด๋–ค ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค:

from diffusers import LMSDiscreteScheduler

lms = LMSDiscreteScheduler.from_config(pipe.scheduler.config)
pipe.scheduler = lms
generator = torch.Generator(device=device).manual_seed(1024)
image = pipe(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5, generator=generator).images[0]
image

์•„๋ž˜ ๊ณต๋ฐฑ์„ ํ™•์ธํ•˜๊ณ  strength ๊ฐ’์„ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •ํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ด ๋ณด์„ธ์š”. strength๋ฅผ ๋‚ฎ๊ฒŒ ์„ค์ •ํ•˜๋ฉด ์›๋ณธ ์ด๋ฏธ์ง€์™€ ๋” ์œ ์‚ฌํ•œ ์ด๋ฏธ์ง€๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž์œ ๋กญ๊ฒŒ ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ [LMSDiscreteScheduler]๋กœ ์ „ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅ์— ์–ด๋–ค ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์„ธ์š”.