Skip to content

Commit 66bf7ea

Browse files
authored
feat: add Mixture-of-Diffusers ControlNet Tile upscaler Pipeline for SDXL (huggingface#10951)
* feat: add Mixture-of-Diffusers ControlNet Tile upscaler Pipeline for SDXL * make style make quality
1 parent b8215b1 commit 66bf7ea

File tree

2 files changed

+1960
-0
lines changed

2 files changed

+1960
-0
lines changed

examples/community/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Please also check out our [Community Scripts](https://github.com/huggingface/dif
5353
| Stable Diffusion Mixture Tiling Pipeline SD 1.5 | A pipeline generates cohesive images by integrating multiple diffusion processes, each focused on a specific image region and considering boundary effects for smooth blending | [Stable Diffusion Mixture Tiling Pipeline SD 1.5](#stable-diffusion-mixture-tiling-pipeline-sd-15) | [![Hugging Face Space](https://img.shields.io/badge/🤗%20Hugging%20Face-Space-yellow)](https://huggingface.co/spaces/albarji/mixture-of-diffusers) | [Álvaro B Jiménez](https://github.com/albarji/) |
5454
| Stable Diffusion Mixture Canvas Pipeline SD 1.5 | A pipeline generates cohesive images by integrating multiple diffusion processes, each focused on a specific image region and considering boundary effects for smooth blending. Works by defining a list of Text2Image region objects that detail the region of influence of each diffuser. | [Stable Diffusion Mixture Canvas Pipeline SD 1.5](#stable-diffusion-mixture-canvas-pipeline-sd-15) | [![Hugging Face Space](https://img.shields.io/badge/🤗%20Hugging%20Face-Space-yellow)](https://huggingface.co/spaces/albarji/mixture-of-diffusers) | [Álvaro B Jiménez](https://github.com/albarji/) |
5555
| Stable Diffusion Mixture Tiling Pipeline SDXL | A pipeline generates cohesive images by integrating multiple diffusion processes, each focused on a specific image region and considering boundary effects for smooth blending | [Stable Diffusion Mixture Tiling Pipeline SDXL](#stable-diffusion-mixture-tiling-pipeline-sdxl) | [![Hugging Face Space](https://img.shields.io/badge/🤗%20Hugging%20Face-Space-yellow)](https://huggingface.co/spaces/elismasilva/mixture-of-diffusers-sdxl-tiling) | [Eliseu Silva](https://github.com/DEVAIEXP/) |
56+
| Stable Diffusion MoD ControlNet Tile SR Pipeline SDXL | This is an advanced pipeline that leverages ControlNet Tile and Mixture-of-Diffusers techniques, integrating tile diffusion directly into the latent space denoising process. Designed to overcome the limitations of conventional pixel-space tile processing, this pipeline delivers Super Resolution (SR) upscaling for higher-quality images, reduced processing time, and greater adaptability. | [Stable Diffusion MoD ControlNet Tile SR Pipeline SDXL](#stable-diffusion-mod-controlnet-tile-sr-pipeline-sdxl) | [![Hugging Face Space](https://img.shields.io/badge/🤗%20Hugging%20Face-Space-yellow)](https://huggingface.co/spaces/elismasilva/mod-control-tile-upscaler-sdxl) | [Eliseu Silva](https://github.com/DEVAIEXP/) |
5657
| FABRIC - Stable Diffusion with feedback Pipeline | pipeline supports feedback from liked and disliked images | [Stable Diffusion Fabric Pipeline](#stable-diffusion-fabric-pipeline) | [Notebook](https://github.com/huggingface/notebooks/blob/main/diffusers/stable_diffusion_fabric.ipynb)| [Shauray Singh](https://shauray8.github.io/about_shauray/) |
5758
| sketch inpaint - Inpainting with non-inpaint Stable Diffusion | sketch inpaint much like in automatic1111 | [Masked Im2Im Stable Diffusion Pipeline](#stable-diffusion-masked-im2im) | - | [Anatoly Belikov](https://github.com/noskill) |
5859
| sketch inpaint xl - Inpainting with non-inpaint Stable Diffusion | sketch inpaint much like in automatic1111 | [Masked Im2Im Stable Diffusion XL Pipeline](#stable-diffusion-xl-masked-im2im) | - | [Anatoly Belikov](https://github.com/noskill) |
@@ -2630,6 +2631,103 @@ image = pipe(
26302631

26312632
![mixture_tiling_results](https://huggingface.co/datasets/elismasilva/results/resolve/main/mixture_of_diffusers_sdxl_1.png)
26322633

2634+
### Stable Diffusion MoD ControlNet Tile SR Pipeline SDXL
2635+
2636+
This pipeline implements the [MoD (Mixture-of-Diffusers)]("https://arxiv.org/pdf/2408.06072") tiled diffusion technique and combines it with SDXL's ControlNet Tile process to generate SR images.
2637+
2638+
This works better with 4x scales, but you can try adjusts parameters to higher scales.
2639+
2640+
````python
2641+
import torch
2642+
from diffusers import DiffusionPipeline, ControlNetUnionModel, AutoencoderKL, UniPCMultistepScheduler, UNet2DConditionModel
2643+
from diffusers.utils import load_image
2644+
from PIL import Image
2645+
2646+
device = "cuda"
2647+
2648+
# Initialize the models and pipeline
2649+
controlnet = ControlNetUnionModel.from_pretrained(
2650+
"brad-twinkl/controlnet-union-sdxl-1.0-promax", torch_dtype=torch.float16
2651+
).to(device=device)
2652+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16).to(device=device)
2653+
2654+
model_id = "SG161222/RealVisXL_V5.0"
2655+
pipe = DiffusionPipeline.from_pretrained(
2656+
model_id,
2657+
torch_dtype=torch.float16,
2658+
vae=vae,
2659+
controlnet=controlnet,
2660+
custom_pipeline="mod_controlnet_tile_sr_sdxl",
2661+
use_safetensors=True,
2662+
variant="fp16",
2663+
).to(device)
2664+
2665+
unet = UNet2DConditionModel.from_pretrained(model_id, subfolder="unet", variant="fp16", use_safetensors=True)
2666+
2667+
#pipe.enable_model_cpu_offload() # << Enable this if you have limited VRAM
2668+
pipe.enable_vae_tiling() # << Enable this if you have limited VRAM
2669+
pipe.enable_vae_slicing() # << Enable this if you have limited VRAM
2670+
2671+
# Set selected scheduler
2672+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
2673+
2674+
# Load image
2675+
control_image = load_image("https://huggingface.co/datasets/DEVAIEXP/assets/resolve/main/1.jpg")
2676+
original_height = control_image.height
2677+
original_width = control_image.width
2678+
print(f"Current resolution: H:{original_height} x W:{original_width}")
2679+
2680+
# Pre-upscale image for tiling
2681+
resolution = 4096
2682+
tile_gaussian_sigma = 0.3
2683+
max_tile_size = 1024 # or 1280
2684+
2685+
current_size = max(control_image.size)
2686+
scale_factor = max(2, resolution / current_size)
2687+
new_size = (int(control_image.width * scale_factor), int(control_image.height * scale_factor))
2688+
image = control_image.resize(new_size, Image.LANCZOS)
2689+
2690+
# Update target height and width
2691+
target_height = image.height
2692+
target_width = image.width
2693+
print(f"Target resolution: H:{target_height} x W:{target_width}")
2694+
2695+
# Calculate overlap size
2696+
normal_tile_overlap, border_tile_overlap = pipe.calculate_overlap(target_width, target_height)
2697+
2698+
# Set other params
2699+
tile_weighting_method = pipe.TileWeightingMethod.COSINE.value
2700+
guidance_scale = 4
2701+
num_inference_steps = 35
2702+
denoising_strenght = 0.65
2703+
controlnet_strength = 1.0
2704+
prompt = "high-quality, noise-free edges, high quality, 4k, hd, 8k"
2705+
negative_prompt = "blurry, pixelated, noisy, low resolution, artifacts, poor details"
2706+
2707+
# Image generation
2708+
generated_image = pipe(
2709+
image=image,
2710+
control_image=control_image,
2711+
control_mode=[6],
2712+
controlnet_conditioning_scale=float(controlnet_strength),
2713+
prompt=prompt,
2714+
negative_prompt=negative_prompt,
2715+
normal_tile_overlap=normal_tile_overlap,
2716+
border_tile_overlap=border_tile_overlap,
2717+
height=target_height,
2718+
width=target_width,
2719+
original_size=(original_width, original_height),
2720+
target_size=(target_width, target_height),
2721+
guidance_scale=guidance_scale,
2722+
strength=float(denoising_strenght),
2723+
tile_weighting_method=tile_weighting_method,
2724+
max_tile_size=max_tile_size,
2725+
tile_gaussian_sigma=float(tile_gaussian_sigma),
2726+
num_inference_steps=num_inference_steps,
2727+
)["images"][0]
2728+
````
2729+
![Upscaled](https://huggingface.co/datasets/DEVAIEXP/assets/resolve/main/1_input_4x.png)
2730+
26332731
### TensorRT Inpainting Stable Diffusion Pipeline
26342732

26352733
The TensorRT Pipeline can be used to accelerate the Inpainting Stable Diffusion Inference run.

0 commit comments

Comments
 (0)