说明文档
---
license: openrail++
base_model: diffusers/stable-diffusion-xl-base-1.0
tags:
- stable-diffusion-xl
- stable-diffusion-xl-diffusers
- stable-diffusion
- text-to-image
- diffusers
- di.FFusion.ai
inference: true
widget:
- text: >-
a dog in colorful exploding clouds, dreamlike surrealism colorful smoke and fire coming
out of it, explosion of data fragments, exploding background,realistic explosion, 3d digital art
example_title: 狗狗 FFusion
- text: >-
a sprinkled donut sitting on top of a table, colorful hyperrealism, everything is made of candy, hyperrealistic digital
painting, covered in sprinkles and crumbs, vibrant colors hyper realism,colorful smoke explosion background
example_title: 甜甜圈 FFusion
- text: >-
a cup of coffee with a tree in it, surreal art, awesome great composition,
surrealism, ice cubes in tree, colorful clouds, perfectly realistic yet surreal
example_title: 咖啡 FFusion
- text: >-
brightly colored headphones with a splash of colorful paint splash, vibing
to music, stunning artwork, music is life, beautiful digital artwork, concept art, cinematic, dramatic, intricate details, dark
lighting
example_title: 耳机 FFusion
- text: >-
high-quality game character digital design, Unreal Engine, Water color painting, Mecha- Monstrous high quality game fantasy rpg character design, dark rainbow Fur Scarf, inside of a Superficial Outhouse, at Twilight, Overdetailed art
example_title: 数字融合
language:
- en
model-index:
- name: FFusion/FFusionXL-BASE
results:
- task:
type: text-to-image
name: 文本生成图像
dataset:
type: poloclub/diffusiondb
name: DiffusionDB
split: train
metrics:
- type: is
value: 4.9797071218490601
name: Inception 分数
verified: true
- type: fid
value: 311.33686580590006
name: Fréchet Inception 距离
verified: true
- type: text-image-similarity
value: 14.368797302246094
name: 相似度分数 (CLIP)
verified: true
thumbnail: >-
https://cdn-uploads.huggingface.co/production/uploads/6380cf05f496d57325c12194/p54u7dEP1u8en0--NMEjS.png
---

