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

Stable Diffusion Web UI + Anaconda环境 + 本地Windows系统部署

Stable Diffusion Web UI + Anaconda环境 + 本地Windows系统部署

最近的很多AIGC模型层出不穷,Stable Diffusion 模型作为一个开源的热门生成式模型,或许对未来的各行各业都能产生深远的影响,了解这个模型并会使用可能是很多人目前想要学习的,本篇博客还原了本人从0-1的一个安装+部署过程,希望对屏幕前的各位读者有用,谢谢!创作不易,喜欢的可以点赞收藏
为什么要用anaconda? 原因其实很简单,anaconda可以做到环境隔离,即在创建好的环境中进行安装,不会影响其他程序或者模型的使用
Stable Diffusion Web UI 项目来源于github,链接:https://github.com/AUTOMATIC1111/stable-diffusion-webui,截至今日有着64.5k的star数量。
Stable Diffusion Web UI 项目部署在本地,可以正常应用的一个界面:

可以看到,我这prompt 框中加入了一些prompt 信息,A painting of a cute goldendoodle in the sky, wearing a suit, natural light, with bright colors, by Studio Ghibli
(入门)Stable Diffusion怎么写prompt?

其中的关键词意思:

提出问题:您想要一张照片还是一幅画?照片的主题是什么?人物?动物或者风景?您想要添加哪些细节?
1 特殊照明。柔和、环形、霓虹
2 环境。室内、室外、水下、太空
3 颜色方案。鲜艳、暗淡、粉彩
4 视角。正面、俯视、侧面
5 背景。纯色、星云、森林
6 采用特定的艺术风格吗?3D渲染、工作室、电影海报
7 特定的照片类型?微距、长焦
回答问题整理出prompt:
现在我们假设来回答上面的问题,然后尝试整理出我们需要的prompt
你想要一张照片还是一幅画? => 画 (painting)
照片的主题是什么? 人物。动物,风景。=> 一只金毛犬 (a cute goldendoodle)
你希望添加什么细节? => 穿着西装 (wearing a suit)
特殊照明。 柔和的、环境的、环形光、霓虹灯=> (natural light)
环境。 室内、室外、水下、太空中 => 在天空中 (in the sky)
色彩方案。 鲜艳的、深色的、粉色的 => 有明亮的颜色 (bright colors)
在特定的艺术风格? 3D渲染,吉卜力工作室,电影海报 => 工作室出品
我们汇总下我们的prompt如下:
A painting of a cute goldendoodle in the sky, wearing a suit, natural light, with bright colors, by Studio Ghibli


利用元素法典https://docs.qq.com/doc/DWHl3am5Zb05QbGVs进行绘制,仅供参考

接下来是我要推荐的博客,我本人也是根据这些博客以及之前的一些模型经验进行的安装:
1、全网最能打的AI部署教程,Anaconda环境完美本地部署Stable-Diffusion-Webui+novelAI
2、【AI作画】使用stable-diffusion-webui搭建AI作画平台

据说还有秋叶整合包,博主并没有特意去找,下一步计划是找一下资源,学习一下

1、创建anaconda环境

1)官方仓库推荐的是python 3.10.6的版本

创建一个命名为sdaigc的python环境

conda create -n sdaigc python=3.10.6

2)安装 torch、torchvision、torchaudio,,这里博主机器的cuda 是11.1版本,但按照之前的经验,在ananconda中安装cuda11.3是可以向下兼容的,
进入pytorch官网https://pytorch.org/get-started/previous-versions/,,使用pip命令下载

# CUDA 11.3
pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0 --extra-index-url https://download.pytorch.org/whl/cu113

验证创建环境,pytorch是否安装正确,可以使用
annconada command输入
activate sdaigc

python
import torch
print(torch.cuda.is_available())
>>True

2、克隆stable-diffusion-webui 至本地,并安装依赖项

拉取stable-diffsion-webui项目,克隆至本地:

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

这里我克隆的位置在E:\stable diffusion\stable-diffusion-webui并提前下好模型权重,放置在E:\stable diffusion\stable-diffusion-webui\models\Stable-diffusion中,
模型权重可以从https://huggingface.co/CompVis/stable-diffusion-v-1-4-original中下载,3.97g
也可以在https://rentry.org/sdmodels中查找所需
或可以通过博主的百度云链接进行下载:链接:https://pan.baidu.com/s/1hFOBkPYSb4-D7Bv9D9GxdQ 提取码:39c6

