File size: 9,997 Bytes
9759118 285bfbb 9759118 285bfbb e515229 285bfbb 70b0c31 285bfbb 302bc19 285bfbb e515229 285bfbb e515229 285bfbb e515229 285bfbb e515229 285bfbb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | ---
language: en
tags:
- medical-imaging
- polyp-segmentation
- dinov3
- vision-transformer
- kvasir-seg
- colonoscopy
- unet
datasets:
- kmader/kvasir-segmentation
metrics:
- dice
- iou
- precision
- recall
- hd95
library_name: pytorch
pipeline_tag: image-segmentation
license: mit
---
# DINOv3 Polyp Segmentation with U-Net Decoder
## Model Description
This model performs **polyp segmentation** in colonoscopy images using a frozen DINOv3-ViT-L/16 backbone with multi-scale feature extraction and a U-Net style decoder with skip connections. The model was trained on the Kvasir-SEG dataset.
**Key Features:**
- ποΈ **U-Net architecture**: Skip connections from shallow stem for precise boundary detection
- π **Multi-scale features**: Extracts DINOv3 features from layers [5, 11, 17, 20, 23] for rich hierarchical representation
- π©Ί **Medical-grade segmentation**: Specifically designed for polyp detection in colonoscopy
- π **Frozen backbone**: Leverages DINOv3's rich visual features without overfitting
- π **Comprehensive metrics**: Evaluated with Dice, IoU, Precision, Recall, and HD95
- π **Cosine annealing**: Uses CosineAnnealingWarmRestarts for better convergence
## Model Architecture
Input Image (256Γ256Γ3)
β
βββββββββββββββββββββββββ¬βββββββββββββββββββββββ
β Shallow Stem β DINOv3 Encoder β
β (Trainable) β (Frozen) β
β β β
β Conv 3β64 (3Γ3) β Layers [5,11,17, β
β Conv 64β128 (stride2)β 20,23] β
β Conv 128β256 (stride2)β Multi-scale concat β
β Conv 256β512 (stride2)β 5 Γ 1024 = 5120 β
βββββββββ¬ββββββββββββββββ΄βββββββββββ¬ββββββββββββ
β Skip Connections β
β [512, 256, 128] β
β β
ββββββββββββββββββββββββββββββββββββββββββββ
β U-Net Decoder (Trainable) β
β β
β Conv 5120β256 + Skip(512) β ConvBlock β
β Upsample β Conv 384β128 + Skip(256) β
β Upsample β Conv 192β64 + Skip(128) β
β Upsample β Final Conv 64β1 (1Γ1) β
ββββββββββββββββββββ¬ββββββββββββββββββββββββ
β
Segmentation Mask (256Γ256Γ1)
## Training Details
| Hyperparameter | Value |
|---------------|-------|
| Backbone | DINOv3-ViT-L/16 (frozen) |
| Multi-scale Layers | [5, 11, 17, 20, 23] |
| Input Resolution | 256Γ256 |
| Batch Size | 96 |
| Epochs | 150 |
| Learning Rate | 1e-4 (initial) |
| Min Learning Rate | 1e-6 |
| Weight Decay | 1e-4 |
| Optimizer | AdamW |
| Scheduler | CosineAnnealingWarmRestarts |
| Scheduler Config | T_0=10, T_mult=2 |
| Loss Function | Focal + Dice (0.7/0.3 weights) |
| Focal Loss Gamma | 2.0 |
| Focal Loss Alpha | 0.25 |
| Trainable Parameters | ~21M (Stem + Decoder) |
### Data Augmentation
- Random 90Β° rotation
- Horizontal/Vertical flips
- ShiftScaleRotate (shift=0.05, scale=0.05, rotate=15Β°)
- MotionBlur/GaussianBlur
- ColorJitter (brightness, contrast, saturation, hue)
## Performance Metrics
### Final Test Set Results
| Metric | Score |
|--------|-------|
| **Dice Score** | **0.8289 Β± 0.0000** |
| **IoU** | **0.7078 Β± 0.0000** |
| **Precision** | 0.7910 Β± 0.0000 |
| **Recall** | 0.8705 Β± 0.0000 |
| **HD95 (pixels)** | 45.46 Β± 0.00 |
| **Best Validation Dice** | 0.7327 |
### Validation Set Results
| Metric | Score |
|--------|-------|
| **Dice Score** | **0.8795 Β± 0.0304** |
| **IoU** | **0.7862 Β± 0.0485** |
| **Precision** | 0.8846 Β± 0.0256 |
| **Recall** | 0.8744 Β± 0.0351 |
| **HD95 (pixels)** | 30.85 Β± 8.90 |
### Training Set Results (Final Epoch)
| Metric | Score |
|--------|-------|
| **Dice Score** | 0.8747 Β± 0.0108 |
| **IoU** | 0.7775 Β± 0.0170 |
| **Precision** | 0.8698 Β± 0.0189 |
| **Recall** | 0.8801 Β± 0.0136 |
| **HD95 (pixels)** | 33.91 Β± 1.69 |
## Usage
### Installation
```bash
pip install torch transformers pillow matplotlib numpy opencv-python albumentations scipy scikit-learn
Basic Inference
python
import torch
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
# Import the model architecture (same as training)
from model import DINOv3Encoder, ShallowStem, UNetDecoder, PolypSegmentationModel
# Load model
model = PolypSegmentationModel.from_pretrained(
"your-username/dinov3-polyp-seg",
device="cuda" if torch.cuda.is_available() else "cpu"
)
# Preprocess image
def preprocess_image(image_path, target_size=(256, 256)):
image = Image.open(image_path).convert('RGB')
image = image.resize(target_size, Image.Resampling.BILINEAR)
# Convert to numpy and normalize
image_array = np.array(image).astype(np.float32) / 255.0
mean = np.array([0.485, 0.456, 0.406]).reshape(1, 1, 3)
std = np.array([0.229, 0.224, 0.225]).reshape(1, 1, 3)
image_array = (image_array - mean) / std
# Convert to tensor [B, C, H, W]
image_tensor = torch.from_numpy(image_array).permute(2, 0, 1).unsqueeze(0)
return image_tensor, image
# Run inference
image_tensor, original_image = preprocess_image("colonoscopy_image.jpg")
with torch.no_grad():
prediction = model(image_tensor)
mask = torch.sigmoid(prediction)
binary_mask = (mask > 0.5).float()
mask_np = binary_mask.squeeze().cpu().numpy()
# Visualize
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
axes[0].imshow(original_image)
axes[0].set_title("Input Image")
axes[1].imshow(mask_np, cmap='gray')
axes[1].set_title("Polyp Segmentation")
axes[2].imshow(original_image)
axes[2].imshow(mask_np, cmap='Reds', alpha=0.5)
axes[2].set_title("Overlay")
plt.show()
Advanced Usage with Metrics
python
from scipy.ndimage import morphology
def compute_hd95(pred, target):
"""Compute Hausdorff Distance 95th percentile"""
if pred.sum() == 0 or target.sum() == 0:
return float('inf')
pred_border = pred - morphology.binary_erosion(pred)
target_border = target - morphology.binary_erosion(target)
pred_coords = np.argwhere(pred_border > 0)
target_coords = np.argwhere(target_border > 0)
distances = []
for p in pred_coords:
dist = np.min(np.sqrt(np.sum((target_coords - p) ** 2, axis=1)))
distances.append(dist)
return np.percentile(distances, 95)
# Batch inference
dataloader = DataLoader(dataset, batch_size=16, shuffle=False)
all_metrics = {'dice': [], 'iou': [], 'hd95': []}
for images, masks in dataloader:
with torch.no_grad():
predictions = model(images)
# Calculate metrics for each image
for pred, mask in zip(predictions, masks):
pred_binary = (torch.sigmoid(pred) > 0.5).float()
# Dice
intersection = (pred_binary * mask).sum()
dice = (2. * intersection) / (pred_binary.sum() + mask.sum() + 1e-6)
# IoU
union = pred_binary.sum() + mask.sum() - intersection
iou = intersection / (union + 1e-6)
# HD95
hd95 = compute_hd95(pred_binary.numpy().squeeze(), mask.numpy().squeeze())
all_metrics['dice'].append(dice.item())
all_metrics['iou'].append(iou.item())
all_metrics['hd95'].append(hd95)
print(f"Average Dice: {np.mean(all_metrics['dice']):.4f} Β± {np.std(all_metrics['dice']):.4f}")
print(f"Average IoU: {np.mean(all_metrics['iou']):.4f} Β± {np.std(all_metrics['iou']):.4f}")
print(f"Average HD95: {np.mean(all_metrics['hd95']):.2f} Β± {np.std(all_metrics['hd95']):.2f}")
Model Limitations
Input size: Fixed to 256Γ256 pixels (resize your images accordingly)
Domain: Trained only on colonoscopy images from Kvasir-SEG
Polyp types: May not generalize to all polyp morphologies
Image quality: Best performance with standard white-light colonoscopy images
## Dataset
Trained on the Kvasir-SEG dataset, which contains 1000 polyp images with corresponding ground truth masks from colonoscopy procedures.
## License
This model is released under the MIT License.
## Citation
If you use this model in your research, please cite:
bibtex
@software{dinov3_polyp_seg,
author = {Amirreza Mehrzadian},
title = {DINOv3 Polyp Segmentation with U-Net Decoder},
year = {2024},
url = {https://huggingface.co/uncleMehrzad/dinov3-polyp-seg}
}
## Acknowledgments
DINOv3 team for the powerful vision backbone
Kvasir-SEG dataset providers for the polyp segmentation data
HuggingFace for model hosting infrastructure
```python
class PolypSegmentationModel(nn.Module):
"""Complete model wrapper matching training architecture"""
def __init__(self, encoder, stem, decoder):
super().__init__()
self.encoder = encoder
self.stem = stem
self.decoder = decoder
def forward(self, x):
vit_features = self.encoder(x)
skip_features = self.stem(x)
return self.decoder(vit_features, skip_features)
@classmethod
def from_pretrained(cls, model_path, config, device="cpu"):
"""Load the complete model from checkpoint"""
checkpoint = torch.load(model_path, map_location=device)
# Initialize components
encoder = DINOv3Encoder(
model_name=config.model_name,
local_path=config.local_model_path,
freeze=True,
layers=config.multi_scale_layers
)
stem = ShallowStem(in_channels=3, base_channels=64)
decoder = UNetDecoder(
vit_channels=encoder.out_channels,
stem_channels=[512, 256, 128],
num_classes=1
)
# Load weights
decoder.load_state_dict(checkpoint['decoder_state_dict'])
stem.load_state_dict(checkpoint['stem_state_dict'])
model = cls(encoder, stem, decoder)
model.to(device)
model.eval()
return model |