当前位置:AIGC资讯 > AIGC > 正文

Ubuntu18.04本地部署Stable-Diffusion-webui绘画

记录Ubuntu下安装Stable-Diffusion-webui,比较简单,以供参考。

系统:Ubuntu 18.04.4 LTS
内存:40G
显卡:32G
硬盘:500G

一、安装cuda

支持安装的cuda版本可以用nvidia-smi命令查看:
 

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.182.03   Driver Version: 470.182.03   CUDA Version: 11.4     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  Tesla V100-SXM2...  Off  | 00000000:00:08.0 Off |                    0 |
| N/A   39C    P0    55W / 300W |      0MiB / 32510MiB |      3%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+


显卡驱动支持的cuda版本最高是11.4,可以在这里下载对应的cuda版本:CUDA Toolkit Archive | NVIDIA Developer(https://developer.nvidia.com/cuda-toolkit-archive)。由于之前安装别的环境,已经装了cuda11.4版本,可以用nvcc -V查看安装的cuda版本。


二、搭建python虚拟环境(为了不影响原系统环境)

使用miniconda,我之前已经安装了。然后使用conda创建虚拟环境sdwebui,使用的python版本为3.10.9(stable-diffusion-webui用的python版本为3.10.6)。

conda create -n sdwebui python==3.10.9

激活环境

conda activate sdwebui

升级pip并更改默认库包下载地址为清华镜像(如果已经有镜像的可以忽略)

    python -m pip install --upgrade pip
    pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

安装torch

在pytorch官网Previous PyTorch Versions | PyTorch(https://pytorch.org/get-started/previous-versions/) 官网不能访问的可以打开:Pytorch和CUDA版本对应关系_TURING.DT的博客-CSDN博客

查看对应的cuda支持的torch版本,并且直接拷贝安装命令。

这里是我的cuda版本对应的安装命令。

conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=10.2 -c pytorch


三、下载并安装stable-diffusion-webui

切换到当前用户目录下,使用git命令下载stable-diffusion-webui源码

git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git

由于国内github不能稳定连接,所以可以从国内的gitee(码云)下载

git clone https://gitee.com/yiouyou/automatic1111-stable-diffusion-webui.git

打开v2-1_768-ema-pruned.ckpt · stabilityai/stable-diffusion-2-1 at main(https://huggingface.co/stabilityai/stable-diffusion-2-1/blob/main/v2-1_768-ema-pruned.ckpt),下载训练模型(大小4.9G)。下载完成后,把v2-1_768-ema-pruned.ckpt这个训练模型放入stable-diffusion-webui的models/Stable-diffusion目录下,这个目录专门存放用于生成AI绘图的绘图元素的基础模型库。后续如果在其他网站比如civitai之类的地方下载的ckpt或者safetensors文件也是放在这个文件夹里面。


然后切换到webui目录下,安装依赖

    cd stable-diffusion-webui/
    pip install -r requirements_versions.txt
    pip install -r requirements.txt

安装完成后,启动stable-diffusion-webui

python launch.py

这里要安装很多依赖,并且要git clone相关包和模型文件,需要科学上网,否则会失败。

最终启动成功后如下图所示,会出来一个url:

(sdwebui) ubuntu@VM-16-ubuntu:~/sdwebui/stable-diffusion-webui$ python launch.py

Python 3.10.9 (main, Mar  8 2023, 10:47:38) [GCC 11.2.0]
Version: v1.4.0
Commit hash: 394ffa7b0a7fff3ec484bcd084e673a8b301ccc8
Installing clip
Installing open_clip
Cloning Stable Diffusion into /home/ubuntu/sdwebui/stable-diffusion-webui/repositories/stable-diffusion-stability-ai...
Cloning K-diffusion into /home/ubuntu/sdwebui/stable-diffusion-webui/repositories/k-diffusion...
Cloning CodeFormer into /home/ubuntu/sdwebui/stable-diffusion-webui/repositories/CodeFormer...
Cloning BLIP into /home/ubuntu/sdwebui/stable-diffusion-webui/repositories/BLIP...
Installing requirements for CodeFormer
Installing requirements
Launching Web UI with arguments:
Matplotlib created a temporary config/cache directory at /tmp/matplotlib-9vmy3hxk because the default path (/home/ubuntu/.config/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
No module 'xformers'. Proceeding without it.
==============================================================================
You are running torch 1.12.1.
The program is tested to work with torch 2.0.0.
To reinstall the desired version, run with commandline flag --reinstall-torch.
Beware that this will cause a lot of large files to be downloaded, as well as
there are reports of issues with training tab on the latest version.

Use --skip-version-check commandline argument to disable this check.
==============================================================================
Calculating sha256 for /home/ubuntu/sdwebui/stable-diffusion-webui/models/Stable-diffusion/v2-1_768-ema-pruned.ckpt: preload_extensions_git_metadata for 7 extensions took 0.00s
Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.
Startup time: 7.8s (import torch: 1.6s, import gradio: 1.5s, import ldm: 0.4s, other imports: 3.2s, load scripts: 0.5s, create ui: 0.5s).
ad2a33c361c1f593c4a1fb32ea81afce2b5bb7d1983c6b94793a26a3b54b08a0
Loading weights [ad2a33c361] from /home/ubuntu/sdwebui/stable-diffusion-webui/models/Stable-diffusion/v2-1_768-ema-pruned.ckpt
Creating model from config: /home/ubuntu/sdwebui/stable-diffusion-webui/repositories/stable-diffusion-stability-ai/configs/stable-diffusion/v2-inference-v.yaml
LatentDiffusion: Running in v-prediction mode
DiffusionWrapper has 865.91 M params.
Applying attention optimization: Doggettx... done.


在浏览器打开这个url,表示环境部署成功。


四、使用stable-diffusion-webui

注意:如果在prompt输入后点击generate无法生成图片,看见黑色小框显示“float 32“之类的关键字,则在webui 页面的 Settings->StableDiffusion 最下面,勾选 float32 的选项框。接着点击上方的Apply settings 应用,然后按F5刷新页面后,即可正常使用。

在左上角的文本框prompt输入提示词,点击Generate,等待数秒钟(我的电脑大概等待15s左右),就会生成一张图。

默认端口是7860,如果想换端口 启动时加参数:python launch.py --port 7770

更新时间 2023-11-14