首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

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

  • 25-04-25 03:21
  • 2236
  • 5899
blog.csdn.net

  来试试 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')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

  输出图像为:

在这里插入图片描述
  改变提示 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]
  • 1
  • 2
  • 3
  • 4
  • 5

  输出图像为:

在这里插入图片描述

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


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')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

  输出图像为:

在这里插入图片描述

  改变提示 prompt:

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

  输出图像为:

在这里插入图片描述

  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')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

  输出图像为:

在这里插入图片描述

  改变提示 prompt:

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

  输出图像为:

在这里插入图片描述

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


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

注:本文转载自blog.csdn.net的风巽·剑染春水的文章"https://blog.csdn.net/qq_43426908/article/details/145244275"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

117
前沿技术
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2024 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top