Buckets:

mishig's picture
|
download
raw
36.1 kB

图像修复

Inpainting 用于替换或编辑图像中的特定区域。这使其成为图像修复的实用工具,例如去除缺陷和伪影,甚至可以用全新的内容替换图像的某个区域。Inpainting 依赖掩码来确定图像中需要填充的区域;待填充的区域由白色像素表示,需要保留的区域由黑色像素表示。白色像素将根据提示词进行填充。

使用 🤗 Diffusers,您可以按以下方式执行图像修复(inpainting):

  1. 使用 AutoPipelineForInpainting 类加载图像修复检查点。该操作会根据检查点自动检测并加载相应的流水线类:
import torch
from diffusers import AutoPipelineForInpainting
from diffusers.utils import load_image, make_image_grid

pipeline = AutoPipelineForInpainting.from_pretrained(
    "kandinsky-community/kandinsky-2-2-decoder-inpaint", dtype=torch.float16
)
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
pipeline.enable_xformers_memory_efficient_attention()

在整个指南中,您会注意到我们使用了 enable_model_cpu_offload() 和 enable_xformers_memory_efficient_attention() 来节省内存并提升推理速度。如果您使用的是 PyTorch 2.0,则无需在 pipeline 上调用 enable_xformers_memory_efficient_attention(),因为它已经在使用 PyTorch 2.0 原生的 scaled-dot product attention。

  1. 加载基础图像和遮罩图像:
init_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/inpaint.png")
mask_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/inpaint_mask.png")
  1. 创建一个用于图像修复的提示词,并将其与基础图像和掩码图像一起传递给管道:
prompt = "a black cat with glowing eyes, cute, adorable, disney, pixar, highly detailed, 8k"
negative_prompt = "bad anatomy, deformed, ugly, disfigured"
image = pipeline(prompt=prompt, negative_prompt=negative_prompt, image=init_image, mask_image=mask_image).images[0]
make_image_grid([init_image, mask_image, image], rows=1, cols=3)
基础镜像



掩码图像



生成的图像

创建掩码图像

在本指南中,为了方便起见,所有代码示例均提供了掩码图像。您可以在自己的图像上进行修复,但需要为其创建一张掩码图像。请使用下方的 Space 轻松创建掩码图像。

上传一张基础图像用于局部重绘,并使用草图工具绘制蒙版。完成后,点击 Run 生成并下载蒙版图像。

Xet Storage Details

Size:
36.1 kB
·
Xet hash:
1714576c745e2563432c4916fd99e1e50996346e50d216601f693b28f11974bc

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.