Spaces:
Running on Zero
Running on Zero
DASH Gaming commited on
Commit ·
0e5e60c
1
Parent(s): d53b7e1
Add bfloat16 autocast and resize check to SAM3 for performance
Browse files129 unmodified lines13013113213313413513613713813914014114233 unmodified lines176177178179180181129 unmodified lines# -- Step 1: SAM3 garment segmentation --t1 = time.time()device = "cuda" if torch.cuda.is_available() else "cpu"if _sam3_model is not None and _sam3_processor is not None:_sam3_model.to(device)with torch.inference_mode():inference_state = _sam3_processor.set_image(img_pil)output = _sam3_processor.set_text_prompt(state=inference_state, prompt=prompt_text)masks = output["masks"]scores = output["scores"]33 unmodified lines# Ensure shape is (H, W)if best_mask.ndim == 3 and best_mask.shape[0] == 1:best_mask = best_mask[0]mask_np = (best_mask > 0).astype(np.uint8) * 255else:129 unmodified lines13013113213313413513613713813914014114214314433 unmodified lines178179180181182183184185186129 unmodified lines# -- Step 1: SAM3 garment segmentation --t1 = time.time()if _sam3_model is not None and _sam3_processor is not None:_sam3_model.to("cuda" if torch.cuda.is_available() else "cpu")try:device = "cuda" if torch.cuda.is_available() else "cpu"dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32with torch.inference_mode(), torch.autocast(device_type=device, dtype=dtype):inference_state = _sam3_processor.set_image(img_pil)output = _sam3_processor.set_text_prompt(state=inference_state, prompt=prompt_text)masks = output["masks"]scores = output["scores"]33 unmodified lines# Ensure shape is (H, W)if best_mask.ndim == 3 and best_mask.shape[0] == 1:best_mask = best_mask[0]if best_mask.shape != (img_pil.height, img_pil.width):best_mask = cv2.resize(best_mask.astype(np.uint8), (img_pil.width, img_pil.height), interpolation=cv2.INTER_NEAREST)mask_np = (best_mask > 0).astype(np.uint8) * 255else:
app.py+11-6