引言

https://github.com/CompVis/stable-diffusion

Stable Diffusion is a latent text-to-image diffusion model.

Stable Diffusion是近年来流行的AI生成图片工具,这里尝试在Mac电脑(非M1)上使用CPU进行图片生成。

整个安装过程异常崎岖,参考了很多博客文章但大部分都进行不下去,要么是系统本身缺某个软件包或版本不对,要么是Python相关的软件包安装不了。

主要参考了这篇文章:

准备工作

  • 1)、将Mac系统升级到13.2.1以上版本,并升级Homebrew相关软件包,主要问题是github访问速度太慢导致失败
$ brew upgrade
  • 2)、设置pypi使用豆瓣镜像源
$ cat ~/.pip/pip.conf

[global]
index-url = https://pypi.douban.com/simple

[install]
use-mirrors =true
mirrors =https://pypi.douban.com/simple/
trusted-host =pypi.douban.com
$ cat ~/.condarc

channels:
  - https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
  - https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - defaults
show_channel_urls: true

$ conda create -n sd python=3.10.6

安装stable-diffusion-webui及其依赖包

  • 1)、下载stable-diffusion-webui:
$ git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
$ cd stable-diffusion-webui/
  • 2)、在对应Python虚拟环境中安装软件包
$ conda activate sd
$ pip3 install -r requirements_versions.txt

......
Installing collected packages: 
gitdb, future, fsspec, frozenlist, fonttools, filelock, entrypoints, 
deprecation, cycler, contourpy, click, charset-normalizer, chardet, 
cachetools, attrs, async-timeout, anyio, aiofiles, absl-py, uvicorn, 
torchsde, torchmetrics, torchdiffeq, starlette, scikit-image, requests, 
pandas, mdit-py-plugins, matplotlib, kornia, jsonschema, httpcore, 
google-auth, GitPython, blendmodes, aiosignal, accelerate, torchvision, 
requests-oauthlib, jsonmerge, huggingface-hub, httpx, filterpy, fastapi, 
altair, aiohttp, transformers, timm, gradio, google-auth-oauthlib, facexlib, 
clean-fid, tensorboard, tb-nightly, pytorch_lightning, basicsr, gfpgan, realesrgan

Successfully installed GitPython-3.1.27 absl-py-1.4.0 accelerate-0.12.0 
aiofiles-23.1.0 aiohttp-3.8.4 aiosignal-1.3.1 altair-4.2.2 anyio-3.6.2 
async-timeout-4.0.2 attrs-22.2.0 basicsr-1.4.2 blendmodes-2022 cachetools-5.3.0 
chardet-4.0.0 charset-normalizer-3.1.0 clean-fid-0.1.29 click-8.1.3 contourpy-1.0.7 
cycler-0.11.0 deprecation-2.1.0 entrypoints-0.4 facexlib-0.2.5 fastapi-0.90.1 
filelock-3.9.0 filterpy-1.4.5 fonttools-4.39.0 frozenlist-1.3.3 fsspec-2023.3.0 
future-0.18.3 gfpgan-1.3.8 gitdb-4.0.10 google-auth-2.16.2 google-auth-oauthlib-0.4.6 
gradio-3.16.2 httpcore-0.15.0 httpx-0.23.3 huggingface-hub-0.13.1 jsonmerge-1.8.0 
jsonschema-4.17.3 kornia-0.6.7 matplotlib-3.7.1 mdit-py-plugins-0.3.5 pandas-1.5.3 
pytorch_lightning-1.7.6 realesrgan-0.3.0 requests-2.25.1 requests-oauthlib-1.3.1 
scikit-image-0.19.2 starlette-0.23.1 tb-nightly-2.13.0a20230311 tensorboard-2.12.0 
timm-0.6.7 torchdiffeq-0.2.3 torchmetrics-0.11.3 torchsde-0.2.5 torchvision-0.14.1 
transformers-4.25.1 uvicorn-0.21.0

这个过程要多执行几次,因为有的时候仅仅下载速度太慢了就导致失败,反复执行直到成功结束;一旦软件包安装成功,基本就可以顺利完成安装了。

  • 3)、疑难杂症:安装软件包grpcio报错
$ brew install openssl

Warning: openssl@3 3.0.8 is already installed and up-to-date.
To reinstall 3.0.8, run:
  brew reinstall openssl@3

$ CFLAGS="-I/opt/homebrew/opt/openssl/include" LDFLAGS="-L/opt/homebrew/opt/openssl/lib" GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=1 GRPC_PYTHON_BUILD_SYSTEM_ZLIB=1 pip install grpcio

......

Successfully built grpcio
Installing collected packages: grpcio
Successfully installed grpcio-1.51.3

