Docker部署Stable-Diffusion-webui

news/2024/7/10 21:44:21 标签: docker, stable diffusion, AIGC, AI作画, 人工智能

前排提示:如果不想折腾,可直接跳到最后获取封装好的容器,一键运行 :D


前言

乘上AI生成的快车,一同看看沿途的风景。

启一个miniconda容器

docker run -itd -v 宿主机内SD项目路径:/tmp --gpus all --ipc host -p 7860:7860 continuumio/miniconda3:latest

打开容器:

docker exec -it 容器id bash
cd /tmp

下载项目代码

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

注:如果搭了梯子也无法 git clone,可以直接下载 项目.zip 文件,然后在本地解压。

安装依赖

先安装 torch和更新setuptools,不然:在使用pip安装basicsr库的时候卡住在Preparing metadata (setup.py)。

pip install --upgrade pip setuptools wheel
pip3 install torch torchvision --index-url https://download.pytorch.org/whl/你的cuda版本,比如:cu121

上面的 index-url 参考 pytorch 官方:

Start Locally | PyTorch

使用清华源安装 requirements_versions.txt :

pip install -r requirements_versions.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

使用阿里源安装 requirements.txt :

pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple

注:清华源安装 requirements.txt 会出现报错:

 Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [26 lines of output]
      Traceback (most recent call last):
        File "/tmp/pip-install-_h0cg0e2/basicsr_f6e24d42eda94c67800f8a48e46b0e6a/setup.py", line 50, in get_hash
          from basicsr.version import __version__
        File "/tmp/pip-install-_h0cg0e2/basicsr_f6e24d42eda94c67800f8a48e46b0e6a/basicsr/__init__.py", line 3, in <module>
          from .archs import *
        File "/tmp/pip-install-_h0cg0e2/basicsr_f6e24d42eda94c67800f8a48e46b0e6a/basicsr/archs/__init__.py", line 5, in <module>
          from basicsr.utils import get_root_logger, scandir
        File "/tmp/pip-install-_h0cg0e2/basicsr_f6e24d42eda94c67800f8a48e46b0e6a/basicsr/utils/__init__.py", line 3, in <module>
          from .img_process_util import USMSharp, usm_sharp
        File "/tmp/pip-install-_h0cg0e2/basicsr_f6e24d42eda94c67800f8a48e46b0e6a/basicsr/utils/img_process_util.py", line 1, in <module>
          import cv2
      ModuleNotFoundError: No module named 'cv2'
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/tmp/pip-install-_h0cg0e2/basicsr_f6e24d42eda94c67800f8a48e46b0e6a/setup.py", line 139, in <module>
          write_version_py()
        File "/tmp/pip-install-_h0cg0e2/basicsr_f6e24d42eda94c67800f8a48e46b0e6a/setup.py", line 67, in write_version_py
          sha = get_hash()
                ^^^^^^^^^^
        File "/tmp/pip-install-_h0cg0e2/basicsr_f6e24d42eda94c67800f8a48e46b0e6a/setup.py", line 53, in get_hash
          raise ImportError('Unable to get git version')
      ImportError: Unable to get git version
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

下载模型

推荐在 Hugging Face 下载模型文件:

Stable Diffusion 2.1 版本:https://huggingface.co/stabilityai/stable-diffusion-2-1/tree/main

把文件下载到本地:(需要登录后才能下载)

注:如果网络不好,可以选择网盘地址(如果失效,和我联系 :D)。

最后,把下载到的模型文件放到./models/Stable-diffusion下。

运行 webui.py

打开 ./modules/launch_utils.py,添加https://ghproxy.com/def prepare_environment函数中的如下位置:

清空 ./repositories 文件夹内的所有文件(如果为空,可忽略),然后依次执行(顺序不要错):

git clone https://github.com/Stability-AI/stablediffusion.git "repositories/stable-diffusion-stability-ai"
git clone https://github.com/crowsonkb/k-diffusion.git "repositories/k-diffusion"
git clone https://github.com/salesforce/BLIP.git "repositories/BLIP"
git clone https://github.com/sczhou/CodeFormer.git "repositories/CodeFormer"

运行 webui.py

可能的报错:ImportError: libGL.so.1: cannot open shared object file: No such file or directory

解决:

pip install opencv-python-headless

可能使用wsl2的同学会遇到一个问题:宿主机无法打开 127.0.0.1:7860

原因参考:使用 WSL 访问网络应用程序

参数对应代码:

modules/cmd_args.py里:

parser.add_argument("--listen", action="store_true", help="launch gradio with 0.0.0.0 as server name, allowing to respond to network requests")

webui.py里:

api.launch(
        server_name="0.0.0.0" if cmd_opts.listen else "127.0.0.1",
        ...
)

