Optimized for datasets with extremely sparse labels (1-100 labeled pixels out of large images).
# Install dependencies
pip install -r requirements.txt
# Train with default settings (UNet + ResNet34 + Focal+Dice loss)
python train.py --data-dir Dataset-trial-difficult
# Train with different model
python train.py --model deeplabv3plus --encoder efficientnet-b4 --loss focal_dice
# Predict on single image
python predict.py --image test_image.jpg --model checkpoints/best_iou_model.pth
# Predict on directory
python predict.py --image-dir test_images/ --model checkpoints/best_iou_model.pth --output results/
As discussed in the conversation, easily switch between different setups:
# UNet (default, lightweight)
python train.py --model unet --encoder resnet34
# DeepLabV3+ (better for fine details)
python train.py --model deeplabv3plus --encoder efficientnet-b4
# UNet++ (better feature fusion)
python train.py --model unetplusplus --encoder resnet50
# Focal + Dice (recommended for sparse labels)
python train.py --loss focal_dice
# Just Dice loss
python train.py --loss dice
# Just Focal loss
python train.py --loss focal
# Basic BCE
python train.py --loss bce
# Patch-based training (default for sparse labels)
python train.py --patch-size 256
# Full image training
python train.py --no-patches
# Different patch sizes
python train.py --patch-size 512 # Larger context
python train.py --patch-size 128 # Smaller, faster
- Extracts patches around sparse labeled pixels
- Adds background patches for class balance
- Configurable patch size and background ratio
- Focal + Dice: Handles extreme class imbalance (recommended)
- Boundary Loss: For pixel-level precision
- Easy switching via command line
- Sliding window for large images (700×2000+)
- Test-time augmentation option
- Efficient overlapping patch handling
- Real-time loss and metric monitoring
- Prediction visualization every few epochs
- Learning rate and gradient tracking
project/
├── train.py # Training script with TensorBoard
├── predict.py # Inference on single/batch images
├── config.py # Flexible configuration system
├── dataset.py # Patch-based dataset for sparse labels
├── models.py # Model creation with easy switching
├── losses.py # Focal+Dice and other loss functions
├── requirements.txt # Dependencies
└── README.md # This file
# Quick training for test day (few epochs)
python train.py --epochs 10 --batch-size 4
# Full training with best settings for sparse data
python train.py --model deeplabv3plus --encoder efficientnet-b4 --loss focal_dice --lr 5e-5 --epochs 50
# Train on easy dataset first
python train.py --data-dir Dataset-trial-easy --epochs 20
# Basic prediction
python predict.py --image unseen_image.jpg --model checkpoints/best_iou_model.pth
# With test-time augmentation
python predict.py --image unseen_image.jpg --model checkpoints/best_iou_model.pth --tta
# Batch prediction
python predict.py --image-dir unseen_images/ --model checkpoints/best_iou_model.pth --output results/
# Custom threshold
python predict.py --image test.jpg --model checkpoints/best_dice_model.pth --threshold 0.3
# Start TensorBoard
tensorboard --logdir runs
# View at http://localhost:6006
TensorBoard shows:
- Training/validation loss curves
- IoU, Dice, accuracy metrics
- Learning rate schedule
- Prediction visualizations
- Loss component breakdown (Focal + Dice)
- Quick setup:
pip install -r requirements.txt
- Train baseline:
python train.py --epochs 20 --data-dir Dataset-trial-difficult
- Monitor: Start TensorBoard to watch training
- Predict:
python predict.py --image-dir unseen_data/ --model checkpoints/best_iou_model.pth
- Visualize: Check output overlays and TensorBoard
# If overfitting
python train.py --lr 2e-5 --batch-size 4
# If underfitting
python train.py --lr 1e-4 --model deeplabv3plus
# If too slow
python train.py --patch-size 128 --batch-size 16
# If poor small object detection
python train.py --loss focal_dice --model unetplusplus
checkpoints/best_iou_model.pth
- Best IoU modelcheckpoints/best_dice_model.pth
- Best Dice modelruns/
- TensorBoard logs
*_mask.png
- Binary segmentation mask*_prob.png
- Probability map*_overlay.png
- Mask overlaid on original imageprediction_summary.txt
- Batch processing summary
- Start simple: UNet + ResNet34 + Focal+Dice
- Monitor early: Use TensorBoard from epoch 1
- Quick iterations: 10-20 epoch experiments first
- Visual check: Always look at overlay results
- Have fallbacks: If one model fails, switch architecture quickly
This pipeline is designed for the sparse segmentation challenge with easy switching between configurations as discussed in the conversation.