akhaliq HF Staff commited on
Commit
84501a5
·
verified ·
1 Parent(s): e280219

Load via create_model_and_transforms so the focal estimator is included (fixes depth=None)

Browse files
Files changed (1) hide show
  1. app.py +12 -7
app.py CHANGED
@@ -15,16 +15,18 @@ try:
15
  except ImportError:
16
  spaces = None
17
 
18
- from depth_pro_2 import DepthPro2, load_rgb, upright
19
  from depth_pro_2.model import to_normalized_tensor
20
 
21
- MODEL_ID = "apple/DepthPro2-H"
22
  MAX_VIDEO_FRAMES = 100
23
 
24
- # The weights load at startup; they are moved to CUDA inside the GPU-decorated
25
- # functions, since ZeroGPU exposes the GPU only during those calls.
26
- model = DepthPro2.from_pretrained(
27
- MODEL_ID, precision=torch.bfloat16 if torch.cuda.is_available() else torch.float32
 
 
 
28
  )
29
 
30
 
@@ -76,7 +78,10 @@ def run_image(image: Image.Image) -> Image.Image:
76
  image.save(path)
77
  img, f_px = load_rgb(path) # f_px: the focal length recorded in the photo, or None
78
  net, transform = _device_and_model()
79
- depth = net.infer(transform(img), f_px=f_px)["depth"] # meters, (H, W)
 
 
 
80
  return Image.fromarray(_colorize(depth.float().cpu().numpy()))
81
 
82
 
 
15
  except ImportError:
16
  spaces = None
17
 
18
+ from depth_pro_2 import create_model_and_transforms, load_rgb, upright
19
  from depth_pro_2.model import to_normalized_tensor
20
 
 
21
  MAX_VIDEO_FRAMES = 100
22
 
23
+ # The weights and the focal-length estimator load at startup via the library's canonical
24
+ # loader (from_pretrained on the per-size repo leaves the FOV estimator unloaded, which makes
25
+ # infer return depth=None for photos without an EXIF focal length). The model is created on CPU
26
+ # and moved to CUDA inside the GPU-decorated functions, since ZeroGPU exposes the GPU only
27
+ # during those calls. bfloat16 is how the models run on a GPU.
28
+ model, _ = create_model_and_transforms(
29
+ "depthpro2-h", device="cpu", precision=torch.bfloat16
30
  )
31
 
32
 
 
78
  image.save(path)
79
  img, f_px = load_rgb(path) # f_px: the focal length recorded in the photo, or None
80
  net, transform = _device_and_model()
81
+ prediction = net.infer(transform(img), f_px=f_px)
82
+ if prediction["depth"] is None:
83
+ raise gr.Error("No focal length recorded in the photo and none could be estimated.")
84
+ depth = prediction["depth"] # meters, (H, W)
85
  return Image.fromarray(_colorize(depth.float().cpu().numpy()))
86
 
87