基于 huggingface diffuser 库云服务器实现 stable diffusion inpaint样例代码

本文详细介绍了如何在本地部署StableDiffusion模型进行图像inpainting,包括环境配置、模型下载和使用HuggingFace库的示例代码。读者可以跟随步骤部署并实现在图片中修复指定区域的功能。
摘要由CSDN通过智能技术生成


一、stable diffusion inpant样例

(一)理论基础

Stable Diffusion是计算机视觉领域的一个生成式大模型,可以用于文生图,图生图,图像inpainting,ControlNet控制生成,图像超分等丰富的任务。inpaint是Stable Diffusion仅重绘图像部分的技术,将画面中被手工遮罩的部分重新绘制,使用stable diffusion实现inpaint的理论可以参考我之前的文章:Stable Diffusion原理解析-inpaint修复图片

(二)样例背景

huggingface收纳了许多最前沿的模型和数据集等有趣的工作,其中包括了diffusers库,基于 huggingface diffuser 库可以自行在云服务器实现一系列有关操作。对于stable diffusion inpaint,huggingface提供了一个运行样例,大致内容如下:
在这里插入图片描述

二、本地部署

接下来实战一下本地部署。

(一)环境配置

  1. 创建一个diffenv环境用于实现上述内容
conda create -n diffenv python=3.8
conda activate diffenv
  1. 下载相关库
pip install diffusers==0.4.0
pip install transformers scipy ftfy
pip instal1 torch torchvision torchaudio
pip install Pillow
pip install requests
pip install --upgrade diffusers[torch]

(二)模型下载

有两种方式将huggingface中的stable-diffusion-inpainting模型下载到本地:

(三)参照样例编写代码

  1. 导入相关库
import PIL
import requests
import torch
from io import BytesIO

from diffusers import StableDiffusionInpaintPipeline
  1. 加载模型
#加载Stable Diffusion Inpainting模型并创建一个可以用于图像修复的pipeline
pipe = StableDiffusionInpaintPipeline.from_pretrained(
    "./stable-diffusion-inpainting",#下载的模型所在的本地地址
    revision="fp16",#使用FP16(半精度浮点数)进行训练
    torch_dtype=torch.float16)


pipe = pipe.to("cuda")#在GPU上进行模型推断
  1. 读取本地图像内容(模型要求图片格式为PIL)
img_path="./example.png"
mask_path="./example-mask.png"

#按照路径打开文件并读取字节数据,然后将字节数据传递给BytesIO,最后用PIL库打开并处理图像
def download_image(path):
    with open(path,'rb') as file:
        image_data=file.read()
    return PIL.Image.open(BytesIO(image_data)).convert("RGB")

init_image = download_image(img_path).resize((512, 512))
mask_image = download_image(mask_path).resize((512, 512))

  1. 模型运行
prompt = "Face of a yellow cat, high resolution, sitting on a park bench"
image = pipe(prompt=prompt, image=init_image, mask_image=mask_image).images[0]
image.save("./yellow_cat_on_park_bench.png")
  1. 实现效果
    在这里插入图片描述输入"Face of a yellow cat, high resolution, sitting on a park bench"运行之后为:
    在这里插入图片描述

三、全部代码

import PIL
import requests
import torch
from io import BytesIO

from diffusers import StableDiffusionInpaintPipeline

'''
def download_image(url):
    response = requests.get(url)
    return PIL.Image.open(BytesIO(response.content)).convert("RGB")


img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"

init_image = download_image(img_url).resize((512, 512))
mask_image = download_image(mask_url).resize((512, 512))
'''

img_path="./example.png"
mask_path="./example-mask.png"
def download_image(path):
    with open(path,'rb') as file:
        image_data=file.read()
    return PIL.Image.open(BytesIO(image_data)).convert("RGB")

init_image = download_image(img_path).resize((512, 512))
mask_image = download_image(mask_path).resize((512, 512))

pipe = StableDiffusionInpaintPipeline.from_pretrained(
    "./stable-diffusion-inpainting",revision="fp16",torch_dtype=torch.float16)


pipe = pipe.to("cuda")

prompt = "Face of a yellow cat, high resolution, sitting on a park bench"
image = pipe(prompt=prompt, image=init_image, mask_image=mask_image).images[0]
image.save("./yellow_cat_on_park_bench.png")
  • 30
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值