Views
No views yet
| Parameter | Value |
|---|---|
| Base model | facebook/sam2.1-hiera-base-plus |
| Method | Mask decoder fine-tuning (encoders frozen) |
| Trainable parameters | 4.2M / 73.3M total (5.75%) |
| Loss function | DiceCE (Dice + Cross-Entropy from MONAI) |
| Dataset | kowndinya23/Kvasir-SEG |
| Training images | 880 |
| Validation images | 120 |
| Hardware | NVIDIA RTX 5090 (32GB VRAM) |
| Training time | ~12 minutes |
| Epochs | 30 |
| Effective batch size | 16 (8 per device x 2 gradient accumulation) |
| Learning rate | 1e-5 (cosine schedule, 20 warmup steps) |
| Precision | bf16 |
| Prompt type | Bounding box (derived from ground truth masks) |
| Framework | Transformers 5.3.0 + MONAI |

1from transformers import AutoProcessor, Sam2Model
2from PIL import Image
3import torch
4import numpy as np
5
6# Load model and processor
7model = Sam2Model.from_pretrained("usama10/sam2-kvasir-polyp-segmentation", dtype=torch.bfloat16)
8processor = AutoProcessor.from_pretrained("usama10/sam2-kvasir-polyp-segmentation")
9model.eval()
10
11# Load a colonoscopy image
12image = Image.open("colonoscopy.jpg").convert("RGB")
13
14# Provide a bounding box prompt [x_min, y_min, x_max, y_max]
15bbox = [[100, 50, 300, 250]] # Approximate polyp location
16
17inputs = processor(images=image, input_boxes=[bbox], return_tensors="pt")
18inputs = {k: v.to(model.device) for k, v in inputs.items()}
19
20with torch.no_grad():
21 outputs = model(**inputs, multimask_output=False)
22
23# Get the predicted mask
24pred_mask = outputs.pred_masks.squeeze().cpu().numpy()
25binary_mask = (pred_mask > 0).astype(np.uint8)