Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
405a06a
1
Parent(s):
274c3ad
updated pipeline example and enabled cache
Browse files- app.py +1 -1
- pipeline/mod_controlnet_tile_sr_sdxl.py +75 -83
app.py
CHANGED
@@ -351,7 +351,7 @@ with gr.Blocks(css=css, theme=Platinum(), title="MoD ControlNet Tile Upscaler")
|
|
351 |
],
|
352 |
fn=predict,
|
353 |
outputs=result,
|
354 |
-
cache_examples=
|
355 |
)
|
356 |
|
357 |
max_tile_size.select(fn=set_maximum_resolution, inputs=[max_tile_size, resolution], outputs=resolution)
|
|
|
351 |
],
|
352 |
fn=predict,
|
353 |
outputs=result,
|
354 |
+
cache_examples=True,
|
355 |
)
|
356 |
|
357 |
max_tile_size.select(fn=set_maximum_resolution, inputs=[max_tile_size, resolution], outputs=resolution)
|
pipeline/mod_controlnet_tile_sr_sdxl.py
CHANGED
@@ -75,94 +75,86 @@ logger = logging.get_logger(__name__) # pylint: disable=invalid-name
|
|
75 |
EXAMPLE_DOC_STRING = """
|
76 |
Examples:
|
77 |
```py
|
78 |
-
# !pip install controlnet_aux
|
79 |
-
from diffusers import (
|
80 |
-
StableDiffusionXLControlNetUnionImg2ImgPipeline,
|
81 |
-
ControlNetUnionModel,
|
82 |
-
AutoencoderKL,
|
83 |
-
)
|
84 |
-
from diffusers.utils import load_image
|
85 |
import torch
|
|
|
|
|
|
|
86 |
from PIL import Image
|
87 |
-
import numpy as np
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/kandinsky/cat.png"
|
93 |
-
)
|
94 |
-
# initialize the models and pipeline
|
95 |
controlnet = ControlNetUnionModel.from_pretrained(
|
96 |
"brad-twinkl/controlnet-union-sdxl-1.0-promax", torch_dtype=torch.float16
|
97 |
-
)
|
98 |
-
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
vae=vae,
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
#
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
```
|
167 |
"""
|
168 |
|
|
|
75 |
EXAMPLE_DOC_STRING = """
|
76 |
Examples:
|
77 |
```py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
import torch
|
79 |
+
from diffusers import ControlNetUnionModel, AutoencoderKL, UniPCMultistepScheduler
|
80 |
+
from pipeline.mod_controlnet_tile_sr_sdxl import StableDiffusionXLControlNetTileSRPipeline, TileWeightingMethod, calculate_overlap
|
81 |
+
from diffusers.utils import load_image
|
82 |
from PIL import Image
|
|
|
83 |
|
84 |
+
device = "cuda"
|
85 |
+
|
86 |
+
# Initialize the models and pipeline
|
|
|
|
|
|
|
87 |
controlnet = ControlNetUnionModel.from_pretrained(
|
88 |
"brad-twinkl/controlnet-union-sdxl-1.0-promax", torch_dtype=torch.float16
|
89 |
+
).to(device=device)
|
90 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16).to(device=device)
|
91 |
+
|
92 |
+
model_id = "SG161222/RealVisXL_V5.0"
|
93 |
+
pipe = StableDiffusionXLControlNetTileSRPipeline.from_pretrained(
|
94 |
+
model_id, controlnet=controlnet, vae=vae, torch_dtype=torch.float16, use_safetensors=True, variant="fp16"
|
95 |
+
).to(device)
|
96 |
+
|
97 |
+
pipe.enable_model_cpu_offload() # << Enable this if you have limited VRAM
|
98 |
+
pipe.enable_vae_tiling() # << Enable this if you have limited VRAM
|
99 |
+
pipe.enable_vae_slicing() # << Enable this if you have limited VRAM
|
100 |
+
|
101 |
+
# Set selected scheduler
|
102 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
103 |
+
|
104 |
+
# Load image
|
105 |
+
control_image = load_image("https://huggingface.co/datasets/DEVAIEXP/assets/resolve/main/1.jpg")
|
106 |
+
original_height = control_image.height
|
107 |
+
original_width = control_image.width
|
108 |
+
print(f"Current resolution: H:{original_height} x W:{original_width}")
|
109 |
+
|
110 |
+
# Pre-upscale image for tiling
|
111 |
+
resolution = 4096
|
112 |
+
tile_gaussian_sigma = 0.3
|
113 |
+
max_tile_size = 1024 # or 1280
|
114 |
+
|
115 |
+
current_size = max(control_image.size)
|
116 |
+
scale_factor = max(2, resolution / current_size)
|
117 |
+
new_size = (int(control_image.width * scale_factor), int(control_image.height * scale_factor))
|
118 |
+
image = control_image.resize(new_size, Image.LANCZOS)
|
119 |
+
|
120 |
+
# Update target height and width
|
121 |
+
target_height = image.height
|
122 |
+
target_width = image.width
|
123 |
+
print(f"Target resolution: H:{target_height} x W:{target_width}")
|
124 |
+
|
125 |
+
# Calculate overlap size
|
126 |
+
normal_tile_overlap, border_tile_overlap = calculate_overlap(target_width, target_height)
|
127 |
+
|
128 |
+
# Set other params
|
129 |
+
tile_weighting_method = TileWeightingMethod.COSINE.value
|
130 |
+
guidance_scale = 4
|
131 |
+
num_inference_steps = 35
|
132 |
+
denoising_strenght = 0.65
|
133 |
+
controlnet_strength = 1.0
|
134 |
+
prompt = "high-quality, noise-free edges, high quality, 4k, hd, 8k"
|
135 |
+
negative_prompt = "blurry, pixelated, noisy, low resolution, artifacts, poor details"
|
136 |
+
|
137 |
+
# Image generation
|
138 |
+
control_image = pipe(
|
139 |
+
image=image,
|
140 |
+
control_image=control_image,
|
141 |
+
control_mode=[6],
|
142 |
+
controlnet_conditioning_scale=float(controlnet_strength),
|
143 |
+
prompt=prompt,
|
144 |
+
negative_prompt=negative_prompt,
|
145 |
+
normal_tile_overlap=normal_tile_overlap,
|
146 |
+
border_tile_overlap=border_tile_overlap,
|
147 |
+
height=target_height,
|
148 |
+
width=target_width,
|
149 |
+
original_size=(original_width, original_height),
|
150 |
+
target_size=(target_width, target_height),
|
151 |
+
guidance_scale=guidance_scale,
|
152 |
+
strength=float(denoising_strenght),
|
153 |
+
tile_weighting_method=tile_weighting_method,
|
154 |
+
max_tile_size=max_tile_size,
|
155 |
+
tile_gaussian_sigma=float(tile_gaussian_sigma),
|
156 |
+
num_inference_steps=num_inference_steps,
|
157 |
+
)["images"][0]
|
158 |
```
|
159 |
"""
|
160 |
|