LEDITS++
LEDITS++ 在 LEDITS++: Limitless Image Editing using Text-to-Image Models (Manuel Brack, Felix Friedrich, Katharina Kornmeier, Linoy Tsaban, Patrick Schramowski, Kristian Kersting, Apolinário Passos 提出)。
该论文的摘要如下:
文本到图像扩散模型最近因其惊人的能力而受到越来越多的关注,它们仅从文本输入就能生成高保真图像。随后的研究工作旨在利用它们的能力并将其应用于真实图像编辑。然而,现有的图像到图像方法通常效率低下、不精确且用途有限。它们要么需要耗时的微调,要么不必要地 сильно 偏离输入图像,和/或缺乏对多个同时编辑的支持。为了解决这些问题,我们介绍了 LEDITS++,这是一种高效、通用且精确的文本图像操作技术。LEDITS++ 的新型反演方法无需调整或优化,并且只需几个扩散步骤即可生成高保真结果。其次,我们的方法支持多个同时编辑,并且与架构无关。第三,我们使用一种新颖的隐式掩蔽技术,该技术将更改限制在相关的图像区域。我们提出了新型 TEdBench++ 基准测试作为我们详尽评估的一部分。我们的结果证明了 LEDITS++ 的能力及其相对于先前方法的改进。项目页面可在 https://leditsplusplus-project.static.hf.space 找到。
我们提供了两个基于不同预训练模型的不同管线。
LEditsPPPipelineStableDiffusion
class diffusers.LEditsPPPipelineStableDiffusion
< source >( vae: AutoencoderKL text_encoder: CLIPTextModel tokenizer: CLIPTokenizer unet: UNet2DConditionModel scheduler: Union safety_checker: StableDiffusionSafetyChecker feature_extractor: CLIPImageProcessor requires_safety_checker: bool = True )
参数
- vae (AutoencoderKL) — 变分自编码器 (VAE) 模型,用于将图像编码和解码为潜在表示。
- text_encoder (CLIPTextModel) — 冻结的文本编码器。Stable Diffusion 使用 CLIP 的文本部分,特别是 clip-vit-large-patch14 变体。
- tokenizer (CLIPTokenizer) — Class CLIPTokenizer 的分词器。
- unet (UNet2DConditionModel) — 用于去噪编码图像潜在空间的条件 U-Net 架构。
- scheduler (DPMSolverMultistepScheduler or DDIMScheduler) — 与
unet
结合使用的调度器,用于去噪编码图像潜在空间。可以是 DPMSolverMultistepScheduler 或 DDIMScheduler 之一。如果传递任何其他调度器,它将自动设置为 DPMSolverMultistepScheduler。 - safety_checker (
StableDiffusionSafetyChecker
) — 评估生成图像是否可能被认为具有攻击性或有害的分类模块。请参阅 模型卡片 了解详细信息。 - feature_extractor (CLIPImageProcessor) — 从生成的图像中提取特征的模型,用作
safety_checker
的输入。
使用 LEDits++ 和 Stable Diffusion 进行文本图像编辑的 Pipeline。
此模型继承自 DiffusionPipeline,并构建于 StableDiffusionPipeline 之上。查看超类文档以获取为所有 pipelines 实现的通用方法(下载、保存、在特定设备上运行等)。
__call__
< source >( negative_prompt: Union = None generator: Union = None output_type: Optional = 'pil' return_dict: bool = True editing_prompt: Union = None editing_prompt_embeds: Optional = None negative_prompt_embeds: Optional = None reverse_editing_direction: Union = False edit_guidance_scale: Union = 5 edit_warmup_steps: Union = 0 edit_cooldown_steps: Union = None edit_threshold: Union = 0.9 user_mask: Optional = None sem_guidance: Optional = None use_cross_attn_mask: bool = False use_intersect_mask: bool = True attn_store_steps: Optional = [] store_averaged_over_steps: bool = True cross_attention_kwargs: Optional = None guidance_rescale: float = 0.0 clip_skip: Optional = None callback_on_step_end: Optional = None callback_on_step_end_tensor_inputs: List = ['latents'] **kwargs ) → LEditsPPDiffusionPipelineOutput or tuple
参数
- negative_prompt (
str
orList[str]
, optional) — 不引导图像生成的 prompt 或 prompts。当不使用 guidance 时忽略(即,如果guidance_scale
小于1
则忽略)。 - generator (
torch.Generator
, optional) — 用于使生成结果具有确定性的一个或一组 torch generator(s)。 - output_type (
str
, optional, defaults to"pil"
) — 生成图像的输出格式。在 PIL:PIL.Image.Image
或np.array
之间选择。 - return_dict (
bool
, optional, defaults toTrue
) — 是否返回 LEditsPPDiffusionPipelineOutput 而不是普通 tuple。 - editing_prompt (
str
orList[str]
, optional) — 用于引导图像生成的 prompt 或 prompts。图像通过设置editing_prompt = None
来重建。prompt 的引导方向应通过reverse_editing_direction
指定。 - editing_prompt_embeds (
torch.Tensor>
, optional) — 预先计算的 embeddings,用于引导图像生成。embedding 的引导方向应通过reverse_editing_direction
指定。 - negative_prompt_embeds (
torch.Tensor
, optional) — 预生成的负面文本 embeddings。可用于轻松调整文本输入(prompt 权重)。如果未提供,则negative_prompt_embeds
从negative_prompt
输入参数生成。 - reverse_editing_direction (
bool
orList[bool]
, optional, defaults toFalse
) —editing_prompt
中对应的 prompt 是应该增加还是减少。 - edit_guidance_scale (
float
orList[float]
, optional, defaults to 5) — 用于引导图像生成的 Guidance scale。如果作为列表提供,则值应与editing_prompt
相对应。edit_guidance_scale
定义为 LEDITS++ 论文 等式 12 中的s_e
。 - edit_warmup_steps (
float
orList[float]
, optional, defaults to 10) — 不应用 guidance 的扩散步数(对于每个 prompt)。 - edit_cooldown_steps (
float
orList[float]
, optional, defaults toNone
) — 超过此扩散步数(对于每个 prompt)后将不再应用 guidance。 - edit_threshold (
float
orList[float]
, optional, defaults to 0.9) — guidance 的 masking 阈值。阈值应与修改的图像区域成比例。‘edit_threshold’ 定义为 LEDITS++ 论文 等式 12 中的 ‘λ’。 - user_mask (
torch.Tensor
, optional) — 用户提供的 mask,用于更好地控制编辑过程。当 LEDITS++ 的隐式 masks 不符合用户偏好时,这很有用。 - sem_guidance (
List[torch.Tensor]
, optional) — 要在生成时应用的预生成 guidance 向量列表。列表的长度必须与num_inference_steps
相对应。 - use_cross_attn_mask (
bool
, defaults toFalse
) — 是否使用 cross-attention masks。当 use_intersect_mask 设置为 true 时,始终使用 Cross-attention masks。Cross-attention masks 定义为 LEDITS++ 论文 等式 12 中的 ‘M^1’。 - use_intersect_mask (
bool
, defaults toTrue
) — masking 项是否计算为 cross-attention masks 和从噪声估计导出的 masks 的交集。Cross-attention mask 定义为 ‘M^1’,从噪声估计导出的 masks 定义为 LEDITS++ 论文 等式 12 中的 ‘M^2’。 - attn_store_steps (
List[int]
, 可选) — 在 AttentionStore 中存储注意力图的步骤。仅用于可视化目的。 - store_averaged_over_steps (
bool
, 默认为True
) — 是否将 'attn_store_steps' 的注意力图存储为在扩散步骤中平均的值。如果为 False,则分别存储每个步骤的注意力图。仅用于可视化目的。 - cross_attention_kwargs (
dict
, 可选) — 一个 kwargs 字典,如果指定,则会传递给self.processor
中定义的AttentionProcessor
。 - guidance_rescale (
float
, 可选, 默认为 0.0) — 来自 Common Diffusion Noise Schedules and Sample Steps are Flawed 的 Guidance rescale 因子。当使用零终端 SNR 时,Guidance rescale 因子应修复过度曝光问题。 - clip_skip (
int
, 可选) — 从 CLIP 中跳过的层数,用于计算 prompt embeddings。值为 1 表示将使用倒数第二层的输出计算 prompt embeddings。 - callback_on_step_end (
Callable
, 可选) — 在推理期间,在每个去噪步骤结束时调用的函数。该函数使用以下参数调用:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
。callback_kwargs
将包含callback_on_step_end_tensor_inputs
指定的所有张量列表。 - callback_on_step_end_tensor_inputs (
List
, 可选) —callback_on_step_end
函数的张量输入列表。列表中指定的张量将作为callback_kwargs
参数传递。您将只能包含管道类的._callback_tensor_inputs
属性中列出的变量。
返回值
LEditsPPDiffusionPipelineOutput 或 tuple
LEditsPPDiffusionPipelineOutput 如果 return_dict
为 True,否则为 tuple
。当返回元组时,第一个元素是包含生成图像的列表,第二个元素是 bool
列表,表示根据 safety_checker
,相应的生成图像是否可能代表“不适合工作场所”(nsfw)的内容。
用于编辑的管道调用函数。 必须预先调用 invert() 方法。 编辑将始终对最后反转的图像执行。
示例
>>> import PIL
>>> import requests
>>> import torch
>>> from io import BytesIO
>>> from diffusers import LEditsPPPipelineStableDiffusion
>>> from diffusers.utils import load_image
>>> pipe = LEditsPPPipelineStableDiffusion.from_pretrained(
... "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
... )
>>> pipe = pipe.to("cuda")
>>> img_url = "https://www.aiml.informatik.tu-darmstadt.de/people/mbrack/cherry_blossom.png"
>>> image = load_image(img_url).convert("RGB")
>>> _ = pipe.invert(image=image, num_inversion_steps=50, skip=0.1)
>>> edited_image = pipe(
... editing_prompt=["cherry blossom"], edit_guidance_scale=10.0, edit_threshold=0.75
... ).images[0]
invert
< source >( image: Union source_prompt: str = '' source_guidance_scale: float = 3.5 num_inversion_steps: int = 30 skip: float = 0.15 generator: Optional = None cross_attention_kwargs: Optional = None clip_skip: Optional = None height: Optional = None width: Optional = None resize_mode: Optional = 'default' crops_coords: Optional = None ) → LEditsPPInversionPipelineOutput
参数
- image (
PipelineImageInput
) — 要编辑的图像的输入。多个输入图像必须默认具有相同的宽高比。 - source_prompt (
str
, 默认为""
) — 描述输入图像的 prompt,将在反演期间用于引导。如果source_prompt
为""
,则禁用引导。 - source_guidance_scale (
float
, 默认为3.5
) — 反演期间引导的强度。 - num_inversion_steps (
int
, 默认为30
) — 丢弃初始skip
步骤后执行的总反演步骤数。 - skip (
float
, 默认为0.15
) — 将被忽略以进行反演和后续生成的初始步骤部分。较低的值将导致对输入图像的更强烈的更改。skip
必须介于0
和1
之间。 - generator (
torch.Generator
, 可选) — 用于使反演具有确定性的torch.Generator
。 - cross_attention_kwargs (
dict
, 可选) — 一个 kwargs 字典,如果指定,则会传递给self.processor
中定义的AttentionProcessor
。 - clip_skip (
int
, 可选) — 从 CLIP 中跳过的层数,用于计算 prompt embeddings。值为 1 表示将使用倒数第二层的输出计算 prompt embeddings。 - height (
int
, 可选, 默认为None
) — 预处理图像的高度。如果为None
,将使用get_default_height_width()
获取默认高度。 - width (
int
, 可选,默认为None
) — 预处理后的宽度。如果为None
,将使用get_default_height_width()
获取默认宽度。 - resize_mode (
str
, 可选, 默认为default
) — 调整大小模式,可以是default
或fill
之一。如果为default
,则会调整图像大小以适应指定的宽度和高度,并且可能无法保持原始宽高比。如果为fill
,则会调整图像大小以适应指定的宽度和高度,保持宽高比,然后在尺寸内居中图像,用图像数据填充空白。如果为crop
,则会调整图像大小以适应指定的宽度和高度,保持宽高比,然后在尺寸内居中图像,裁剪多余部分。请注意,resize_modefill
和crop
仅支持 PIL 图像输入。 - crops_coords (
List[Tuple[int, int, int, int]]
, 可选, 默认为None
) — 批次中每个图像的裁剪坐标。如果为None
,则不会裁剪图像。
输出将包含调整大小后的输入图像和相应的 VAE 重建。
用于图像反演的管道函数,如 LEDITS++ Paper 中所述。如果调度器设置为 DDIMScheduler,则将执行 edit-friendly DPDM 提出的反演。
encode_prompt
< source >( device num_images_per_prompt enable_edit_guidance negative_prompt = None editing_prompt = None negative_prompt_embeds: Optional = None editing_prompt_embeds: Optional = None lora_scale: Optional = None clip_skip: Optional = None )
参数
- num_images_per_prompt (
int
) — 每个 prompt 应生成的图像数量 - enable_edit_guidance (
bool
) — 是否执行任何编辑或改为重建输入图像 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的提示或提示列表。如果未定义,则必须传递negative_prompt_embeds
。当不使用引导时忽略(即,如果guidance_scale
小于1
,则忽略)。 - editing_prompt (
str
或List[str]
, 可选) — 要编码的编辑提示。如果未定义,则必须传递editing_prompt_embeds
。 - editing_prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。 可用于轻松调整文本输入,例如 提示权重。 如果未提供,将从prompt
输入参数生成文本嵌入。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负面文本嵌入。 可用于轻松调整文本输入,例如 提示权重。 如果未提供,将从negative_prompt
输入参数生成 negative_prompt_embeds。 - lora_scale (
float
, 可选) — 如果加载了 LoRA 层,则将应用于文本编码器所有 LoRA 层的 LoRA 比例。 - clip_skip (
int
, 可选) — 在计算提示嵌入时,要从 CLIP 跳过的层数。 值 1 表示预最终层的输出将用于计算提示嵌入。
将提示编码为文本编码器隐藏状态。
LEditsPPPipelineStableDiffusionXL
class diffusers.LEditsPPPipelineStableDiffusionXL
< source >( vae: AutoencoderKL text_encoder: CLIPTextModel text_encoder_2: CLIPTextModelWithProjection tokenizer: CLIPTokenizer tokenizer_2: CLIPTokenizer unet: UNet2DConditionModel scheduler: Union image_encoder: CLIPVisionModelWithProjection = None feature_extractor: CLIPImageProcessor = None force_zeros_for_empty_prompt: bool = True add_watermarker: Optional = None )
参数
- vae (AutoencoderKL) — 变分自编码器 (VAE) 模型,用于将图像编码和解码为潜在表示形式以及从潜在表示形式解码图像。
- text_encoder (CLIPTextModel) — 冻结的文本编码器。 Stable Diffusion XL 使用 CLIP 的文本部分,特别是 clip-vit-large-patch14 变体。
- text_encoder_2 (CLIPTextModelWithProjection) — 第二个冻结的文本编码器。 Stable Diffusion XL 使用 CLIP 的文本和池化部分,特别是 laion/CLIP-ViT-bigG-14-laion2B-39B-b160k 变体。
- tokenizer (CLIPTokenizer) — CLIPTokenizer 类的分词器。
- tokenizer_2 (CLIPTokenizer) — CLIPTokenizer 类的第二个分词器。
- unet (UNet2DConditionModel) — 条件 U-Net 架构,用于对编码后的图像潜在空间进行去噪。
- scheduler (DPMSolverMultistepScheduler 或 DDIMScheduler) — 与
unet
结合使用的调度器,用于对编码后的图像潜在空间进行去噪。 可以是 DPMSolverMultistepScheduler 或 DDIMScheduler 之一。 如果传递任何其他调度器,它将自动设置为 DPMSolverMultistepScheduler。 - force_zeros_for_empty_prompt (
bool
, 可选, 默认为"True"
) — 负面提示嵌入是否应始终强制设置为 0。 另请参阅stabilityai/stable-diffusion-xl-base-1-0
的配置。 - add_watermarker (
bool
, 可选) — 是否使用 invisible_watermark library 库为输出图像添加水印。 如果未定义,如果已安装该软件包,则默认为 True,否则将不使用水印。
使用 LEDits++ 和 Stable Diffusion XL 进行文本图像编辑的 Pipeline。
此模型继承自 DiffusionPipeline,并构建于 StableDiffusionXLPipeline 之上。 查看超类文档,了解为所有 pipeline 实现的通用方法(下载、保存、在特定设备上运行等)。
此外,pipeline 还继承了以下加载方法
- LoRA: LEditsPPPipelineStableDiffusionXL.load_lora_weights()
- Ckpt: loaders.FromSingleFileMixin.from_single_file()
以及以下保存方法
- LoRA:
loaders.StableDiffusionXLPipeline.save_lora_weights
__call__
< source >( denoising_end: Optional = None negative_prompt: Union = None negative_prompt_2: Union = None negative_prompt_embeds: Optional = None negative_pooled_prompt_embeds: Optional = None ip_adapter_image: Union = None output_type: Optional = 'pil' return_dict: bool = True cross_attention_kwargs: Optional = None guidance_rescale: float = 0.0 crops_coords_top_left: Tuple = (0, 0) target_size: Optional = None editing_prompt: Union = None editing_prompt_embeddings: Optional = None editing_pooled_prompt_embeds: Optional = None reverse_editing_direction: Union = False edit_guidance_scale: Union = 5 edit_warmup_steps: Union = 0 edit_cooldown_steps: Union = None edit_threshold: Union = 0.9 sem_guidance: Optional = None use_cross_attn_mask: bool = False use_intersect_mask: bool = False user_mask: Optional = None attn_store_steps: Optional = [] store_averaged_over_steps: bool = True clip_skip: Optional = None callback_on_step_end: Optional = None callback_on_step_end_tensor_inputs: List = ['latents'] **kwargs ) → LEditsPPDiffusionPipelineOutput 或 tuple
参数
- denoising_end (
float
, 可选) — 如果指定,则确定在有意提前终止之前要完成的总去噪过程的比例(介于 0.0 和 1.0 之间)。因此,返回的样本仍将保留大量噪声,具体取决于调度程序选择的离散时间步长。当此pipeline构成“去噪器混合”多pipeline设置的一部分时,理想情况下应使用 denoising_end 参数,如 [**Refining the Image - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的提示或提示列表。如果未定义,则必须传递negative_prompt_embeds
。当不使用引导时忽略(即,如果guidance_scale
小于1
,则忽略)。 - negative_prompt_2 (
str
或List[str]
, 可选) — 不用于引导图像生成的提示或提示列表,这些提示将发送到tokenizer_2
和text_encoder_2
。如果未定义,则在两个文本编码器中都使用negative_prompt
- negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负面文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,则将从negative_prompt
输入参数生成 negative_prompt_embeds。 - negative_pooled_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负面池化文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,则将从negative_prompt
输入参数生成池化的 negative_prompt_embeds。 ip_adapter_image — (PipelineImageInput
, 可选): 与 IP 适配器一起使用的可选图像输入。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。在 PIL:PIL.Image.Image
或np.array
之间选择。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput
而不是普通元组。 - callback (
Callable
, 可选) — 一个函数,将在推理期间每callback_steps
步调用一次。该函数将使用以下参数调用:callback(step: int, timestep: int, latents: torch.Tensor)
。 - callback_steps (
int
, 可选, 默认为 1) — 调用callback
函数的频率。如果未指定,则将在每一步调用回调。 - cross_attention_kwargs (
dict
, 可选) — 一个 kwargs 字典,如果指定,则会传递给AttentionProcessor
,如 diffusers.models.attention_processor 中self.processor
下定义的那样。 - guidance_rescale (
float
, 可选, 默认为 0.7) — 由 Common Diffusion Noise Schedules and Sample Steps are Flawed 提出的 Guidance rescale 因子。guidance_scale
在 Common Diffusion Noise Schedules and Sample Steps are Flawed 的公式 16 中定义为φ
。 Guidance rescale 因子应修复使用零终端 SNR 时的过度曝光。 - crops_coords_top_left (
Tuple[int]
, 可选, 默认为 (0, 0)) —crops_coords_top_left
可用于生成看起来像是从crops_coords_top_left
位置向下“裁剪”的图像。通常通过将crops_coords_top_left
设置为 (0, 0) 来获得良好居中的图像。SDXL 微调的一部分,如 https://huggingface.co/papers/2307.01952 的 2.2 节中所述。 - target_size (
Tuple[int]
, 可选, 默认为 (1024, 1024)) — 在大多数情况下,target_size
应设置为生成图像的所需高度和宽度。如果未指定,则默认为(width, height)
。SDXL 微调的一部分,如 https://huggingface.co/papers/2307.01952 的 2.2 节中所述。 - editing_prompt (
str
或List[str]
, 可选) — 用于引导图像生成的提示或提示列表。通过设置editing_prompt = None
来重建图像。提示的引导方向应通过reverse_editing_direction
指定。 - editing_prompt_embeddings (
torch.Tensor
, 可选) — 预生成的编辑文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,则将从editing_prompt
输入参数生成 editing_prompt_embeddings。 - editing_pooled_prompt_embeddings (
torch.Tensor
, 可选) — 预生成的池化编辑文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,则将从editing_prompt
输入参数生成 editing_prompt_embeddings。 - reverse_editing_direction (
bool
或List[bool]
, 可选, 默认为False
) —editing_prompt
中对应的提示是应该增加还是减少。 - edit_guidance_scale (
float
或List[float]
, 可选, 默认为 5) — 用于引导图像生成的 Guidance scale。如果作为列表提供,则值应对应于editing_prompt
。edit_guidance_scale
定义为 LEDITS++ Paper 公式 12 中的s_e
。 - edit_warmup_steps (
float
或List[float]
, 可选, 默认为 10) — 不应用 guidance 的扩散步骤数(对于每个提示)。 - edit_cooldown_steps (
float
或List[float]
, 可选, 默认为None
) — 不再应用 guidance 的扩散步骤数(对于每个提示)。 - edit_threshold (
float
或List[float]
, 可选, 默认为 0.9) — guidance 的 masking 阈值。阈值应与修改的图像区域成比例。“edit_threshold”定义为 LEDITS++ Paper 公式 12 中的 ‘λ’。 - sem_guidance (
List[torch.Tensor]
, 可选) — 在生成时应用的预生成 guidance 向量列表。列表的长度必须与num_inference_steps
相对应。 use_cross_attn_mask — 是否使用 cross-attention masks。当 use_intersect_mask 设置为 true 时,始终使用 cross-attention masks。Cross-attention masks 定义为 LEDITS++ paper 公式 12 中的 ‘M^1’。 use_intersect_mask — masking 项是否计算为 cross-attention masks 和从噪声估计中导出的 masks 的交集。Cross-attention mask 定义为 ‘M^1’,从噪声估计中导出的 masks 定义为 LEDITS++ paper 公式 12 中的 ‘M^2’。 user_mask — 用户提供的 mask,用于更好地控制编辑过程。当 LEDITS++ 的隐式 masks 不符合用户偏好时,这很有用。 attn_store_steps — 在 AttentionStore 中存储 attention maps 的步骤。仅用于可视化目的。 store_averaged_over_steps — ‘attn_store_steps’ 的 attention maps 是否存储为在扩散步骤中平均的值。如果为 False,则每个步骤的 attention maps 将单独存储。仅用于可视化目的。 - clip_skip (
int
, 可选) — 在计算提示嵌入时要从 CLIP 跳过的层数。值为 1 表示预最终层的输出将用于计算提示嵌入。 - callback_on_step_end (
Callable
, 可选) — 一个函数,在推理期间的每个去噪步骤结束时调用。该函数使用以下参数调用:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
。callback_kwargs
将包含callback_on_step_end_tensor_inputs
指定的所有张量列表。 - callback_on_step_end_tensor_inputs (
List
, 可选) —callback_on_step_end
函数的张量输入列表。列表中指定的张量将作为callback_kwargs
参数传递。您将只能包含 pipeline 类的._callback_tensor_inputs
属性中列出的变量。
返回值
LEditsPPDiffusionPipelineOutput 或 tuple
LEditsPPDiffusionPipelineOutput 如果 return_dict
为 True,否则为 `tuple`。当返回元组时,第一个元素是包含生成图像的列表。
用于编辑的 pipeline 的调用函数。必须预先调用 invert() 方法。编辑将始终针对最后反转的图像执行。
示例
>>> import torch
>>> import PIL
>>> import requests
>>> from io import BytesIO
>>> from diffusers import LEditsPPPipelineStableDiffusionXL
>>> pipe = LEditsPPPipelineStableDiffusionXL.from_pretrained(
... "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16
... )
>>> pipe = pipe.to("cuda")
>>> def download_image(url):
... response = requests.get(url)
... return PIL.Image.open(BytesIO(response.content)).convert("RGB")
>>> img_url = "https://www.aiml.informatik.tu-darmstadt.de/people/mbrack/tennis.jpg"
>>> image = download_image(img_url)
>>> _ = pipe.invert(image=image, num_inversion_steps=50, skip=0.2)
>>> edited_image = pipe(
... editing_prompt=["tennis ball", "tomato"],
... reverse_editing_direction=[True, False],
... edit_guidance_scale=[5.0, 10.0],
... edit_threshold=[0.9, 0.85],
... ).images[0]
invert
< source >( image: Union source_prompt: str = '' source_guidance_scale = 3.5 negative_prompt: str = None negative_prompt_2: str = None num_inversion_steps: int = 50 skip: float = 0.15 generator: Optional = None crops_coords_top_left: Tuple = (0, 0) num_zero_noise_steps: int = 3 cross_attention_kwargs: Optional = None ) → LEditsPPInversionPipelineOutput
参数
- image (
PipelineImageInput
) — 要编辑的图像的输入。多个输入图像必须默认具有相同的宽高比。 - source_prompt (
str
, defaults to""
) — 描述输入图像的提示词,将在反演过程中用作引导。如果source_prompt
为""
,则禁用引导。 - source_guidance_scale (
float
, defaults to3.5
) — 反演过程中引导的强度。 - negative_prompt (
str
或List[str]
, 可选) — 不引导图像生成的提示或提示列表。如果未定义,则必须传递negative_prompt_embeds
来代替。当不使用引导时会被忽略(即,如果guidance_scale
小于1
则会被忽略)。 - negative_prompt_2 (
str
或List[str]
, 可选) — 不引导图像生成的提示或提示列表,将被发送到tokenizer_2
和text_encoder_2
。如果未定义,则negative_prompt
将用于两个文本编码器。 - num_inversion_steps (
int
, defaults to50
) — 丢弃初始skip
步数后执行的总反演步数。 - skip (
float
, defaults to0.15
) — 将为反演和后续生成忽略的初始步骤的比例。较低的值将导致对输入图像的更强烈的更改。skip
必须介于0
和1
之间。 - generator (
torch.Generator
, 可选) — 用于使反演具有确定性的torch.Generator
。 - crops_coords_top_left (
Tuple[int]
, 可选, defaults to (0, 0)) —crops_coords_top_left
可用于生成看起来像是从crops_coords_top_left
位置向下“裁剪”的图像。通常通过将crops_coords_top_left
设置为 (0, 0) 来获得有利的、居中良好的图像。这是 SDXL 微调的一部分,如 https://huggingface.co/papers/2307.01952 的 2.2 节中所述。 - num_zero_noise_steps (
int
, defaults to3
) — 最终扩散步骤的数量,这些步骤将不会重新对当前图像进行去噪。如果没有将任何步骤设置为零,SD-XL 与 DPMSolverMultistepScheduler 结合使用会产生噪点伪影。 - cross_attention_kwargs (
dict
, 可选) — 一个 kwargs 字典,如果指定,则会作为self.processor
下定义的AttentionProcessor
的参数传递,定义在 diffusers.models.attention_processor 中。
输出将包含调整大小后的输入图像和相应的 VAE 重建。
用于图像反演的管道函数,如 LEDITS++ Paper 中所述。如果调度器设置为 DDIMScheduler,则将执行 edit-friendly DPDM 提出的反演。
encode_prompt
< source >( device: Optional = None num_images_per_prompt: int = 1 negative_prompt: Optional = None negative_prompt_2: Optional = None negative_prompt_embeds: Optional = None negative_pooled_prompt_embeds: Optional = None lora_scale: Optional = None clip_skip: Optional = None enable_edit_guidance: bool = True editing_prompt: Optional = None editing_prompt_embeds: Optional = None editing_pooled_prompt_embeds: Optional = None )
参数
- num_images_per_prompt (
int
) — 每个提示应生成的图像数量 - negative_prompt (
str
或List[str]
, 可选) — 不引导图像生成的提示或提示列表。如果未定义,则必须传递negative_prompt_embeds
来代替。 - negative_prompt_2 (
str
或List[str]
, 可选) — 不引导图像生成的提示或提示列表,将被发送到tokenizer_2
和text_encoder_2
。如果未定义,则negative_prompt
将用于两个文本编码器。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负面文本嵌入。可用于轻松调整文本输入,例如 提示词权重。如果未提供,则将从negative_prompt
输入参数生成 negative_prompt_embeds。 - negative_pooled_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负面池化文本嵌入。可用于轻松调整文本输入,例如 提示词权重。如果未提供,则将从negative_prompt
输入参数生成 pooled negative_prompt_embeds。 - lora_scale (
float
, 可选) — 如果加载了 LoRA 层,则将应用于文本编码器的所有 LoRA 层的 lora 缩放比例。 - clip_skip (
int
, 可选) — 从 CLIP 中跳过的层数,用于计算提示词嵌入。值为 1 表示预最终层的输出将用于计算提示词嵌入。 - enable_edit_guidance (
bool
) — 是否引导向编辑提示词。 - editing_prompt (
str
或List[str]
, 可选) — 要编码的编辑提示词。如果未定义且 ‘enable_edit_guidance’ 为 True,则必须传递editing_prompt_embeds
来代替。 - editing_prompt_embeds (
torch.Tensor
, 可选) — 预生成的编辑文本嵌入。可用于轻松调整文本输入,例如 提示词权重。如果未提供且 ‘enable_edit_guidance’ 为 True,则将从editing_prompt
输入参数生成 editing_prompt_embeds。 - editing_pooled_prompt_embeds (
torch.Tensor
, 可选) — 预生成的编辑池化文本嵌入。可用于轻松调整文本输入,例如 提示词权重。如果未提供,则将从editing_prompt
输入参数生成 pooled editing_pooled_prompt_embeds。
将提示编码为文本编码器隐藏状态。
get_guidance_scale_embedding
< source >( w: Tensor embedding_dim: int = 512 dtype: dtype = torch.float32 ) → torch.Tensor
LEditsPPDiffusionPipelineOutput
class diffusers.pipelines.LEditsPPDiffusionPipelineOutput
< source >( images: Union nsfw_content_detected: Optional )
LEdits++ Diffusion 管道的输出类。
LEditsPPInversionPipelineOutput
class diffusers.pipelines.LEditsPPInversionPipelineOutput
< source >( images: Union vae_reconstruction_images: Union )
参数
- input_images (
List[PIL.Image.Image]
或np.ndarray
) — 裁剪和调整大小后的输入图像列表,作为长度为batch_size
的 PIL 图像,或形状为(batch_size, height, width, num_channels)
的 NumPy 数组。 - vae_reconstruction_images (
List[PIL.Image.Image]
或np.ndarray
) — 所有输入图像的 VAE 重建列表,作为长度为batch_size
的 PIL 图像,或形状为(batch_size, height, width, num_channels)
的 NumPy 数组。
LEdits++ Diffusion 管道的输出类。