Hello World

吞风吻雨葬落日 欺山赶海踏雪径

0%

Stable Diffusion 初探

近期AIGC比较火热,看下是否能帮助业务做一些提效降本的事情。首先尝试一下 Stable Diffusion

环境准备

安装python3.10+版本,测试python3.11不支持。

clone stable-diffusion-webui 到本地。

下载对应模型,这里下载默认模型 Stable Diffusion 1.5 (v1-5-pruned-emaonly.ckpt),
模型放入 stable-diffusion-webui/models/Stable-diffusion 目录下。

配置一下webui-user.sh 设置一下指定的python执行文件以及对应的虚拟环境目录

1
2
3
4
5
6
7
8
# python3 executable
python_cmd="/usr/local/bin/python3.10"

# git executable
#export GIT="git"

# python3 venv without trailing slash (defaults to ${install_dir}/${clone_dir}/venv)
venv_dir="venv"

直接执行 webui.sh 即可,中途会下载很多依赖,最好全局科学上网,否则会很慢。

最终显示

1
2
3
Textual inversion embeddings loaded(0):
Model loaded in 25.0s (calculate hash: 3.0s, load weights from disk: 2.0s, create model: 14.3s, apply weights to model: 2.5s, apply half(): 2.3s, move model to device: 0.7s, load textual inversion embeddings: 0.2s).
Running on local URL: http://127.0.0.1:7860

表示已经启动成功。

问题记录

request.urlopen(url) 报错:CERTIFICATE_VERIFY_FAILED

修改 stable-diffusion-webui/modules/ui_extensions.py (参考异常栈) , 修改成如下代码,不校验证书。【参考

1
2
3
4
5
6
import ssl
......
context = ssl._create_unverified_context()
import urllib.request
with urllib.request.urlopen(url,context=context) as response:
text = response.read()

术语

Torch

Torch 是一个支持机器深度学习的科学计算框架,优先使用GPU。

PyTorch PyTorch是一个开源的Python机器学习库,基于Torch,用于自然语言处理等应用程序。
PyTorch的前身是Torch,其底层和Torch框架一样,但是使用Python重新写了很多内容,不仅更加灵活,支持动态图,而且提供了Python接口。
它是由Facebook人工智能研究院(FAIR)推出的一个Python深度学习框架,不仅能够实现强大的GPU加速,同时还支持动态神经网络。

cuda

CUDA(Compute Unified Device Architecture)是 NVIDIA 发明的一种并行计算平台和编程模型。它通过利用图形处理器 (GPU) 的处理能力,可大幅提升计算性能。

cuda版本 与 先看驱动版本对应关系: https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html#cuda-major-component-versions
安装教程 配置显卡cuda与配置pytorch
cuda下载

cuDNN 是NVIDIA 推出的用于深度神经网络的GPU加速库,他强调性能,易用性和低内存开销。cudnn下载
安装教程 Cuda和Cudnn详细安装教程
参考 GPU,CUDA,cuDNN的理解

xformers

xformers 优化加速图片生成。

kohya_ss

kohya_ss This repository provides a Windows-focused Gradio GUI for Kohya’s Stable Diffusion trainers. The GUI allows you to set the training parameters and generate and run the required CLI commands to train the model.
模型训练的图形程序。

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
git clone https://github.com/bmaltais/kohya_ss.git
cd kohya_ss

python -m venv venv
.\venv\Scripts\activate

pip install torch==1.12.1+cu116 torchvision==0.13.1+cu116 --extra-index-url https://download.pytorch.org/whl/cu116
pip install --use-pep517 --upgrade -r requirements.txt
pip install -U -I --no-deps https://github.com/C43H66N12O12S2/stable-diffusion-webui/releases/download/f/xformers-0.0.14.dev0-cp310-cp310-win_amd64.whl

cp .\bitsandbytes_windows\*.dll .\venv\Lib\site-packages\bitsandbytes\
cp .\bitsandbytes_windows\cextension.py .\venv\Lib\site-packages\bitsandbytes\cextension.py
cp .\bitsandbytes_windows\main.py .\venv\Lib\site-packages\bitsandbytes\cuda_setup\main.py

accelerate config

dreambooth

dreambooth is a method to personalize text-to-image models like stable diffusion given just a few (3~5) images of a subject.
少量图片的训练方法。

Gradio

gradio 开源的 Python 框架,用于快速搭建机器学习与数据科学的web应用。

参考

stable-diffusion-webui

civitai

Protogen x3.4

ControlNet
ControlNet GitHUb

Diffusers 文档

dreambooth 说明