激活创建好的conda环境后,进入stable-diffusion-webui文件主目录,使用以下指令安装依赖

pip install -r requirements.txt

没有报错后,在stable-diffusion-webui文件主目录使用以下指令,运行stable-diffusion-webui

python lauch.py

然而会出现以下的N多问题
!!!由于我解决这些问题时,并未对报错的提示进行截图整理或者复制,这里主要借用了【AI作画】使用stable-diffusion-webui搭建AI作画平台整理的内容,以下的截图均使用这篇博客的!!!

我遇到的问题一:

安装CLIP时提示Connection was aborted, errno 10053
出错时的错误打印如下:

(novelai) E:\workspace\02_Python\novalai\stable-diffusion-webui>python launch.py
Python 3.10.6 | packaged by conda-forge | (main, Oct 24 2022, 16:02:16) [MSC v.1916 64 bit (AMD64)]
Commit hash: b8f2dfed3c0085f1df359b9dc5b3841ddc2196f0
Installing clip
Traceback (most recent call last):
  File "E:\workspace\02_Python\novalai\stable-diffusion-webui\launch.py", line 251, in <module>
    prepare_enviroment()
  File "E:\workspace\02_Python\novalai\stable-diffusion-webui\launch.py", line 178, in prepare_enviroment
    run_pip(f"install {clip_package}", "clip")
  File "E:\workspace\02_Python\novalai\stable-diffusion-webui\launch.py", line 63, in run_pip
    return run(f'"{python}" -m pip {args} --prefer-binary{index_url_line}', desc=f"Installing {desc}", errdesc=f"Couldn't install {desc}")
  File "E:\workspace\02_Python\novalai\stable-diffusion-webui\launch.py", line 34, in run
    raise RuntimeError(message)
RuntimeError: Couldn't install clip.
Command: "D:\anaconda3\envs\novelai\python.exe" -m pip install git+https://github.com/openai/CLIP.git@d50d76daa670286dd6cacf3bcd80b5e4823fc8e1 --prefer-binary
Error code: 1
stdout: Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting git+https://github.com/openai/CLIP.git@d50d76daa670286dd6cacf3bcd80b5e4823fc8e1
  Cloning https://github.com/openai/CLIP.git (to revision d50d76daa670286dd6cacf3bcd80b5e4823fc8e1) to c:\users\yefuf\appdata\local\temp\pip-req-build-f8w7kbzg

stderr:   Running command git clone --filter=blob:none --quiet https://github.com/openai/CLIP.git 'C:\Users\yefuf\AppData\Local\Temp\pip-req-build-f8w7kbzg'
  fatal: unable to access 'https://github.com/openai/CLIP.git/': OpenSSL SSL_read: Connection was aborted, errno 10053
  error: subprocess-exited-with-error

  git clone --filter=blob:none --quiet https://github.com/openai/CLIP.git 'C:\Users\yefuf\AppData\Local\Temp\pip-req-build-f8w7kbzg' did not run successfully.
  exit code: 128

  See above for output.

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

git clone --filter=blob:none --quiet https://github.com/openai/CLIP.git 'C:\Users\yefuf\AppData\Local\Temp\pip-req-build-f8w7kbzg' did not run successfully.
exit code: 128

See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

通过访CLIP项目GitHub主页,发现该项目可以通过如下命令进行安装解决。

pip install ftfy regex tqdm
pip install git+https://github.com/openai/CLIP.git

我遇到的问题二:项目启动中提示Connection was reset in connection to github.com
出错时的错误打印如下:

(novelai) E:\workspace\02_Python\novalai\stable-diffusion-webui>python launch.py
Python 3.10.6 | packaged by conda-forge | (main, Oct 24 2022, 16:02:16) [MSC v.1916 64 bit (AMD64)]
Commit hash: b8f2dfed3c0085f1df359b9dc5b3841ddc2196f0
Cloning Stable Diffusion into repositories\stable-diffusion...
Cloning Taming Transformers into repositories\taming-transformers...
Traceback (most recent call last):
  File "E:\workspace\02_Python\novalai\stable-diffusion-webui\launch.py", line 251, in <module>
    prepare_enviroment()
  File "E:\workspace\02_Python\novalai\stable-diffusion-webui\launch.py", line 201, in prepare_enviroment
    git_clone(taming_transformers_repo, repo_dir('taming-transformers'), "Taming Transformers", taming_transformers_commit_hash)
  File "E:\workspace\02_Python\novalai\stable-diffusion-webui\launch.py", line 85, in git_clone
    run(f'"{git}" clone "{url}" "{dir}"', f"Cloning {name} into {dir}...", f"Couldn't clone {name}")
  File "E:\workspace\02_Python\novalai\stable-diffusion-webui\launch.py", line 34, in run
    raise RuntimeError(message)
