Diffusers documentation

프롬프팅

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

프롬프팅

Prompts는 모델이 생성해야 할 내용을 설명합니다. 좋은 Prompts는 상세하고 구체적이며 구조화되어 있어 더 나은 이미지와 비디오를 생성합니다.

이 가이드는 효과적인 프롬프트 작성 방법을 보여주고, 프롬프트를 더 강력하게 만드는 기법을 소개합니다.

좋은 프롬프트 작성

효과적인 프롬프트에는 세 가지 핵심 요소가 필요합니다.

  1. Subject - 생성하려는 대상. 프롬프트를 여기서 시작하세요.
  2. 스타일 - 매체 또는 미학. 어떻게 보여야 하는가?
  3. Context - 행동, 설정, 무드에 대한 세부 정보.

이 요소들을 키워드 목록이 아닌 구조화된 서술로 활용하세요. 최신 모델은 키워드 매칭보다 자연어 이해 능력이 더 뛰어납니다. 먼저 간단하게 시작하고, 점차 세부 사항을 추가해 나가세요.

더 나은 프롬프트를 만드는 데에는 컨텍스트가 특히 중요합니다. 조명, 예술적 디테일, 무드를 추가해 보세요.

평화로운 여름 오후, 풀장 속 잎사귀에 누워 있는 귀여운 고양이, 로파이 아트 스타일, 일러스트.
평화로운 여름 오후, 반짝이는 연못 위 떠 있는 잎사귀에 귀여운 고양이가 늘어져 있습니다. 맑은 물결이 물 표면을 따라 일렁이고, 햇살이 부드럽고 매끄러운 하이라이트를 드리웁니다. 이 일러스트는 세련된 선과 조화로운 색감으로 섬세하고 정교하게 표현되어 있으며, 애니메이션에서 영감을 받은 시각적으로 편안한 무드와 함께 이완되고 고요하며 환상적인 로파이(Lo-fi) 분위기를 자아냅니다.

구체적으로 작성하고 컨텍스트를 추가하세요. 렌즈 유형, 초점 거리, 카메라 앵글, 심도(Depth of Field)와 같은 사진 용어를 사용하세요.

프롬프트 구조를 개선하는 데 도움이 되는 prompt enhancer를 사용해 보세요.

가이던스와 네거티브

대부분의 Diffusers 파이프라인은 여전히 classifier-free guidance와 선택적 negative_prompt를 사용하여 샘플링을 제어합니다.

Stable Diffusion 계열 파이프라인에서는 guidance_scale과 negative_prompt를 전달합니다. guidance_scale 값이 높을수록 프롬프트를 더 밀접하게 따릅니다. 값이 너무 높으면 결과가 부자연스럽거나 과포화되어 보일 수 있습니다.

import torch
from diffusers import DiffusionPipeline

pipeline = DiffusionPipeline.from_pretrained(
    "stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16, device_map="cuda"  # or "mps", "xpu", "cpu"
)
image = pipeline(
    prompt="a cozy reading nook with afternoon light",
    negative_prompt="blurry, low quality, distorted",
    guidance_scale=7.5,
).images[0]

Qwen-Image, Flux와 같은 새로운 체크포인트에서는 일반적으로 true_cfg_scale과 negative_prompt를 함께 사용하여 클래식 CFG를 적용합니다. true CFG가 활성화된 경우(true_cfg_scale > 1이고 negative_prompt가 있는 경우)에도 negative는 여전히 적용됩니다. guidance_scale 인수는 transformer가 이를 지원하는 경우 distilled 또는 embedded guidance를 의미하며, 이는 true CFG와 동일하지 않습니다. 로드하는 모델의 pipeline API를 확인하세요.

Modular Diffusers는 guidance 알고리즘을 guider로 대체할 수 있습니다. Guiders를 참조하세요.

프롬프트 가중치

Prompt weighting은 일부 단어의 강도를 높이고 다른 단어의 강도를 낮춥니다. attention score를 스케일링하여 각 개념이 미치는 영향의 정도를 제어할 수 있습니다.

Diffusers는 prompt_embeds와 pooled_prompt_embeds 인수를 통해 이를 처리하며, 이 인수는 스케일링된 텍스트 임베딩 벡터를 받습니다. 이러한 임베딩을 생성하려면 sd_embed 라이브러리를 사용하세요. 또한 더 긴 프롬프트도 지원합니다. 더 간단한 구문으로 Stable Diffusion 계열의 가중치를 적용하려면 Compel을 사용할 수도 있습니다.

sd_embed 라이브러리는 Stable Diffusion, Stable Diffusion XL, Stable Diffusion 3, Stable Cascade, Flux만 지원합니다. 프롬프트 가중치는 이미 프롬프트를 충실히 따르는 Flux 계열 모델에서는 항상 도움이 되지 않습니다.

uv pip install git+https://github.com/xhinker/sd_embed.git@main

수치 배율 또는 괄호를 사용하여 가중 텍스트를 포맷합니다. 괄호가 많을수록 가중치가 강해집니다.

형식multiplier
(cat)1.1배 증가
((cat))1.21배 증가
(cat:1.5)1.5배 증가
(cat:0.5)weight를 0.5×로 설정

가중 프롬프트를 생성하여 get_weighted_text_embeddings_sdxl에 전달하고 임베딩을 생성합니다.

negative_prompt_embeds와 negative_pooled_prompt_embeds에 negative prompt를 전달할 수도 있습니다.

import torch
from diffusers import DiffusionPipeline
from sd_embed.embedding_funcs import get_weighted_text_embeddings_sdxl

pipeline = DiffusionPipeline.from_pretrained(
    "Lykon/dreamshaper-xl-1-0", dtype=torch.bfloat16, device_map="cuda"  # or "mps", "xpu", "cpu"
)

prompt = """
A (cute cat:1.4) lounges on a (floating leaf:1.2) in a (sparkling pool:1.1) during a peaceful summer afternoon.
Gentle ripples reflect pastel skies, while (sunlight:1.1) casts soft highlights. The illustration is smooth and polished
with elegant, sketchy lines and subtle gradients, evoking a ((whimsical, nostalgic, dreamy lofi atmosphere:2.0)), 
(anime-inspired:1.6), calming, comforting, and visually serene.
"""

prompt_embeds, _, pooled_prompt_embeds, *_ = get_weighted_text_embeddings_sdxl(pipeline, prompt=prompt)

prompt_embeds와 pooled_prompt_embeds에 임베딩을 전달하여 이미지를 생성하세요.

image = pipeline(prompt_embeds=prompt_embeds, pooled_prompt_embeds=pooled_prompt_embeds).images[0]
Update on GitHub