<div style="display: flex; flex-wrap: wrap; gap: 2px;">
<a href="https://huggingface.co/FFusion/"><img src="https://img.shields.io/badge/ONNX_Version-Available-brightgreen" alt="ONNX 版本可用"></a>
<a href="https://huggingface.co/FFusion/"><img src="https://img.shields.io/badge/OpenVINO-Support-blue" alt="OpenVINO 支持"></a>
<a href="https://huggingface.co/FFusion/"><img src="https://img.shields.io/badge/Compatibility-Intel%20|%20AMD%20|%20NVIDIA-orange" alt="Intel/AMD/NVIDIA 兼容"></a>
</div>
## 🌟 概述
- 🚀 快速训练:针对高速训练进行了优化,可快速进行实验。
- 🧩 多功能性:适用于各种应用和标准,从自然语言处理到计算机视觉。
- 🎓 按需训练:作为训练自定义模型的基础,满足您的需求。
- 🌐 多语言支持:支持多语言模型训练。
- 🛡️ 稳健架构:基于成熟技术构建,确保稳定性和可靠性。
## 📜 模型描述
FFusionXL "Base" 是一个旨在加速训练过程的基础模型。它以灵活性为核心设计理念,可作为在各种标准上训练自定义模型的基础,推动创新和效率。
<div style="display: flex; flex-wrap: wrap; gap: 2px;">
<a href="#"><img src="https://img.shields.io/badge/Safetensor-FP16%20%26%20FP32-blue" alt="Safetensor 检查点"></a>
<a href="#"><img src="https://img.shields.io/badge/Diffusers(Safetensors)-FP16%20%26%20FP32-green" alt="Diffusers(safetensors)"></a>
<a href="#"><img src="https://img.shields.io/badge/Diffusers(PyTorch%20Bin)-FP16%20%26%20FP32-orange" alt="Diffusers(pytorch bin)"></a>
<a href="#"><img src="https://img.shields.io/badge/ONNX-Unoptimized%20FP32-red" alt="ONNX 未优化 FP32"></a>
<a href="#"><img src="https://img.shields.io/badge/ONNX%20Optimized-FP16%20DirectML%20Support-blueviolet" alt="ONNX 优化 FP16 完整 DirectML 支持"></a>
<a href="#"><img src="https://img.shields.io/badge/Intel®%20OpenVINO™-FP32%20%26%20FP16-brightgreen" alt="Intel® OpenVINO™ FP32 & FP16"></a>
</div>
**可用于训练的格式:**
- Safetensor 检查点 fp16 & fp32
- Diffusers(safetensors) FP 16 & FP32
- Diffusers(pytorch bin) FP16 & FP32
- ONNX 未优化 FP32
- **ONNX 优化** FP16 完整 **DirectML** 支持 / AMD / NVIDIA
- Intel® OpenVINO™ FP32 - 未优化
- **Intel® OpenVINO™** FP16
- **训练者:** FFusion AI
- **模型类型:** 基于扩散的文本生成图像模型
- **许可证:** [FFXL 研究许可证](https://huggingface.co/FFusion/FFusionXL-09-SDXL/blob/main/LICENSE.md)
- **模型描述:** 这是一个基于 SDXL 训练的模型,可用于根据文本提示生成和修改图像。它是一个 [潜在扩散模型](https://arxiv.org/abs/2112.10752),使用两个固定的预训练文本编码器([OpenCLIP-ViT/G](https://github.com/mlfoundations/open_clip) 和 [CLIP-ViT/L](https://github.com/openai/CLIP/tree/main))。
- **更多信息资源:** [arXiv 上的 SDXL 论文](https://arxiv.org/abs/2307.01952)。
## 📊 模型来源
- **演示:** [FFusionXL SDXL 演示](https://huggingface.co/spaces/FFusion/FFusionXL-SDXL-DEMO)

## 目录
1. [📌 ONNX 版本](#📌-onnx-版本)
1. [🔖 ### 📌 ONNX 详情](#🔖-###-📌-onnx-详情)
2. [🔖 ### 📌 AMD 对 Microsoft® DirectML 优化 Stable Diffusion 的支持](#🔖-###-📌-amd-对-microsoft®-directml-优化-stable-diffusion-的支持)
3. [🔖 ### 📌 ONNX 推理说明](#🔖-###-📌-onnx-推理说明)
4. [🔖 ### 📌 文本生成图像](#🔖-###-📌-文本生成图像)
2. [📌 Intel® OpenVINO™ 版本](#📌-intel®-openvino™-版本)
1. [📌 使用 FFusion/FFusionXL-BASE 进行 OpenVINO 推理](#📌-使用-ffusion/ffusionxl-base-进行-openvino-推理)
2. [🔖 ### 📌 安装依赖](#🔖-###-📌-安装依赖)
3. [🔖 ### 📌 文本生成图像](#🔖-###-📌-文本生成图像-1)
4. [🔖 ### 📌 带文本反转的文本生成图像](#🔖-###-📌-带文本反转的文本生成图像)
5. [🔖 ### 📌 图像生成图像](#🔖-###-📌-图像生成图像)
6. [🔖 ### 📌 优化图像输出](#🔖-###-📌-优化图像输出)
3. [📜 第三部分:🧨 模型 Diffusers、快速 LoRa 加载和训练](#📜-第三部分-🧨-模型-diffusers、快速-lora-加载和训练)
1. [📌 模型 Diffusers:释放 FFusion/FFusionXL-BASE 的力量](#📌-模型-diffusers:释放-ffusion/ffusionxl-base-的力量)
2. [📌 安装依赖](#📌-安装依赖)
3. [📌 训练](#📌-训练)
4. [📌 推理](#📌-推理)
5. [📌 训练](#📌-训练-1)
6. [📌 微调文本编码器和 UNet](#📌-微调文本编码器和-unet)
7. [📌 推理](#📌-推理-1)
4. [📌 评估](#📌-评估)
### ### 📌 ONNX 版本

我们自豪地宣布推出完全优化的 Microsoft ONNX 版本,专门兼容最新的 DirectML 执行提供程序。所有 ONNX 文件都已优化(量化)为 fp16,可在所有设备上实现快速推理和训练。
VAE_Decoder 保持 fp32,设置如下:
```json
"float16": false,
"use_gpu": true,
"keep_io_types": true,
"force_fp32_ops": ["RandomNormalLike"]
以避免黑屏和渲染损坏。一旦有完整的 fp16 VAE 解码器的适当解决方案,我们将进行更新。VAE 编码器和其他所有内容都已完全优化 🤟。
我们的 ONNX 使用 ONNX v8 进行了优化:
- 生产者: onnxruntime.transformers 1.15.1
- 导入: ai.onnx v18, com.microsoft.nchwc v1, ai.onnx.ml v3, com.ms.internal.nhwc v19, ai.onnx.training v1, ai.onnx.preview.training v1, com.microsoft v1, com.microsoft.experimental v1, org.pytorch.aten v1, com.microsoft.dml v1, graph: torch_jit
🔖 ### 📌 ONNX 详情
NETRON 详情:

安装
macOS:下载 .dmg 文件或运行 brew install --cask netron
Linux:下载 .AppImage 文件或运行 snap install netron
Windows:下载 .exe 安装程序或运行 winget install -s winget netron
https://netron.app/
-- NETRON 浏览器版本:启动 文本编码器

--NETRON 浏览器版本:启动 文本编码器 2

--NETRON 浏览器版本:启动 VAE 解码器
--NETRON 浏览器版本:启动 VAE 编码器

--NETRON 浏览器版本:启动 UNET
🔖 ### 📌 AMD 对 Microsoft® DirectML 优化 Stable Diffusion 的支持

AMD 已发布对 Microsoft DirectML 优化 Stable Diffusion 的支持,并与 Microsoft 密切合作,在 AMD 设备上实现最佳性能。
Microsoft DirectML AMD Microsoft DirectML Stable Diffusion
🔖 ### 📌 ONNX 推理说明

🔖 ### 📌 文本生成图像
以下是如何加载 ONNX Stable Diffusion 模型并使用 ONNX Runtime 运行推理的示例:
from optimum.onnxruntime import ORTStableDiffusionPipeline
model_id = "FFusion/FFusionXL-BASE"
pipeline = ORTStableDiffusionPipeline.from_pretrained(model_id)
prompt = "sailing ship in storm by Leonardo da Vinci"
images = pipeline(prompt).images
### 📌 Intel® OpenVINO™ 版本
还包含一个转换后的 Intel® OpenVINO™ 模型,用于推理测试和训练。尚未应用量化和优化。
### 📌 使用 FFusion/FFusionXL-BASE 进行 OpenVINO 推理
🔖 ### 📌 安装依赖
在使用 OVStableDiffusionXLPipeline 之前,请确保已安装 diffusers 和 invisible_watermark。您可以按以下方式安装这些库:
pip install diffusers
pip install invisible-watermark>=0.2.0
🔖 ### 📌 文本生成图像
以下是如何加载 FFusion/FFusionXL-BASE OpenVINO 模型并使用 OpenVINO Runtime 运行推理的示例:
from optimum.intel import OVStableDiffusionXLPipeline
model_id = "FFusion/FFusionXL-BASE"
base = OVStableDiffusionXLPipeline.from_pretrained(model_id)
prompt = "train station by Caspar David Friedrich"
image = base(prompt).images[0]
image.save("train_station.png")
🔖 ### 📌 带文本反转的文本生成图像
首先,您可以在不带文本反转的情况下运行原始管道:
from optimum.intel import OVStableDiffusionXLPipeline
import numpy as np
model_id = "FFusion/FFusionXL-BASE"
prompt = "charturnerv2, multiple views of the same character in the same outfit, a character turnaround of a beautiful cyber female wearing a black corset and pink latex shirt, scifi best quality, intricate details."
np.random.seed(0)
base = OVStableDiffusionXLPipeline.from_pretrained(model_id, export=False, compile=False)
base.compile()
image1 = base(prompt, num_inference_steps=50).images[0]
image1.save("sdxl_without_textual_inversion.png")
然后,您可以加载 charturnerv2 文本反转嵌入,并使用相同的提示再次运行管道:
# 重置 stable diffusion 管道
base.clear_requests()
# 将文本反转加载到 stable diffusion 管道
base.load_textual_inversion("./charturnerv2.pt", "charturnerv2")
# 在第一次推理之前编译模型
base.compile()
image2 = base(prompt, num_inference_steps=50).images[0]
image2.save("sdxl_with_textual_inversion.png")

🔖 ### 📌 图像生成图像
以下是如何加载 PyTorch FFusion/FFusionXL-BASE 模型,即时转换为 OpenVINO,并使用 OpenVINO Runtime 运行图像生成图像推理的示例:
from optimum.intel import OVStableDiffusionXLImg2ImgPipeline
from diffusers.utils import load_image
model_id = "FFusion/FFusionXL-BASE-refiner-1.0"
pipeline = OVStableDiffusionXLImg2ImgPipeline.from_pretrained(model_id, export=True)
url = "https://huggingface.co/datasets/optimum/documentation-images/resolve/main/intel/openvino/sd_xl/castle_friedrich.png"
image = load_image(url).convert("RGB")
prompt = "medieval castle by Caspar David Friedrich"
image = pipeline(prompt, image=image).images[0]
pipeline.save_pretrained("openvino-FF-xl-refiner-1.0")
🔖 ### 📌 优化图像输出
可以通过使用像 FFusion/FFusionXL-BASE-refiner-1.0 这样的模型来优化图像。在这种情况下,您只需要从基础模型输出潜在表示。
from optimum.intel import OVStableDiffusionXLImg2ImgPipeline
model_id = "FFusion/FFusionXL-BASE-refiner-1.0"
refiner = OVStableDiffusionXLImg2ImgPipeline.from_pretrained(model_id, export=True)
image = base(prompt=prompt, output_type="latent").images[0]
image = refiner(prompt=prompt, image=image[None, :]).images[0]
📜 第三部分:🧨 模型 Diffusers、快速 LoRa 加载和训练
### 📌 模型 Diffusers:释放 FFusion/FFusionXL-BASE 的力量
无论您是艺术家、研究人员还是 AI 爱好者,我们的模型都旨在让您的旅程顺畅而精彩。 请确保将 diffusers 升级到 >= 0.19.3:
pip install diffusers --upgrade
此外,请确保安装 transformers、safetensors、accelerate 和隐形水印:
pip install invisible_watermark transformers accelerate safetensors
然后您可以按以下方式使用模型:
from diffusers import DiffusionPipeline
import torch
pipe = DiffusionPipeline.from_pretrained("FFusion/FFusionXL-09-SDXL", torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
pipe.to("cuda")
# 如果使用 torch < 2.0
# pipe.enable_xformers_memory_efficient_attention()
prompt = "An astronaut riding a green horse"
images = pipe(prompt=prompt).images[0]
📜 Diffusers 训练指南:使用 LoRA 训练 FFusion/FFusionXL-BASE
Stable Diffusion XL 文本生成图像微调
train_text_to_image_sdxl.py 脚本展示了如何在您自己的数据集上微调 Stable Diffusion XL (SDXL)。
🚨 此脚本是实验性的。该脚本对整个模型进行微调,模型经常过拟合并遇到灾难性遗忘等问题。建议尝试不同的超参数以在您的数据集上获得最佳结果。🚨
📜 使用 PyTorch 本地运行
### 📌 安装依赖
在运行脚本之前,请确保安装库的训练依赖项:
重要提示
为确保您可以成功运行最新版本的示例脚本,我们强烈建议从源代码安装并保持安装更新,因为我们经常更新示例脚本并安装一些特定于示例的要求。为此,在新的虚拟环境中执行以下步骤:
git clone https://github.com/huggingface/diffusers
cd diffusers
pip install -e .
然后进入 examples/text_to_image 文件夹并运行
pip install -r requirements_sdxl.txt
并使用以下命令初始化 🤗Accelerate 环境:
accelerate config
或者使用默认的 accelerate 配置,无需回答关于您环境的问题
accelerate config default
或者如果您的环境不支持交互式 shell(例如笔记本)
from accelerate.utils import write_basic_config
write_basic_config()
运行 accelerate config 时,如果我们将 torch 编译模式指定为 True,则可以显著提高速度。
### 📌 训练
export MODEL_NAME="FFusion/FFusionXL-BASE"
export VAE="madebyollin/sdxl-vae-fp16-fix"
export DATASET_NAME="lambdalabs/pokemon-blip-captions"
accelerate launch train_text_to_image_sdxl.py \
--pretrained_model_name_or_path=$MODEL_NAME \
--pretrained_vae_model_name_or_path=$VAE \
--dataset_name=$DATASET_NAME \
--enable_xformers_memory_efficient_attention \
--resolution=512 --center_crop --random_flip \
--proportion_empty_prompts=0.2 \
--train_batch_size=1 \
--gradient_accumulation_steps=4 --gradient_checkpointing \
--max_train_steps=10000 \
--use_8bit_adam \
--learning_rate=1e-06 --lr_scheduler="constant" --lr_warmup_steps=0 \
--mixed_precision="fp16" \
--report_to="wandb" \
--validation_prompt="a cute Sundar Pichai creature" --validation_epochs 5 \
--checkpointing_steps=5000 \
--output_dir="sdxl-pokemon-model" \
--push_to_hub
注意:
train_text_to_image_sdxl.py(diffusers/examples/text_to_image)脚本会预计算文本嵌入和 VAE 编码并将它们保存在内存中。虽然对于像lambdalabs/pokemon-blip-captions这样较小的数据集可能不是问题,但当脚本用于更大的数据集时,肯定会导致内存问题。为此,您需要将这些预计算的表示单独序列化到磁盘,并在微调过程中加载它们。请参阅 此 PR 进行更深入的讨论。- 训练脚本计算密集,可能无法在 Tesla T4 等消费级 GPU 上运行。
- 上面显示的训练命令在训练轮次之间执行中间质量验证,并将结果记录到 Weights and Biases。
--report_to、--validation_prompt和--validation_epochs是此处相关的 CLI 参数。 examples/text_to_image
### 📌 推理
from diffusers import DiffusionPipeline
import torch
model_path = "FFusion/FFusionXL-BASE" # <-- 将此更改为您新训练的模型
pipe = DiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float16)
pipe.to("cuda")
prompt = "A pokemon with green eyes and red legs."
image = pipe(prompt, num_inference_steps=30, guidance_scale=7.5).images[0]
image.save("pokemon.png")
📜 Stable Diffusion XL (SDXL) LoRA 训练示例
大型语言模型的低秩适应最初由 Microsoft 在 LoRA: Low-Rank Adaptation of Large Language Models 中提出,作者为 Edward J. Hu, Yelong Shen, Phillip Wallis, Zeyuan Allen-Zhu, Yuanzhi Li, Shean Wang, Lu Wang, Weizhu Chen。
简而言之,LoRA 允许通过向现有权重添加秩分解矩阵对并仅训练这些新添加的权重来适应预训练模型。这有几个优点:
- 以前的预训练权重保持冻结,因此模型不会出现灾难性遗忘。
- 秩分解矩阵的参数比原始模型少得多,这意味着训练的 LoRA 权重易于移植。
- LoRA 注意力层允许通过
scale参数控制模型向新训练图像适应的程度。
cloneofsimo 是第一个在流行的 lora GitHub 仓库中尝试 Stable Diffusion LoRA 训练的人。
使用 LoRA,可以在 Tesla T4、Tesla V100 等消费级 GPU 上在自定义图像-标题对数据集上微调 Stable Diffusion。
### 📌 训练
首先,您需要按照安装部分中的说明设置开发环境。确保设置 MODEL_NAME 和 DATASET_NAME 环境变量。这里我们将使用 Stable Diffusion XL 1.0-base 和 Pokemons 数据集。
注意:在训练过程中定期生成样本图像来监控训练进度非常有用。Weights and Biases 是一个很好的解决方案,可以轻松查看训练期间生成的图像。您只需要在训练前运行 pip install wandb 即可自动记录图像。
export MODEL_NAME="FFusion/FFusionXL-BASE"
export DATASET_NAME="lambdalabs/pokemon-blip-captions"
对于此示例,我们希望将训练的 LoRA 嵌入直接存储在 Hub 上,因此我们需要登录并添加 --push_to_hub 标志。
huggingface-cli login
现在我们可以开始训练了!
accelerate launch train_text_to_image_lora_sdxl.py \
--pretrained_model_name_or_path=$MODEL_NAME \
--dataset_name=$DATASET_NAME --caption_column="text" \
--resolution=1024 --random_flip \
--train_batch_size=1 \
--num_train_epochs=2 --checkpointing_steps=500 \
--learning_rate=1e-04 --lr_scheduler="constant" --lr_warmup_steps=0 \
--seed=42 \
--output_dir="sd-pokemon-model-lora-sdxl" \
--validation_prompt="cute dragon creature" --report_to="wandb" \
--push_to_hub
上述命令还将在微调过程中运行推理并将结果记录到 Weights and Biases。
### 📌 微调文本编码器和 UNet
该脚本还允许您与 unet 一起微调 text_encoder。
🚨 训练文本编码器需要额外的内存。
将 --train_text_encoder 参数传递给训练脚本以启用微调 text_encoder 和 unet:
accelerate launch train_text_to_image_lora_sdxl.py \
--pretrained_model_name_or_path=$MODEL_NAME \
--dataset_name=$DATASET_NAME --caption_column="text" \
--resolution=1024 --random_flip \
--train_batch_size=1 \
--num_train_epochs=2 --checkpointing_steps=500 \
--learning_rate=1e-04 --lr_scheduler="constant" --lr_warmup_steps=0 \
--seed=42 \
--output_dir="sd-pokemon-model-lora-sdxl-txt" \
--train_text_encoder \
--validation_prompt="cute dragon creature" --report_to="wandb" \
--push_to_hub
### 📌 推理
使用上述命令训练模型后,加载训练的 LoRA 权重后,只需使用 DiffusionPipeline 即可完成推理。您需要传递 output_dir 来加载 LoRA 权重,在本例中为 sd-pokemon-model-lora-sdxl。
from diffusers import DiffusionPipeline
import torch
model_path = "takuoko/sd-pokemon-model-lora-sdxl"
pipe = DiffusionPipeline.from_pretrained("FFusion/FFusionXL-BASE", torch_dtype=torch.float16)
pipe.to("cuda")
pipe.load_lora_weights(model_path)
prompt = "A pokemon with green eyes and red legs."
image = pipe(prompt, num_inference_steps=30, guidance_scale=7.5).images[0]
image.save("pokemon.png")
### 📌 评估


利用 yuvalkirstain/PickScore_v1 模型,此分析由 FFusion.AI 进行。这是对 Stable Diffusion 模型提示胜率和准确性测试持续研究的重要贡献。
📧 如有任何疑问或支持,请联系 di@ffusion.ai。我们将全程为您提供帮助!
FFusion/FFusionXL-BASE
作者 FFusion
创建时间: 2023-07-27 14:59:49+00:00
更新时间: 2023-08-17 20:52:00+00:00
在 Hugging Face 上查看