这个grpcio软件包反复执行好几次都无法成功,最后查看到这个解决方法得以顺利安装, 参看:

下载模型

文件比较大,下载好放到对应位置即可;网站上各种模型都有,都是下载到对应目录即可。

只用CPU

$ cat ~/.bashrc

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

启动程序的时候要设置两个环境变量,这样就只使用CPU,不使用CUDA相关的GPU功能。

修改启动文件(lanuch.py)

  • 修改这个函数下的部分代码:
def prepare_environment():
    ...
  • 修改内容:
222c222
<     torch_command = os.environ.get('TORCH_COMMAND', "pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 --extra-index-url https://download.pytorch.org/whl/cu117")
---
>     torch_command = os.environ.get('TORCH_COMMAND', "pip install torch==1.13.1 torchvision==0.14.1 --extra-index-url https://download.pytorch.org/whl/cu117")
227,229c227,229
<     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")
<     openclip_package = os.environ.get('OPENCLIP_PACKAGE', "git+https://github.com/mlfoundations/open_clip.git@bb6e834e9c70d9c27d0dc3ecedeebeaeb1ffad6b")
---
>     gfpgan_package = os.environ.get('GFPGAN_PACKAGE', "git+https://ghproxy.com/https://github.com/TencentARC/GFPGAN.git@8d2447a2d918f8eba5a4a01463fd48e45126a379")
>     clip_package = os.environ.get('CLIP_PACKAGE', "git+https://ghproxy.com/https://github.com/openai/CLIP.git@d50d76daa670286dd6cacf3bcd80b5e4823fc8e1")
>     openclip_package = os.environ.get('OPENCLIP_PACKAGE', "git+https://ghproxy.com/https://github.com/mlfoundations/open_clip.git@bb6e834e9c70d9c27d0dc3ecedeebeaeb1ffad6b")
231,235c231,235
<     stable_diffusion_repo = os.environ.get('STABLE_DIFFUSION_REPO', "https://github.com/Stability-AI/stablediffusion.git")
<     taming_transformers_repo = os.environ.get('TAMING_TRANSFORMERS_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('CODEFORMER_REPO', 'https://github.com/sczhou/CodeFormer.git')
<     blip_repo = os.environ.get('BLIP_REPO', 'https://github.com/salesforce/BLIP.git')
---
>     stable_diffusion_repo = os.environ.get('STABLE_DIFFUSION_REPO', "https://ghproxy.com/https://github.com/Stability-AI/stablediffusion.git")
>     taming_transformers_repo = os.environ.get('TAMING_TRANSFORMERS_REPO', "https://ghproxy.com/https://github.com/CompVis/taming-transformers.git")
>     k_diffusion_repo = os.environ.get('K_DIFFUSION_REPO', 'https://ghproxy.com/https://github.com/crowsonkb/k-diffusion.git')
>     codeformer_repo = os.environ.get('CODEFORMER_REPO', 'https://ghproxy.com/https://github.com/sczhou/CodeFormer.git')
>     blip_repo = os.environ.get('BLIP_REPO', 'https://ghproxy.com/https://github.com/salesforce/BLIP.git')

基本就是torch_command中去掉cuda相关内容,另一个是为github网址加上代理前缀https://ghproxy.com/以加速下载。

启动程序

$ conda activate sd 

$ cd stable-diffusion-webui

$ python launch.py

Python 3.10.6 (main, Oct 24 2022, 11:04:34) [Clang 12.0.0 ]
Commit hash: 0cc0ee1bcb4c24a8c9715f66cede06601bfc00c8

Installing requirements for Web UI
Launching Web UI with arguments: --lowvram --precision full --no-half

Warning: caught exception 'Torch not compiled with CUDA enabled', memory monitor disabled

No module 'xformers'. Proceeding without it.

Loading weights [14749efc0a] from /AI/stable-diffusion-webui/models/Stable-diffusion/sd-v1-4-full-ema.ckpt
Creating model from config: /AI/stable-diffusion-webui/configs/v1-inference.yaml
LatentDiffusion: Running in eps-prediction mode
DiffusionWrapper has 859.52 M params.

Applying cross attention optimization (InvokeAI).
Textual inversion embeddings loaded(0):
Model loaded in 177.6s (load weights from disk: 110.9s, find config: 0.2s, load config: 0.1s, 
create model: 2.0s, apply weights to model: 63.3s, apply dtype to VAE: 0.8s).

Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.

顺利启动,访问:http://127.0.0.1:7860/ 即可,但电脑会变得很卡;稍微做点复杂的事情就直接卡死,基本干不了啥,还是得有带GPU的高性能电脑才行。