AI models for gas pipe detection and segmentation, developed as part of the EXPAND CoRe 1 project.
This repository trains and evaluates a deep learning model that segments gas pipes in images, then derives pipe-level detection metrics (accuracy, precision, recall, F1) on top of the raw pixel-level segmentation.
The core pipeline:
- Loads a custom gas-pipe image dataset (
GasPipeDataset) and splits it into train/validation/test sets. - Applies data augmentation (resize, flips, rotation, brightness/contrast jitter, Gaussian blur, normalization) via Albumentations.
- Trains a U-Net segmentation model (
src/models/unet.py) to predict pipe masks. - Evaluates the trained model across multiple decision thresholds and reports:
- Segmentation metrics: Dice coefficient, IoU score, pixel accuracy
- Detection metrics: detection accuracy, precision, recall, F1-score
- Pixel classification metrics: pixel precision, recall, F1-score
- Automatically selects the best threshold (by IoU) and generates a confusion matrix plus qualitative prediction visualizations.
core-pipe-detection/
├── configs/ # Experiment / model configuration files
├── data/ # Dataset location (see Data section)
├── src/
│ ├── datasets/
│ │ └── GasPipeDataset.py # Custom PyTorch Dataset + train/val split helper
│ ├── models/
│ │ └── unet.py # U-Net model, training loop, evaluation & plotting utilities
│ ├── logging.py # Custom logging handler
│ └── parser.py # Config/argument parser
├── requirements.txt
└── train.py # Main entry point: train + evaluate the model
Install everything with:
pip install -r requirements.txtNote: A CUDA-capable GPU is strongly recommended for training.
GasPipeDataset expects an image/mask dataset laid out under a single root directory (path configured via dataset.path in your config). The dataset loader automatically creates an 80/20 train/validation split (train_ratio=0.8, fixed random_state=42 for reproducibility). Place your data under the data/ directory, or point the config at your own dataset location.
Runs are driven by a config file (see the configs/ folder) parsed via src/parser.py. At minimum, a config should define:
dataset:
path: /path/to/dataset
model:
batch_size: 8
n_epochs: 50
lr: 0.0001
checkpoints: /path/to/checkpoints/model.pthTrain (and evaluate) a model with:
python train.py --config configs/<your_config>.yamlWhat happens under the hood:
- If a checkpoint already exists at
config.model.checkpoints, it is loaded; otherwise a new U-Net is created. - The model is trained for
config.model.n_epochsepochs, and training/validation loss curves plus Dice scores are plotted and saved next to the checkpoint (*_training_history.png). - The best checkpoint is reloaded and evaluated across five thresholds (
0.5–0.9); per-threshold results are saved to*_threshold_analysis.json. - The threshold with the highest IoU is used to generate a confusion matrix (
*_confusion_matrix.png) and a qualitative sample of predictions.
For a given checkpoint path (e.g. checkpoints/model.pth), training produces:
| File | Description |
|---|---|
model.pth |
Best model weights |
model_training_history.png |
Train/val loss and validation Dice score curves |
model_threshold_analysis.json |
Segmentation/detection/pixel metrics at each tested threshold |
model_confusion_matrix.png |
Confusion matrix at the best-performing threshold |