elismasilva commited on
Commit
405a06a
·
1 Parent(s): 274c3ad

updated pipeline example and enabled cache

Browse files
Files changed (2) hide show
  1. app.py +1 -1
  2. 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=False,
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
- prompt = "A cat"
90
- # download an image
91
- image = load_image(
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
- pipe = StableDiffusionXLControlNetUnionImg2ImgPipeline.from_pretrained(
100
- "stabilityai/stable-diffusion-xl-base-1.0",
101
- controlnet=controlnet,
102
- vae=vae,
103
- torch_dtype=torch.float16,
104
- variant="fp16",
105
- ).to("cuda")
106
- # `enable_model_cpu_offload` is not recommended due to multiple generations
107
- height = image.height
108
- width = image.width
109
- ratio = np.sqrt(1024.0 * 1024.0 / (width * height))
110
- # 3 * 3 upscale correspond to 16 * 3 multiply, 2 * 2 correspond to 16 * 2 multiply and so on.
111
- scale_image_factor = 3
112
- base_factor = 16
113
- factor = scale_image_factor * base_factor
114
- W, H = int(width * ratio) // factor * factor, int(height * ratio) // factor * factor
115
- image = image.resize((W, H))
116
- target_width = W // scale_image_factor
117
- target_height = H // scale_image_factor
118
- images = []
119
- crops_coords_list = [
120
- (0, 0),
121
- (0, width // 2),
122
- (height // 2, 0),
123
- (width // 2, height // 2),
124
- 0,
125
- 0,
126
- 0,
127
- 0,
128
- 0,
129
- ]
130
- for i in range(scale_image_factor):
131
- for j in range(scale_image_factor):
132
- left = j * target_width
133
- top = i * target_height
134
- right = left + target_width
135
- bottom = top + target_height
136
- cropped_image = image.crop((left, top, right, bottom))
137
- cropped_image = cropped_image.resize((W, H))
138
- images.append(cropped_image)
139
- # set ControlNetUnion input
140
- result_images = []
141
- for sub_img, crops_coords in zip(images, crops_coords_list):
142
- new_width, new_height = W, H
143
- out = pipe(
144
- prompt=[prompt] * 1,
145
- image=sub_img,
146
- control_image=[sub_img],
147
- control_mode=[6],
148
- width=new_width,
149
- height=new_height,
150
- num_inference_steps=30,
151
- crops_coords_top_left=(W, H),
152
- target_size=(W, H),
153
- original_size=(W * 2, H * 2),
154
- )
155
- result_images.append(out.images[0])
156
- new_im = Image.new("RGB", (new_width * scale_image_factor, new_height * scale_image_factor))
157
- new_im.paste(result_images[0], (0, 0))
158
- new_im.paste(result_images[1], (new_width, 0))
159
- new_im.paste(result_images[2], (new_width * 2, 0))
160
- new_im.paste(result_images[3], (0, new_height))
161
- new_im.paste(result_images[4], (new_width, new_height))
162
- new_im.paste(result_images[5], (new_width * 2, new_height))
163
- new_im.paste(result_images[6], (0, new_height * 2))
164
- new_im.paste(result_images[7], (new_width, new_height * 2))
165
- new_im.paste(result_images[8], (new_width * 2, new_height * 2))
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