基于 https://github.com/AUTOMATIC1111/stable-diffusion-webui
本文记录于 2023-08-20,以下方法可能失效,请审慎参考
注意升级 git 到2.x版本 git --version
yum install zlib-devel + https://juejin.cn/post/7071910670056292389
https://zhuanlan.zhihu.com/p/639164507
https://huggingface.co/docs/huggingface_hub/main/en/guides/download
yum install git-lfs
git lfs install
python代码下载模型
https://huggingface.co/stabilityai/stable-diffusion-2-1/tree/main
# pip install huggingface_hub
from huggingface_hub import snapshot_download, hf_hub_download
snapshot_download(repo_id="stabilityai/stable-diffusion-2-1", cache_dir='/your_path/')
hf_hub_download(repo_id="stabilityai/stable-diffusion-2-1", filename="v2-1_768-ema-pruned.safetensors", cache_dir='/your_path/')
hf_hub_download(repo_id="runwayml/stable-diffusion-v1-5", filename="v1-5-pruned-emaonly.safetensors", cache_dir='/your_path/')
ImportError: cannot import name 'rank_zero_only' from 'pytorch_lightning.utilities.distributed'
https://github.com/AUTOMATIC1111/stable-diffusion-webui/discussions/4111
from pytorch_lightning.utilities.rank_zero import rank_zero_only
ImportError: cannot import name 'get_device' from 'basicsr.utils.misc'
https://github.com/sczhou/CodeFormer/issues/193
# basicsr\utils\misc.py
import torch
def get_device():
if torch.cuda.is_available():
return torch.device("cuda")
else:
return torch.device("cpu")
def gpu_is_available():
return torch.cuda.is_available()
在Centos服务器部署的 SD
现在在本地windows使用cmd命令,进行端口转发
ssh-keygen -R ip
ssh -L 7860:localhost:7860 user@ip
或者 使用 mobaxterm 启动隧道 tunneling
python launch.py
$ python launch.py
Python 3.10.12 (main, Jul 5 2023, 18:54:27) [GCC 11.2.0]
Version: v1.5.1
Commit hash: 68f336bd994bed5442ad95bad6b6ad5564a5409a
Launching Web UI with arguments:
No module 'xformers'. Proceeding without it.
Loading weights [dcd690123c] from /opt/bdp/stable-diffusion-webui/models/Stable-diffusion/models--stabilityai--stable-diffusion-2-1/snapshots/5cae40e6a2745ae2b01ad92ae5043f95f23644d6/v2-1_768-ema-pruned.safetensors
Running on local URL: http://127.0.0.1:7860
To create a public link, set `share=True` in `launch()`.
Startup time: 10.9s (launcher: 2.2s, import torch: 2.7s, import gradio: 0.9s, setup paths: 1.2s, other imports: 2.3s, load scripts: 0.7s, create ui: 0.6s, gradio launch: 0.2s).
然后在本地浏览器打开 http://localhost:7860/
,即可使用远程的SD
https://zhuanlan.zhihu.com/p/629845245
sh webui.sh --nowebui --xformers
nohup python launch.py --nowebui --xformers > nohup.out 2>&1 &
(sd) [web@bdp-measure3 stable-diffusion-webui]$ sh webui.sh --nowebui --xformers
################################################################
Install script for stable-diffusion + Web UI
Tested on Debian 11 (Bullseye)
################################################################
################################################################
Running on web user
################################################################
################################################################
Repo already cloned, using it as install directory
################################################################
################################################################
Create and activate python venv
################################################################
################################################################
Launching launch.py...
################################################################
Cannot locate TCMalloc (improves CPU memory usage)
Python 3.8.16 (default, Mar 2 2023, 03:21:46)
[GCC 11.2.0]
Version: v1.5.1
Commit hash: 68f336bd994bed5442ad95bad6b6ad5564a5409a
Installing xformers
Launching API server with arguments: --nowebui --xformers
Loading weights [6ce0161689] from /opt/bdp/stable-diffusion-webui/models/Stable-diffusion/models--runwayml--stable-diffusion-v1-5/snapshots/c9ab35ff5f2c362e9e22fbafe278077e196057f0/v1-5-pruned-emaonly.safetensors
Startup time: 176.5s (launcher: 151.6s, import torch: 9.7s, import gradio: 2.4s, setup paths: 5.3s, other imports: 3.1s, setup codeformer: 0.2s, load scripts: 2.5s, reload hypernetworks: 0.1s, lora_script.py: 1.7s).
INFO: Started server process [26450]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:7861 (Press CTRL+C to quit)
Creating model from config: /opt/bdp/stable-diffusion-webui/configs/v1-inference.yaml
LatentDiffusion: Running in eps-prediction mode
DiffusionWrapper has 859.52 M params.
Applying attention optimization: xformers... done.
Model loaded in 158.2s (load weights from disk: 1.9s, create model: 19.9s, apply weights to model: 119.6s, apply half(): 15.2s, move model to device: 1.3s, calculate empty prompt: 0.3s).
INFO: 127.0.0.1:64184 - "GET /docs HTTP/1.1" 200 OK
INFO: 127.0.0.1:64184 - "GET /openapi.json HTTP/1.1" 200 OK
进入 http://localhost:7861/docs
可以查看接口
import json
import base64
import requests
def submit_post(url: str, data: dict):
return requests.post(url, data=json.dumps(data))
def save_encoded_image(b64_image: str, output_path: str):
with open(output_path, 'wb') as image_file:
image_file.write(base64.b64decode(b64_image))
if __name__ == '__main__':
txt2img_url = r'http://127.0.0.1:7861/sdapi/v1/txt2img'
data = {'prompt': 'an old chinese building near by the mountain, and sunset in the sky.',
'negative_prompt': '',
'sampler_index': 'DPM++ SDE',
'seed': 1234,
'steps': 20,
'width': 512,
'height': 512,
'cfg_scale': 8}
response = submit_post(txt2img_url, data)
save_image_path = r'sd-test.png'
save_encoded_image(response.json()['images'][0], save_image_path)
给定提示词:an old chinese building near by the mountain, and sunset in the sky.
生成图片如下:
自定义Stable Diffusion扩展:https://www.jianshu.com/p/82709696f4aa