test:基于扩散模型的实时交互式图像生成项目

用户可借助该项目实现实时交互式图像生成,其核心是创新的扩散流水线,具备Stream Batch、Residual Classifier-Free Guidance等关键特性,能显著提升扩散模型的性能与GPU利用率。【此简介由AI生成】

分支1Tags0
文件最后提交记录最后更新时间
1 年前
1 年前
1 年前
1 年前
1 年前
1 年前
1 年前
1 年前
1 年前
1 年前
1 年前
1 年前
1 年前
1 年前
1 年前
1 年前

StreamDiffusion

English | 日本語 | 한국어

StreamDiffusion:实时交互式生成的流水线级解决方案

作者: Akio Kodaira*Chenfeng Xu*、Toshiki Hazama*、Takanori YoshimotoKohei OhnoShogo MitsuhoriSoichi SuganoHanying ChoZhijian LiuMasayoshi TomizukaKurt Keutzer

StreamDiffusion 是一种创新的扩散流水线,专为实时交互式生成而设计。它为当前基于扩散的图像生成技术带来了显著的性能提升。

arXiv Hugging Face Papers

我们衷心感谢 Taku FujimotoRadamés Ajna 以及 Hugging Face 团队提供的宝贵反馈、友好支持和富有洞察力的讨论。

主要特性

  1. 流批处理(Stream Batch)

    • 通过高效的批处理操作简化数据处理流程。
  2. 残差无分类器引导(Residual Classifier-Free Guidance) - 了解更多

    • 改进的引导机制,最大限度地减少计算冗余。
  3. 随机相似性过滤器(Stochastic Similarity Filter) - 了解更多

    • 通过先进的过滤技术提高 GPU 利用率。
  4. IO 队列(IO Queues)

    • 高效管理输入和输出操作,确保执行更流畅。
  5. KV 缓存预计算(Pre-Computation for KV-Caches)

    • 优化缓存策略以加速处理过程。
  6. 模型加速工具(Model Acceleration Tools)

    • 利用多种工具进行模型优化和性能提升。

以下是在 GPU:RTX 4090CPU:Core i9-13900K操作系统:Ubuntu 22.04.3 LTS 环境下,使用我们提出的 StreamDiffusion 流水线生成图像时的性能数据。

model 去噪步数 Txt2Img 帧率 Img2Img 帧率
SD-turbo 1 106.16 93.897
LCM-LoRA
+
KohakuV2
4 38.023 37.133

欢迎通过提供的链接探索每个特性,了解 StreamDiffusion 的更多功能。如果您觉得本项目对您有帮助,请考虑引用我们的工作:

@article{kodaira2023streamdiffusion,
      title={StreamDiffusion: A Pipeline-level Solution for Real-time Interactive Generation},
      author={Akio Kodaira and Chenfeng Xu and Toshiki Hazama and Takanori Yoshimoto and Kohei Ohno and Shogo Mitsuhori and Soichi Sugano and Hanying Cho and Zhijian Liu and Kurt Keutzer},
      year={2023},
      eprint={2312.12491},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}

安装

步骤0:克隆此仓库

git clone https://github.com/cumulo-autumn/StreamDiffusion.git

步骤1:配置环境

您可以通过 pip、conda 或 Docker(下文将进行说明)安装 StreamDiffusion。

conda create -n streamdiffusion python=3.10
conda activate streamdiffusion

python -m venv .venv
# Windows
.\.venv\Scripts\activate
# Linux
source .venv/bin/activate

步骤2:安装 PyTorch

选择适合您系统的版本。

CUDA 11.8

pip3 install torch==2.1.0 torchvision==0.16.0 xformers --index-url https://download.pytorch.org/whl/cu118

CUDA 12.1

pip3 install torch==2.1.0 torchvision==0.16.0 xformers --index-url https://download.pytorch.org/whl/cu121

详情:https://pytorch.org/

步骤3:安装 StreamDiffusion

面向用户

安装 StreamDiffusion

