Hi!请登陆

Stable Diffusion AI 绘画项目搭建详解,并运行案例 (2023更新)

2023-3-1 355 3/1

开始之前

先把人家的git放过来:https://github.com/CompVis/stable-diffusion
40.7k 的stars, flask 和 django 两个web框架也不过如此数量级吧。
就是给一段文字,它能按照文字描述给你画画。画出来的还挺好看,是一个text-to-image diffusion model
是基于transformer 和多模态的一个产品。

Stable Diffusion is a latent text-to-image diffusion model. Thanks to a generous compute donation from Stability AI and support from LAION, we were able to train a Latent Diffusion Model on 512x512 images from a subset of the LAION-5B database. Similar to Google’s Imagen, this model uses a frozen CLIP ViT-L/14 text encoder to condition the model on text prompts. With its 860M UNet and 123M text encoder, the model is relatively lightweight and runs on a GPU with at least 10GB VRAM.
这一段是我从git上copy下来的,翻译:
Stable Diffusion 是文本到图像的扩散模型,多亏了来自Stability AI和 LAION的支持我们能基于数据集LAION-5B database训练出一个512x512图像的潜在扩散模型。和 Google的画像类似,我们这个模型用了fronzen的CLIP vv - l /14文本编码器来根据文本提示调整模型,由于60M UNet 和 123M text encoder,这个模型是轻量级别的,并且可以跑在最低10GB显存的GPU上,[妈的,这还是轻量级的],

1.安装环境

1.1 基础环境

  • 没有环境新建环境

A suitable conda environment named ldm can be created and activated with:

conda env create -f environment.yaml
conda activate ldm

 

  • 存在环境后的更新环境
    You can also update an existing latent diffusion environment by running
conda install pytorch torchvision -c pytorch
pip install transformers==4.19.2 diffusers invisible-watermark
pip install -e .

 

可能遇到的问题:

ERROR: Command errored out with exit status 128: git clone -q https://github.com/openai/CLIP.git /home/jianming_ge/workplace/stable-diffusion-main/src/clip Check the logs for full command output.

 

Stable Diffusion AI 绘画项目搭建详解,并运行案例 (2023更新)
原因在这里,是因为server网络连不到git上。
我的解决方案:

 pip install taming-transformers
 pip install clip
 # 最后
 pip install -e .

 

1.2 权重文件

权重文件在git的这里。
Stable Diffusion AI 绘画项目搭建详解,并运行案例 (2023更新)

我选的是stable-diffusion-v1-4,
https://huggingface.co/CompVis/stable-diffusion-v1-4

2.牛刀小试

2.1 用法在这里

Stable Diffusion AI 绘画项目搭建详解,并运行案例 (2023更新)
漫长的下载过程

(py39_torch1.10.1) [jianming_ge@localhost workplace]$ /home/jianming_ge/miniconda3/envs/py39_torch1.10.1/bin/python /home/jianming_ge/workplace/ldm.py
Downloading ()ain/model_index.json: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 543/543 [00:00<00:00, 86.8kB/s]
Downloading ()rocessor_config.json: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 342/342 [00:00<00:00, 85.7kB/s]
Downloading ()_checker/config.json: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████| 4.56k/4.56k [00:00<00:00, 916kB/s]
Downloading ()_encoder/config.json: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 592/592 [00:00<00:00, 160kB/s]
Downloading ()cheduler_config.json: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 313/313 [00:00<00:00, 62.5kB/s]
Downloading ()nfig-checkpoint.json: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 209/209 [00:00<00:00, 43.9kB/s]
Downloading ()okenizer_config.json: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 806/806 [00:00<00:00, 186kB/s]
Downloading ()3d4/unet/config.json: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 743/743 [00:00<00:00, 318kB/s]
Downloading ()cial_tokens_map.json: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 472/472 [00:00<00:00, 101kB/s]
Downloading ()nfig-checkpoint.json:   0%|                                                                                                                 | 0.00/209 [00:00<?, ?B/s/home/jianming_ge/miniconda3/envs/ldm/bin/python /home/jianming_ge/workplace/ldm.py                                                                         | 0.00/806 [00:00<?, ?B/s]
Downloading ()23d4/vae/config.json: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 522/522 [00:00<00:00, 176kB/s]
Downloading ()tokenizer/merges.txt: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████| 525k/525k [00:16<00:00, 31.7kB/s]
Downloading ()tokenizer/vocab.json: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 1.06M/1.06M [00:30<00:00, 34.8kB/s]
Downloading ()"pytorch_model.bin";: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████| 492M/492M [06:19<00:00, 1.30MB/s]
Downloading ()_pytorch_model.bin";: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████| 335M/335M [11:17<00:00, 494kB/s]
Downloading (…)"pytorch_model.bin";:   3%|███▌                                                                                                  | 41.9M/1.22G [00:26<13:22, 1.46MB/s]
Downloading (…)"pytorch_model.bin";:  26%|██████████████████████████▉                                                                             | 315M/1.22G [06:06<23:42, 634kB/s]
Downloading (…)"pytorch_model.bin";:  47%|█████████████████████████████████████████████████▎                                                      | 577M/1.22G [11:12<14:50, 718kB/s]
Downloading (…)"pytorch_model.bin";:  89%|██████████████████████████████████████████████████████████████████████████████████████████▌           | 1.08G/1.22G [16:20<01:12, 1.88MB/s]
Downloading (…)_pytorch_model.bin";:  38%|██████████████████████████████████████▌                                                               | 1.30G/3.44G [16:14<21:55, 1.63MB/s]
Downloading ()_pytorch_model.bin";: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████| 335M/335M [11:17<00:00, 458kB/s]

 

深度学习一半时间在鼓捣环境啊
出来了
Stable Diffusion AI 绘画项目搭建详解,并运行案例 (2023更新)
全套代码在这里

import torch
from diffusers import StableDiffusionPipeline

model_id = "CompVis/stable-diffusion-v1-4"
device = "cuda"


pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to(device)

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

 

命令行下10几秒钟一张
模型提示输入这个会快一些:

pip install accelerate

Stable Diffusion AI 绘画项目搭建详解,并运行案例 (2023更新)

3.封装api

待续

总结

好玩是挺好玩,但是就拉个git没学到东西啊,算法工程师真的就是git拉个项目么。。。。呜呜呜
唯一遇到的问题是可能服务器上安装不上因为
Stable Diffusion AI 绘画项目搭建详解,并运行案例 (2023更新)
你连上git

相关推荐