前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >stable-diffusion-webui [Centos]安装记录

stable-diffusion-webui [Centos]安装记录

作者头像
Michael阿明
发布2023-08-21 08:18:13
1K0
发布2023-08-21 08:18:13
举报

基于 https://github.com/AUTOMATIC1111/stable-diffusion-webui

本文记录于 2023-08-20,以下方法可能失效,请审慎参考

1. 安装

注意升级 git 到2.x版本 git --version yum install zlib-devel + https://juejin.cn/post/7071910670056292389

https://zhuanlan.zhihu.com/p/639164507

2. 下载模型

https://huggingface.co/docs/huggingface_hub/main/en/guides/download

代码语言:javascript
复制
yum install git-lfs
git lfs install

python代码下载模型

https://huggingface.co/stabilityai/stable-diffusion-2-1/tree/main

代码语言:javascript
复制
# 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/')

3. 解决报错

  • ImportError: cannot import name 'rank_zero_only' from 'pytorch_lightning.utilities.distributed'

https://github.com/AUTOMATIC1111/stable-diffusion-webui/discussions/4111

代码语言:javascript
复制
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

代码语言:javascript
复制
# 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()

4. UI 模式

在Centos服务器部署的 SD

现在在本地windows使用cmd命令,进行端口转发

代码语言:javascript
复制
ssh-keygen -R ip
ssh -L 7860:localhost:7860 user@ip

或者 使用 mobaxterm 启动隧道 tunneling

在这里插入图片描述
在这里插入图片描述
  • 在服务器上启动 python launch.py
代码语言:javascript
复制
$ 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

在这里插入图片描述
在这里插入图片描述

5. API模式

https://zhuanlan.zhihu.com/p/629845245

  • sh webui.sh --nowebui --xformers
  • 或者 后台运行 nohup python launch.py --nowebui --xformers > nohup.out 2>&1 &
代码语言:javascript
复制
(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 可以查看接口

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-08-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 安装
  • 2. 下载模型
  • 3. 解决报错
  • 4. UI 模式
  • 5. API模式
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档