按装部署

最近看Stable Diffusion开源了,据说比Disco Diffusion更快,于是从git上拉取了项目尝试本地部署。

这里是官网介绍:https://stability.ai/blog/stable-diffusion-public-release

首先把代码拉下来,

https://github.com/CompVis/stable-diffusion.git

然后下载模型,模型有几种,需要去 https://huggingface.co/CompVis 下载,这里还需要注册账号,先注册下

如果我们用sd-v1-4.ckpt这个模型,就在该网页里点击stable-diffusion-v-1-4-original那一项:

image

跳转过去,找到Download the weights下面的sd-v1-4.ckpt即可点击下载,通常几G,下载时间较长。
其次就是按照readme的文档,link一下模型:

mkdir -p models/ldm/stable-diffusion-v1/
ln -s <path/to/model.ckpt> models/ldm/stable-diffusion-v1/model.ckpt

然后运行测试:

python scripts/txt2img.py --prompt "a photograph of an astronaut riding a horse" --plms

找不到taming 的问题

运行例子过程中就有这个问题:

(stable_diffusion_env) fcbai@fcbai-NUC11PAHi7:~/workspace/github/stable-diffusion$ python scripts/txt2img.py --prompt "a photograph of an astronaut riding a horse" --plms
/home/fcbai/.virtualenvs/stable_diffusion_env/lib/python3.10/site-packages/transformers/models/clip/feature_extraction_clip.py:28: FutureWarning: The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please use CLIPImageProcessor instead.
warnings.warn(
Global seed set to 42
Loading model from models/ldm/stable-diffusion-v1/model.ckpt
Global Step: 470000
Traceback (most recent call last):
File "/home/fcbai/workspace/github/stable-diffusion/scripts/txt2img.py", line 352, in <module>
main()
File "/home/fcbai/workspace/github/stable-diffusion/scripts/txt2img.py", line 246, in main
model = load_model_from_config(config, f"{opt.ckpt}")
File "/home/fcbai/workspace/github/stable-diffusion/scripts/txt2img.py", line 55, in load_model_from_config
model = instantiate_from_config(config.model)
File "/home/fcbai/workspace/github/stable-diffusion/ldm/util.py", line 85, in instantiate_from_config
return get_obj_from_str(config["target"])(**config.get("params", dict()))
File "/home/fcbai/workspace/github/stable-diffusion/ldm/util.py", line 93, in get_obj_from_str
return getattr(importlib.import_module(module, package=None), cls)
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/home/fcbai/workspace/github/stable-diffusion/ldm/models/diffusion/ddpm.py", line 24, in <module>
from ldm.models.autoencoder import VQModelInterface, IdentityFirstStage, AutoencoderKL
File "/home/fcbai/workspace/github/stable-diffusion/ldm/models/autoencoder.py", line 6, in <module>
from taming.modules.vqvae.quantize import VectorQuantizer2 as VectorQuantizer
ModuleNotFoundError: No module named 'taming'

我开始以为是缺失taming package,所以安装了transformers的taming:

pip install taming-transformers

后来发现不对:

Traceback (most recent call last):
File "/home/fcbai/workspace/github/stable-diffusion/scripts/txt2img.py", line 352, in <module>
main()
File "/home/fcbai/workspace/github/stable-diffusion/scripts/txt2img.py", line 246, in main
model = load_model_from_config(config, f"{opt.ckpt}")
File "/home/fcbai/workspace/github/stable-diffusion/scripts/txt2img.py", line 55, in load_model_from_config
model = instantiate_from_config(config.model)
File "/home/fcbai/workspace/github/stable-diffusion/ldm/util.py", line 85, in instantiate_from_config
return get_obj_from_str(config["target"])(**config.get("params", dict()))
File "/home/fcbai/workspace/github/stable-diffusion/ldm/util.py", line 93, in get_obj_from_str
return getattr(importlib.import_module(module, package=None), cls)
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/home/fcbai/workspace/github/stable-diffusion/ldm/models/diffusion/ddpm.py", line 24, in <module>
from ldm.models.autoencoder import VQModelInterface, IdentityFirstStage, AutoencoderKL
File "/home/fcbai/workspace/github/stable-diffusion/ldm/models/autoencoder.py", line 6, in <module>
from taming.modules.vqvae.quantize import VectorQuantizer2 as VectorQuantizer
ImportError: cannot import name 'VectorQuantizer2' from 'taming.modules.vqvae.quantize' (/home/fcbai/.virtualenvs/stable_diffusion_env/lib/python3.10/site-packages/taming/modules/vqvae/quantize.py)

再去看issue,发现这个是个bug,可以看:https://github.com/CompVis/taming-transformers/issues/176
解决方法就是:

git clone https://github.com/CompVis/taming-transformers
cd stable-diffusion
cp -dr ../taming-transformers/taming/ ./taming

VectorQuantizer2是新的测试功能,还没有出现在正式库中,最后我的所有freeze内容是:

appdirs==1.4.4
apsw==3.36.0.post1
apturl==0.5.2
attrs==21.2.0
backcall==0.2.0
bcrypt==3.2.0
beautifulsoup4==4.10.0
beniget==0.4.1
blinker==1.4
Brlapi==0.8.3
Brotli==1.0.9
certifi==2020.6.20
chardet==4.0.0
chrome-gnome-shell==0.0.0
click==8.0.3
colorama==0.4.4
command-not-found==0.3
configobj==5.0.6
cryptography==3.4.8
css-parser==1.0.7
cssselect==1.1.0
cssutils==1.0.2
cupshelpers==1.0
cycler==0.11.0
dbus-python==1.2.18
decorator==4.4.2
defer==1.0.6
distlib==0.3.4
distro==1.7.0
distro-info===1.1build1
dnspython==2.1.0
docker==5.0.3
docker-compose==1.29.2
dockerpty==0.4.1
docopt==0.6.2
duplicity==0.8.21
fasteners==0.14.1
feedparser==6.0.8
filelock==3.7.1
fonttools==4.29.1
fs==2.4.12
future==0.18.2
gast==0.5.2
html2text==2020.1.16
html5-parser==0.4.10
html5lib==1.1
httplib2==0.20.2
idna==3.3
ifaddr==0.1.7
importlib-metadata==4.6.4
iotop==0.6
ipython==7.31.1
jedi==0.18.0
jeepney==0.7.1
jsonschema==3.2.0
keyring==23.5.0
kiwisolver==1.3.2
language-selector==0.1
launchpadlib==1.10.16
lazr.restfulclient==0.14.4
lazr.uri==1.0.6
lockfile==0.12.2
louis==3.20.0
lxml==4.8.0
lz4==3.1.3+dfsg
Mako==1.1.3
Markdown==3.3.6
MarkupSafe==2.0.1
matplotlib==3.5.1
matplotlib-inline==0.1.3
mechanize==0.4.7
mercurial==6.1.1
monotonic==1.6
more-itertools==8.10.0
mpmath==0.0.0
msgpack==1.0.3
netifaces==0.11.0
numpy==1.21.5
oauthlib==3.2.0
olefile==0.46
packaging==21.3
paramiko==2.9.3
parso==0.8.1
pbr==5.9.0
pexpect==4.8.0
pickleshare==0.7.5
Pillow==9.0.1
platformdirs==2.5.2
ply==3.11
prompt-toolkit==3.0.28
psutil==5.9.0
ptyprocess==0.7.0
py7zr==0.11.3+dfsg
pycairo==1.20.1
pychm==0.8.6
pycryptodomex==3.11.0
pycups==2.0.1
Pygments==2.11.2
PyGObject==3.42.1
PyJWT==2.3.0
PyNaCl==1.5.0
PyOpenGL==3.1.5
pyparsing==2.4.7
PyQt5==5.15.6
PyQt5-sip==12.9.1
PyQtWebEngine==5.15.5
pyrsistent==0.18.1
pyserial==3.5
python-apt==2.4.0+ubuntu1
python-dateutil==2.8.1
python-debian===0.1.43ubuntu1
python-dotenv==0.19.2
pythran==0.10.0
pytz==2022.1
pyxattr==0.7.2
pyxdg==0.27
PyYAML==5.4.1
regex==2021.11.10
reportlab==3.6.8
repoze.lru==0.7
requests==2.25.1
requests-toolbelt==0.9.1
Routes==2.5.1
scipy==1.8.0
SecretStorage==3.3.1
sgmllib3k==1.0.0
six==1.16.0
soupsieve==2.3.1
stcgal==1.6
stevedore==3.5.0
sympy==1.9
systemd-python==234
terminator==2.1.1
texttable==1.6.4
tqdm==4.64.1
traitlets==5.1.1
ubuntu-advantage-tools==8001
ubuntu-drivers-common==0.0.0
ufoLib2==0.13.1
ufw==0.36.1
unattended-upgrades==0.1
unicodedata2==14.0.0
urllib3==1.26.5
usb-creator==0.3.7
vboxapi==1.0
virtualenv==20.14.1
virtualenv-clone==0.5.7
virtualenvwrapper==4.8.4
wadllib==1.3.6
wcwidth==0.2.5
webencodings==0.5.1
WebOb==1.8.6
websocket-client==1.2.3
xdg==5
xkit==0.0.0
youtube-dl==2021.12.17
zeroconf==0.38.3
zipp==1.0.0

其中关于torch需要这样安装:

pip3 install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu117

考虑到测试的完整性,我使用了nightly版本。


扫码手机观看或分享: