btxrd-workstation / src /models /medsam_wrapper.py
amitaddi
feat: deploy bone tumor segmentation workstation
1bd13a5
Raw History Blame Contribute Delete
5.78 kB
"""
MedSAM Wrapper for Bone Tumor Segmentation.
MedSAM is a SAM variant fine-tuned on 1.5M medical image-mask pairs,
providing a strong medical imaging prior compared to vanilla SAM/SAM2.
Supports:
- Zero-shot inference with bounding box prompts
- Fine-tuning on BTXRD
"""
import os
import numpy as np
import torch
from typing import Optional, List, Dict, Tuple
# MedSAM imports
try:
from segment_anything import sam_model_registry, SamPredictor
MEDSAM_AVAILABLE = True
except ImportError:
try:
# Alternative import path
from segment_anything import sam_model_registry, SamPredictor
MEDSAM_AVAILABLE = True
except ImportError:
MEDSAM_AVAILABLE = False
print("WARNING: MedSAM/segment_anything not installed.")
print("Install with: pip install git+https://github.com/bowang-lab/MedSAM.git")
MEDSAM_CHECKPOINT_URL = "https://zenodo.org/records/10689643/files/medsam_vit_b.pth"
class MedSAMWrapper:
"""
Wrapper around MedSAM for bone tumor segmentation.
MedSAM uses SAM's ViT-B architecture, fine-tuned on medical images.
It only supports bounding box prompts (no point prompts).
"""
def __init__(self,
checkpoint_path: Optional[str] = None,
device: str = 'cuda'):
"""
Args:
checkpoint_path: Path to medsam_vit_b.pth
device: 'cuda' or 'cpu'
"""
if not MEDSAM_AVAILABLE:
raise RuntimeError("segment_anything not installed for MedSAM")
self.device = device
if checkpoint_path is None:
checkpoint_path = self._get_or_download_checkpoint()
# Build MedSAM (uses SAM's ViT-B architecture)
self.model = sam_model_registry['vit_b'](checkpoint=checkpoint_path)
self.model = self.model.to(device)
self.model.eval()
self.predictor = SamPredictor(self.model)
print(f"MedSAM (ViT-B) loaded on {device}")
def _get_or_download_checkpoint(self) -> str:
"""Download MedSAM checkpoint if not present."""
cache_dir = os.path.join(os.path.expanduser('~'), '.cache', 'medsam')
os.makedirs(cache_dir, exist_ok=True)
filepath = os.path.join(cache_dir, 'medsam_vit_b.pth')
if not os.path.exists(filepath):
print(f"Downloading MedSAM checkpoint...")
print(f" URL: {MEDSAM_CHECKPOINT_URL}")
print(f" Saving to: {filepath}")
import urllib.request
urllib.request.urlretrieve(MEDSAM_CHECKPOINT_URL, filepath)
print(" Download complete!")
return filepath
def set_image(self, image: np.ndarray):
"""
Set image for prediction.
Args:
image: (H, W, 3) RGB numpy array
"""
self.predictor.set_image(image)
def predict_with_box(self,
box: np.ndarray,
multimask_output: bool = False) -> Tuple[np.ndarray, float]:
"""
Predict segmentation using bounding box prompt.
Args:
box: (4,) [x1, y1, x2, y2] in pixel coordinates
multimask_output: Return multiple mask candidates
Returns:
mask: (H, W) binary mask
score: Predicted IoU
"""
# MedSAM expects box as (1, 4) tensor
box_torch = torch.tensor(box, dtype=torch.float32, device=self.device)
box_transformed = self.predictor.transform.apply_boxes_torch(
box_torch.unsqueeze(0),
self.predictor.original_size
)
masks, scores, logits = self.predictor.predict_torch(
point_coords=None,
point_labels=None,
boxes=box_transformed,
multimask_output=multimask_output,
)
# Move to CPU and take best
masks = masks.cpu().numpy()
scores = scores.cpu().numpy()
best_idx = scores[0].argmax()
mask = masks[0, best_idx]
score = float(scores[0, best_idx])
return mask.astype(np.uint8), score
def predict_all_objects(self,
image: np.ndarray,
prompts: List[Dict]) -> List[Dict]:
"""
Predict masks for all objects using box prompts.
Args:
image: (H, W, 3) RGB
prompts: List of prompt dicts with 'box', 'class_id'
Returns:
List of result dicts
"""
self.set_image(image)
results = []
for prompt in prompts:
mask, score = self.predict_with_box(prompt['box'])
results.append({
'mask': mask,
'score': score,
'class_id': prompt['class_id'],
'prompt_type': 'box',
})
return results
def combine_masks(self, results: List[Dict], shape: Tuple[int, int]) -> np.ndarray:
"""Combine per-object masks into single binary mask."""
import cv2
combined = np.zeros(shape, dtype=np.uint8)
for r in results:
mask = r['mask']
if mask.shape != shape:
mask = cv2.resize(mask, (shape[1], shape[0]),
interpolation=cv2.INTER_NEAREST)
combined = np.maximum(combined, mask)
return combined
if __name__ == '__main__':
if MEDSAM_AVAILABLE:
print("MedSAM dependencies available!")
else:
print("MedSAM not installed. Run:")
print(" pip install git+https://github.com/bowang-lab/MedSAM.git")