svjack's picture
Upload 1392 files
43b7e92 verified
|
raw
history blame
18.6 kB

์ปค๋ฎค๋‹ˆํ‹ฐ ํŒŒ์ดํ”„๋ผ์ธ

**์ปค๋ฎค๋‹ˆํ‹ฐ ํŒŒ์ดํ”„๋ผ์ธ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ด ์ด์Šˆ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

์ปค๋ฎค๋‹ˆํ‹ฐ ์˜ˆ์ œ๋Š” ์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ ์ถ”๊ฐ€ํ•œ ์ถ”๋ก  ๋ฐ ํ›ˆ๋ จ ์˜ˆ์ œ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ํ‘œ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ๋ชจ๋“  ์ปค๋ฎค๋‹ˆํ‹ฐ ์˜ˆ์ œ์— ๋Œ€ํ•œ ๊ฐœ์š”๋ฅผ ํ™•์ธํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ฝ”๋“œ ์˜ˆ์ œ๋ฅผ ํด๋ฆญํ•˜๋ฉด ๋ณต์‚ฌํ•˜์—ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ ์˜ˆ์ œ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ปค๋ฎค๋‹ˆํ‹ฐ๊ฐ€ ์˜ˆ์ƒ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์ด์Šˆ๋ฅผ ๊ฐœ์„คํ•˜๊ณ  ์ž‘์„ฑ์ž์—๊ฒŒ ํ•‘์„ ๋ณด๋‚ด์ฃผ์„ธ์š”.

์˜ˆ ์„ค๋ช… ์ฝ”๋“œ ์˜ˆ์ œ ์ฝœ๋žฉ ์ €์ž
CLIP Guided Stable Diffusion CLIP ๊ฐ€์ด๋“œ ๊ธฐ๋ฐ˜์˜ Stable Diffusion์œผ๋กœ ํ…์ŠคํŠธ์—์„œ ์ด๋ฏธ์ง€๋กœ ์ƒ์„ฑํ•˜๊ธฐ CLIP Guided Stable Diffusion ์ฝœ๋žฉ์—์„œ ์—ด๊ธฐ Suraj Patil
One Step U-Net (Dummy) ์ปค๋ฎค๋‹ˆํ‹ฐ ํŒŒ์ดํ”„๋ผ์ธ์„ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์˜ˆ์‹œ(์ฐธ๊ณ  https://github.com/huggingface/diffusers/issues/841) One Step U-Net - Patrick von Platen
Stable Diffusion Interpolation ์„œ๋กœ ๋‹ค๋ฅธ ํ”„๋กฌํ”„ํŠธ/์‹œ๋“œ ๊ฐ„ Stable Diffusion์˜ latent space ๋ณด๊ฐ„ Stable Diffusion Interpolation - Nate Raw
Stable Diffusion Mega ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ๊ฐ–์ถ˜ ํ•˜๋‚˜์˜ Stable Diffusion ํŒŒ์ดํ”„๋ผ์ธ Text2Image, Image2Image and Inpainting Stable Diffusion Mega - Patrick von Platen
Long Prompt Weighting Stable Diffusion ํ† ํฐ ๊ธธ์ด ์ œํ•œ์ด ์—†๊ณ  ํ”„๋กฌํ”„ํŠธ์—์„œ ํŒŒ์‹ฑ ๊ฐ€์ค‘์น˜ ์ง€์›์„ ํ•˜๋Š” ํ•˜๋‚˜์˜ Stable Diffusion ํŒŒ์ดํ”„๋ผ์ธ, Long Prompt Weighting Stable Diffusion - SkyTNT
Speech to Image ์ž๋™ ์Œ์„ฑ ์ธ์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  Stable Diffusion์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. Speech to Image - Mikail Duzenli

์ปค์Šคํ…€ ํŒŒ์ดํ”„๋ผ์ธ์„ ๋ถˆ๋Ÿฌ์˜ค๋ ค๋ฉด diffusers/examples/community์— ์žˆ๋Š” ํŒŒ์ผ ์ค‘ ํ•˜๋‚˜๋กœ์„œ custom_pipeline ์ธ์ˆ˜๋ฅผ DiffusionPipeline์— ์ „๋‹ฌํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ž์‹ ๋งŒ์˜ ํŒŒ์ดํ”„๋ผ์ธ์ด ์žˆ๋Š” PR์„ ๋ณด๋‚ด์ฃผ์‹œ๋ฉด ๋น ๋ฅด๊ฒŒ ๋ณ‘ํ•ฉํ•ด๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

pipe = DiffusionPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4", custom_pipeline="filename_in_the_community_folder"
)

