AI文本生图模型Stable Diffusion部署教程

发布时间:2024年01月13日

本文基于CentOS8进行Stable Diffusion开源框架部署.

1. DNS配置(但是今天出现了偶尔无法下载问题)

为了加速Github访问,我在本机配置如下

(sd) [root@shenjian stable-diffusion-webui]# cat /etc/hosts
127.0.0.1 shenjian localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
185.199.111.133 raw.githubusercontent.com
20.205.243.166 github.com

具体IP可通过https://tool.chinaz.com/dns/github.com获取当前可用最快IP

2. 下载 stable-diffusion-webui 仓库
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
cd stable-diffusion-webui
(base) [root@shenjian ai]# git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
正克隆到 'stable-diffusion-webui'...
remote: Enumerating objects: 30459, done.
remote: Counting objects: 100% (148/148), done.
remote: Compressing objects: 100% (81/81), done.
remote: Total 30459 (delta 84), reused 111 (delta 67), pack-reused 30311
接收对象中: 100% (30459/30459), 33.39 MiB | 12.59 MiB/s, 完成.
处理 delta 中: 100% (21311/21311), 完成.\
3. 下载其他仓库

webui 仅仅是一个外层界面包装,核心功能是通过其他仓库(或以仓库的形式、或以安装包的形式)支撑的。其他仓库需放在 ./repositories 文件夹下:

mkdir repositories

然后依次下载以下仓库,并切换到合适的版本以避免可能的版本适配问题:

stablediffusion

git clone https://github.com/Stability-AI/stablediffusion.git ./repositories/stable-diffusion-stability-ai
git -C ./repositories/stable-diffusion-stability-ai checkout cf1d67a6fd5ea1aa600c4df58e5b47da45f6bdbf

taming-transformers

git clone https://github.com/CompVis/taming-transformers.git ./repositories/taming-transformers
git -C ./repositories/taming-transformers checkout 24268930bf1dce879235a7fddd0b2355b84d7ea6

k-diffusion

git clone https://github.com/crowsonkb/k-diffusion.git ./repositories/k-diffusion
git -C ./repositories/k-diffusion checkout c9fe758757e022f05ca5a53fa8fac28889e4f1cf

CodeFormer

git clone https://github.com/sczhou/CodeFormer.git ./repositories/CodeFormer
git -C ./repositories/CodeFormer checkout c5b4593074ba6214284d6acd5f1719b6c5d739af

BLIP

git clone https://github.com/salesforce/BLIP.git ./repositories/BLIP
git -C ./repositories/BLIP checkout 48211a1594f1321b00f14c9f7a5b4813144b2fb9
4. 建立并激活新 conda 环境

conda加速配置

vim ~/.condarc

替换内容如下

channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/Paddle/
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

然后创建虚拟环境

conda create -n sd-webui python=3.10
5. 安装 requirements.txt 中的依赖包
pip install torch torchvision -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install tb-nightly
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

安装其他依赖包

pip install ftfy regex tqdm -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install open_clip_torch xformers -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -r repositories/CodeFormer/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
# 方式一:可尝试该安装方式,不行换另外一种
pip install git+https://github.com/openai/CLIP.git
# 方式二:
git clone https://github.com/openai/CLIP.git
cd CLIP 
python setup.py install
6. 下载基础模型

链接下载基础模型(Stable Diffusion v2.1,safetensors 格式,5.21 GB),放在 ./models/Stable-diffusion/ 下。当然也可以下载其他基础模型

7. 启动webui.py
# 有同学python webui.py启动,我这边报错了,然后用下面命令启动
# 其实在webui.sh中包含了上述大部分步骤
./webui.sh -f --skip-torch-cuda-test --precision full --no-half

OK,启动成功

Calculating sha256 for /home/shenjian/ai/stable-diffusion-webui/models/Stable-diffusion/v2-1_512-ema-pruned.safetensors: Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.
Startup time: 230.0s (prepare environment: 226.1s, import torch: 1.4s, import gradio: 0.5s, setup paths: 0.4s, other imports: 0.4s, setup codeformer: 0.2s, load scripts: 0.4s, create ui: 0.3s, gradio launch: 0.1s).
df955bdf6b682338ea9b55dfc0d8f3475aadf4836e204893d28b82355e0956d2
Loading weights [df955bdf6b] from /home/shenjian/ai/stable-diffusion-webui/models/Stable-diffusion/v2-1_512-ema-pruned.safetensors
Creating model from config: /home/shenjian/ai/stable-diffusion-webui/repositories/stable-diffusion-stability-ai/configs/stable-diffusion/v2-inference.yaml
Applying attention optimization: InvokeAI... done.
Model loaded in 5.6s (calculate hash: 3.6s, load weights from disk: 0.2s, find config: 0.7s, create model: 0.2s, apply weights to model: 0.7s, calculate empty prompt: 0.2s).

访问http://127.0.0.1:7860

欢迎关注公众号算法小生

文章来源:https://blog.csdn.net/SJshenjian/article/details/135569334
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。