Zhenyu Li commited on
Commit
3555a61
β€’
1 Parent(s): 379bdb1
app.py CHANGED
@@ -197,7 +197,7 @@ def rescale(A, lbound=-1, ubound=1):
197
  A_max = np.max(A)
198
  return (ubound - lbound) * (A - A_min) / (A_max - A_min) + lbound
199
 
200
- def process(input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, guess_mode, strength, scale, seed, eta, mode, patch_number, resolution, patch_size):
201
  with torch.no_grad():
202
  w, h = input_image.size
203
 
@@ -212,8 +212,11 @@ def process(input_image, prompt, a_prompt, n_prompt, num_samples, image_resoluti
212
  gc.collect()
213
  torch.cuda.empty_cache()
214
 
215
- # colored_depth = colorize_depth_maps(detected_map) * 255
216
- colored_depth = colorize(detected_map)
 
 
 
217
  detected_map = F.interpolate(torch.from_numpy(detected_map).unsqueeze(dim=0).unsqueeze(dim=0), (image_resolution, image_resolution), mode='bicubic', align_corners=True).squeeze().numpy()
218
 
219
  H, W = detected_map.shape
@@ -282,11 +285,15 @@ Please refer to our [project webpage](https://zhyever.github.io/patchfusion), [p
282
 
283
  The overall pipeline: image --> (PatchFusion) --> depth --> (controlnet) --> generated image.
284
 
285
- As for the PatchFusion, it works on default 4k (2160x3840) resolution. All input images will be resized to 4k before passing through PatchFusion as default. It means if you have a higher resolution image, you might want to increase the processing resolution in the advanced option (You would also change the patch size to 1/4 image resolution). Because of the tiling strategy, our PatchFusion would not use more memory or time for even higher resolution inputs if properly setting parameters. The output depth map is resized to the original image resolution. Download for better visualization quality. 16-Bit Raw Depth = (pred_depth * 256).to(uint16).
 
 
 
 
286
 
287
  For ControlNet, it works on default 896x896 resolution. Again, all input images will be resized to 896x896 before passing through ControlNet as default. You might be not happy because the 4K->896x896 downsampling, but limited by the GPU resource, this demo could only achieve this. This is the memory bottleneck. The output is not resized back to the image resolution for fast inference (Well... It's still so slow now... :D).
288
 
289
- We provide some tips might be helpful: (1) Try our experimental demo (see our project website) running on a local 80G gpu (you could try high-resolution generation there, like the one in our paper). But of course, it would be expired soon (in two days maybe); (2) Clone our code repo, and look for a gpu with more than 24G memory; (3) Clone our code repo, run the depth estimation (there are another demos for depth estimation and image-to-3D), and search for another guided high-resolution image generation strategy; (4) Some kind people give this space a stronger gpu support.
290
  """
291
 
292
  with gr.Blocks() as demo:
@@ -295,27 +302,15 @@ with gr.Blocks() as demo:
295
 
296
  with gr.Row():
297
  gr.Markdown("## Control Stable Diffusion with Depth Maps")
298
- with gr.Row():
299
- with gr.Column():
300
- # input_image = gr.Image(source='upload', type="pil")
301
- input_image = gr.Image(label="Input Image", type='pil')
302
- prompt = gr.Textbox(label="Prompt (input your description)", value='A cozy cottage in an oil painting, with rich textures and vibrant green foliage')
303
- run_button = gr.Button("Run")
304
-
305
- generated_image = gr.Image(label="Generated Map", elem_id='img-display-output')
306
-
307
- with gr.Row():
308
- depth_image = gr.Image(label="Depth Map", elem_id='img-display-output')
309
- with gr.Row():
310
- raw_file = gr.File(label="16-Bit Raw Depth, Multiplier:256")
311
-
312
  with gr.Row():
313
  with gr.Accordion("Advanced options", open=False):
314
- # mode = gr.Radio(["P49", "R"], label="Tiling mode", info="We recommand using P49 for fast evaluation and R with 1024 patches for best visualization results, respectively", elem_id='mode', value='R'),
315
- mode = gr.Radio(["P49", "R"], label="Tiling mode", info="We recommand using P49 for fast evaluation and R with 1024 patches for best visualization results, respectively", elem_id='mode', value='P49'),
316
  patch_number = gr.Slider(1, 1024, label="Please decide the number of random patches (Only useful in mode=R)", step=1, value=256)
317
  resolution = gr.Textbox(label="(PatchFusion) Proccessing resolution (Default 4K. Use 'x' to split height and width.)", elem_id='mode', value='2160x3840')
318
  patch_size = gr.Textbox(label="(PatchFusion) Patch size (Default 1/4 of image resolution. Use 'x' to split height and width.)", elem_id='mode', value='540x960')
 
319
 
320
  num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1)
321
  image_resolution = gr.Slider(label="ControlNet image resolution (higher resolution will lead to OOM)", minimum=256, maximum=1024, value=896, step=64)
@@ -329,7 +324,21 @@ with gr.Blocks() as demo:
329
  a_prompt = gr.Textbox(label="Added prompt", value='best quality, extremely detailed')
330
  n_prompt = gr.Textbox(label="Negative prompt", value='worst quality, low quality, lose details')
331
 
332
- ips = [input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, guess_mode, strength, scale, seed, eta, mode[0], patch_number, resolution, patch_size]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
  run_button.click(fn=process, inputs=ips, outputs=[depth_image, generated_image, raw_file])
334
  examples = gr.Examples(
335
  inputs=[input_image, depth_image, generated_image],
@@ -347,7 +356,7 @@ with gr.Blocks() as demo:
347
  "examples/4_gen.png",
348
  ],
349
  [
350
- "examples/example_3.jpeg",
351
  "examples/1_depth.png",
352
  "examples/1_gen.png",
353
  ],],
 
197
  A_max = np.max(A)
198
  return (ubound - lbound) * (A - A_min) / (A_max - A_min) + lbound
199
 
200
+ def process(input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, guess_mode, strength, scale, seed, eta, mode, patch_number, resolution, patch_size, color_map):
201
  with torch.no_grad():
202
  w, h = input_image.size
203
 
 
212
  gc.collect()
213
  torch.cuda.empty_cache()
214
 
215
+ if color_map == 'magma':
216
+ colored_depth = colorize(detected_map)
217
+ else:
218
+ colored_depth = colorize_depth_maps(detected_map) * 255
219
+
220
  detected_map = F.interpolate(torch.from_numpy(detected_map).unsqueeze(dim=0).unsqueeze(dim=0), (image_resolution, image_resolution), mode='bicubic', align_corners=True).squeeze().numpy()
221
 
222
  H, W = detected_map.shape
 
285
 
286
  The overall pipeline: image --> (PatchFusion) --> depth --> (controlnet) --> generated image.
287
 
288
+ As for the PatchFusion, it works on default 4k (2160x3840) resolution. All input images will be resized to 4k before passing through PatchFusion as default. It means if you have a higher resolution image, you might want to increase the processing resolution in the advanced option (You would also change the patch size to 1/4 image resolution). Because of the tiling strategy, our PatchFusion would not use more memory or time for even higher resolution inputs if properly setting parameters.
289
+
290
+ The output depth map is resized to the original image resolution. Download for better visualization quality. 16-Bit Raw Depth = (pred_depth * 256).to(uint16).
291
+
292
+ We provide two color maps to render depth map, which are magma (more common in supervised depth estimation) and spectral (better looking). Please choose from the advanced option.
293
 
294
  For ControlNet, it works on default 896x896 resolution. Again, all input images will be resized to 896x896 before passing through ControlNet as default. You might be not happy because the 4K->896x896 downsampling, but limited by the GPU resource, this demo could only achieve this. This is the memory bottleneck. The output is not resized back to the image resolution for fast inference (Well... It's still so slow now... :D).
295
 
296
+ We provide some tips might be helpful: (1) Try our experimental demo (check our github) running on a local 80G gpu (you could try high-resolution generation there, like the one in our paper). But of course, it would be expired soon (in two days maybe); (2) Clone our code repo, and look for a gpu with more than 24G memory; (3) Clone our code repo, run the depth estimation (there are another demos for depth estimation and image-to-3D), and search for another guided high-resolution image generation strategy; (4) Some kind people give this space a stronger gpu support.
297
  """
298
 
299
  with gr.Blocks() as demo:
 
302
 
303
  with gr.Row():
304
  gr.Markdown("## Control Stable Diffusion with Depth Maps")
305
+
 
 
 
 
 
 
 
 
 
 
 
 
 
306
  with gr.Row():
307
  with gr.Accordion("Advanced options", open=False):
308
+ # mode = gr.Radio(["P49", "R"], label="Tiling mode", info="We recommand using P49 for fast evaluation and R with 1024 patches for best visualization results, respectively", elem_id='mode', value='R')
309
+ mode = gr.Radio(["P49", "R"], label="Tiling mode", info="We recommand using P49 for fast evaluation and R with 1024 patches for best visualization results, respectively", elem_id='mode', value='P49')
310
  patch_number = gr.Slider(1, 1024, label="Please decide the number of random patches (Only useful in mode=R)", step=1, value=256)
311
  resolution = gr.Textbox(label="(PatchFusion) Proccessing resolution (Default 4K. Use 'x' to split height and width.)", elem_id='mode', value='2160x3840')
312
  patch_size = gr.Textbox(label="(PatchFusion) Patch size (Default 1/4 of image resolution. Use 'x' to split height and width.)", elem_id='mode', value='540x960')
313
+ color_map = gr.Radio(["magma", "spectral"], label="Colormap used to render depth map", elem_id='mode', value='magma')
314
 
315
  num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1)
316
  image_resolution = gr.Slider(label="ControlNet image resolution (higher resolution will lead to OOM)", minimum=256, maximum=1024, value=896, step=64)
 
324
  a_prompt = gr.Textbox(label="Added prompt", value='best quality, extremely detailed')
325
  n_prompt = gr.Textbox(label="Negative prompt", value='worst quality, low quality, lose details')
326
 
327
+ with gr.Row():
328
+ with gr.Column():
329
+ # input_image = gr.Image(source='upload', type="pil")
330
+ input_image = gr.Image(label="Input Image", type='pil')
331
+ prompt = gr.Textbox(label="Prompt (input your description)", value='A cozy cottage in an oil painting, with rich textures and vibrant green foliage')
332
+ run_button = gr.Button("Run")
333
+
334
+ generated_image = gr.Image(label="Generated Map", elem_id='img-display-output')
335
+
336
+ with gr.Row():
337
+ depth_image = gr.Image(label="Depth Map", elem_id='img-display-output')
338
+ with gr.Row():
339
+ raw_file = gr.File(label="16-Bit Raw Depth, Multiplier:256")
340
+
341
+ ips = [input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, guess_mode, strength, scale, seed, eta, mode, patch_number, resolution, patch_size, color_map]
342
  run_button.click(fn=process, inputs=ips, outputs=[depth_image, generated_image, raw_file])
343
  examples = gr.Examples(
344
  inputs=[input_image, depth_image, generated_image],
 
356
  "examples/4_gen.png",
357
  ],
358
  [
359
+ "examples/example_1.jpeg",
360
  "examples/1_depth.png",
361
  "examples/1_gen.png",
362
  ],],
examples/1_depth.png CHANGED

Git LFS Details

  • SHA256: 754674d1092256c97e46cb031176885f3c18004231775727e968c063fe3dfd06
  • Pointer size: 132 Bytes
  • Size of remote file: 8.96 MB

Git LFS Details

  • SHA256: c6f3f5b9ae0ae7c01ae16ce445288a8fbde163295103fcdd661b2278c73c45fb
  • Pointer size: 132 Bytes
  • Size of remote file: 1.06 MB
examples/1_gen.png CHANGED

Git LFS Details

  • SHA256: a9fc5dde56d0c6da3005fdb7af4e38d804469004973979e4217ae135953b005d
  • Pointer size: 132 Bytes
  • Size of remote file: 1.88 MB

Git LFS Details

  • SHA256: da6b1e5c98e35135dcc270dc2f0c6e82b71921833fde6713c8ccce88235b3ec5
  • Pointer size: 132 Bytes
  • Size of remote file: 1.98 MB
examples/3_gen.png DELETED

Git LFS Details

  • SHA256: 5d1f56c5006fe8584e064e12e98fd61ea6cdfadbbc5786e4d24b45e07e826579
  • Pointer size: 132 Bytes
  • Size of remote file: 1.12 MB
examples/{3_depth.png β†’ example_1.jpeg} RENAMED
File without changes
examples/example_5.jpeg DELETED

Git LFS Details

  • SHA256: e788f6b66bc31926e5ce908e0e35060a60013a105d40043f3548427123c5601f
  • Pointer size: 132 Bytes
  • Size of remote file: 1.61 MB