#for Latest Version (recommended)
pip install git+https://github.com/cumulo-autumn/StreamDiffusion.git@main#egg=streamdiffusion[tensorrt]


#or


#for Stable Version
pip install streamdiffusion[tensorrt]

安装 TensorRT 扩展

python -m streamdiffusion.tools.install-tensorrt

(仅适用于 Windows)如果您安装的是稳定版本(pip install streamdiffusion[tensorrt]),可能还需要额外安装 pywin32。

pip install --force-reinstall pywin32

面向开发者

python setup.py develop easy_install streamdiffusion[tensorrt]
python -m streamdiffusion.tools.install-tensorrt

Docker 安装(支持 TensorRT)

git clone https://github.com/cumulo-autumn/StreamDiffusion.git
cd StreamDiffusion
docker build -t stream-diffusion:latest -f Dockerfile .
docker run --gpus all -it -v $(pwd):/home/ubuntu/streamdiffusion stream-diffusion:latest

快速开始

您可以在examples目录中尝试StreamDiffusion。

画像3 画像4
画像5 画像6

实时文本生成图像演示

demo/realtime-txt2img目录中提供了一个交互式的文本生成图像演示!

实时图像生成图像演示

demo/realtime-img2img目录中提供了一个实时图像生成图像演示,可通过网络浏览器使用实时网络摄像头 feed 或屏幕捕获!

使用示例

我们提供了一个如何使用StreamDiffusion的简单示例。有关更详细的示例,请参阅examples目录。

图像到图像

import torch
from diffusers import AutoencoderTiny, StableDiffusionPipeline
from diffusers.utils import load_image

from streamdiffusion import StreamDiffusion
from streamdiffusion.image_utils import postprocess_image

# You can load any models using diffuser's StableDiffusionPipeline
pipe = StableDiffusionPipeline.from_pretrained("KBlueLeaf/kohaku-v2.1").to(
    device=torch.device("cuda"),
    dtype=torch.float16,
)

# Wrap the pipeline in StreamDiffusion
stream = StreamDiffusion(
    pipe,
    t_index_list=[32, 45],
    torch_dtype=torch.float16,
)

# If the loaded model is not LCM, merge LCM
stream.load_lcm_lora()
stream.fuse_lora()
# Use Tiny VAE for further acceleration
stream.vae = AutoencoderTiny.from_pretrained("madebyollin/taesd").to(device=pipe.device, dtype=pipe.dtype)
# Enable acceleration
pipe.enable_xformers_memory_efficient_attention()


prompt = "1girl with dog hair, thick frame glasses"
# Prepare the stream
stream.prepare(prompt)

# Prepare image
init_image = load_image("assets/img2img_example.png").resize((512, 512))

# Warmup >= len(t_index_list) x frame_buffer_size
for _ in range(2):
    stream(init_image)

# Run the stream infinitely
while True:
    x_output = stream(init_image)
    postprocess_image(x_output, output_type="pil")[0].show()
    input_response = input("Press Enter to continue or type 'stop' to exit: ")
    if input_response == "stop":
        break

文本转图像

import torch
from diffusers import AutoencoderTiny, StableDiffusionPipeline

from streamdiffusion import StreamDiffusion
from streamdiffusion.image_utils import postprocess_image

# You can load any models using diffuser's StableDiffusionPipeline
pipe = StableDiffusionPipeline.from_pretrained("KBlueLeaf/kohaku-v2.1").to(
    device=torch.device("cuda"),
    dtype=torch.float16,
)

# Wrap the pipeline in StreamDiffusion
# Requires more long steps (len(t_index_list)) in text2image
# You recommend to use cfg_type="none" when text2image
stream = StreamDiffusion(
    pipe,
    t_index_list=[0, 16, 32, 45],
    torch_dtype=torch.float16,
    cfg_type="none",
)

