Diffusers 文档
示例绘画
并获得增强的文档体验
开始使用
示例绘画
示例绘画:基于示例的扩散模型图像编辑由 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 ) → StableDiffusionPipelineOutput 或 tuple
参数
- example_image (
torch.Tensor
或PIL.Image.Image
或List[PIL.Image.Image]
) — 用于引导图像生成的示例图像。 - image (
torch.Tensor
或PIL.Image.Image
或List[PIL.Image.Image]
) — 要修复的图像批次的Image
或张量(图像的某些部分被mask_image
遮罩,并根据prompt
重新绘制)。 - mask_image (
torch.Tensor
或PIL.Image.Image
或List[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 (
str
或List[str]
, 可选) — 用于引导图像生成中不包含内容的提示或提示列表。如果未定义,则需要传递negative_prompt_embeds
。当不使用引导时(guidance_scale < 1
),此参数将被忽略。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示生成的图像数量。 - eta (
float
, 可选, 默认为 0.0) — 对应于 DDIM 论文中的参数 eta (η)。仅适用于 DDIMScheduler,在其他调度器中被忽略。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 一个torch.Generator
,用于使生成具有确定性。 - latents (
torch.Tensor
, 可选) — 从高斯分布中采样的预生成噪声潜在变量,用作图像生成的输入。可用于使用不同的提示调整相同的生成。如果未提供,则使用提供的随机generator
进行采样生成一个潜在变量张量。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。选择PIL.Image
或np.array
。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回 StableDiffusionPipelineOutput 而不是普通元组。 - callback (
Callable
, 可选) — 在推理期间每callback_steps
步调用的函数。该函数调用时带有以下参数:callback(step: int, timestep: int, latents: torch.Tensor)
。 - callback_steps (
int
, 可选, 默认为 1) — 调用callback
函数的频率。如果未指定,则在每个步骤都调用回调。
返回
StableDiffusionPipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 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]] )
Stable Diffusion 管道的输出类。