用扩散模型进行图像编辑

使用 Hugging Face 的多模态模型

James Chapman

Curriculum Manager, DataCamp

Diffusers

扩散模型从噪声生成图像的步骤

  • 训练为将噪声映射为图像
  • CLIP + 扩散 → 两种条件生成
    • 生成:文本 → 图像
    • 修改:文本+图像 → 图像
1 https://huggingface.co/docs/diffusers
使用 Hugging Face 的多模态模型

自定义图像编辑

ControlNet:图像与文本引导的条件生成

展示使用 Canny 滤波与 ControlNet 的自定义图像编辑流程图

1 https://stable-diffusion-art.com/controlnet/
使用 Hugging Face 的多模态模型

自定义图像编辑

from diffusers.utils import load_image

image = load_image("http://301.nz/o81bf")

import cv2 from PIL import Image import numpy as np image = cv2.Canny(np.array(image), 100, 200)
image = image[:, :, None] image = np.concatenate([image, image, image], axis=2)

蒙娜丽莎图像

使用 Hugging Face 的多模态模型

自定义图像编辑

from diffusers.utils import load_image

image = load_image("http://301.nz/o81bf")

import cv2
from PIL import Image
import numpy as np

image = cv2.Canny(np.array(image), 100, 200)
image = image[:, :, None]
image = np.concatenate([image, image, image], 
                       axis=2)
canny_image = Image.fromarray(image)

蒙娜丽莎及其 Canny 滤波版本

使用 Hugging Face 的多模态模型

自定义图像编辑

from diffusers import StableDiffusionControlNetPipeline
from diffusers import ControlNetModel
import torch

controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", 
                                             torch_dtype=torch.float16)


pipe = StableDiffusionControlNetPipeline.from_pretrained( "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16 )
pipe = pipe.to("cuda")
使用 Hugging Face 的多模态模型

自定义图像编辑

prompt = ["Albert Einstein, 
          best quality, 
          extremely detailed"]

generator = [ torch.Generator(device="cuda").manual_seed(2)]
output = pipe( prompt, canny_image, negative_prompt=["monochrome, lowres, bad anatomy, worst quality, low quality"], generator=generator, num_inference_steps=20)

以蒙娜丽莎姿势出现的阿尔伯特·爱因斯坦

使用 Hugging Face 的多模态模型

图像修复(Inpainting)

  • 在指定区域内生成新内容

无遮罩的蒙娜丽莎

使用 Hugging Face 的多模态模型

图像修复(Inpainting)

  • 在指定区域内生成新内容
  • 二值遮罩:白色(1)、黑色(0
  • 遮罩可来自分割或由用户预先定义(例如使用 InpaintingMask-Generation

蒙娜丽莎与我们的遮罩

使用 Hugging Face 的多模态模型

图像修复(Inpainting)

from diffusers import StableDiffusionControlNetInpaintPipeline, ControlNetModel


controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11p_sd15_inpaint", torch_dtype=torch.float16, use_safetensors=True)
pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained( "stable-diffusion-v1-5/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16, use_safetensors=True )
使用 Hugging Face 的多模态模型

图像修复(Inpainting)

def make_inpaint_condition(image, image_mask):
    image = np.array(image.convert("RGB")).astype(np.float32) / 255.0
    image_mask = np.array(image_mask.convert("L")).astype(np.float32) / 255.0


image[image_mask > 0.5] = -1.0
image = np.expand_dims(image, 0).transpose(0, 3, 1, 2) image = torch.from_numpy(image) return image control_image = make_inpaint_condition(init_image, mask_image)
使用 Hugging Face 的多模态模型

图像修复(Inpainting)

output = pipe(
    "The head of the mona lisa in the 
  same style and quality as the original
  mona lisa with a clear smile and a 
  slightly smaller head size",

num_inference_steps=40,
eta=1.0,
image=init_image, mask_image=mask_image, control_image=control_image, ).images[0]

蒙娜丽莎现在露出微笑

使用 Hugging Face 的多模态模型

Vamos praticar!

使用 Hugging Face 的多模态模型

Preparing Video For Download...