【Diffusion实战】基于 Stable Diffusion 实现 Img2Img、Inpainting 和 Dep ...

打印 上一主题 下一主题

主题 1664|帖子 1664|积分 4992

  来试试 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
  1. import torch
  2. from PIL import Image
  3. from matplotlib import pyplot as plt
  4. from diffusers import StableDiffusionImg2ImgPipeline
  5. init_image = Image.open('./dog.png').convert("RGB")
  6. device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
  7. model_id = "E:/Code/kuosan/stable-diffusion-2-1-base"
  8. img2img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id).to(device)
  9. result_image = img2img_pipe(
  10.     prompt="An oil painting of a man on a bench", # 图像编辑文本提示
  11.     image = init_image, # 输入待编辑的图片
  12.     strength = 0.7, # 设为 0 时文本编辑不起作用,设为 1 时作用强度最大
  13. ).images[0]
  14. # View the result
  15. fig, axs = plt.subplots(1, 2, figsize=(12, 5))
  16. axs[0].imshow(init_image);axs[0].set_title('Input Image');axs[0].axis('off')
  17. axs[1].imshow(result_image);axs[1].set_title('Result');axs[1].axis('off')
复制代码
  输出图像为:

  改变提示 prompt 和 strength 能获得各种各样的图像:
  1. result_image = img2img_pipe(
  2.     prompt="There was a withered tree on the moor", # 图像编辑文本提示
  3.     image = init_image, # 输入待编辑的图片
  4.     strength = 0.8, # 设为 0 时文本编辑不起作用,设为 1 时作用强度最大
  5. ).images[0]
复制代码
  输出图像为:

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

2、Inpainting

  Inpainting 可以保留一张图像中一部分不变,在给定的其他部分生成新的内容;
  预训练 pipeline 下载:booksforcharlie/stable-diffusion-inpainting
  1. import torch
  2. import numpy as np
  3. from PIL import Image
  4. from matplotlib import pyplot as plt
  5. from diffusers import StableDiffusionInpaintPipeline
  6. init_image = Image.open('./dog.png').convert("RGB")
  7. mask_image = Image.open('./dog_mask.png').convert("L")
  8. device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
  9. model_id = "E:/Code/kuosan/stable-diffusion-inpainting"
  10. inpaint_pipe = StableDiffusionInpaintPipeline.from_pretrained(model_id).to(device)
  11. prompt = "A small robot, high resolution, sitting on a park bench"
  12. result_image = inpaint_pipe(prompt=prompt, image=init_image, mask_image=mask_image).images[0]
  13. fig, axs = plt.subplots(1, 3, figsize=(12, 5))
  14. axs[0].imshow(init_image);axs[0].set_title('Input Image');axs[0].axis('off')
  15. axs[1].imshow(mask_image);axs[1].set_title('Mask');axs[1].axis('off')
  16. axs[2].imshow(result_image);axs[2].set_title('Result');axs[2].axis('off')
复制代码
  输出图像为:

  改变提示 prompt:
  1. prompt = "A sunflower, high resolution, stands beside a park bench"
复制代码
  输出图像为:

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

3、Depth2Image

  Depth2Image 可以或许使用差别的颜色或纹理生成新图片;
  预训练 pipeline 下载:stabilityai/stable-diffusion-2-depth
  1. import torch
  2. import numpy as np
  3. from PIL import Image
  4. from matplotlib import pyplot as plt
  5. from diffusers import StableDiffusionDepth2ImgPipeline
  6. init_image = Image.open('./dog.png').convert("RGB")
  7. device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
  8. model_id = "E:/Code/kuosan/stable-diffusion-2-depth"
  9. Depth2Img_pipe = StableDiffusionDepth2ImgPipeline.from_pretrained(model_id).to(device)
  10. prompt = "A man on a bench"
  11. result_image = Depth2Img_pipe(prompt=prompt, image=init_image).images[0]
  12. fig, axs = plt.subplots(1, 2, figsize=(12, 5))
  13. axs[0].imshow(init_image);axs[0].set_title('Input Image');axs[0].axis('off')
  14. axs[1].imshow(result_image);axs[1].set_title('Result');axs[1].axis('off')
复制代码
  输出图像为:

  改变提示 prompt:
  1. prompt = "Cartoon style, high resolution, featuring a little kitten happily playing"
复制代码
  输出图像为:

  照旧比力忠于原图的,哈哈…

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

张春

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表