Please enable Javascript to view the contents

如何在无 GPU 的 macOS 上运行 Stable Diffusion

 ·  ☕ 1 分钟

1. 运行 Stable Diffusion 推荐配置

  • 内存: 不低于 16 GB DDR4 或 DDR5
  • 存储: 不低于 10 GB 可用空间
  • GPU: 不低于 6 GB 显存 N 卡

如果硬件达不到要求,也可以使用各种优化 fork 兼容更低配置的硬件,但生成时间会增长。

当前的开发主机配置为:

  • 2.9 GHz 8-Core Intel Core i7
  • 16 GB 2666 MHz DDR4
  • 250 GB SSD

由于没有 GPU,生成图片时,需要多等待一会儿。

2. macOS 上运行 Stable Diffusion

  • 安装 anaconda
1
brew install --cask anaconda
  • 配置 PATH
1
echo 'export PATH=/usr/local/anaconda3/bin:$PATH' >> ~/.zshrc
  • conda 初始化 Shell
1
conda init zsh

这里需要关闭窗口,重新登录 Terminal。

  • 下载并进入项目目录
1
2
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui
cd stable-diffusion-webui
  • 创建 Python 环境
1
conda create -n stabel python=3.10.6
  • 激活 Python 环境,并安装依赖
1
2
conda activate stabel 
pip3 install -r requirements_versions.txt
  • 下载模型

前往 https://huggingface.co/CompVis/stable-diffusion-v-1-4-original 下载 sd-v1-4.ckpt 或者 sd-v1-4-full-ema.ckpt 文件,放置到 models/Stable-diffusion ⽬录下。huggingface 上也有很多其他模型可以下载使用,也能在线体验。比如 https://huggingface.co/spaces/IDEA-CCNL/Taiyi-Stable-Diffusion-Chinese

  • 修改运行参数,跳过 GPU 检测,参考[1]
1
export COMMANDLINE_ARGS="--lowvram --precision full --no-half --skip-torch-cuda-test --listen --port 7860"
  • 运行项目
1
python launch.py

在本地访问 http://127.0.0.1:7860 即可打开 UI。

如果启动时,出现类似 stderr: Running command git clone --filter=blob:none --quiet https://github.com/mlfoundations/open_clip.git 报错,可以编辑 launch.py 找到 def prepare_environment() 函数,修改 git+https://https://github.com/git+https://mirror.ghproxy.com/https://github.com/ 即可。

3. Text-to-Image 测试

Prompt 有很多的撰写技巧、句式、修饰词;Stable Diffusion 也有很多参数可以调整。但本篇主要描述的是在无 GPU 情况下,在 macOS 下运行 Stable Diffusion,因此在此仅输入 bird ,进行测试。生成的图片如下图:

4. 参考

  1. https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/1742

微信公众号
作者
微信公众号