【文生图系列】 Stable Diffusion v2复现教程

基础环境承接Stable Diffusion v1, 详情请见我的博文【文生图系列】 Stable Diffusion v1复现教程。然后更新pytorch和torchvision的版本,因为要使用GPU和xformers,需要下载gpu版本的pytorch。再下载open-clip-torch库文件,安装命令如下所示:

conda install pytorch == 1.12.1 torchvision == 0.13.1 torchaudio0.12.1 cudatoolkit=11.3 -c pytorch
pip install open-clip-torch
2.7.0
pip install -e .

pytorch官网上寻找下载命令,pytorch为1.12.1时,有cuda 10.2, 11.3 和11.6的版本,我选择了cuda为11.3版本,那么对应的cudatoolkit=11.3,pytorch安装命令如上所示。

xformers

xformers能够有效地利用GPU,未安装xformers,我的GPU内存为15多G,会出现CUDA out of memory的错误,利用xformer,运行scripts/txt2img.py时,GPU内存占用最高为13多G。

官方文档中是cuda-11.4,由于我的pytorch的cuda是11.3,按照官方教程安装xformers,会出现xFormers wasn’t build with CUDA support cuda错误。所以将cuda的版本更改为11.3。

export CUDA_HOME=/usr/local/cuda-11.3
conda install -c nvidia/label/cuda-11.3.0 cuda-nvcc
conda install -c conda-forge gcc
conda install -c conda-forge gxx_linux-64==9.5.0

下载并安装xformers,在pip install -e .时,会耗费较长时间。

cd …
git clone https://github.com/facebookresearch/xformers.git
cd xformers
git submodule update --init --recursive
pip install -r requirements.txt
pip install -e .
cd …/stablediffusion

bug 记录

  1. Torch not compiled with CUDA,需要下载带有cuda版本的pytorch
  2. xFormers wasn’t build with CUDA support cuda版本不匹配,就如上述所述,将cuda版本由11.4更改为11.3,然后重新安装xformers

txt2img

下载stable-diffusion-2-1 模型,下载链接如下所示。stable-diffusion-2-1 是在stable-diffusion-2模型上微调得到的。数据未变,设置punsafe=0.1额外训练了55k步,然后调整punsafe=0.98后再额外训练155k步。

v2-1_768-ema-pruned.ckpt下载: https://huggingface.co/stabilityai/stable-diffusion-2-1/resolve/main/v2-1_768-ema-pruned.ckpt

sd v2版本和sd v1版本的超参数几乎一样,这里不再赘述,具体见【文生图系列】 Stable Diffusion v1复现教程。sd v2相比于v1支持更高分辨,能生成更高清的图片。

python scripts/txt2img.py --prompt “a professional photograph of an astronaut riding a horse” --ckpt v2-1_768-ema-pruned.ckpt --config configs/stable-diffusion/v2-inference-v.yaml --H 768 --W 768 --device cuda

宇航员骑马的生成图片如下所示,相比于v1,生成的图片更全面,也更有质感。马生成的不错,不够露脸的宇航员有些恐怖。

在这里插入图片描述ps: 原图近乎9M,CSDN最高上传5M的图片,将图片进行了压缩到了3M。

租赁的服务器,明天就要到期了。配置完环境之后,趁着最后的时间极速测试一下。下图是生成的一张白发帅哥,超级帅气,很像贝克汉姆。

python scripts/txt2img.py --prompt “a best-quality photo of an extremely handsome young man with white hair who is muscle” --ckpt v2-1_768-ema-pruned.ckpt --config configs/stable-diffusion/v2-inference-v.yaml --H 768 --W 768 --device cuda

在这里插入图片描述

python scripts/txt2img.py --prompt “a best-quality photo of a classical Chinese Tang Dynasty beauty who is drinking tea” --ckpt v2-1_768-ema-pruned.ckpt --config configs/stable-diffusion/v2-inference-v.yaml --H 768 --W 768 --device cuda

和sd v1相比,相同的提示词,“唐朝美女喝茶的提示词”,sd v2能把品茗这个动作画出来,但是服饰不怎么像唐朝。

在这里插入图片描述
原图近乎9M,CSDN最高上传5M的图片,将图片进行了压缩到了3M。

diffusers

使用diffusers库能够更简单更有效率地运行sd 2。

Cannot initialize model with low cpu memory usage because accelerate was not found in the environment. Defaulting to low_cpu_mem_usage=False. It is strongly recommended to install accelerate for faster and less memory-intense model loading. You can do so with:

pip install accelerate safetensors

import torch
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler

model_id = "stabilityai/stable-diffusion-2-1"

# Use the DPMSolverMultistepScheduler (DPM-Solver++) scheduler here instead
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe.enable_attention_slicing()#如果出现CUDA out of memory.加这行代码
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda")

prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]
    
image.save("astronaut_rides_horse.png")

"a photo of an astronaut riding a horse on mars"在火星上骑马的生成图片如下所示,宇航员、马和火星三个要素都具备,而且riding这个动词也能够很好地表现出来。同样的prompt,sd v2相比于sd V1版本的生成图像更全面。

在这里插入图片描述

参考

  1. Stability-AI/stablediffusion
  2. stabilityai/stable-diffusion-2-1
  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

马鹤宁

谢谢

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

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

打赏作者

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

抵扣说明:

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

余额充值