# If the loaded model is not LCM, merge LCM
stream.load_lcm_lora()
stream.fuse_lora()
# Use Tiny VAE for further acceleration
stream.vae = AutoencoderTiny.from_pretrained("madebyollin/taesd").to(device=pipe.device, dtype=pipe.dtype)
# Enable acceleration
pipe.enable_xformers_memory_efficient_attention()


prompt = "1girl with dog hair, thick frame glasses"
# Prepare the stream
stream.prepare(prompt)

# Warmup >= len(t_index_list) x frame_buffer_size
for _ in range(4):
    stream()

# Run the stream infinitely
while True:
    x_output = stream.txt2img()
    postprocess_image(x_output, output_type="pil")[0].show()
    input_response = input("Press Enter to continue or type 'stop' to exit: ")
    if input_response == "stop":
        break

您可以通过使用 SD-Turbo 来加快速度。

更快的生成速度

替换上述示例中的以下代码。

pipe.enable_xformers_memory_efficient_attention()

from streamdiffusion.acceleration.tensorrt import accelerate_with_tensorrt

stream = accelerate_with_tensorrt(
    stream, "engines", max_batch_size=2,
)

它需要 TensorRT 扩展和时间来构建引擎,但会比上述示例更快。

可选功能

随机相似度过滤器

demo

随机相似度过滤器通过在与前一帧变化较小时减少转换操作,从而降低视频输入时的处理量,进而减轻 GPU 处理负载,如上述 GIF 中的红框所示。使用方法如下:

stream = StreamDiffusion(
    pipe,
    [32, 45],
    torch_dtype=torch.float16,
)
stream.enable_similar_image_filter(
    similar_image_filter_threshold,
    similar_image_filter_max_skip_frame,
)

以下是可在函数中作为参数设置的参数:

similar_image_filter_threshold

  • 处理暂停前,前一帧与当前帧的相似度阈值。

similar_image_filter_max_skip_frame

  • 暂停期间恢复转换前的最大间隔。

残差CFG(RCFG)

rcfg

RCFG 是一种近似实现 CFG 的方法,其计算复杂度与不使用 CFG 的情况相比具有竞争力。可通过 StreamDiffusion 中的 cfg_type 参数进行指定。RCFG 有两种类型:一种是未指定负提示项的 RCFG Self-Negative,另一种是可指定负提示的 RCFG Onetime-Negative。在计算复杂度方面,若将不使用 CFG 时的复杂度记为 N,常规 CFG 的复杂度记为 2N,则 RCFG Self-Negative 可在 N 步内完成计算,而 RCFG Onetime-Negative 可在 N+1 步内完成计算。

使用方法如下:

# w/0 CFG
cfg_type = "none"
# CFG
cfg_type = "full"
# RCFG Self-Negative
cfg_type = "self"
# RCFG Onetime-Negative
cfg_type = "initialize"
stream = StreamDiffusion(
    pipe,
    [32, 45],
    torch_dtype=torch.float16,
    cfg_type=cfg_type,
)
stream.prepare(
    prompt="1girl, purple hair",
    guidance_scale=guidance_scale,
    delta=delta,
)

delta 对 RCFG 的有效性具有调节作用。

开发团队

AkiAraratChenfeng XuddPn08kizamimiramuneteftefTonimonoVerb

(*按字母顺序排列)

致谢

本 GitHub 仓库中的视频和图像演示是使用 LCM-LoRA + KohakuV2 以及 SD-Turbo 生成的。

特别感谢 LCM-LoRA 的作者们 提供 LCM-LoRA,感谢 Kohaku BlueLeaf(@KBlueleaf)提供 KohakuV2 模型,以及感谢 Stability AI 提供 SD-Turbo

KohakuV2 模型可从 CivitaiHugging Face 下载。

SD-Turbo 也可在 Hugging Face Space 获取。

贡献者

项目介绍

用户可借助该项目实现实时交互式图像生成,其核心是创新的扩散流水线,具备Stream Batch、Residual Classifier-Free Guidance等关键特性,能显著提升扩散模型的性能与GPU利用率。【此简介由AI生成】

定制我的领域