์‚ฌ์šฉ ์˜ˆ์‹œ

CLIP ๊ฐ€์ด๋“œ ๊ธฐ๋ฐ˜์˜ Stable Diffusion

๋ชจ๋“  ๋…ธ์ด์ฆˆ ์ œ๊ฑฐ ๋‹จ๊ณ„์—์„œ ์ถ”๊ฐ€ CLIP ๋ชจ๋ธ์„ ํ†ตํ•ด Stable Diffusion์„ ๊ฐ€์ด๋“œํ•จ์œผ๋กœ์จ CLIP ๋ชจ๋ธ ๊ธฐ๋ฐ˜์˜ Stable Diffusion์€ ๋ณด๋‹ค ๋” ์‚ฌ์‹ค์ ์ธ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ์ฝ”๋“œ๋Š” ์•ฝ 12GB์˜ GPU RAM์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

from diffusers import DiffusionPipeline
from transformers import CLIPImageProcessor, CLIPModel
import torch


feature_extractor = CLIPImageProcessor.from_pretrained("laion/CLIP-ViT-B-32-laion2B-s34B-b79K")
clip_model = CLIPModel.from_pretrained("laion/CLIP-ViT-B-32-laion2B-s34B-b79K", torch_dtype=torch.float16)


guided_pipeline = DiffusionPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4",
    custom_pipeline="clip_guided_stable_diffusion",
    clip_model=clip_model,
    feature_extractor=feature_extractor,
    torch_dtype=torch.float16,
)
guided_pipeline.enable_attention_slicing()
guided_pipeline = guided_pipeline.to("cuda")

prompt = "fantasy book cover, full moon, fantasy forest landscape, golden vector elements, fantasy magic, dark light night, intricate, elegant, sharp focus, illustration, highly detailed, digital painting, concept art, matte, art by WLOP and Artgerm and Albert Bierstadt, masterpiece"

generator = torch.Generator(device="cuda").manual_seed(0)
images = []
for i in range(4):
    image = guided_pipeline(
        prompt,
        num_inference_steps=50,
        guidance_scale=7.5,
        clip_guidance_scale=100,
        num_cutouts=4,
        use_cutouts=False,
        generator=generator,
    ).images[0]
    images.append(image)

# ์ด๋ฏธ์ง€ ๋กœ์ปฌ์— ์ €์žฅํ•˜๊ธฐ
for i, img in enumerate(images):
    img.save(f"./clip_guided_sd/image_{i}.png")

