在 MacBook Pro M1/M2 上安装和运行 Stable Diffusion
April 2nd, 2023

站长最近玩 Midjourney 比较多,一直想体验 SD 来着,就从网上找了不少视频和文字教程,来回折腾了将近2小时,终于安装和运行成功了,这里分享一下完整的实操过程。

提示:电脑默认开了梯子,同时我的 Mac 硬件配置如下。

一、安装 Homebrew

打开 Mac 上的终端,搜索框里输入”终端 或者 terminal”, 就能看到了,调出搜索框快捷键“command+空格”。

在终端内复制执行下面这段代码(官方版):

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

如果网络问题一直没反应或者报错,可以用国内镜像版:

/bin/bash -c "$(curl -fsSL https://gitee.com/ineo6/homebrew-install/raw/master/install.sh)"

检测是否安装成功 homebrew

brew -v

如果会显示 homebrew 的版本信息,则安装成功,比如我的是:Homebrew 4.0.10 版本。

二、安装 Python

在终端窗口复制下面的代码然后按回车运行:

brew install cmake protobuf rust python@3.10 git wget

三、安装 stable-diffusion-webui

3.1 安装 git

终端内复制并运行:

brew install git

3.2 克隆到电脑

终端内复制并运行:

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

如果这一步执行不成功,就替换成下面的代码重新运行:

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

3.3 下载 AI 基础模型

以 stable diffusion 2.0 训练模型为例,打开网页

页面的 “Files and versions” tab 里面找到并下载 768-v-ema.ckpt

点击 download

下载好后,把下载好的 ckpt 文件放在 Mac 文件夹:“你的用户名”> stable-diffusion-webui>Models>Stable-diffusion 文件夹里。

提示,用 command+空格,打开搜索框,输入:stable-diffusion-webui,即可打开文件夹位置,然后再打开 Models>Stable-diffusion 文件夹。

四、更改配置文件

在 stable-diffusion-webui 文件夹中找到 launch.py 文件

用默认的“文本编辑”打开并进行修改,可以使用 command+F 进行搜索,在出现 https://github.com 的地方,在它的前面加上https://github.moeyy.xyz/ (这个地址如果无限也可以找其他的镜像源)即可,修改完成后的结果参考如下:

gfpgan_package = os.environ.get('GFPGAN_PACKAGE', "git+https://github.moeyy.xyz/https://github.com/TencentARC/GFPGAN.git@8d2447a2d918f8eba5a4a01463fd48e45126a379")
clip_package = os.environ.get('CLIP_PACKAGE', "git+https://github.moeyy.xyz/https://github.com/openai/CLIP.git@d50d76daa670286dd6cacf3bcd80b5e4823fc8e1")
openclip_package = os.environ.get('OPENCLIP_PACKAGE', "git+https://github.moeyy.xyz/https://github.com/mlfoundations/open_clip.git@bb6e834e9c70d9c27d0dc3ecedeebeaeb1ffad6b")

xformers_windows_package = os.environ.get('XFORMERS_WINDOWS_PACKAGE', 'https://github.moeyy.xyz/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.moeyy.xyz/https://github.com/Stability-AI/stablediffusion.git")
taming_transformers_repo = os.environ.get('TAMING_TRANSFORMERS_REPO', "https://github.moeyy.xyz/https://github.com/CompVis/taming-transformers.git")
k_diffusion_repo = os.environ.get('K_DIFFUSION_REPO', 'https://github.moeyy.xyz/https://github.com/crowsonkb/k-diffusion.git')
codeformer_repo = os.environ.get('CODEFORMER_REPO', 'https://github.moeyy.xyz/https://github.com/sczhou/CodeFormer.git')
blip_repo = os.environ.get('BLIP_REPO', 'https://github.moeyy.xyz/https://github.com/salesforce/BLIP.git')

修改完后,保存,关闭文件。

五、运行 stable-diffusion-webui

注意:运行下面的代码前,请先将全局梯子关闭,否则后续会出现诸如:

expecting value: line 1 column 1 char 0

这类错误。

在终端复制下面的代码并运行:

cd stable-diffusion-webui

再复制下面代码并运行:

./webui.sh

这一步可能需要花费较长时间,因为会下载几个 SD 必需的应用,比如 gfpgan 等。

这时候等几分钟,直到终端界面上出现:

“Running on local URL: http://127.0.0.1:7860
To create a public link, set `share=True` in `launch()`. ”

则说明安装已经成功。

六、使用 stable-diffusion-webui

在浏览器地址栏输入 http://127.0.0.1:7860

正常就会打开如下界面:

此时默认的模型就是我们刚才下载的 stable diffusion 2.0,随便测试一下,4 张生图大概花了 30 秒,成功!

七、其他

如果想更换更多模型,比如网上很多的动漫、明星真人等,可以到这 2 个网站下载:

自己可以多测试一些 model 感受下。

不得不说,用了 Midjourney 的 Pro 付费版后,再来体验 SD 感觉就很香!

常用的一些资源网站:

17gongai 导航

uisdc 优设

prompt 示例

全文完。

Subscribe to 0xwest
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.
More from 0xwest

Skeleton

Skeleton

Skeleton