基礎環(huán)境承接Stable Diffusion v1, 詳情請見我的博文【文生圖系列】 Stable Diffusion v1復現(xiàn)教程。然后更新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-torch2.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,會出現(xiàn)CUDA out of memory的錯誤,利用xformer,運行scripts/txt2img.py時,GPU內存占用最高為13多G。
官方文檔中是cuda-11.4,由于我的pytorch的cuda是11.3,按照官方教程安裝xformers,會出現(xiàn)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 記錄
- Torch not compiled with CUDA,需要下載帶有cuda版本的pytorch
- 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模型上微調得到的。數(shù)據(jù)未變,設置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版本的超參數(shù)幾乎一樣,這里不再贅述,具體見【文生圖系列】 Stable Diffusion v1復現(xiàn)教程。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。
租賃的服務器,明天就要到期了。配置完環(huán)境之后,趁著最后的時間極速測試一下。下圖是生成的一張白發(fā)帥哥,超級帥氣,很像貝克漢姆。
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 tolow_cpu_mem_usage=False
. It is strongly recommended to installaccelerate
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()#如果出現(xiàn)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這個動詞也能夠很好地表現(xiàn)出來。同樣的prompt,sd v2相比于sd V1版本的生成圖像更全面。文章來源:http://www.zghlxwxcb.cn/news/detail-500455.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-500455.html
參考
- Stability-AI/stablediffusion
- stabilityai/stable-diffusion-2-1
到了這里,關于【文生圖系列】 Stable Diffusion v2復現(xiàn)教程的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!