์ด๋ฏธ์ง€` ๋ชฉ๋ก์—๋Š” ๋กœ์ปฌ์— ์ €์žฅํ•˜๊ฑฐ๋‚˜ ๊ตฌ๊ธ€ ์ฝœ๋žฉ์— ์ง์ ‘ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๋Š” PIL ์ด๋ฏธ์ง€ ๋ชฉ๋ก์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์•ˆ์ •์ ์ธ ํ™•์‚ฐ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํ’ˆ์งˆ์ด ๋†’์€ ๊ฒฝํ–ฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์œ„์˜ ์Šคํฌ๋ฆฝํŠธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค:

clip_guidance.

One Step Unet

์˜ˆ์‹œ "one-step-unet"๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

from diffusers import DiffusionPipeline

pipe = DiffusionPipeline.from_pretrained("google/ddpm-cifar10-32", custom_pipeline="one_step_unet")
pipe()

์ฐธ๊ณ : ์ด ์ปค๋ฎค๋‹ˆํ‹ฐ ํŒŒ์ดํ”„๋ผ์ธ์€ ๊ธฐ๋Šฅ์œผ๋กœ ์œ ์šฉํ•˜์ง€ ์•Š์œผ๋ฉฐ ์ปค๋ฎค๋‹ˆํ‹ฐ ํŒŒ์ดํ”„๋ผ์ธ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์˜ ์˜ˆ์‹œ์ผ ๋ฟ์ž…๋‹ˆ๋‹ค(https://github.com/huggingface/diffusers/issues/841 ์ฐธ์กฐ).

Stable Diffusion Interpolation

๋‹ค์Œ ์ฝ”๋“œ๋Š” ์ตœ์†Œ 8GB VRAM์˜ GPU์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์•ฝ 5๋ถ„ ์ •๋„ ์†Œ์š”๋ฉ๋‹ˆ๋‹ค.

from diffusers import DiffusionPipeline
import torch

pipe = DiffusionPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4",
    torch_dtype=torch.float16,
    safety_checker=None,  # Very important for videos...lots of false positives while interpolating
    custom_pipeline="interpolate_stable_diffusion",
).to("cuda")
pipe.enable_attention_slicing()

frame_filepaths = pipe.walk(
    prompts=["a dog", "a cat", "a horse"],
    seeds=[42, 1337, 1234],
    num_interpolation_steps=16,
    output_dir="./dreams",
    batch_size=4,
    height=512,
    width=512,
    guidance_scale=8.5,
    num_inference_steps=50,
)

walk(...)ํ•จ์ˆ˜์˜ ์ถœ๋ ฅ์€output_dir`์— ์ •์˜๋œ ๋Œ€๋กœ ํด๋”์— ์ €์žฅ๋œ ์ด๋ฏธ์ง€ ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ˆ์ •์ ์œผ๋กœ ํ™•์‚ฐ๋˜๋Š” ๋™์˜์ƒ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•ˆ์ •๋œ ํ™•์‚ฐ์„ ์ด์šฉํ•œ ๋™์˜์ƒ ์ œ์ž‘ ๋ฐฉ๋ฒ•๊ณผ ๋” ๋งŽ์€ ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ https://github.com/nateraw/stable-diffusion-videos ์—์„œ ํ™•์ธํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

Stable Diffusion Mega

The Stable Diffusion Mega ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•˜๋ฉด Stable Diffusion ํŒŒ์ดํ”„๋ผ์ธ์˜ ์ฃผ์š” ์‚ฌ์šฉ ์‚ฌ๋ก€๋ฅผ ๋‹จ์ผ ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

#!/usr/bin/env python3
from diffusers import DiffusionPipeline
import PIL
import requests
from io import BytesIO
import torch


def download_image(url):
    response = requests.get(url)
    return PIL.Image.open(BytesIO(response.content)).convert("RGB")


pipe = DiffusionPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4",
    custom_pipeline="stable_diffusion_mega",
    torch_dtype=torch.float16,
)
pipe.to("cuda")
pipe.enable_attention_slicing()


### Text-to-Image

images = pipe.text2img("An astronaut riding a horse").images

### Image-to-Image

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

prompt = "A fantasy landscape, trending on artstation"

images = pipe.img2img(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5).images

### Inpainting

img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
init_image = download_image(img_url).resize((512, 512))
mask_image = download_image(mask_url).resize((512, 512))

prompt = "a cat sitting on a bench"
images = pipe.inpaint(prompt=prompt, image=init_image, mask_image=mask_image, strength=0.75).images

์œ„์— ํ‘œ์‹œ๋œ ๊ฒƒ์ฒ˜๋Ÿผ ํ•˜๋‚˜์˜ ํŒŒ์ดํ”„๋ผ์ธ์—์„œ 'ํ…์ŠคํŠธ-์ด๋ฏธ์ง€ ๋ณ€ํ™˜', '์ด๋ฏธ์ง€-์ด๋ฏธ์ง€ ๋ณ€ํ™˜', '์ธํŽ˜์ธํŒ…'์„ ๋ชจ๋‘ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Long Prompt Weighting Stable Diffusion

ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•˜๋ฉด 77๊ฐœ์˜ ํ† ํฐ ๊ธธ์ด ์ œํ•œ ์—†์ด ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ "()"๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹จ์–ด ๊ฐ€์ค‘์น˜๋ฅผ ๋†’์ด๊ฑฐ๋‚˜ "[]"๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹จ์–ด ๊ฐ€์ค‘์น˜๋ฅผ ๋‚ฎ์ถœ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‹จ์ผ ํด๋ž˜์Šค์—์„œ Stable Diffusion ํŒŒ์ดํ”„๋ผ์ธ์˜ ์ฃผ์š” ์‚ฌ์šฉ ์‚ฌ๋ก€๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

