以前偶尔会使用Midjourney生成一些图片,现在使用的头像就是当时花钱在Midjourney上生成的。前段时间从某鱼上拍了一台性价比还不错的macbook,想着不如自己部署Stable Diffusion(以下简称SD)尝试一下。
网上有很多教程,但是SD持续在更新,仍然碰到了很多新的问题,花了大半天才搞定。最后更新时间:2024年3月9日。
1. 设备
型号:macbook pro 14寸
CPU:M2 MAX (12+38)
内存:96G
硬盘:8T
操作系统:maxOS 14.3.1
2. 安装过程
2.1. 参考官方教程
2.2. 安装brew
使用国内的安装源,我使用的是中科大,速度还不错。根据提示一路下去就行。
bash
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

配置环境变量
bash
echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
2.3. 安装GIT等依赖
比官方教程少了git,因为我的git使用anaconda来安装和隔离。
brew install cmake protobuf rust git wget
2.4. 安装anaconda
因为python版本有多种,一些软件又依赖特定的版本,所以使用anaconda进行隔离。
bash
brew install --cask anaconda
echo 'export PATH="/opt/homebrew/anaconda3/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
conda init zsh
conda create --name sdwebui python=3.10.6
conda activate sdwebui
2.5. 下载stable-diffusion-webui
bash
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui
2.6. 下载模型
训练或微调模型对我来说过于复杂,所以我直接下载了几个模型试用。
国外的站点:civitai.com/
国内的站点:www.liblib.ai/
试用的话,两个网站下载都比较快,根据自己的喜好就行。其中liblib当前支持不需要登录就能下载,更方便一些。
下载完成后,放到:"./stable-diffusion-webui/models/Stable-diffusion/"目录下。我下了好几个模型做测试用。

2.7. 启动及排错
进入目录"./stable-diffusion-webui/",启动"./webui.sh"。
bash
./webui.sh
2.7.1. 下载报错
启动后,会自行安装很多依赖包,但网络不好时,容易出现下载失败,需要多次重试。
经实践发现,建议早上安装。晚上网络特别差,经常下载到一半就被断掉,尝试了十几次都失败。最后在早上只重试了两次,就成功。

2.7.2. 缺少openai/clip-vit-large-patch14类库
各依赖下载完成后,在启动时报"OSError: Can't load tokenizer for 'openai/clip-vit-large-patch14'."。

解决方案:手动下载huggingface.co/openai/clip... "目录下,如果目录不存在就创建。


需要先安装git-lft,否则会报:"git-lfs filter-process: git-lfs: command not found"。

安装脚本:
brew install git-lfs
git lfs install
重新下载,报没有权限,需要生成ssh公私钥,再上传到站点。

先在本地生成ssh 公私钥,使用命令行窗口执行:
javascript
git config --global user.name "yinmo"
git config --global user.email "yinmo_sc@foxmail.com"
ssh-keygen -t rsa -C "yinmo_sc@foxmail.com"
ssh-add ~/.ssh/id_rsa
通过cat ~/.ssh/id_rsa.pub展示公钥内容,复制放到huggingface.co中的SSH and GPG Keys中配置。

重新下载成功,并复制到"./stable-diffusion-webui/openai/"目录下。

重新运行"./webui.sh",成功的话,会自动打开游览器:http://127.0.0.1:7860/。

2.8. 安装中文包
默认是英文界面,网上有些简洁的办法安装中文界面,我安装失败,所以手动安装。
打开"Extensions->Available",找到:raw.githubusercontent.com/AUTOMATIC11...,使用浏览器打开。

搜索"chinese",找到"github.com/dtlnor/stab...


下载后解压放到目录"./stable-diffusion-webui/extensions-builtin/"下面。

进入"Setting -> User Interface -> User Interface -> Localization",选择"zh_CN",保存配置,重新加载UI。

出现中文界面。

3. 测试数据
试用模型"epicrealismXL_v4Photoreal",生成样例如下:



试用了"LEOSAM HelloWorld SDXL真实感大模型_v5.0.safetensors",生成样例如下:




4. 整体感受
如果生成的图片比较小,一次只生成一张图,基本上是秒出,且内存消耗在20G左右。如果图片比较大,且一次生成多张,就会比较耗时和耗内存。
现在网上已经有很多公开的模型,根据自己的需求采用不同的模型,比如有真实感的,有二次元的,有建筑的,有电影风格的等等。
如果想生成精美的图,往往需要多个模型配合,比如上面的熊猫,还需要其它一些模型修饰。