codic commited on
Commit
6d42413
·
verified ·
1 Parent(s): f819620

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -13
app.py CHANGED
@@ -22,35 +22,44 @@ else:
22
  print("Using ZeroGPU")
23
  torch.cuda.empty_cache()
24
 
25
- # Load the pre-trained model (Marigold Depth Prediction with fp16 support)
26
  pipe = diffusers.MarigoldDepthPipeline.from_pretrained(
27
  "prs-eth/marigold-depth-lcm-v1-0", variant="fp16", torch_dtype=torch.float16
28
  ).to(device)
29
 
 
 
 
 
 
 
 
 
 
30
  @spaces.GPU()
31
  def predict_depth(image: Image.Image):
32
- # Prepare image for processing
33
- image = image.convert("RGB") # Ensure image is in RGB format
34
 
35
- # Define ensemble size and inference steps for improved precision
36
- ensemble_size = 10
37
- num_inference_steps = 50
38
 
39
- # Perform depth prediction with ensemble and uncertainty output
40
  depth = pipe(image, ensemble_size=ensemble_size, num_inference_steps=num_inference_steps, output_uncertainty=True)
41
 
42
- # Visualize the depth and uncertainty
43
  vis = pipe.image_processor.visualize_depth(depth.prediction)
44
  uncertainty = pipe.image_processor.visualize_uncertainty(depth.uncertainty)
45
 
46
- # Convert to images
47
  depth_image = vis[0]
48
  uncertainty_image = uncertainty[0]
49
 
50
- # Save or return the images as needed
51
  return depth_image, uncertainty_image
52
 
53
- # Create a Gradio interface for the depth prediction
54
  iface = gr.Interface(
55
  fn=predict_depth,
56
  inputs=gr.Image(type="pil", label="Upload Image or Provide URL"),
@@ -59,8 +68,8 @@ iface = gr.Interface(
59
  gr.Image(type="pil", label="Uncertainty Map")
60
  ],
61
  title="Depth Prediction with Marigold",
62
- description="Upload an image or provide a URL to generate its depth map and uncertainty visualization using the Marigold Depth model. The prediction includes a depth map and an uncertainty map showing model confidence."
63
  )
64
 
65
- # Launch the interface
66
  iface.launch(debug=True, show_api=True, share=True)
 
22
  print("Using ZeroGPU")
23
  torch.cuda.empty_cache()
24
 
25
+ # Load the pre-trained Marigold Depth Prediction model (fp16 support for faster computations)
26
  pipe = diffusers.MarigoldDepthPipeline.from_pretrained(
27
  "prs-eth/marigold-depth-lcm-v1-0", variant="fp16", torch_dtype=torch.float16
28
  ).to(device)
29
 
30
+ # Optimizations:
31
+ # 1. Compile the UNet model for performance improvement
32
+ pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
33
+
34
+ # 2. Load a custom VAE for potentially better results
35
+ pipe.vae = diffusers.AutoencoderTiny.from_pretrained(
36
+ "madebyollin/taesd", torch_dtype=torch.float16
37
+ ).cuda()
38
+
39
  @spaces.GPU()
40
  def predict_depth(image: Image.Image):
41
+ # Ensure the image is in RGB format
42
+ image = image.convert("RGB")
43
 
44
+ # Define ensemble size, inference steps, and output uncertainty for improved precision
45
+ ensemble_size = 10 # Higher ensemble size for more accurate predictions
46
+ num_inference_steps = 50 # Increased number of inference steps for better depth estimation
47
 
48
+ # Perform depth prediction with the configured parameters
49
  depth = pipe(image, ensemble_size=ensemble_size, num_inference_steps=num_inference_steps, output_uncertainty=True)
50
 
51
+ # Visualize depth and uncertainty maps
52
  vis = pipe.image_processor.visualize_depth(depth.prediction)
53
  uncertainty = pipe.image_processor.visualize_uncertainty(depth.uncertainty)
54
 
55
+ # Convert depth and uncertainty maps to images
56
  depth_image = vis[0]
57
  uncertainty_image = uncertainty[0]
58
 
59
+ # Return both depth and uncertainty images
60
  return depth_image, uncertainty_image
61
 
62
+ # Create Gradio interface for depth prediction
63
  iface = gr.Interface(
64
  fn=predict_depth,
65
  inputs=gr.Image(type="pil", label="Upload Image or Provide URL"),
 
68
  gr.Image(type="pil", label="Uncertainty Map")
69
  ],
70
  title="Depth Prediction with Marigold",
71
+ description="Upload an image or provide a URL to generate its depth map and uncertainty visualization using the Marigold Depth model. The prediction includes both a depth map and an uncertainty map showing the model's confidence."
72
  )
73
 
74
+ # Launch the Gradio interface
75
  iface.launch(debug=True, show_api=True, share=True)