pytorch

from diffusers import DiffusionPipeline
import torch

pipe = DiffusionPipeline.from_pretrained(
    "hakurei/waifu-diffusion", custom_pipeline="lpw_stable_diffusion", torch_dtype=torch.float16
)
pipe = pipe.to("cuda")

prompt = "best_quality (1girl:1.3) bow bride brown_hair closed_mouth frilled_bow frilled_hair_tubes frills (full_body:1.3) fox_ear hair_bow hair_tubes happy hood japanese_clothes kimono long_sleeves red_bow smile solo tabi uchikake white_kimono wide_sleeves cherry_blossoms"
neg_prompt = "lowres, bad_anatomy, error_body, error_hair, error_arm, error_hands, bad_hands, error_fingers, bad_fingers, missing_fingers, error_legs, bad_legs, multiple_legs, missing_legs, error_lighting, error_shadow, error_reflection, text, error, extra_digit, fewer_digits, cropped, worst_quality, low_quality, normal_quality, jpeg_artifacts, signature, watermark, username, blurry"

pipe.text2img(prompt, negative_prompt=neg_prompt, width=512, height=512, max_embeddings_multiples=3).images[0]

onnxruntime

from diffusers import DiffusionPipeline
import torch

pipe = DiffusionPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4",
    custom_pipeline="lpw_stable_diffusion_onnx",
    revision="onnx",
    provider="CUDAExecutionProvider",
)

prompt = "a photo of an astronaut riding a horse on mars, best quality"
neg_prompt = "lowres, bad anatomy, error body, error hair, error arm, error hands, bad hands, error fingers, bad fingers, missing fingers, error legs, bad legs, multiple legs, missing legs, error lighting, error shadow, error reflection, text, error, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry"

pipe.text2img(prompt, negative_prompt=neg_prompt, width=512, height=512, max_embeddings_multiples=3).images[0]

ํ† ํฐ ์ธ๋ฑ์Šค ์‹œํ€€์Šค ๊ธธ์ด๊ฐ€ ์ด ๋ชจ๋ธ์— ์ง€์ •๋œ ์ตœ๋Œ€ ์‹œํ€€์Šค ๊ธธ์ด๋ณด๋‹ค ๊ธธ๋ฉด(*** > 77). ์ด ์‹œํ€€์Šค๋ฅผ ๋ชจ๋ธ์—์„œ ์‹คํ–‰ํ•˜๋ฉด ์ธ๋ฑ์‹ฑ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค`. ์ •์ƒ์ ์ธ ํ˜„์ƒ์ด๋‹ˆ ๊ฑฑ์ •ํ•˜์ง€ ๋งˆ์„ธ์š”.

Speech to Image

๋‹ค์Œ ์ฝ”๋“œ๋Š” ์‚ฌ์ „ํ•™์Šต๋œ OpenAI whisper-small๊ณผ Stable Diffusion์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜ค๋””์˜ค ์ƒ˜ํ”Œ์—์„œ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

import torch

import matplotlib.pyplot as plt
from datasets import load_dataset
from diffusers import DiffusionPipeline
from transformers import (
    WhisperForConditionalGeneration,
    WhisperProcessor,
)


device = "cuda" if torch.cuda.is_available() else "cpu"

ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")

audio_sample = ds[3]

text = audio_sample["text"].lower()
speech_data = audio_sample["audio"]["array"]

model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small").to(device)
processor = WhisperProcessor.from_pretrained("openai/whisper-small")

diffuser_pipeline = DiffusionPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4",
    custom_pipeline="speech_to_image_diffusion",
    speech_model=model,
    speech_processor=processor,
    
    torch_dtype=torch.float16,
)

diffuser_pipeline.enable_attention_slicing()
diffuser_pipeline = diffuser_pipeline.to(device)

output = diffuser_pipeline(speech_data)
plt.imshow(output.images[0])

์œ„ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ์˜ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€๋ฅผ ๋ณด์ž…๋‹ˆ๋‹ค.

image