jboth commited on
Commit
84293b6
·
verified ·
1 Parent(s): 09ceeeb

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +13 -5
app.py CHANGED
@@ -176,7 +176,7 @@ def diagnose():
176
  lines.append(f"config: {Path(CONFIG_PATH).exists()}")
177
  return "\n".join(lines)
178
 
179
- @spaces.GPU(duration=300)
180
  def reconstruct_objects(image: np.ndarray):
181
  if image is None:
182
  return None, None, "No image"
@@ -186,8 +186,9 @@ def reconstruct_objects(image: np.ndarray):
186
  print(f"GPU: {torch.cuda.get_device_name()}")
187
 
188
  from sam2.automatic_mask_generator import SAM2AutomaticMaskGenerator
189
- sam2_gen = SAM2AutomaticMaskGenerator.from_pretrained("facebook/sam2-hiera-large")
190
- print(f" SAM2 loaded ({time.time()-t0:.0f}s)")
 
191
 
192
  image_np = np.array(image) if not isinstance(image, np.ndarray) else image
193
  masks = sam2_gen.generate(image_np)
@@ -200,12 +201,19 @@ def reconstruct_objects(image: np.ndarray):
200
  preview[best_mask] = (preview[best_mask] * 0.5 + np.array([0, 255, 0]) * 0.5).astype(np.uint8)
201
  print(f" {len(masks)} masks ({time.time()-t0:.0f}s)")
202
 
 
 
 
 
 
203
  from inference import Inference
 
204
  sam3d = Inference(CONFIG_PATH, compile=False)
205
- print(f" SAM3D loaded ({time.time()-t0:.0f}s)")
206
 
 
207
  result = sam3d(image=image_np, mask=best_mask, seed=42)
208
- print(f" Reconstructed ({time.time()-t0:.0f}s)")
209
  if result is None:
210
  return None, preview, "Reconstruction returned None"
211
 
 
176
  lines.append(f"config: {Path(CONFIG_PATH).exists()}")
177
  return "\n".join(lines)
178
 
179
+ @spaces.GPU(duration=600)
180
  def reconstruct_objects(image: np.ndarray):
181
  if image is None:
182
  return None, None, "No image"
 
186
  print(f"GPU: {torch.cuda.get_device_name()}")
187
 
188
  from sam2.automatic_mask_generator import SAM2AutomaticMaskGenerator
189
+ print(f" Loading SAM2... (VRAM: {torch.cuda.memory_allocated()/1e9:.1f}GB)")
190
+ sam2_gen = SAM2AutomaticMaskGenerator.from_pretrained("facebook/sam2-hiera-small")
191
+ print(f" SAM2 loaded ({time.time()-t0:.0f}s, VRAM: {torch.cuda.memory_allocated()/1e9:.1f}GB)")
192
 
193
  image_np = np.array(image) if not isinstance(image, np.ndarray) else image
194
  masks = sam2_gen.generate(image_np)
 
201
  preview[best_mask] = (preview[best_mask] * 0.5 + np.array([0, 255, 0]) * 0.5).astype(np.uint8)
202
  print(f" {len(masks)} masks ({time.time()-t0:.0f}s)")
203
 
204
+ # Free SAM2 to save VRAM for SAM3D
205
+ del sam2_gen
206
+ torch.cuda.empty_cache()
207
+ print(f" SAM2 freed (VRAM: {torch.cuda.memory_allocated()/1e9:.1f}GB)")
208
+
209
  from inference import Inference
210
+ print(f" Loading SAM3D... (VRAM: {torch.cuda.memory_allocated()/1e9:.1f}GB)")
211
  sam3d = Inference(CONFIG_PATH, compile=False)
212
+ print(f" SAM3D loaded ({time.time()-t0:.0f}s, VRAM: {torch.cuda.memory_allocated()/1e9:.1f}GB)")
213
 
214
+ print(f" Running reconstruction... (VRAM: {torch.cuda.memory_allocated()/1e9:.1f}GB)")
215
  result = sam3d(image=image_np, mask=best_mask, seed=42)
216
+ print(f" Reconstructed ({time.time()-t0:.0f}s, VRAM: {torch.cuda.memory_allocated()/1e9:.1f}GB)")
217
  if result is None:
218
  return None, preview, "Reconstruction returned None"
219