A PyTorch implementation of SegFormer-based semantic segmentation for Near-Infrared (NIR) jugular vein images.
The repository provides:
- 5-Fold Cross Validation training
- Test-Time Augmentation (TTA)
- Ensemble inference across folds
- Automatic threshold sweeping
- Video inference
- Multiple segmentation losses
- SegFormer (MiT-B0–B5 backbones)
- Dice Loss
- Focal Loss
- Dice + Focal Loss
- Tversky Loss
- Focal Tversky Loss
- CLAHE preprocessing
- Albumentations data augmentation
- Ensemble inference
- Automatic threshold optimisation
- IoU and Dice evaluation
.
├── configs.py
├── dataset.py
├── train.py
├── infer.py
├── infer_video.py
├── evaluate.py
├── model.py
├── loss.py
├── requirements.txt
├── README.md
│
├── checkpoints/
├── data/
│ ├── train/
│ ├── val/
│ └── test/
│
└── reports/
Expected directory layout:
data/
│
├── train/
│ ├── images/
│ └── masks/
│
├── val/
│ ├── images/
│ └── masks/
│
└── test/
├── images/
└── masks/
Images may be
- JPG
- PNG
- JPEG
- TIFF
Masks must be
PNG
with the same filename stem as the image.
Example
001.jpg
001.png
Clone the repository
git clone https://github.com/<username>/<repository>.git
cd <repository>Create a virtual environment (optional)
python -m venv venvActivate it.
Install the appropriate PyTorch build for your system from:
https://pytorch.org/get-started/locally/
For example, with CUDA 12.1:
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121Then install the remaining dependencies:
pip install -r requirements.txtConfigure hyperparameters inside
configs.py
Start training
python train.pyTraining performs 5-Fold Cross Validation and stores the best checkpoint for each fold.
Saved checkpoints
checkpoints/
best_model_fold1.pth
best_model_fold2.pth
...
best_model_fold5.pth
from infer import infer
infer(
image_path="data/test/images/sample.png",
fold=1,
output_path="prediction.png",
)from infer import infer_directory
infer_directory(
input_dir="data/test/images",
output_dir="predictions",
ensemble=True,
)from infer_video import infer_video
infer_video(
video_path="input.mp4",
output_path="output.mp4",
ensemble=True,
overlay=True,
)Run
python evaluate.pyThe script automatically
- loads all fold checkpoints
- performs ensemble inference
- sweeps multiple thresholds
- computes IoU and Dice
- identifies the optimal threshold
- saves masks and overlay images
- exports CSV reports
Generated outputs are written under
reports/
Most settings can be modified from
configs.py
including
- SegFormer backbone
- image size
- learning rate
- batch size
- number of folds
- loss function
- threshold
- CLAHE
- Test-Time Augmentation
nvidia/mit-b0
nvidia/mit-b1
nvidia/mit-b2
nvidia/mit-b3
nvidia/mit-b4
nvidia/mit-b5
dice
focal
dice_focal
tversky
tversky_focal
Training
checkpoints/
training_log.txt
best_model_fold1.pth
...
Evaluation
reports/
threshold_report.txt
threshold_sweep.csv
threshold_sweep_summary.csv
masks/
overlays/
This project is released under the MIT License.