🚀 SDXL-controlnet: Depth
このモデルは、StableDiffusionXLのベースモデルであるstabilityai/stable-diffusion-xl-base-1.0
を深度条件付きで学習したControlNetの重みです。以下にいくつかのサンプル画像を示します。
プロンプト: spiderman lecture, photorealistic

🚀 クイックスタート
まずは必要なライブラリをインストールしましょう。
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を用いて700GPU時間を費やしました。
バッチサイズ
データ並列を用いて、単一GPUのバッチサイズを8とし、合計バッチサイズを256としています。
ハイパーパラメータ
学習率は1e-5の定数を使用しています。
混合精度
学習にはfp16の混合精度を使用しています。
📄 ライセンス
このモデルのライセンスはOpenRail++です。