akhaliq HF Staff commited on
Commit
54be9ba
·
verified ·
1 Parent(s): 6989108

Load backbone + focal estimator from the Hub (fixes truncated CDN checkpoint)

Browse files
Files changed (1) hide show
  1. app.py +16 -7
app.py CHANGED
@@ -17,17 +17,26 @@ try:
17
  except ImportError:
18
  spaces = None
19
 
20
- from depth_pro_2 import create_model_and_transforms, load_rgb, upright
21
  from depth_pro_2.model import to_normalized_tensor
 
 
 
22
 
23
  MAX_VIDEO_FRAMES = 100
24
 
25
- # Weights and focal-length estimator load at startup via the library's canonical loader
26
- # (create_model_and_transforms; from_pretrained on the per-size repo leaves the FOV estimator
27
- # unloaded, which makes infer return depth=None for photos without an EXIF focal length).
28
- # The model is created on CPU and moved to CUDA inside the GPU-decorated functions, since
29
- # ZeroGPU exposes the GPU only during those calls. bfloat16 is how the models run on a GPU.
30
- model, _ = create_model_and_transforms("depthpro2-h", device="cpu", precision=torch.bfloat16)
 
 
 
 
 
 
31
 
32
  # ColorBrewer "Spectral" stops, dark red (near) to violet (far), interpolated to a 256-entry LUT.
33
  _SPECTRAL = np.array(
 
17
  except ImportError:
18
  spaces = None
19
 
20
+ from depth_pro_2 import DepthPro2, load_rgb, upright
21
  from depth_pro_2.model import to_normalized_tensor
22
+ from depth_pro_2.network.fov import FOVEstimator
23
+ from huggingface_hub import hf_hub_download
24
+ from safetensors.torch import load_file
25
 
26
  MAX_VIDEO_FRAMES = 100
27
 
28
+ # Weights load from the Hub: the backbone from apple/DepthPro2-H (whose config has "fov": false,
29
+ # so the focal estimator is attached separately from apple/DepthPro2-focal, its own repo). The
30
+ # preset loader's CDN checkpoints are what a previous build failed on (truncated download), and
31
+ # Hub downloads resume instead of corrupting. The model is created on CPU and moved to CUDA
32
+ # inside the GPU-decorated functions, since ZeroGPU exposes the GPU only during those calls.
33
+ # bfloat16 is how the models run on a GPU.
34
+ model = DepthPro2.from_pretrained("apple/DepthPro2-H", precision=torch.bfloat16)
35
+ model.fov = FOVEstimator(precision=torch.bfloat16)
36
+ model.fov.load_state_dict(
37
+ load_file(hf_hub_download("apple/DepthPro2-focal", "model.safetensors"))
38
+ )
39
+ model.eval()
40
 
41
  # ColorBrewer "Spectral" stops, dark red (near) to violet (far), interpolated to a 256-entry LUT.
42
  _SPECTRAL = np.array(