Diffusers代码学习-链式Inpainting

链式Inpainting可以与其他Diffusers管道链接以编辑其输出。这通常有助于提高其他扩散管道的输出质量,如果您使用多个管道,将它们链接在一起以将输出保持在潜在空间中,并可以重复使用相同的管道组件,以便更加节省内存。

1. 文本到图像Inpainting

将文生图管道和Inpainting管道可以修复生成的图像,并且不必首先提供基本图像。这使您可以方便地将喜爱的文本直接形成图像输出,而无需生成全新的图像。

# 以下代码为程序运行进行设置,并引入文生图和Inpainting自动管道
import os

os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"

import torch
from diffusers import AutoPipelineForText2Image, AutoPipelineForInpainting
from diffusers.utils import load_image, make_image_grid

# 以下代码加载文生图模型
pipeline = AutoPipelineForText2Image.from_pretrained(
    "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
)

# 这里使用了[~DiffusionPipeline.enable_model_cpu_offload]
# 和[~DiffersionPipelines.enable_xformers_memory_efficient_attention]
# 来节省内存并提高推理速度。
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
# 如果没有安装xFormers或使用的是PyTorch 2.0或更高版本,则无需使用代码
pipeline.enable_xformers_memory_efficient_attention()

# 以下代码通过提示词生成图片
text2image = pipeline("concept art digital painting of an elven castle, inspired by lord of the rings, highly detailed, 8k").images[0]

# 以下代码加载蒙图
mask_image = load_image("https://hf-mirror.com/datasets/huggingface/documentation-images/resolve/main/diffusers/inpaint_text-chain-mask.png")

# 以下代码加载Inpaiting模型
pipeline = AutoPipelineForInpainting.from_pretrained(
    "kandinsky-community/kandinsky-2-2-decoder-inpaint", torch_dtype=torch.float16
)

# 这里使用了[~DiffusionPipeline.enable_model_cpu_offload]
# 和[~DiffersionPipelines.enable_xformers_memory_efficient_attention]
# 来节省内存并提高推理速度。
pipeline.enable_model_cpu_offload()
# remove following line if xFormers is not installed or you have PyTorch 2.0 or higher installed
# 如果没有安装xFormers或使用的是PyTorch 2.0或更高版本,则无需使用代码
pipeline.enable_xformers_memory_efficient_attention()

# 以下代码基于提示词,及蒙图生成Inpainting后的图片。这里是想增加一个瀑布
prompt = "digital painting of a fantasy waterfall, cloudy"
image = pipeline(prompt=prompt, image=text2image, mask_image=mask_image).images[0]

# 使用make_image_grid方法将几张图片放在一期对比显示
make_image_grid([text2image, mask_image, image], rows=1, cols=3).show()

上面的代码都是基于原有的代码进行拼接形成的新代码,因此无需过多额外解释。输出结果如下(可以看出根据蒙图的形状直接生成了瀑布区域):

  • 22
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

duhaining1976

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值