• Stable Diffusion部署教程,开启你的AI绘图之路


    本文环境

    系统:Ubuntu 20.04 64位

    内存:32G

    环境安装

    2.1 安装GPU驱动

    英伟达官网根据显卡型号、操作系统、CUDA等查询驱动版本。官网查询链接https://www.nvidia.com/Download/index.aspx?lang=en-us
    注意这里的CUDA版本,如未安装CUDA可以先选择一个版本,稍后再安装CUDA.

    image.png

    点击Search

    image.png


    如上图,查询到合适的版本为510. 然后可以使用apt安装对应驱动版本,使用apt安装更方便一些。

    1. # 安装510版本驱动
    2. sudo apt install nvidia-driver-510
    3. # 查看驱动信息
    4. nvidia-smi

     当然你也可以使用官网下载的run文件进行安装

    sudo chmod +x NVIDIA-Linux-x86_64-510.108.03.run

    安装

    sudo ./NVIDIA-Linux-x86_64-510.108.03.run

    安装步骤操作之后就可以完成安装了

    输入nvidia-smi查看显卡

    1. chen@chen:~$ nvidia-smi
    2. Sat Jun 22 08:50:27 2024
    3. +-----------------------------------------------------------------------------+
    4. | NVIDIA-SMI 510.39.01 Driver Version: 510.39.01 CUDA Version: 11.6 |
    5. |-------------------------------+----------------------+----------------------+
    6. | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
    7. | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
    8. | | | MIG M. |
    9. |===============================+======================+======================|
    10. | 0 Tesla M40 On | 00000000:01:00.0 Off | 0 |
    11. | N/A 53C P8 17W / 250W | 3MiB / 11520MiB | 0% Default |
    12. | | | N/A |
    13. +-------------------------------+----------------------+----------------------+
    14. +-----------------------------------------------------------------------------+
    15. | Processes: |
    16. | GPU GI CI PID Type Process name GPU Memory |
    17. | ID ID Usage |
    18. |=============================================================================|
    19. | No running processes found |
    20. +-----------------------------------------------------------------------------+

    安装CUDA

    访问英伟达开发者网站先选择CUDA版本(版本要对应2.1中GPU驱动支持的CUDA版本),再根据操作系统选择对应CUDA安装命令,访问链接https://developer.nvidia.com/cuda-toolkit-archive

    image.png

    如上面安装确定所选择驱动对应的CUDA版本为11.6,根据安装命令安装, 以下命令适用Ubuntu 20.04 x86_64, GPU驱动510版本

    1. wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
    2. sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
    3. wget https://developer.download.nvidia.com/compute/cuda/11.6.2/local_installers/cuda-repo-ubuntu2004-11-6-local_11.6.2-510.47.03-1_amd64.deb
    4. sudo dpkg -i cuda-repo-ubuntu2004-11-6-local_11.6.2-510.47.03-1_amd64.deb
    5. sudo apt-key add /var/cuda-repo-ubuntu2004-11-6-local/7fa2af80.pub
    6. sudo apt-get update
    7. sudo apt-get -y install cuda

    2.3 安装Python 3.10

    Stable Diffusion WebUI目前最低支持Python 3.10,所以直接安装3.10版本,安装命令:

    1. apt install software-properties-common
    2. add-apt-repository ppa:deadsnakes/ppa
    3. apt update
    4. apt install python3.10
    5. python3.10 --verison

    PIP设置国内源,由于默认源在国外,所以安装可能经常会出现timeout等问题,使用国内源可以很大程度避免下载包timeout的情况。将如下内容复制到文件~/.pip/pip.conf当中,如没有该文件,先创建touch ~/.pip/pip.conf

    1. [global]
    2. index-url = https://pypi.tuna.tsinghua.edu.cn/simple
    3. [install]
    4. trusted-host = https://pypi.tuna.tsinghua.edu.cn

    但是有一种比较推荐的方法就是使用 Anaconda

     安装Anaconda

    非常推荐使用Anaconda。Anaconda可以便捷获取包且对包能够进行管理,同时对Python环境可以统一管理的发行版本。安装命令也很简单:

    1. wget https://repo.anaconda.com/archive/Anaconda3-5.3.1-Linux-x86_64.sh
    2. bash ./Anaconda3-5.3.1-Linux-x86_64.sh

    安装步骤安装Anaconda,最后一部选择是否要安装vscode可以选N

    建Python3.10.9环境,并使用该环境

    1. conda create -n python3.10.9 python==3.10.9
    2. conda activate python3.10.9

    2.5 安装PyTorch

    首先在PyTorch官网查询对应CUDA版本的Torch,如上述章节2.2中CUDA 11.6需要安装pytorch1.13.1

    1. # 使用conda安装,两种安装方式二选一
    2. conda install pytorch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 pytorch-cuda=11.6 -c pytorch -c nvidia
    3. # 使用pip安装,两种安装方式二选一
    4. pip install torch==1.13.1+cu116 torchvision==0.14.1+cu116 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu116

    我是使用pip安装的

    三、部署Stable Diffusion WebUI

    3.1 下载stable-diffusion-webui

    注意首先激活Python3.10环境:

    conda activate python3.10.9
    

    然后下载stable-diffusion-webui

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

    如果遇到项目clone不下来可以使用我下面的加速地址

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

    安装依赖

    cd到stable-diffusion-webui目录安装相应的依赖,如有访问网络超时、失败等,注意按照章节2.3中设置国内源,如果再次失败,重试几次一般都可完成安装。

    1. cd stable-diffusion-webui
    2. pip install -r requirements_versions.txt
    3. pip install -r requirements.txt

    启动stable-diffusion-webui

    安装完成后,执行如下启动命令:

    python launch.py --listen --enable-insecure-extension-access
    

    这一步骤会下载一些常用模型,如果遇到下载失败,根据报错提示在huggingface.co下载模型放到对应目录,如下载stable-diffusion-v1-5模型,搜索找到https://huggingface.co/runwayml/stable-diffusion-v1-5/tree/main

    每次启动都需要输入一长串命令,比较麻烦,可以写一个shell

    sudo vim start.sh

    里面输入

    sudo /home/chen/anaconda3/envs/python3.10.9/bin/python launch.py --listen --enable-insecure-extension-access
    

    /home/chen是当前我的用户目录,anaconda3创建的虚拟环境是python3.10.9 就写这个python路径anaconda3/envs/python3.10.9

    sudo chmod +x start.sh

    启动项目

    1. chen@chen:/data/stable-diffusion-webui$ ./start.sh
    2. [sudo] password for chen:
    3. Sorry, try again.
    4. [sudo] password for chen:
    5. Python 3.10.9 (main, Mar 8 2023, 10:47:38) [GCC 11.2.0]
    6. Version: v1.9.4
    7. Commit hash: feee37d75f1b168768014e4634dcb156ee649c05
    8. Launching Web UI with arguments: --listen --enable-insecure-extension-access
    9. no module 'xformers'. Processing without...
    10. No SDP backend available, likely because you are running in pytorch versions < 2.0. In fact, you are using PyTorch 1.13.1+cu116. You might want to consider upgrading.
    11. no module 'xformers'. Processing without...
    12. No module 'xformers'. Proceeding without it.
    13. ==============================================================================
    14. You are running torch 1.13.1+cu116.
    15. The program is tested to work with torch 2.1.2.
    16. To reinstall the desired version, run with commandline flag --reinstall-torch.
    17. Beware that this will cause a lot of large files to be downloaded, as well as
    18. there are reports of issues with training tab on the latest version.
    19. Use --skip-version-check commandline argument to disable this check.
    20. ==============================================================================
    21. Loading weights [63d370e256] from /data/stable-diffusion-webui/models/Stable-diffusion/a31_style.safetensors
    22. Running on local URL: http://0.0.0.0:7860
    23. To create a public link, set `share=True` in `launch()`.
    24. Startup time: 14.5s (prepare environment: 2.3s, import torch: 4.9s, import gradio: 1.3s, setup paths: 1.3s, initialize shared: 0.3s, other imports: 1.3s, list SD models: 0.2s, load scripts: 1.5s, create ui: 0.8s, gradio launch: 0.6s).
    25. Creating model from config: /data/stable-diffusion-webui/configs/v1-inference.yaml
    26. /home/chen/anaconda3/envs/python3.10.9/lib/python3.10/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.
    27. warnings.warn(

    访问服务器ip:7860

    随便画一张图试试

  • 相关阅读:
    Java培训了6个月,我竟然是第一个拿到offer的
    【TSP问题】基于蜜蜂算法解决旅行商问题附Matlab代码
    go每日一题——内嵌,json.Marshal函数
    【记录】算法 - C/C++版
    前端面试(手写题)
    try catch 中的finally什么时候运行
    我们在讲的 Database Plus,到底能解决什么样的问题?
    神经网络前向和后向传播推导(一):概览
    Python数学基础2
    思科认证 | 思科认证考点汇总及常见考试注意事项
  • 原文地址:https://blog.csdn.net/cjj2006/article/details/139884769