Diffusers 文档

示例绘画

Hugging Face's logo
加入 Hugging Face 社区

并获取增强的文档体验

开始使用

示例绘画

Paint by Example: Exemplar-based Image Editing with Diffusion Models 由 Binxin Yang, Shuyang Gu, Bo Zhang, Ting Zhang, Xuejin Chen, Xiaoyan Sun, Dong Chen, Fang Wen 撰写。

该论文的摘要是

语言引导的图像编辑最近取得了巨大的成功。在本文中,我们首次研究了示例引导的图像编辑,以实现更精确的控制。我们通过利用自监督训练来解耦和重组源图像和示例来实现这一目标。然而,朴素的方法会造成明显的融合伪影。我们仔细分析了它,并提出了信息瓶颈和强数据增强,以避免直接复制和粘贴示例图像的简单解决方案。同时,为了确保编辑过程的可控性,我们为示例图像设计了任意形状的蒙版,并利用无分类器引导来增加与示例图像的相似性。整个框架涉及扩散模型的单次前向传播,无需任何迭代优化。我们证明了我们的方法实现了令人印象深刻的性能,并能够在野外图像上进行高保真度的可控编辑。

原始代码库可以在 Fantasy-Studio/Paint-by-Example 找到,您可以在 demo 中试用。

提示

官方 Fantasy-Studio/Paint-by-Example 检查点支持示例绘画。该检查点从 CompVis/stable-diffusion-v1-4 暖启动,以修复部分遮罩的图像,这些图像以示例图像和参考图像为条件。

请务必查看调度器指南,了解如何探索调度器速度和质量之间的权衡,并查看跨 pipelines 重用组件部分,了解如何有效地将相同的组件加载到多个 pipelines 中。

PaintByExamplePipeline

class diffusers.PaintByExamplePipeline

< >

( vae: AutoencoderKL image_encoder: PaintByExampleImageEncoder unet: UNet2DConditionModel scheduler: typing.Union[diffusers.schedulers.scheduling_ddim.DDIMScheduler, diffusers.schedulers.scheduling_pndm.PNDMScheduler, diffusers.schedulers.scheduling_lms_discrete.LMSDiscreteScheduler] safety_checker: StableDiffusionSafetyChecker feature_extractor: CLIPImageProcessor requires_safety_checker: bool = False )

参数

  • vae (AutoencoderKL) — 变分自动编码器 (VAE) 模型,用于将图像编码和解码为潜在表示形式。
  • image_encoder (PaintByExampleImageEncoder) — 编码示例输入图像。 unet 以示例图像为条件,而不是文本提示。
  • tokenizer (CLIPTokenizer) — 用于标记文本的 CLIPTokenizer
  • unet (UNet2DConditionModel) — 用于对编码后的图像潜在空间进行去噪的 UNet2DConditionModel
  • scheduler (SchedulerMixin) — 调度器,与 unet 结合使用,以对编码后的图像潜在空间进行去噪。可以是 DDIMSchedulerLMSDiscreteSchedulerPNDMScheduler 之一。
  • safety_checker (StableDiffusionSafetyChecker) — 分类模块,用于评估生成的图像是否可能被认为是冒犯性或有害的。有关模型潜在危害的更多详细信息,请参阅模型卡
  • feature_extractor (CLIPImageProcessor) — CLIPImageProcessor,用于从生成的图像中提取特征;用作 safety_checker 的输入。

🧪 这是一个实验性功能!

使用 Stable Diffusion 的图像引导图像修复管线。

此模型继承自 DiffusionPipeline。查看超类文档,了解为所有管线实现的通用方法(下载、保存、在特定设备上运行等)。

__call__

< >

