【Diffusion实战】基于 Stable Diffusion 实现 Img2Img、Inpainting 和 Depth2Image(Pytorch代码详解)

  来试试 Stable Diffusion 在图像编辑中的应用吧~


Diffusion实战篇:
  【Diffusion实战】训练一个diffusion模型生成S曲线(Pytorch代码详解)
  【Diffusion实战】训练一个diffusion模型生成蝴蝶图像(Pytorch代码详解)
  【Diffusion实战】引导一个diffusion模型根据文字生成图像(Pytorch代码详解)
  【Diffusion实战】训练一个类别引导diffusion模型(Pytorch代码详解)
  【Diffusion实战】基于Stable Diffusion实现文本到图像的生成(Pytorch代码详解)
Diffusion综述篇:
  【Diffusion综述】医学图像分析中的扩散模型(一)
  【Diffusion综述】医学图像分析中的扩散模型(二)
  【Diffusion综述】扩散模型在 MRI 影像中的应用


1、Img2Img

  Img2Img 可以利用文字提示实现图对图的转换;
  预训练pipeline下载:stabilityai/stable-diffusion-2-1-base

import torch
from PIL import Image
from matplotlib import pyplot as plt
from diffusers import StableDiffusionImg2ImgPipeline

init_image = Image.open('./dog.png').convert("RGB")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

model_id = "E:/Code/kuosan/stable-diffusion-2-1-base"
img2img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id).to(device)

result_image = img2img_pipe(
    prompt="An oil painting of a man on a bench", # 图像编辑文本提示
    image = init_image, # 输入待编辑的图片
    strength = 0.7, # 设为 0 时文本编辑不起作用,设为 1 时作用强度最大
).images[0]

# View the result
fig, axs = plt.subplots(1, 2, figsize=(12, 5))
axs[0].imshow(init_image);axs[0].set_title('Input Image');axs[0].axis('off')
axs[1].imshow(result_image);axs[1].set_title('Result');axs[1].axis('off')

  输出图像为:

在这里插入图片描述
  改变提示 prompt 和 strength 能获得各种各样的图像:

result_image = img2img_pipe(
    prompt="There was a withered tree on the moor", # 图像编辑文本提示
    image = init_image, # 输入待编辑的图片
    strength = 0.8, # 设为 0 时文本编辑不起作用,设为 1 时作用强度最大
).images[0]

  输出图像为:

在这里插入图片描述

  大家可以自己玩玩,虽然有时候生成的图像是有点子抽象…


2、Inpainting

  Inpainting 可以保留一张图像中一部分不变,在给定的其他部分生成新的内容;
  预训练 pipeline 下载:booksforcharlie/stable-diffusion-inpainting

import torch
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
from diffusers import StableDiffusionInpaintPipeline

init_image = Image.open('./dog.png').convert("RGB")
mask_image = Image.open('./dog_mask.png').convert("L")

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model_id = "E:/Code/kuosan/stable-diffusion-inpainting"
inpaint_pipe = StableDiffusionInpaintPipeline.from_pretrained(model_id).to(device)

prompt = "A small robot, high resolution, sitting on a park bench"

result_image = inpaint_pipe(prompt=prompt, image=init_image, mask_image=mask_image).images[0]

fig, axs = plt.subplots(1, 3, figsize=(12, 5))
axs[0].imshow(init_image);axs[0].set_title('Input Image');axs[0].axis('off')
axs[1].imshow(mask_image);axs[1].set_title('Mask');axs[1].axis('off')
axs[2].imshow(result_image);axs[2].set_title('Result');axs[2].axis('off')

  输出图像为:

在这里插入图片描述

  改变提示 prompt:

prompt = "A sunflower, high resolution, stands beside a park bench"

  输出图像为:

在这里插入图片描述

  emmm…就是这向日葵上似乎带了点狗毛…


3、Depth2Image

  Depth2Image 能够使用不同的颜色或纹理生成新图片;
  预训练 pipeline 下载:stabilityai/stable-diffusion-2-depth

import torch
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
from diffusers import StableDiffusionDepth2ImgPipeline

init_image = Image.open('./dog.png').convert("RGB")

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model_id = "E:/Code/kuosan/stable-diffusion-2-depth"
Depth2Img_pipe = StableDiffusionDepth2ImgPipeline.from_pretrained(model_id).to(device)

prompt = "A man on a bench"

result_image = Depth2Img_pipe(prompt=prompt, image=init_image).images[0]

fig, axs = plt.subplots(1, 2, figsize=(12, 5))
axs[0].imshow(init_image);axs[0].set_title('Input Image');axs[0].axis('off')
axs[1].imshow(result_image);axs[1].set_title('Result');axs[1].axis('off')

  输出图像为:

在这里插入图片描述

  改变提示 prompt:

prompt = "Cartoon style, high resolution, featuring a little kitten happily playing"

  输出图像为:

在这里插入图片描述

  还是比较忠于原图的,哈哈…


  pipeline 太强了,简直 0 帧起手~

### 使用 Stable Diffusion 进行图像修复 (Inpainting) #### 准备工作 为了使用 Stable Diffusion 实现图像修复,需先准备好待处理的图片以及安装好支持 Inpainting 功能的相关软件环境。通常情况下,这涉及到配置 Python 环境并安装特定版本的 PyTorch 其他依赖库。 #### 图像与掩码准备 选择一张想要编辑的照片,在需要移除或替换的内容上创建一个遮罩层。这个遮罩定义了哪些像素会被视为缺失数据进而由算法填补。可以通过图形编辑工具如 Photoshop 或 GIMP 来制作这样的二值化蒙版文件[^1]。 #### 设置参数 当一切就绪之后,启动用于执行 Inpainting 的应用程序界面或者命令行脚本。对于重绘强度这一关键参数而言,建议将其初始设定为 0.5 左右,因为过高的数值可能导致修补后的区域与其他部分显得格格不入;相反地,如果设得太低,则可能造成细节不够清晰的问题[^3]。 ```bash python run_inpaint.py --input_image path/to/input.jpg \ --mask_image path/to/mask.png \ --output_dir output_folder/ \ --strength 0.5 ``` 上述代码展示了调用 `run_inpaint.py` 脚本来运行一次完整的 Inpainting 流程,其中指定了原始图片路径 (`path/to/input.jpg`)、对应的掩码位置 (`path/to/mask.png`) 及保存最终成果的位置 (`output_folder/`) ,同时还设置了之前提到过的重绘力度(`--strength 0.5`)。 通过以上步骤即可利用 Stable Diffusion 完成基本的图像修复任务。当然实际操作过程中还存在更多高级选项可供调节以获得更理想的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值