注1: 对于同一个局域网下,不在宿主机上直接打开网页的同学,可以在你的主机浏览器里输入:

宿主机ip:7860

注2:如果要用CPU推理( :D ) ,可能出现报错:

RuntimeError: "xxx" not implemented for 'Half'

解决:启动 webui.py 的时候,再加上参数 --no-half

注3:如果报错 CUDA out of memory:

torch.cuda.OutOfMemoryError: CUDA out of memory. {省略一长串}. If reserved but unallocated memory is large try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

解决:只需把 PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:32可以加到系统环境变量里面:(镜像里我已加上)

接下来就可以愉快地生成图片啦!

资源消耗

生成图片分辨率设置为768(SD 2.0 使用 768*768 分辨率图片训练所得),8G显存已经无法满足生成需求,因此单张图片生成时间极其缓慢(花费31.8s):

Ubuntu平台:因为能使用更多显存,3060 12G的生成速度快于 3070 8G(其实也可能和WSL性能损耗有关)

最后得到一张效果较为不错的照片:

标醉里不知天在水,满船清梦压星河题

一键运行:

对于不想折腾的同学,可以直接下载我封装好的镜像:网盘地址

打开终端,运行如下命令:

cd 镜像名称.tar所在的路径
docker load -i wind_Stable_Diffusion_Webui.tar
docker run -it --rm --gpus all --ipc host -p 7860:7860 wind/stable_diffusion_webui:v1
python webui.py

入股一键运行出现报错,可参考前面的详细教程。

—End


http://www.niftyadmin.cn/n/5346342.html

相关文章

前出深入-机器学习

文章目录 一、K近邻算法1.1 先画一个散列图1.2 使用K最近算法建模拟合数据1.3 进行预测1.4 K最近邻算法处理多元分类问题1.5 K最近邻算法用于回归分析1.6 K最近邻算法项目实战-酒的分类1.6.1 对数据进行分析1.6.2 生成训练数据集和测试数据集1.6.3 使用K最近邻算法对数据进行建…

Fastbee开源物联网项目RoadMap

架构优化 代码简化业务&协议解耦关键组件支持横向拓展网络协议支持横向拓展&#xff0c;包括&#xff1a;mqtt broker,tcp,coap,udp,sip等协议插件化编码脚本化业务代码模版化消息总线 功能优化 网关/子网关&#xff1a;上线&#xff0c;绑定&#xff0c;拓扑&#xff0…

TensorFlow 深度学习 开发环境搭建 全教程

PyTorch 深度学习 开发环境搭建 全教程 TensorFlow 深度学习 开发环境搭建 全教程 1、指定清华源命令 -i https://pypi.tuna.tsinghua.edu.cn/simple2、conda安装 这是AI开发环境的全家桶&#xff0c;官网下载链接Anaconda | Start Coding Immediately 尽量不要选择太新版本…

Element-Plus如何实现表单校验和表单重置

一&#xff1a;页面布局介绍&#xff1a; 这是我刚刚用基于vue3element-plus写好的一个部门管理的页面 基本的增删改查已经写好&#xff0c;下面我只提供页面的template和style的代码&#xff1a; template <template><el-card class"box-card"><…

5.列表选择弹窗(BottomListPopup)

愿你出走半生,归来仍是少年&#xff01; 环境&#xff1a;.NET 7、MAUI 从底部弹出的列表选择弹窗。 1.布局 <?xml version"1.0" encoding"utf-8" ?> <toolkit:Popup xmlns"http://schemas.microsoft.com/dotnet/2021/maui"xmlns…

Vue3基础:pnpm是什么?npm和pnpm的区别?如何使用pnpm?

pnpm 是一个流行的 JavaScript 包管理器&#xff0c;类似于 npm 和 yarn。它是 performant npm 的缩写&#xff0c;意在表明它是一个更高效的 npm 替代品。pnpm 的主要特点和优势包括&#xff1a; 高效的存储空间使用 pnpm 使用称为“内容寻址存储”的机制来存储 npm 包。这意…

中间件安全

中间件安全 vulhub漏洞复现&#xff1a;https://vulhub.org/操作教程&#xff1a;https://www.freebuf.com/sectool/226207.html 一、Apache Apache(音译为阿帕奇)是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上&#xff0c;由于其跨平台和…

Ubuntu2204+ROS2(humble)+usb_cam内参标定

1、安装usb_cam包 sudo apt install ros-humble-usb-cam# 测试打开相机 ros2 launch usb_cam camera.launch.py# 显示图像 ros2 run image_view image_view image:/camera1/image_raw 2、安装 camera_calibration sudo apt install ros-humble-camera-calibration 3、执行标…