( example_image: typing.Union[torch.Tensor, PIL.Image.Image] image: typing.Union[torch.Tensor, PIL.Image.Image] mask_image: typing.Union[torch.Tensor, PIL.Image.Image] height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: int = 50 guidance_scale: float = 5.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_images_per_prompt: typing.Optional[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 ) StableDiffusionPipelineOutput or tuple

参数

  • example_image (torch.TensorPIL.Image.ImageList[PIL.Image.Image]) — 用于引导图像生成的示例图像。
  • image (torch.TensorPIL.Image.ImageList[PIL.Image.Image]) — 表示要进行图像修复的图像批次的 Image 或张量(图像的某些部分被 mask_image 遮罩,并根据 prompt 重新绘制)。
  • mask_image (torch.TensorPIL.Image.ImageList[PIL.Image.Image]) — Image 或张量,表示要遮罩 image 的图像批次。蒙版中的白色像素将被重新绘制,而黑色像素将被保留。如果 mask_image 是 PIL 图像,则在使用前将其转换为单通道(亮度)。如果它是张量,则它应包含一个颜色通道 (L) 而不是 3 个,因此预期的形状将为 (B, H, W, 1)
  • height (int, optional, 默认为 self.unet.config.sample_size * self.vae_scale_factor) — 生成图像的高度像素。
  • width (int, optional, 默认为 self.unet.config.sample_size * self.vae_scale_factor) — 生成图像的宽度像素。
  • num_inference_steps (int, optional, 默认为 50) — 去噪步骤的数量。 更多的去噪步骤通常会带来更高质量的图像,但会牺牲推理速度。
  • guidance_scale (float, optional, 默认为 7.5) — 较高的 guidance scale 值会鼓励模型生成与文本 prompt 紧密相关的图像,但会降低图像质量。当 guidance_scale > 1 时,将启用 Guidance scale。
  • negative_prompt (strList[str], optional) — 用于指导图像生成中不应包含的内容的提示或提示列表。 如果未定义,则需要传递 negative_prompt_embeds。 当不使用 guidance 时(guidance_scale < 1),将被忽略。
  • num_images_per_prompt (int, optional, 默认为 1) — 每个 prompt 生成的图像数量。
  • eta (float, optional, 默认为 0.0) — 对应于 DDIM 论文中的参数 eta (η)。 仅适用于 DDIMScheduler,在其他调度器中将被忽略。
  • generator (torch.GeneratorList[torch.Generator], optional) — 用于使生成具有确定性的 torch.Generator
  • latents (torch.Tensor, optional) — 预生成的噪声潜在空间,从高斯分布中采样,用作图像生成的输入。 可用于使用不同的 prompt 调整相同的生成结果。 如果未提供,则会通过使用提供的随机 generator 进行采样来生成潜在空间张量。
  • output_type (str, optional, 默认为 "pil") — 生成图像的输出格式。 在 PIL.Imagenp.array 之间选择。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 StableDiffusionPipelineOutput 而不是一个普通的元组。
  • callback (Callable, 可选) — 一个函数,它在推理期间每隔 callback_steps 步被调用。该函数被调用时带有以下参数:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, 可选, 默认为 1) — 调用 callback 函数的频率。如果未指定,则在每个步骤都调用回调函数。

返回

StableDiffusionPipelineOutputtuple

如果 return_dictTrue,则返回 StableDiffusionPipelineOutput,否则返回一个 tuple,其中第一个元素是包含生成图像的列表,第二个元素是 bool 列表,指示相应的生成图像是否包含“不适宜工作场所观看”(nsfw)内容。

用于生成流程的调用函数。

示例

>>> import PIL
>>> import requests
>>> import torch
>>> from io import BytesIO
>>> from diffusers import PaintByExamplePipeline


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


>>> img_url = (
...     "https://raw.githubusercontent.com/Fantasy-Studio/Paint-by-Example/main/examples/image/example_1.png"
... )
>>> mask_url = (
...     "https://raw.githubusercontent.com/Fantasy-Studio/Paint-by-Example/main/examples/mask/example_1.png"
... )
>>> example_url = "https://raw.githubusercontent.com/Fantasy-Studio/Paint-by-Example/main/examples/reference/example_1.jpg"

>>> init_image = download_image(img_url).resize((512, 512))
>>> mask_image = download_image(mask_url).resize((512, 512))
>>> example_image = download_image(example_url).resize((512, 512))

>>> pipe = PaintByExamplePipeline.from_pretrained(
...     "Fantasy-Studio/Paint-by-Example",
...     torch_dtype=torch.float16,
... )
>>> pipe = pipe.to("cuda")

>>> image = pipe(image=init_image, mask_image=mask_image, example_image=example_image).images[0]
>>> image

StableDiffusionPipelineOutput

class diffusers.pipelines.stable_diffusion.StableDiffusionPipelineOutput

< >

( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] nsfw_content_detected: typing.Optional[typing.List[bool]] )

参数

  • images (List[PIL.Image.Image]np.ndarray) — 长度为 batch_size 的去噪 PIL 图像列表,或形状为 (batch_size, height, width, num_channels) 的 NumPy 数组。
  • nsfw_content_detected (List[bool]) — 列表,指示相应的生成图像是否包含“不适宜工作场所观看”(nsfw)内容;如果无法执行安全检查,则为 None

Stable Diffusion 流程的输出类。

< > 在 GitHub 上更新