🚀 SDXL-controlnet: Depth
SDXL-controlnet: Depth 是基于 stabilityai/stable-diffusion-xl-base-1.0 训练的 ControlNet 权重,支持深度条件控制。以下为你展示一些示例图片。
提示词:蜘蛛侠演讲,逼真写实风格

🚀 快速开始
📦 安装指南
首先,你需要安装必要的库:
pip install accelerate transformers safetensors diffusers
💻 使用示例
基础用法
安装完成后,即可运行以下代码:
import torch
import numpy as np
from PIL import Image
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
from diffusers.utils import load_image
depth_estimator = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to("cuda")
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-hybrid-midas")
controlnet = ControlNetModel.from_pretrained(
"diffusers/controlnet-depth-sdxl-1.0",
variant="fp16",
use_safetensors=True,
torch_dtype=torch.float16,
)
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
controlnet=controlnet,
vae=vae,
variant="fp16",
use_safetensors=True,
torch_dtype=torch.float16,
)
pipe.enable_model_cpu_offload()
def get_depth_map(image):
image = feature_extractor(images=image, return_tensors="pt").pixel_values.to("cuda")
with torch.no_grad(), torch.autocast("cuda"):
depth_map = depth_estimator(image).predicted_depth
depth_map = torch.nn.functional.interpolate(
depth_map.unsqueeze(1),
size=(1024, 1024),
mode="bicubic",
align_corners=False,
)
depth_min = torch.amin(depth_map, dim=[1, 2, 3], keepdim=True)
depth_max = torch.amax(depth_map, dim=[1, 2, 3], keepdim=True)
depth_map = (depth_map - depth_min) / (depth_max - depth_min)
image = torch.cat([depth_map] * 3, dim=1)
image = image.permute(0, 2, 3, 1).cpu().numpy()[0]
image = Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))
return image
prompt = "stormtrooper lecture, photorealistic"
image = load_image("https://huggingface.co/lllyasviel/sd-controlnet-depth/resolve/main/images/stormtrooper.png")
controlnet_conditioning_scale = 0.5
depth_image = get_depth_map(image)
images = pipe(
prompt, image=depth_image, num_inference_steps=30, controlnet_conditioning_scale=controlnet_conditioning_scale,
).images
images[0]
images[0].save(f"stormtrooper.png")
更多详细信息,请查阅 StableDiffusionXLControlNetPipeline
的官方文档。
🔧 技术细节
训练
我们的训练脚本基于官方提供的训练脚本构建,详情请见 此处。
训练数据和计算资源
- 训练数据:模型在 LAION-Aesthetics V2 数据集的 300 万图像 - 文本对上进行训练。
- 计算资源:模型在 80GB A100 GPU 上进行了 700 小时的训练。
批量大小
采用数据并行方式,单 GPU 批量大小为 8,总批量大小为 256。
超参数
学习率固定为 1e-5。
混合精度
使用 fp16 混合精度训练。
📄 许可证
本项目采用 OpenRAIL++ 许可证。
属性 |
详情 |
模型类型 |
SDXL-controlnet: Depth |
训练数据 |
LAION-Aesthetics V2 数据集的 300 万图像 - 文本对 |
计算资源 |
80GB A100 GPU,700 小时 |
批量大小 |
单 GPU 批量大小 8,总批量大小 256 |
学习率 |
1e-5 |
混合精度 |
fp16 |
许可证 |
OpenRAIL++ |