前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在m1/m2芯片的mac电脑上运行Stable Diffusion的全步骤

在m1/m2芯片的mac电脑上运行Stable Diffusion的全步骤

作者头像
大帅老猿
发布2022-11-11 16:40:21
3.1K0
发布2022-11-11 16:40:21
举报
文章被收录于专栏:大帅老猿

最近开源社区里最火的项目肯定有Stable Diffussion一个席位。作为一个AI通过文本描述绘画的开源项目,其优秀的生成结果和极快的生成速度也让它在AI绘画项目中一夜封神。

不过遗憾的是,这类AI项目要在苹果公司的自研芯片m1/m2上运行有点麻烦。虽然技术社区有一些文档和教程,但要真正跟着教程把项目所需的环境部署好,还是要爬很多坑的。我把整个爬坑的过程记录下来,希望能对所有使用m1/m2芯片mac的朋友有所帮助。大家都应该体验下这个如此有趣的项目。

注意:学会科学上网是必须的,不然各种依赖的安装速度能慢哭你。

微信的规则进行了调整

希望大家阅读时点点 “ 在看

觉得不错也请点个 “ 分享

这样大帅的文章才能继续出现在你的订阅列表里

1. Python环境

我们需要Python3.10来运行Stable Diffusion。如何确定你电脑中Python的版本呢?

打开终端输入:

代码语言:javascript
复制
python3 -V
#output Python 3.10.6

如果输出的Python版本是3.10及以上的话,就可以继续下一步了。如果还不是的话,就需要我们更新Python

我们可以通过安装Homebrew来安装或更新Python。

代码语言:javascript
复制
brew update
brew install python

在进行下面的步骤前,请务必确保你电脑中Python版本大于3.10。

2. 克隆仓库以及安装依赖

首先我们需要克隆Stable Diffusion这个项目仓库

代码语言:javascript
复制
git clone -b apple-silicon-mps-support https://github.com/bfirsh/stable-diffusion.git
cd stable-diffusion
mkdir -p models/ldm/stable-diffusion-v1/

然后创建并激活python的虚拟环境

代码语言:javascript
复制
python3 -m pip install virtualenv
python3 -m virtualenv venv

source venv/bin/activate

注意以下这行代码

代码语言:javascript
复制
source venv/bin/activate

每次我们打开终端想要运行Stable Diffusion的时候,都需要先使用它!

接下来,安装此项目所需的依赖

代码语言:javascript
复制
pip install -r requirements.txt

在安装过程中如果看到一些错误类似Failed building wheel for onnx ,我们可以通过brew安装这些系统包来解决

代码语言:javascript
复制
brew install Cmake protobuf rust

3. 下载权重包

前往 Hugging Face repository ,点击Access repository按钮。(可能需要先注册一个账号)

然后在该页面上下载sd-v1-4.ckpt (约4 GB大小),将其重命名为model.ckpt并保存至上面创建的目录models/ldm/stable-diffusion-v1中。

*请确保以下路径的有效性models/ldm/stable-diffusion-v1/model.ckpt

4. 快乐的玩耍

如果以上步骤都没有报错(这不太可能!),那么我们就可以在终端中输入以下内容让AI为我们绘画了

代码语言:javascript
复制
python scripts/txt2img.py \
  --prompt "a monkey riding a horse in outer space, like a knight" \
  --n_samples 1 --n_iter 1 --plms

其中prompt参数最为关键,AI会通过这个参数中的文字描述来进行绘画,你怎么描述,它就怎么画!

比如:

代码语言:javascript
复制
python scripts/txt2img.py \
  --prompt "Asia girl, glossy eyes, face, long hair, fantasy, elegant, highly detailed, digital painting, artstation, concept art, smooth, illustration, renaissance, flowy, melting, round moons, rich clouds, very detailed, volumetric light, mist, fine art, textured oil over canvas, epic fantasy art, very colorful, ornate intricate scales, fractal gems, 8 k, hyper realistic, high contrast"  \
  --plms --ddim_steps 100 --H 512 --W 512 --seed 8

