Diffusers 文档

示例绘画

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

示例绘画

示例绘画:基于示例的扩散模型图像编辑由 Binxin Yang、Shuyang Gu、Bo Zhang、Ting Zhang、Xuejin Chen、Xiaoyan Sun、Dong Chen、Fang Wen 撰写。

论文摘要如下:

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

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

提示

示例绘画由官方的Fantasy-Studio/Paint-by-Example检查点支持。该检查点从CompVis/stable-diffusion-v1-4预热,以根据示例图像和参考图像修复部分遮罩的图像。

请务必查看调度器指南,了解如何权衡调度器速度和质量,并参阅跨管道重用组件部分,了解如何高效地将相同组件加载到多个管道中。

PaintByExamplePipeline

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 )

__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 ) StableDiffusionPipelineOutputtuple

参数

  • 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, 可选, 默认为 self.unet.config.sample_size * self.vae_scale_factor) — 生成图像的高度(像素)。
  • width (int, 可选, 默认为 self.unet.config.sample_size * self.vae_scale_factor) — 生成图像的宽度(像素)。
  • num_inference_steps (int, 可选, 默认为 50) — 去噪步数。更多去噪步数通常会带来更高质量的图像,但推理速度会变慢。
  • guidance_scale (float, 可选, 默认为 7.5) — 较高的引导比例值会鼓励模型生成与文本 prompt 紧密相关的图像,但会降低图像质量。当 guidance_scale > 1 时,启用引导比例。
  • negative_prompt (strList[str], 可选) — 用于引导图像生成中不包含内容的提示或提示列表。如果未定义,则需要传递 negative_prompt_embeds。当不使用引导时(guidance_scale < 1),此参数将被忽略。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示生成的图像数量。
  • eta (float, 可选, 默认为 0.0) — 对应于 DDIM 论文中的参数 eta (η)。仅适用于 DDIMScheduler,在其他调度器中被忽略。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个 torch.Generator,用于使生成具有确定性。
  • latents (torch.Tensor, 可选) — 从高斯分布中采样的预生成噪声潜在变量,用作图像生成的输入。可用于使用不同的提示调整相同的生成。如果未提供,则使用提供的随机 generator 进行采样生成一个潜在变量张量。
  • output_type (str, 可选, 默认为 "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,其中第一个元素是生成的图像列表,第二个元素是指示相应生成的图像是否包含“不适合工作”(nsfw)内容的 bool 列表。

用于生成的管道的调用函数。

示例

>>> 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

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 上更新