
Stable Diffusion XL搭建
介绍了SDXL的安装过程,涉及webui工程下载、base基础模型、refiner模型、vae模型的安装。
本文参考:Stable Diffusion XL1.0正式发布了,赶紧来尝鲜吧-云海天教程
Stable Diffision最新模型SDXL 1.0使用全教程 - 知乎
1、SDXL与SD的区别
(1)分辨率得到了提升
原先使用SD生成图片,一般都是生成512*512(模型就是基于这个分辨率进行训练的)的图然后再进行放大,以达到高清出图的效果。
这次SDXL1.0直接使用1024*1024的图片训练底模
(2)SDXL1.0由base模型和refiner模型共同组成
SDXL由文生图的base模型和图生图进行优化放大的refiner模型组成,所以生图过程中会先运行基础模型,然后再运行细化模型。基础模型设置全局组成,而细化模型则添加了更多的细节。
2、安装的硬软件环境
硬件:如果希望在GPU上运行,则需要GPU显存在16G以上,否则很难运行。
软件:python需要在3.10以上。
3、下载Stable Diffusion WebUI源码到本地
执行命令:git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
将代码从GitHub下载到了本地
4、 切换到支持SDXL的版本
支持SDXL的webUI版本需要v1.5.0以上。
在stable-diffusion-webui目录下运行:
git checkout -b v1.6.0
5、启动WebUI服务
python launch.py --listen --port 12346 --theme dark --xformers --enable-insecure-extension-access
首次执行该命令后,会自动下载相关算法源码到repositories中,以及相关模型到对应的目录下。
如果运行成功则直接跳到第8步,否则按照6-9步依次手工处理相关问题。
6、安装错误处理
如果出错,则需要手工下载这些代码放到指定目录。以下过程仅针对启动不成功时需要手工安装的步骤:
(1)下载stablediffusion源码
提示错误:“Command: "git" clone "https://github.com/Stability-AI/stablediffusion.git" "/xxx/stable-diffusion-webui/repositories/stable-diffusion-stability-ai"”
则手工再执行git clone https://github.com/Stability-AI/stablediffusion.git ,(如果下载还是不成功则从git中下载zip文件,放到相应目录后解压,该方法适用以下的其他源码。)然后更名为stable-diffusion-stability-ai。
(2)下载k-diffusion源码
手工执行git clone https://github.com/crowsonkb/k-diffusion.git或手工下载zip包
(3)下载CodeFormer源码
手工执行git clone https://github.com/sczhou/CodeFormer.git或手工下载zip包
(4)报错:Please use pip<24.1 if you need to use this version.
stderr: WARNING: Ignoring version 0.2.5 of torchsde since it has invalid metadata:
Requested torchsde==0.2.5 from http://mirrors.cloud.aliyuncs.com/pypi/packages/73/8d/efd3e7b31ea854d0bd6886aa3cf44914adce113a6d460850af41ac1dd4dd/torchsde-0.2.5-py3-none-any.whl#sha256=4c34373a94a357bdf60bbfee00c850f3563d634491555820b900c9a4f7eff300 (from -r requirements_versions.txt (line 30)) has invalid metadata: .* suffix can only be used with `==` or `!=` operators
numpy (>=1.19.*) ; python_version >= "3.7"
~~~~~~~^
Please use pip<24.1 if you need to use this version.
ERROR: Could not find a version that satisfies the requirement torchsde==0.2.5 (from versions: 0.2.5, 0.2.6)
ERROR: No matching distribution found for torchsde==0.2.5
原因:pip版本过高了。
解决方案:pip降级
pip install --upgrade pip==24.0
(5)报错:TypeError: AsyncConnectionPool.__init__() got an unexpected keyword argument 'socket_options'
Launching Web UI with arguments: --listen --port 12346 --theme dark --xformers --enable-insecure-extension-access --disable-safe-unpickl
Traceback (most recent call last):
File "/data/work/xiehao/stable-diffusion-webui/launch.py", line 48, in <module>
main()
File "/data/work/xiehao/stable-diffusion-webui/launch.py", line 44, in main
start()
File "/data/work/xiehao/stable-diffusion-webui/modules/launch_utils.py", line 432, in start
import webui
File "/data/work/xiehao/stable-diffusion-webui/webui.py", line 13, in <module>
initialize.imports()
File "/data/work/xiehao/stable-diffusion-webui/modules/initialize.py", line 21, in imports
import gradio # noqa: F401
File "/data/work/anaconda3/envs/sd/lib/python3.10/site-packages/gradio/__init__.py", line 3, in <module>
import gradio.components as components
File "/data/work/anaconda3/envs/sd/lib/python3.10/site-packages/gradio/components/__init__.py", line 1, in <module>
from gradio.components.annotated_image import AnnotatedImage
File "/data/work/anaconda3/envs/sd/lib/python3.10/site-packages/gradio/components/annotated_image.py", line 12, in <module>
from gradio import utils
File "/data/work/anaconda3/envs/sd/lib/python3.10/site-packages/gradio/utils.py", line 353, in <module>
class AsyncRequest:
File "/data/work/anaconda3/envs/sd/lib/python3.10/site-packages/gradio/utils.py", line 372, in AsyncRequest
client = httpx.AsyncClient()
File "/data/work/anaconda3/envs/sd/lib/python3.10/site-packages/httpx/_client.py", line 1397, in __init__
self._transport = self._init_transport(
File "/data/work/anaconda3/envs/sd/lib/python3.10/site-packages/httpx/_client.py", line 1445, in _init_transport
return AsyncHTTPTransport(
File "/data/work/anaconda3/envs/sd/lib/python3.10/site-packages/httpx/_transports/default.py", line 275, in __init__
self._pool = httpcore.AsyncConnectionPool(
TypeError: AsyncConnectionPool.__init__() got an unexpected keyword argument 'socket_options'
解决方案:httpx安装的版本不对,你可以看看你安装环境下的httpx的版本是哪个,我发现我的是0.25.1。重新安装pip install httpx==0.24.1 --force-reinstall。
(6)错误:cannot import name 'cached_download' from 'huggingface_hub'
*** Error loading script: hotshot_xl_model_controller.py
Traceback (most recent call last):
File "/data/work/xiehao/stable-diffusion-webui/modules/scripts.py", line 382, in load_scripts
script_module = script_loading.load_module(scriptfile.path)
File "/data/work/xiehao/stable-diffusion-webui/modules/script_loading.py", line 10, in load_module
module_spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/data/work/xiehao/stable-diffusion-webui/extensions/Hotshot-XL-Automatic1111/scripts/hotshot_xl_model_controller.py", line 9, in <module>
from hotshot_xl.models.temporal_layers import HotshotXLTemporalLayers
File "/data/work/xiehao/stable-diffusion-webui/extensions/Hotshot-XL-Automatic1111/hotshot_xl/__init__.py", line 15, in <module>
from diffusers.utils import (
File "/data/work/anaconda3/envs/sd/lib/python3.10/site-packages/diffusers/__init__.py", line 5, in <module>
from .utils import (
File "/data/work/anaconda3/envs/sd/lib/python3.10/site-packages/diffusers/utils/__init__.py", line 36, in <module>
from .dynamic_modules_utils import get_class_from_dynamic_module
File "/data/work/anaconda3/envs/sd/lib/python3.10/site-packages/diffusers/utils/dynamic_modules_utils.py", line 28, in <module>
from huggingface_hub import HfFolder, cached_download, hf_hub_download, model_info
ImportError: cannot import name 'cached_download' from 'huggingface_hub' (/data/work/anaconda3/envs/sd/lib/python3.10/site-packages/huggingface_hub/__init__.py)
解决方案:0.26 及以上的版本不支持 cached_download 了。该库好像对别的库没有啥影响,所以直接装一个旧版本就行了:
pip install huggingface_hub==0.25.2 -i https://pypi.tuna.tsinghua.edu.cn/simple
7、py的requirements_versions.txt安装错误处理
报错信息:
处理方法:
pip3 install -r requirements_versions.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
直接官网下载不通的话,建议换成清华大学的数据源进行安装。
8、下载SDXL大模型
(1)首先考虑直接从HuggingFace中下载模型
SDXL大模型涉及stable-diffusion-xl-base和stable-diffusion-xl-refiner两部分,链接地址为:
https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0.safetensors
https://huggingface.co/stabilityai/stable-diffusion-xl-refiner-1.0/resolve/main/sd_xl_refiner_1.0.safetensors
这两个文件是底模,大约7个G每个文件,下载到GPU服务器后,需要放到stable-diffusion-webui/models/Stable-diffusion文件夹中
针对base模型,如果直接使用stablebilityai的stable-diffusion-xl-base-1.0模型,自己使用时效果可能没那么好,我则从c站下载自己喜欢style的基模,比如:https://civitai.com/models/139565/realistic-stock-photo
这个是真实电影感比较强的SDXL1.0的base模型。
而refiner模型则还是使用huggingface提供的refiner模型。
(2)如果HuggingFace和civitai无法连通,可以从https://aliendao.cn/下载对应模型。
9、下载VAE模型
(1)首先下载vaeapprox-sdxl.pt模型(必选)
WebUI启动过程中可能会出现如下类似错误:
Downloading VAEApprox model to: /xxx/stable-diffusion-webui/models/VAE-approx/vaeapprox-sdxl.pt
TimeoutError: [Errno 60] Operation timed out
During handling of the above exception, another exception occurred:
这是因为缺少一个vaeapprox-sdxl.pt模型,一般自动下载会很容易失败,推荐大家去GitHub上手动下载,链接地址为:
https://github.com/AUTOMATIC1111/stable-diffusion-webui/releases/tag/v1.0.0-pre
下载后放到stable-diffusion-webui/models/VAE-approx/目录下即可
(2)安装VAE模型(可选)
https://huggingface.co/stabilityai/sdxl-vae/resolve/main/sdxl_vae.safetensors
下载后放到stable-diffusion-webui/models/VAE目录下即可。
10、使用WebUI的SDXL功能
webui通过以下命令执行成功后,
python launch.py --listen --port 12346 --theme dark --xformers --enable-insecure-extension-access
然后chrome浏览器中输入<服务器ip>:12346后即打开了SD的WebUI服务如下:
注意点1的位置:选择SDXL的base模型
注意点2的位置:选择SDXL的refiner模型,该模型会在base模型运行进展到80%(Refiner的Switch at参数)时切换为refiner模型继续执行。
注意点3的位置:将分辨率从默认的512*512调整为1024*1024,这个是SDXL和SD的显著差异之一。
根据以下prompt我们生成测试图片,
prompt及相关参数信息:
photograph close up portrait of Embraced couple enjoying in a movie in theatre , cinematic 4k epic detailed 4k epic detailed photograph shot on kodak detailed bokeh cinematic hbo dark moody
Steps: 35, Sampler: DPM++ 2M Karras, CFG scale: 7, Seed: 1847092677, Size: 1024x1024, Model hash: 2d44ce378d, Model: realisticStockPhoto_v10, Refiner: sd_xl_refiner_1.0 [7440042bbd], Refiner switch at: 0.8, Version: v1.6.0
图片达到了电影感的效果:
此生成过程耗时1分32秒,占用12.9G的显存。
如果进展到这里都没有问题,那么请开始SDXL的旅程吧!!
更多推荐
所有评论(0)