开始使用免费开始使用

自定义图像编辑

AI 图像生成已经很酷了,但有些模型甚至支持自定义图像编辑。这是一种多模态的图像生成方式,同时接收文本提示和源图像作为输入。请尝试用 StableDiffusionControlNetPipeline 将这幅梵高的著名自画像修改为卡通角色 Snoopy

Famous Van Gogh painting

注意:扩散模型推理可能需要较长时间,因此我们已为您预加载了生成的图像。即使运行不同的提示词,也不会生成新图像。

图像的 Canny 滤波版本已为您创建(canny_image)。已从 diffusers 库导入 StableDiffusionControlNetPipelineControlNetModel 类。生成器列表(generator)也已创建。

本练习是课程的一部分

使用 Hugging Face 的多模态模型

查看课程

练习说明

  • lllyasviel/sd-controlnet-canny 检查点加载 ControlNetModel
  • runwayml/stable-diffusion-v1-5 检查点加载 StableDiffusionControlNetPipeline,并传入提供的 controlnet
  • 使用提供的 promptcanny_image,以及 negative_promptgenerator 运行该 pipeline。

交互式实操练习

通过完成这段示例代码来试试这个练习。

## NOTE: no imports are required for this exercise
# Load a ControlNetModel from the pretrained checkpoint
controlnet = ____("____", torch_dtype=torch.float16)

# Load a pretrained StableDiffusionControlNetPipeline using the ControlNetModel
pipe = ____(
    "____", controlnet=____, torch_dtype=torch.float16
)
pipe = pipe.to("cuda")

prompt = ["Snoopy, best quality, extremely detailed"]

# Run the pipeline
output = pipe(
    ____,
    ____,
    negative_prompt=["monochrome, lowres, bad anatomy, worst quality, low quality"],
    generator=____,
    num_inference_steps=20,
)

plt.imshow(output.images[0])
plt.show()
编辑并运行代码