在 Apple Silicon Mac 上施咒画妹子

当时 NovelAI 那玩意在国内非常火爆的时候,我看着群友们超大显存的 Windows 电脑,抱着自己的 14 寸 MacBook Pro 瑟瑟发抖。不过,凭借 Apple 自家的 Core ML Stable Diffusion,我也可以在本地施咒画妹子了!

其实 Diffusers 已经推出了官方 Mac 应用,但是这个应用还不能使用 Hugging Face 上其它的模型。因为我们要画妹子,所以现在先凑合一下吧(

环境准备

首先,需要下载并安装这些:

  • Core ML Stable Diffusion 项目本体
  • Anaconda
  • Hugging Face 的命令行工具。使用 Homebrew,可以这样安装:brew install huggingface-cli

然后,去注册一个 Hugging Face 帐号,根据 官方说明 申请及应用 User Access Token。

接下来,就可以根据 Core ML Stable Diffusion 项目的 官方说明,设置运行环境和安装依赖:

# 以后再运行前,只要执行中间两行的命令即可
conda create -n coreml_stable_diffusion python=3.8 -y
conda activate coreml_stable_diffusion
cd /path/to/cloned/ml-stable-diffusion/repository
pip install -e .

寻找模型

(如果你已经找到要用的模型并完成了转换工作,可以跳过这一步)

因为我们要画妹子,所以需要找一个合适的模型转换成 Core ML 格式。我觉得 dreamlike-art/dreamlike-anime-1.0hakurei/waifu-diffusion 都还不错,不过你可以自己在 Hugging Face 上面找带有 stable-diffusion 标签的其它模型来用。

dreamlike-art/dreamlike-anime-1.0 为例,我们把它转换成 Core ML 格式:

python \
    -m python_coreml_stable_diffusion.torch2coreml \
    --convert-unet \
    --convert-text-encoder \
    --convert-vae-decoder \
    --convert-safety-checker \
    -o converted \
    --model-version dreamlike-art/dreamlike-anime-1.0

在这里,-o 指定的是转换后的模型输出路径,--model-version 指定的是模型名称。记下来这两个参数,接下来还会用到。

模型转换通常需要等待数分钟。

施咒

根据 官方说明,我们需要执行以下命令:

python \
    -m python_coreml_stable_diffusion.pipeline \
    --prompt "anime, masterpiece, high quality, 1girl, solo, long hair, looking at viewer, blush, smile, bangs, blue eyes, skirt, medium breasts, iridescent, gradient, colorful, besides a cottage, in the country" \
    --model-version dreamlike-art/dreamlike-anime-1.0 \
    -i converted \
    -o output \
    --compute-unit ALL \
    --seed 20

其中:

  • --prompt:生成图片用的 Prompt
  • --model-version:模型名称
  • -i:转换后的模型存放路径,也就是刚才 -o 指定的
  • -o:输出图片路径
  • --seed:随机数种子

稍等一会儿,就可以在指定的输出图片路径看到图片了!

上述命令输出的图片