This repository provides a reproducible pipeline for multiclass image segmentation using PyTorch. It supports multiple architectures (DeepLabV3, UNet, UNet++, DeepLabV3+) and includes tools for training, evaluation, metrics, and visualization.
For a detailed explanation of the data loading and training process, see DATA_AND_TRAINING.md.
- Modular Codebase: Organized into config, data, models, training, evaluation, and utils.
- Supported Models:
- Standard (SMP-backed): DeepLabV3, UNet, UNet++, DeepLabV3+
- Original Implementations:
UNet_original,DeepLabV1_original,DeepLabV2_original,DeepLabV3_original
- Backbone: Configurable via
BACKBONE(default:resnet101). - Data Loading: Efficient loading with caching and optimized workers.
- Augmentation: Comprehensive pipeline (Geometric, Pixel-level, Noise) using Albumentations.
- Training: Mixed precision (AMP), global reproducibility, early stopping, and checkpointing.
- Transfer Learning: Fine-tune from existing checkpoints with automatic shape mismatch handling (useful for varying class counts) and encoder freezing via
TRANSFER_LEARNINGconfig. - Self-Supervised Learning: Built-in Masked Autoencoder (MAE) style pre-training for U-Net architectures to improve performance on small datasets without requiring labeled ground truth.
- Self-Training (Pseudo-Labeling): Leverages unlabeled data using a Teacher-Student framework. Confidence-based filtering and dual-stream loaders allow the model to learn from unannotated satellite imagery using reliable automated predictions.
- Evaluation: Automated generation of metrics, confusion matrices, and visualizations.
- Python 3.8+
- PyTorch with CUDA support recommended
- Clone the repository:
git clone <repo_url> cd <repo_dir>
- Create and activate a virtual environment:
python3 -m venv .venv source .venv/bin/activate - Install dependencies:
pip install -r requirements.txt
- Configure Environment:
cp .env.example .env # Edit .env to set DATASET_DIR=/absolute/path/to/dataset - Run Training:
docker-compose up --build andros-segmentation
- Run Evaluation/Mask Generation:
docker-compose run --rm evaluate docker-compose run --rm generate-masks
All scripts support a --config argument to specify the configuration file (default: config/config.yaml).
python train.py --config config/config.yaml- K-Fold Cross-Validation: Set
K_FOLDS > 1in config. - Ensembling: Set
ENSEMBLE: truein config. - Model Selection: Controlled via
MODEL_SETin config (standard,originals,all). - Standard Model Subset: Optionally set
STANDARD_MODELS(list) to run only selected standard models, e.g.['DeepLabV3', 'UNet']. - Transfer Learning: Settings in
config.yaml:TRANSFER_LEARNING: truePRETRAINED_CHECKPOINT_DIR: 'checkpoints/'PRETRAINED_WEIGHT_SUFFIX: '_pretrained.pth'(or_best.pth)FREEZE_ENCODER: true/false
python pretrain.py --config config/config.yaml- Trains the model to reconstruct patch-masked inputs, building rich feature representations without labeled data.
- Generates both random masks and object-centric masks (using Sobel edge-density) to force high-level structural learning.
- Controlled via
PRETRAIN_EPOCHS,MASK_RATIO,PATCH_SIZE, andOBJECT_CENTRIC_EPOCH. - Seamlessly integrates with downstream training using
TRANSFER_LEARNING: trueandPRETRAINED_WEIGHT_SUFFIX: '_pretrained.pth'. - See SELF_SUPERVISED_LEARNING.md for dedicated SSL instructions.
SELF_TRAINING: true
UNLABELED_IMG_PATH: "/path/to/unlabeled"- Simultaneously learns from labeled data and confident Teacher-generated pseudo-labels.
- Automatically handles dual-stream batching (50% labeled, 50% unlabeled).
- Configurable
PSEUDO_LABEL_THRESHOLDandIGNORE_INDEX. - See SELF_TRAINING.md for technical details and implementation logic.
python evaluate.py --config config/config.yaml- Generates metrics and plots in
outputs/.
python generate_masks.py --config config/config.yaml- Generates segmentation masks for test images in
outputs/<ModelName>/masks/.
python evaluation/visualize_history.py- Visualizes loss and metric trends from
outputs/training_history.npy.
python evaluation/visualize_augmentation.py- Generates a grid of original vs. augmented image-mask pairs in
outputs/debug/augmentation_validation.pngto assess augmentation methods.
config/: Configuration files (YAML).utils/: Data loading and transformations.models/: Model architectures.training/: Training logic and loss functions.evaluation/: Metrics and visualization tools.train.py,evaluate.py,generate_masks.py: Entry points.
Andros Dataset/
train/
image/
mask/
val/ (Optional, used if PRE_SPLIT_DATASET: true)
image/
mask/
test/
image/
mask/
Update paths in config/config.yaml. Set PRE_SPLIT_DATASET: true if your dataset already includes a val/ partition (this will force K_FOLDS=1).
Run unit tests:
pytest tests --maxfail=3 -v -rw[License Information]