Diffusers 文档
语义引导
并获取增强的文档体验
开始
语义引导
扩散模型的语义引导在 SEGA: Instructing Text-to-Image Models using Semantic Guidance 中被提出,并为图像生成提供了强大的语义控制。对文本提示的微小更改通常会导致完全不同的输出图像。然而,借助 SEGA,可以对图像进行各种更改,这些更改可以轻松直观地控制,同时保持与原始图像构图的一致性。
论文摘要如下:
文本到图像扩散模型最近因其惊人的能力而备受关注,它们仅通过文本即可生成高保真图像。然而,实现与用户意图一致的一次性生成几乎是不可能的,而且输入提示的微小变化通常会导致图像产生很大差异。这使得用户几乎无法进行语义控制。为了让用户能够掌控,我们展示了如何与扩散过程互动,以沿语义方向灵活地引导它。这种语义引导 (SEGA) 可以推广到任何使用无分类器引导的生成架构。更重要的是,它允许进行细微和广泛的编辑、构图和风格的变化,以及优化整体艺术构思。我们通过在潜在空间和像素空间的扩散模型(如 Stable Diffusion、Paella 和 DeepFloyd-IF)上执行各种任务,证明了 SEGA 的有效性,从而为它的多功能性、灵活性以及相对于现有方法的改进提供了强有力的证据。
请务必查看调度器指南,了解如何探索调度器速度和质量之间的权衡,并查看在多个pipeline中重用组件部分,了解如何有效地将相同组件加载到多个pipeline中。
SemanticStableDiffusionPipeline
class diffusers.SemanticStableDiffusionPipeline
< source >( vae: AutoencoderKL text_encoder: CLIPTextModel tokenizer: CLIPTokenizer unet: UNet2DConditionModel scheduler: KarrasDiffusionSchedulers safety_checker: StableDiffusionSafetyChecker feature_extractor: CLIPImageProcessor requires_safety_checker: bool = True )
参数
- vae (AutoencoderKL) — 变分自编码器 (VAE) 模型,用于将图像编码和解码为潜在表示形式。
- text_encoder (CLIPTextModel) — 冻结的文本编码器 (clip-vit-large-patch14)。
- tokenizer (CLIPTokenizer) — 用于token化文本的
CLIPTokenizer
。 - unet (UNet2DConditionModel) — 用于对编码后的图像潜在空间进行去噪的
UNet2DConditionModel
。 - scheduler (SchedulerMixin) — 与
unet
结合使用以对编码后的图像潜在空间进行去噪的调度器。可以是 DDIMScheduler、LMSDiscreteScheduler 或 PNDMScheduler 之一。 - safety_checker (
Q16SafetyChecker
) — 分类模块,用于评估生成的图像是否可能被认为是冒犯性或有害的。请参阅 模型卡,了解有关模型潜在危害的更多详细信息。 - feature_extractor (CLIPImageProcessor) — 用于从生成的图像中提取特征的
CLIPImageProcessor
;用作safety_checker
的输入。
使用 Stable Diffusion 和潜在空间编辑的文本到图像生成pipeline。
此模型继承自DiffusionPipeline 并基于StableDiffusionPipeline 构建。查看超类文档,了解为所有pipeline实现的通用方法(下载、保存、在特定设备上运行等)。
__call__
< source >( prompt: typing.Union[str, typing.List[str]] height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: int = 50 guidance_scale: float = 7.5 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_images_per_prompt: int = 1 eta: float = 0.0 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: int = 1 editing_prompt: typing.Union[str, typing.List[str], NoneType] = None editing_prompt_embeddings: typing.Optional[torch.Tensor] = None reverse_editing_direction: typing.Union[bool, typing.List[bool], NoneType] = False edit_guidance_scale: typing.Union[float, typing.List[float], NoneType] = 5 edit_warmup_steps: typing.Union[int, typing.List[int], NoneType] = 10 edit_cooldown_steps: typing.Union[int, typing.List[int], NoneType] = None edit_threshold: typing.Union[float, typing.List[float], NoneType] = 0.9 edit_momentum_scale: typing.Optional[float] = 0.1 edit_mom_beta: typing.Optional[float] = 0.4 edit_weights: typing.Optional[typing.List[float]] = None sem_guidance: typing.Optional[typing.List[torch.Tensor]] = None ) → ~pipelines.semantic_stable_diffusion.SemanticStableDiffusionPipelineOutput
or tuple
参数
- prompt (
str
orList[str]
) — 用于引导图像生成的prompt。 - height (
int
, optional, defaults toself.unet.config.sample_size * self.vae_scale_factor
) — 生成图像的高度,以像素为单位。 - width (
int
, optional, defaults toself.unet.config.sample_size * self.vae_scale_factor
) — 生成图像的宽度,以像素为单位。 - num_inference_steps (
int
, optional, defaults to 50) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的图像,但会牺牲更慢的推理速度。 - guidance_scale (
float
, optional, defaults to 7.5) — 较高的引导尺度值鼓励模型生成与文本prompt
紧密相关的图像,但会牺牲较低的图像质量。当guidance_scale > 1
时,启用引导尺度。 - negative_prompt (
str
orList[str]
, optional) — 用于引导图像生成中不包含的内容的prompt。如果未定义,则需要传递negative_prompt_embeds
代替。当不使用引导时(guidance_scale < 1
)忽略。 - num_images_per_prompt (
int
, optional, defaults to 1) — 每个prompt生成的图像数量。 - eta (
float
, optional, defaults to 0.0) — 对应于 DDIM 论文中的参数 eta (η)。仅适用于 DDIMScheduler,在其他调度器中被忽略。 - generator (
torch.Generator
orList[torch.Generator]
, optional) — 用于使生成具有确定性的torch.Generator
。 - latents (
torch.Tensor
, optional) — 预生成的噪声潜在空间,从高斯分布中采样,用作图像生成的输入。可用于通过不同的prompt调整相同的生成。如果未提供,则通过使用提供的随机generator
进行采样来生成潜在空间张量。 - output_type (
str
, optional, defaults to"pil"
) — 生成图像的输出格式。在PIL.Image
或np.array
之间选择。 - return_dict (
bool
, optional, defaults toTrue
) — 是否返回 StableDiffusionPipelineOutput 而不是普通元组。 - callback (
Callable
, optional) — 一个在推理过程中每callback_steps
步调用的函数。该函数被以下参数调用:callback(step: int, timestep: int, latents: torch.Tensor)
。 - callback_steps (
int
, optional, defaults to 1) — 调用callback
函数的频率。如果未指定,则在每个步骤都调用回调。 - editing_prompt (
str
orList[str]
, optional) — 用于语义引导的提示或提示列表。通过设置editing_prompt = None
禁用语义引导。提示的引导方向应通过reverse_editing_direction
指定。 - editing_prompt_embeddings (
torch.Tensor
, optional) — 用于语义引导的预计算嵌入。嵌入的引导方向应通过reverse_editing_direction
指定。 - reverse_editing_direction (
bool
orList[bool]
, optional, defaults toFalse
) — 指示editing_prompt
中相应提示是应该增加还是减少。 - edit_guidance_scale (
float
orList[float]
, optional, defaults to 5) — 语义引导的引导比例。如果以列表形式提供,则值应与editing_prompt
相对应。 - edit_warmup_steps (
float
orList[float]
, optional, defaults to 10) — 不应用语义引导的扩散步骤数(对于每个提示)。动量在这些步骤中计算,并在所有预热期结束后应用。 - edit_cooldown_steps (
float
orList[float]
, optional, defaults toNone
) — 之后不再应用语义引导的扩散步骤数(对于每个提示)。 - edit_threshold (
float
orList[float]
, optional, defaults to 0.9) — 语义引导的阈值。 - edit_momentum_scale (
float
, optional, defaults to 0.1) — 添加到每个扩散步骤的语义引导的动量比例。如果设置为 0.0,则禁用动量。动量已经在预热期间建立(对于小于sld_warmup_steps
的扩散步骤)。动量仅在所有预热期结束后添加到潜在引导。 - edit_mom_beta (
float
, optional, defaults to 0.4) — 定义语义引导动量如何建立。edit_mom_beta
指示保留先前动量的多少。动量已经在预热期间建立(对于小于edit_warmup_steps
的扩散步骤)。 - edit_weights (
List[float]
, optional, defaults toNone
) — 指示每个单独的概念应在多大程度上影响整体引导。如果未提供权重,则所有概念均等地应用。 - sem_guidance (
List[torch.Tensor]
, optional) — 要在生成时应用的预生成引导向量列表。列表的长度必须与num_inference_steps
相对应。
Returns
~pipelines.semantic_stable_diffusion.SemanticStableDiffusionPipelineOutput
或 tuple
如果 return_dict
为 True
,则返回 ~pipelines.semantic_stable_diffusion.SemanticStableDiffusionPipelineOutput
,否则返回 tuple
,其中第一个元素是包含生成图像的列表,第二个元素是 bool
列表,指示相应的生成图像是否包含“不适合工作场所观看”(nsfw)内容。
用于生成的pipeline的调用函数。
示例
>>> import torch
>>> from diffusers import SemanticStableDiffusionPipeline
>>> pipe = SemanticStableDiffusionPipeline.from_pretrained(
... "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
... )
>>> pipe = pipe.to("cuda")
>>> out = pipe(
... prompt="a photo of the face of a woman",
... num_images_per_prompt=1,
... guidance_scale=7,
... editing_prompt=[
... "smiling, smile", # Concepts to apply
... "glasses, wearing glasses",
... "curls, wavy hair, curly hair",
... "beard, full beard, mustache",
... ],
... reverse_editing_direction=[
... False,
... False,
... False,
... False,
... ], # Direction of guidance i.e. increase all concepts
... edit_warmup_steps=[10, 10, 10, 10], # Warmup period for each concept
... edit_guidance_scale=[4, 5, 5, 5.4], # Guidance scale for each concept
... edit_threshold=[
... 0.99,
... 0.975,
... 0.925,
... 0.96,
... ], # Threshold for each concept. Threshold equals the percentile of the latent space that will be discarded. I.e. threshold=0.99 uses 1% of the latent dimensions
... edit_momentum_scale=0.3, # Momentum scale that will be added to the latent guidance
... edit_mom_beta=0.6, # Momentum beta
... edit_weights=[1, 1, 1, 1, 1], # Weights of the individual concepts against each other
... )
>>> image = out.images[0]
SemanticStableDiffusionPipelineOutput
class diffusers.pipelines.semantic_stable_diffusion.pipeline_output.SemanticStableDiffusionPipelineOutput
< source >( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] nsfw_content_detected: typing.Optional[typing.List[bool]] )
Stable Diffusion pipeline 的输出类。