RuntimeError: Couldn't clone Taming Transformers.
Command: "git" clone "https://github.com/CompVis/taming-transformers.git" "repositories\taming-transformers"
Error code: 128
stdout: <empty>
stderr: Cloning into 'repositories\taming-transformers'...
fatal: unable to access 'https://github.com/CompVis/taming-transformers.git/': OpenSSL SSL_connect: Connection was reset in connection to github.com:443

查看lauch.py中的代码可以发现,程序在启动时有对依赖项目进行检查,如项目不存在,则克隆下来。

def prepare_enviroment():
    torch_command = os.environ.get('TORCH_COMMAND', "pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 --extra-index-url https://download.pytorch.org/whl/cu113")
    requirements_file = os.environ.get('REQS_FILE', "requirements_versions.txt")
    commandline_args = os.environ.get('COMMANDLINE_ARGS', "")

    gfpgan_package = os.environ.get('GFPGAN_PACKAGE', "git+https://github.com/TencentARC/GFPGAN.git@8d2447a2d918f8eba5a4a01463fd48e45126a379")
    clip_package = os.environ.get('CLIP_PACKAGE', "git+https://github.com/openai/CLIP.git@d50d76daa670286dd6cacf3bcd80b5e4823fc8e1")
    deepdanbooru_package = os.environ.get('DEEPDANBOORU_PACKAGE', "git+https://github.com/KichangKim/DeepDanbooru.git@d91a2963bf87c6a770d74894667e9ffa9f6de7ff")

    xformers_windows_package = os.environ.get('XFORMERS_WINDOWS_PACKAGE', 'https://github.com/C43H66N12O12S2/stable-diffusion-webui/releases/download/f/xformers-0.0.14.dev0-cp310-cp310-win_amd64.whl')

    stable_diffusion_repo = os.environ.get('STABLE_DIFFUSION_REPO', "https://github.com/CompVis/stable-diffusion.git")
    taming_transformers_repo = os.environ.get('TAMING_REANSFORMERS_REPO', "https://github.com/CompVis/taming-transformers.git")
    k_diffusion_repo = os.environ.get('K_DIFFUSION_REPO', 'https://github.com/crowsonkb/k-diffusion.git')
    codeformer_repo = os.environ.get('CODEFORMET_REPO', 'https://github.com/sczhou/CodeFormer.git')
    blip_repo = os.environ.get('BLIP_REPO', 'https://github.com/salesforce/BLIP.git')

    stable_diffusion_commit_hash = os.environ.get('STABLE_DIFFUSION_COMMIT_HASH', "69ae4b35e0a0f6ee1af8bb9a5d0016ccb27e36dc")
    taming_transformers_commit_hash = os.environ.get('TAMING_TRANSFORMERS_COMMIT_HASH', "24268930bf1dce879235a7fddd0b2355b84d7ea6")
    k_diffusion_commit_hash = os.environ.get('K_DIFFUSION_COMMIT_HASH', "f4e99857772fc3a126ba886aadf795a332774878")
    codeformer_commit_hash = os.environ.get('CODEFORMER_COMMIT_HASH', "c5b4593074ba6214284d6acd5f1719b6c5d739af")
    blip_commit_hash = os.environ.get('BLIP_COMMIT_HASH', "48211a1594f1321b00f14c9f7a5b4813144b2fb9")

因此,还是利用git bash重新执行下述git命令,预先将项目克隆下来。

git clone https://github.com/Stability-AI/stablediffusion.git "E:\stable diffusion\stable-diffusion-webui\repositories\stable-diffusion-stability-ai"

git clone https://github.com/CompVis/taming-transformers.git "repositories\taming-transformers"

git clone https://github.com/crowsonkb/k-diffusion.git "repositories\k-diffusion"

git clone https://github.com/sczhou/CodeFormer.git "repositories\CodeFormer"

