AI Agent commited on
Commit
b90ddcd
·
1 Parent(s): 240b539

CRITICAL FIX: Remove redundant checkpoint loading that was corrupting model weights. build_sam3_image_model() already loads them correctly

Browse files
Files changed (1) hide show
  1. app.py +3 -27
app.py CHANGED
@@ -86,33 +86,9 @@ processor = None
86
  if model_installed:
87
  print(f"Loading SAM 3 onto {device}...")
88
  model = build_sam3_image_model()
89
- # ── SYSTEM RAM OOM PROTECTION ────────────────────────────────────
90
- # HF Spaces strictly enforce a 16GB System RAM limit. Loading 6.9GB models
91
- # out of nested dictionary checkpoints will spike RAM past 20GB during copies!
92
- # Aggressively force Python to garbage collect unpinned tensors instantly.
93
- import gc
94
-
95
- state_dict = torch.load(ckpt_path, map_location="cpu", weights_only=True)
96
- state_dict_inner = state_dict.pop("model") if "model" in state_dict else state_dict
97
-
98
- image_state_dict = {}
99
- for k, v in state_dict_inner.items():
100
- if k.startswith("detector."):
101
- image_state_dict[k.replace("detector.", "", 1)] = v
102
- else:
103
- image_state_dict[k] = v
104
-
105
- # Purge the original 3.45GB raw checkpoint instantly
106
- del state_dict
107
- del state_dict_inner
108
- gc.collect()
109
-
110
- model.load_state_dict(image_state_dict, strict=False)
111
-
112
- # Purge the 3.45GB filtered checkpoint array instantly
113
- del image_state_dict
114
- gc.collect()
115
-
116
  model.to(device)
117
  model.to(torch.float32) # Lossless 32-bit execution prevents BFloat16 exponent overflows (NaNs)
118
 
 
86
  if model_installed:
87
  print(f"Loading SAM 3 onto {device}...")
88
  model = build_sam3_image_model()
89
+ # NOTE: build_sam3_image_model() already loads checkpoint weights internally
90
+ # via _load_checkpoint(). DO NOT load the checkpoint again manually—it corrupts
91
+ # the weights with incorrect key mappings and causes zero masks!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  model.to(device)
93
  model.to(torch.float32) # Lossless 32-bit execution prevents BFloat16 exponent overflows (NaNs)
94