我的m1 max 64G内存需要花20秒左右,在运行结束之后,就可以去outputs/txt2img-samples/查看AI绘制的画作了。

下面所有的图都是AI通过这个描述画出来的,我觉得这个技术可以称之为“脑补技术”。

我遇到的坑,以及解决办法

  1. pip install -r requirements.txt这一步里,安装依赖taming-transformers时始终报错。

解决办法:不管它,直接开始运行。

  1. 手动安装所有遗漏的依赖 由于pip install -r requirements.txt时报错了,所以有一些依赖没有安装上。在运行的时候会这些缺失的依赖会一一提示
代码语言:javascript
复制
ModuleNotFoundError: No module named 'PIL'
代码语言:javascript
复制
每当遇到这类问题的时候,我们都手动安装一下依赖包
代码语言:javascript
复制
pip install pillow
代码语言:javascript
复制
需要注意的是,提示缺失的依赖包名称和安装时的名称并不完全一致,比如`PIL`实际的包名称叫`pillow`,`imwatermark`叫做`invisible-watermark`等。
  1. NotImplementedError: The operator 'aten::index.Tensor' is not current implemented for the MPS device 遇到这个问题时,我们需要设置一个环境变量PYTORCH_ENABLE_MPS_FALLBACK,值为1
代码语言:javascript
复制
export PYTORCH_ENABLE_MPS_FALLBACK=1

附:全部可配参数,大家玩得开心:P

代码语言:javascript
复制
usage: txt2img.py [-h] [--prompt [PROMPT]] [--outdir [OUTDIR]] [--skip_grid] [--skip_save] [--ddim_steps DDIM_STEPS] [--plms] [--laion400m] [--fixed_code] [--ddim_eta DDIM_ETA]
                  [--n_iter N_ITER] [--H H] [--W W] [--C C] [--f F] [--n_samples N_SAMPLES] [--n_rows N_ROWS] [--scale SCALE] [--from-file FROM_FILE] [--config CONFIG] [--ckpt CKPT]
                  [--seed SEED] [--precision {full,autocast}]

optional arguments:
  -h, --help            show this help message and exit
  --prompt [PROMPT]     the prompt to render
  --outdir [OUTDIR]     dir to write results to
  --skip_grid           do not save a grid, only individual samples. Helpful when evaluating lots of samples
  --skip_save           do not save individual samples. For speed measurements.
  --ddim_steps DDIM_STEPS
                        number of ddim sampling steps
  --plms                use plms sampling
  --laion400m           uses the LAION400M model
  --fixed_code          if enabled, uses the same starting code across samples
  --ddim_eta DDIM_ETA   ddim eta (eta=0.0 corresponds to deterministic sampling
  --n_iter N_ITER       sample this often
  --H H                 image height, in pixel space
  --W W                 image width, in pixel space
  --C C                 latent channels
  --f F                 downsampling factor
  --n_samples N_SAMPLES
                        how many samples to produce for each given prompt. A.k.a. batch size
  --n_rows N_ROWS       rows in the grid (default: n_samples)
  --scale SCALE         unconditional guidance scale: eps = eps(x, empty) + scale * (eps(x, cond) - eps(x, empty))
  --from-file FROM_FILE
                        if specified, load prompts from this file
  --config CONFIG       path to config which constructs model
  --ckpt CKPT           path to checkpoint of model
  --seed SEED           the seed (for reproducible sampling)
  --precision {full,autocast}
                        evaluate at this precision
代码语言:javascript
复制
代码语言:javascript
复制

代码语言:javascript
复制

微信的规则进行了调整

希望大家阅读时点点 “ 在看

觉得不错也请点个 “ 分享

这样大帅的文章才能继续出现在你的订阅列表里

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-09-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 大帅老猿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. Python环境
  • 2. 克隆仓库以及安装依赖
  • 3. 下载权重包
  • 4. 快乐的玩耍
  • 我遇到的坑,以及解决办法
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档