git clone https://github.com/salesforce/BLIP.git  "repositories\BLIP"

文件夹如下所示:

3、再次运行

python lauch.py

会发现,有以下报错:

(sdaigc) E:\stable diffusion\stable-diffusion-webui>python launch.py
Python 3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:29:51) [MSC v.1929 64 bit (AMD64)]
Commit hash: 22bcc7be428c94e9408f589966c2040187245d81
Installing requirements for Web UI
Launching Web UI with arguments:
No module 'xformers'. Proceeding without it.
==============================================================================
You are running torch 1.11.0+cu113.
The program is tested to work with torch 1.13.1.
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 E:\stable diffusion\stable-diffusion-webui\models\Stable-diffusion\sd-v1-4.ckpt: fe4efff1e174c627256e44ec2991ba279b3816e364b49f9be2abc0b3ff3f8556
Loading weights [fe4efff1e1] from E:\stable diffusion\stable-diffusion-webui\models\Stable-diffusion\sd-v1-4.ckpt
Error verifying pickled file from E:\stable diffusion\stable-diffusion-webui\models\Stable-diffusion\sd-v1-4.ckpt:
Traceback (most recent call last):
  File "E:\stable diffusion\stable-diffusion-webui\modules\safe.py", line 135, in load_with_extra
    check_pt(filename, extra_handler)
  File "E:\stable diffusion\stable-diffusion-webui\modules\safe.py", line 93, in check_pt
    unpickler.load()
  File "E:\stable diffusion\stable-diffusion-webui\modules\safe.py", line 30, in persistent_load
    return TypedStorage()
  File "E:\Anaconda\envs\sdaigc\lib\site-packages\torch\storage.py", line 270, in __init__
    assert type(self) != _TypedStorage, (
AssertionError: Calling __init__ this way is only supported in _TypedStorage's child classes. _TypedStorage can only be directly instantiated when kwargs 'wrap_storage' and 'dtype' are given.


The file may be malicious, so the program is not going to read it.
You can skip this check with --disable-safe-unpickle commandline argument.


loading stable diffusion model: AttributeError
Traceback (most recent call last):
  File "E:\stable diffusion\stable-diffusion-webui\webui.py", line 139, in initialize
    modules.sd_models.load_model()
  File "E:\stable diffusion\stable-diffusion-webui\modules\sd_models.py", line 415, in load_model
    state_dict = get_checkpoint_state_dict(checkpoint_info, timer)
  File "E:\stable diffusion\stable-diffusion-webui\modules\sd_models.py", line 262, in get_checkpoint_state_dict
    res = read_state_dict(checkpoint_info.filename)
  File "E:\stable diffusion\stable-diffusion-webui\modules\sd_models.py", line 248, in read_state_dict
    sd = get_state_dict_from_checkpoint(pl_sd)
  File "E:\stable diffusion\stable-diffusion-webui\modules\sd_models.py", line 197, in get_state_dict_from_checkpoint
    pl_sd = pl_sd.pop("state_dict", pl_sd)
AttributeError: 'NoneType' object has no attribute 'pop'

主要是这句话:

The file may be malicious, so the program is not going to read it.
You can skip this check with --disable-safe-unpickle commandline argument.

所以再次运行如下指令:

python launch.py --disable-safe-unpickle

运行成功

(sdaigc) E:\stable diffusion\stable-diffusion-webui>python launch.py --disable-safe-unpickle
Python 3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:29:51) [MSC v.1929 64 bit (AMD64)]
Commit hash: 22bcc7be428c94e9408f589966c2040187245d81
Installing requirements for Web UI
Launching Web UI with arguments: --disable-safe-unpickle
No module 'xformers'. Proceeding without it.
==============================================================================
You are running torch 1.11.0+cu113.
The program is tested to work with torch 1.13.1.
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.
==============================================================================
Loading weights [fe4efff1e1] from E:\stable diffusion\stable-diffusion-webui\models\Stable-diffusion\sd-v1-4.ckpt
Creating model from config: E:\stable diffusion\stable-diffusion-webui\configs\v1-inference.yaml
LatentDiffusion: Running in eps-prediction mode
DiffusionWrapper has 859.52 M params.
Downloading (…)olve/main/vocab.json: 100%|███████████████████████████████████████████| 961k/961k [00:02<00:00, 386kB/s]
Downloading (…)olve/main/merges.txt: 100%|███████████████████████████████████████████| 525k/525k [00:01<00:00, 517kB/s]
Downloading (…)cial_tokens_map.json: 100%|█████████████████████████████████████████████| 389/389 [00:00<00:00, 390kB/s]
Downloading (…)okenizer_config.json: 100%|█████████████████████████████████████████████| 905/905 [00:00<00:00, 890kB/s]
Downloading (…)lve/main/config.json: 100%|████████████████████████████████████████| 4.52k/4.52k [00:00<00:00, 4.59MB/s]
Applying cross attention optimization (Doggettx).
Textual inversion embeddings loaded(0):
Model loaded in 50.5s (load weights from disk: 2.9s, create model: 15.2s, apply weights to model: 25.7s, apply half(): 1.9s, move model to device: 2.5s, load textual inversion embeddings: 2.3s).
Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.
Startup time: 60.4s (import torch: 2.5s, import gradio: 2.6s, import ldm: 1.0s, other imports: 1.5s, load scripts: 0.9s, load SD checkpoint: 51.0s, create ui: 0.4s, gradio launch: 0.4s).
100%|██████████████████████████████████████████████████████████████████████████████████| 20/20 [00:29<00:00,  1.49s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████| 20/20 [00:24<00:00,  1.24s/it]
100%|██████████████████████████████████████████████████████████████████████████████████| 20/20 [00:27<00:00,  1.38s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████| 20/20 [00:26<00:00,  1.35s/it]
100%|██████████████████████████████████████████████████████████████████████████████████| 20/20 [00:22<00:00,  1.14s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████| 20/20 [00:22<00:00,  1.13s/it]
100%|██████████████████████████████████████████████████████████████████████████████████| 20/20 [00:23<00:00,  1.20s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████| 20/20 [00:23<00:00,  1.18s/it]
 15%|████████████▍                                                                      | 3/20 [00:04<00:27,  1.59s/it]
Total progress:  15%|██████████                                                         | 3/20 [00:04<00:24,  1.45s/it]
100%|██████████████████████████████████████████████████████████████████████████████████| 20/20 [00:22<00:00,  1.13s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████| 20/20 [00:22<00:00,  1.12s/it]
100%|██████████████████████████████████████████████████████████████████████████████████| 20/20 [00:45<00:00,  2.28s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████| 20/20 [00:45<00:00,  2.28s/it]
Data shape for DDIM sampling is (2, 4, 64, 64), eta 0.0████████████████████████████████| 20/20 [00:45<00:00,  2.20s/it]
Running DDIM Sampling with 50 timesteps
DDIM Sampler:   8%|█████▌                                                               | 4/50 [00:13<02:39,  3.46s/it]
Total progress:   8%|█████▎                                                             | 4/50 [00:12<02:29,  3.24s/it]
Data shape for DDIM sampling is (1, 4, 64, 64), eta 0.0                                 | 4/50 [00:12<02:05,  2.72s/it]
Running DDIM Sampling with 50 timesteps
DDIM Sampler: 100%|████████████████████████████████████████████████████████████████████| 50/50 [01:08<00:00,  1.36s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████| 50/50 [01:07<00:00,  1.35s/it]
Total progress: 100%|██████████████████████████████████████████████████████████████████| 50/50 [01:07<00:00,  1.35s/it]

除此之外还有一个**No module ‘xformers’. Proceeding without it.**的提示,这里可以无视它,我也找了以下两篇博客用以解决该问题,仅供参考
No module ‘xformers‘. Proceeding without it.
安装Stable Diffusion WebUI 几种报错的解决方法

4、关于Stable Diffusion WebUI汉化

1)安装sd-webui-bilingual-localization插件,在扩展选项卡的“install from”输入URL:https://github.com/journey-ad/sd-webui-bilingual-localization,点击完成安装。

在“installed”看到已经完成安装的sd-webui-bilingual-localization插件,重启UI。

2)下载汉化包

stable-diffusion-webui-localization-zh_CN汉化包

将压缩包解压,找到localizations目录下的zh_CN.json文件,把它放到<stable diffusion根目录>/localizations下。

再次运行如下指令:

python launch.py --disable-safe-unpickle --autolaunch

选中zh_CN

汉化版UI界面

参考博客:
stable diffusion汉化教程
Stable Diffusion AI 绘画工具汉化

更新时间 2023-12-04