首页 » 鲜货 » 正文

Stable Diffusion的安装和问题解决

小时候比较喜欢画画,如今AI盛行,各种绘画工具层出不穷,网上也看了很多,stable diffusion(sd)和midjunery(mj)算是主流,国内的文心、天工、甚至抖音什么的,我也尝试过,但是总的来说,跟sd和mj相比真的不好用。后来我找了网上一些运营号问,大多采用的是mj,不过我个人还是想试试,亲自对比一下,鉴于mj的付费,我先试了sd,下面是我安装使用过程中的一些经历。

我的是Mac,所以就按照这个系统来了

一、安装homebrew

$ /bin/bash -c "$(curl -fsSL https://gitee.com/ineo6/homebrew-install/raw/master/install.sh)"
// 等待即可,检查是否安装成功
brew -V
// 打印出 homebrew 4.0.11 表示安装成功

二、安装python

brew install cmake protobuf rust python@3.10 git wget

也可以去官网下载安装包3.10.9 或者3.10.10,手动安装

三、下载SD包

1、下载资料包,解压安装。链接: https://pan.baidu.com/s/1_a2dnt8ABG1txxmUSG9QOg?pwd=v1yz 提取码: v1yz

2、从git仓库直接下载

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

四、下载模型

可以使用上面资料包里的模型,也可以自己从官网下载https://huggingface.co/runwayml/stable-diffusion-v1-5/blob/main/v1-5-pruned-emaonly.ckpt
等模型。

这里需要注意的是,下载后放置目录stable-diffusion-webui/models/Stable-diffusion/下。模型有ckpt文件,也有safetensors文件,统统放进去就行。

五、运行

在终端中进入你安装的“stable-diffusion-webui”目录,运行 ./webui.sh

第一次运行会安装一些依赖,这个过程会遇到诸多问题,下面我列举几个。

六、运行问题解决

1、python版本问题

sd运行python是3,但是默认是2,一般来说是不进行覆盖的,覆盖它可能会导致系统脚本和其他依赖默认 Python 的软件出现问题。一般我们采用软连的方式,如:sudo ln -s /usr/local/bin/python3 /usr/bin/python,但是你会发现报错了ln: /usr/bin/python: Operation not permitted,也查过其他各种删除方式都不得行。

首先安装高版本的python后,按照下面的步骤来:

(1、查看版本

which python 
// /usr/bin/python
which python3
// /usr/local/bin/python3

(2、编辑profile文件

vi ~/.bash_profile
// 末尾追加内容
alias python="/usr/local/bin/python3" 
alias python2="/usr/bin/python"

(3、生效启用

source ~/.bash_profile

这里还有个问题,如果新开一个终端界面,需要重启这个命令才能生效。

2、No SDP backend available

Launching Web UI with arguments: --skip-torch-cuda-test --upcast-sampling --no-half-vae --use-cpu interrogate
no module 'xformers'. Processing without...
No SDP backend available, likely because you are running in pytorch versions < 2.0. In fact, you are using PyTorch 1.12.1. You might want to consider upgrading.
no module 'xformers'. Processing without...
No module 'xformers'. Proceeding without it.
Style database not found: /Users/villen/stable-diffusion-webui/styles.csv
Warning: caught exception 'Torch not compiled with CUDA enabled', memory monitor disabled
==============================================================================
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.
==============================================================================
Downloading: "https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors" to /Users/villen/stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned-emaonly.safetensors

WARNING:modules.mac_specific:MPS garbage collection failed
Traceback (most recent call last):
  File "/Users/villen/stable-diffusion-webui/modules/mac_specific.py", line 38, in torch_mps_gc
    from torch.mps import empty_cache
ModuleNotFoundError: No module named 'torch.mps'                

原因:torch 的版本不对
解决方案:在安装目录stable-diffusion-webui 下,编辑webui-macos-env.sh文件
我的原来的配置文件如下;

export install_dir="$HOME"
export COMMANDLINE_ARGS="--skip-torch-cuda-test --upcast-sampling --no-half-vae --use-cpu interrogate"
export TORCH_COMMAND="pip install torch==1.12.1 torchvision==0.13.1"
export K_DIFFUSION_REPO="https://github.com/brkirch/k-diffusion.git"
export K_DIFFUSION_COMMIT_HASH="51c9778f269cedb55a4d88c79c0246d35bdadb71"
export PYTORCH_ENABLE_MPS_FALLBACK=1

修改好的配置文件如下:

(1、在COMMANDLINE_ARGS 参数的后面添加了–reinstall-torch
(2、export TORCH_COMMAND=“pip install torch2.0.1 torchvision0.15.2”,将torch的版本修改成2.0的

export install_dir="$HOME"
export COMMANDLINE_ARGS="--skip-torch-cuda-test --upcast-sampling --no-half-vae --use-cpu interrogate --reinstall-torch"
export TORCH_COMMAND="pip install torch==2.0.1 torchvision==0.15.2"
#export TORCH_COMMAND="pip install torch==1.12.1 torchvision==0.13.1"
export K_DIFFUSION_REPO="https://github.com/brkirch/k-diffusion.git"
export K_DIFFUSION_COMMIT_HASH="51c9778f269cedb55a4d88c79c0246d35bdadb71"
export PYTORCH_ENABLE_MPS_FALLBACK=1

3、Symbol not found: _VTRegisterSupplementalVideoDecoderIfAvailable

这个提示其实还是跟依赖版本有关,opencv版本太老,可以这样手工更新一下:

cd venv/bin 
source activate 
./python3.10 -m pip install --upgrade pip 
pip3 install opencv-python==4.6.0.66 deactivate

4、”LayerNormKernelImpl” not implemented for ‘Half’

或者也会有这样的提示Failed to create model quickly; will retry using slow method.其实都是显卡的问题,一般我们工作自用的电脑的显存都不太能够的,所以这里需要修改一下运行方式:

编辑webui-user.sh,添加:

export COMMANDLINE_ARGS="--precision full --no-half --skip-torch-cuda-test

或者我个人采用以下运行方式:

./webui.sh --precision full --no-half

大家注意这里的half,如果你有足够的计算资源和内存,最简单的解决方案是将数据类型更改为全精度(float32),我们这里强调一半,其实就是半精度替代实现方案。有兴趣可以去了解一下精度问题。

七、总结

本地跑起来了,确实比较慢,风扇都要转费了。SD跑图除了需要通常所说的描述要到位,还要选合适的模型或者插件,才能做出理想的图片。

以上是本地SD安装过程中遇到的问题,毕竟免费,随后会跟大家聊一下它和midjunery的区别以及我个人的取舍。