diffusers/controlnet-depth-sdxl-1.0
21038k
1 2---3license: openrail++4base_model: stabilityai/stable-diffusion-xl-base-1.05tags:6- stable-diffusion-xl7- stable-diffusion-xl-diffusers8- text-to-image9- diffusers10- controlnet11inference: false12---13 14# SDXL-controlnet: Depth15 16These are controlnet weights trained on stabilityai/stable-diffusion-xl-base-1.0 with depth conditioning. You can find some example images in the following. 17 18prompt: spiderman lecture, photorealistic1920 21## Usage22 23Make sure to first install the libraries:24 25```bash26pip install accelerate transformers safetensors diffusers27```28 29And then we're ready to go:30 31```python32import torch33import numpy as np34from PIL import Image35 36from transformers import DPTFeatureExtractor, DPTForDepthEstimation37from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL38from diffusers.utils import load_image39 40 41depth_estimator = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to("cuda")42feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-hybrid-midas")43controlnet = ControlNetModel.from_pretrained(44 "diffusers/controlnet-depth-sdxl-1.0",45 variant="fp16",46 use_safetensors=True,47 torch_dtype=torch.float16,48)49vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)50pipe = StableDiffusionXLControlNetPipeline.from_pretrained(51 "stabilityai/stable-diffusion-xl-base-1.0",52 controlnet=controlnet,53 vae=vae,54 variant="fp16",55 use_safetensors=True,56 torch_dtype=torch.float16,57)58pipe.enable_model_cpu_offload()59 60def get_depth_map(image):61 image = feature_extractor(images=image, return_tensors="pt").pixel_values.to("cuda")62 with torch.no_grad(), torch.autocast("cuda"):63 depth_map = depth_estimator(image).predicted_depth64 65 depth_map = torch.nn.functional.interpolate(66 depth_map.unsqueeze(1),67 size=(1024, 1024),68 mode="bicubic",69 align_corners=False,70 )71 depth_min = torch.amin(depth_map, dim=[1, 2, 3], keepdim=True)72 depth_max = torch.amax(depth_map, dim=[1, 2, 3], keepdim=True)73 depth_map = (depth_map - depth_min) / (depth_max - depth_min)74 image = torch.cat([depth_map] * 3, dim=1)75 76 image = image.permute(0, 2, 3, 1).cpu().numpy()[0]77 image = Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))78 return image79 80 81prompt = "stormtrooper lecture, photorealistic"82image = load_image("https://huggingface.co/lllyasviel/sd-controlnet-depth/resolve/main/images/stormtrooper.png")83controlnet_conditioning_scale = 0.5 # recommended for good generalization84 85depth_image = get_depth_map(image)86 87images = pipe(88 prompt, image=depth_image, num_inference_steps=30, controlnet_conditioning_scale=controlnet_conditioning_scale,89).images90images[0]91 92images[0].save(f"stormtrooper.png")93```94 95For more details, check out the official documentation of [`StableDiffusionXLControlNetPipeline`](https://huggingface.co/docs/diffusers/main/en/api/pipelines/controlnet_sdxl).96 97### Training98 99Our training script was built on top of the official training script that we provide [here](https://github.com/huggingface/diffusers/blob/main/examples/controlnet/README_sdxl.md). 100 101#### Training data and Compute102The model is trained on 3M image-text pairs from LAION-Aesthetics V2. The model is trained for 700 GPU hours on 80GB A100 GPUs.103 104#### Batch size105Data parallel with a single GPU batch size of 8 for a total batch size of 256.106 107#### Hyper Parameters108The constant learning rate of 1e-5.109 110#### Mixed precision111fp16