Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
---
|
| 3 |
+
license: openrail++
|
| 4 |
+
base_model: stabilityai/stable-diffusion-xl-base-1.0
|
| 5 |
+
tags:
|
| 6 |
+
- stable-diffusion
|
| 7 |
+
- stable-diffusion-diffusers
|
| 8 |
+
- text-to-image
|
| 9 |
+
- diffusers
|
| 10 |
+
- controlnet
|
| 11 |
+
inference: false
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# SDXL-controlnet: Depth
|
| 15 |
+
|
| 16 |
+
These 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 |
+
|
| 18 |
+
## Usage
|
| 19 |
+
|
| 20 |
+
Make sure to first install the libraries:
|
| 21 |
+
|
| 22 |
+
```bash
|
| 23 |
+
pip install accelerate transformers safetensors opencv-python diffusers
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
And then we're ready to go:
|
| 27 |
+
|
| 28 |
+
```python
|
| 29 |
+
import torch
|
| 30 |
+
import numpy as np
|
| 31 |
+
from PIL import Image
|
| 32 |
+
|
| 33 |
+
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
| 34 |
+
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
|
| 35 |
+
from diffusers.utils import load_image
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
depth_estimator = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas").to("cuda")
|
| 39 |
+
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-hybrid-midas")
|
| 40 |
+
controlnet = ControlNetModel.from_pretrained(
|
| 41 |
+
"diffusers/controlnet-depth-sdxl-1.0",
|
| 42 |
+
variant="fp16",
|
| 43 |
+
use_safetensors=True,
|
| 44 |
+
torch_dtype=torch.float16,
|
| 45 |
+
).to("cuda")
|
| 46 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16).to("cuda")
|
| 47 |
+
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
| 48 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 49 |
+
controlnet=controlnet,
|
| 50 |
+
vae=vae,
|
| 51 |
+
variant="fp16",
|
| 52 |
+
use_safetensors=True,
|
| 53 |
+
torch_dtype=torch.float16,
|
| 54 |
+
).to("cuda")
|
| 55 |
+
pipe.enable_model_cpu_offload()
|
| 56 |
+
|
| 57 |
+
def get_depth_map(image):
|
| 58 |
+
image = feature_extractor(images=image, return_tensors="pt").pixel_values.to("cuda")
|
| 59 |
+
with torch.no_grad(), torch.autocast("cuda"):
|
| 60 |
+
depth_map = depth_estimator(image).predicted_depth
|
| 61 |
+
|
| 62 |
+
depth_map = torch.nn.functional.interpolate(
|
| 63 |
+
depth_map.unsqueeze(1),
|
| 64 |
+
size=(1024, 1024),
|
| 65 |
+
mode="bicubic",
|
| 66 |
+
align_corners=False,
|
| 67 |
+
)
|
| 68 |
+
depth_min = torch.amin(depth_map, dim=[1, 2, 3], keepdim=True)
|
| 69 |
+
depth_max = torch.amax(depth_map, dim=[1, 2, 3], keepdim=True)
|
| 70 |
+
depth_map = (depth_map - depth_min) / (depth_max - depth_min)
|
| 71 |
+
image = torch.cat([depth_map] * 3, dim=1)
|
| 72 |
+
|
| 73 |
+
image = image.permute(0, 2, 3, 1).cpu().numpy()[0]
|
| 74 |
+
image = Image.fromarray((image * 255.0).clip(0, 255).astype(np.uint8))
|
| 75 |
+
return image
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
prompt = "stormtrooper lecture, photorealistic"
|
| 79 |
+
image = load_image("https://huggingface.co/lllyasviel/sd-controlnet-depth/resolve/main/images/stormtrooper.png")
|
| 80 |
+
controlnet_conditioning_scale = 0.5 # recommended for good generalization
|
| 81 |
+
|
| 82 |
+
depth_image = get_depth_map(image)
|
| 83 |
+
|
| 84 |
+
images = pipe(
|
| 85 |
+
prompt, image=depth_image, num_inference_steps=30, controlnet_conditioning_scale=controlnet_conditioning_scale,
|
| 86 |
+
).images
|
| 87 |
+
images[0]
|
| 88 |
+
|
| 89 |
+
images[0].save(f"stormtrooper.png")
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
To more details, check out the official documentation of [`StableDiffusionXLControlNetPipeline`](https://huggingface.co/docs/diffusers/main/en/api/pipelines/controlnet_sdxl).
|
| 93 |
+
|
| 94 |
+
### Training
|
| 95 |
+
|
| 96 |
+
Our 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).
|
| 97 |
+
|
| 98 |
+
#### Training data and Compute
|
| 99 |
+
The model is trained on 3M image-text pairs from LAION-Aesthetics V2. The model is trained for 700 GPU hours on 80GB A100 GPUs.
|
| 100 |
+
|
| 101 |
+
#### Batch size
|
| 102 |
+
Data parallel with a single gpu batch size of 8 for a total batch size of 256.
|
| 103 |
+
|
| 104 |
+
#### Hyper Parameters
|
| 105 |
+
Constant learning rate of 1e-5.
|
| 106 |
+
|
| 107 |
+
#### Mixed precision
|
| 108 |
+
fp16
|