A university-level implementation of the U-Net architecture from:
Ronneberger, O., Fischer, P., & Brox — U-Net: Convolutional Networks for Biomedical Image Segmentation (2015)
This project is intended for learning and coursework. It implements the core architecture described in the paper using PyTorch.
- Contracting encoder path
- Expanding decoder path
- Valid
3x3convolutions - ReLU activations
2x2max pooling2x2transposed convolutions- Cropped skip connections
- Final
1x1classification convolution - Binary segmentation dataset
- Cross-entropy + Dice training loss
- IoU and Dice metrics
- Training, evaluation, and prediction scripts
- Unit tests
For a 572x572 input, this implementation produces a 388x388 output, matching the spatial dimensions of the original valid-convolution U-Net.
U-Net-implementation/
├── configs/config.yaml
├── data/
│ ├── images/
│ └── masks/
├── results/
├── scripts/
│ ├── train.py
│ ├── evaluate.py
│ └── predict.py
├── src/
│ ├── datasets/
│ ├── losses/
│ ├── models/
│ ├── training/
│ └── utils/
├── tests/test_unet.py
├── requirements.txt
├── PAPER_NOTES.md
└── README.md
git clone https://github.com/Khan-Fazal-sys/U-Net-implementation.git
cd U-Net-implementation
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtWindows:
python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txtPlace paired images and masks here:
data/
├── images/
│ ├── 001.png
│ ├── 002.png
│ └── ...
└── masks/
├── 001.png
├── 002.png
└── ...
Images and masks should have matching filenames.
For binary segmentation:
0= background- non-zero = foreground
The default input size is 572x572.
python scripts/train.pyOr override settings:
python scripts/train.py --epochs 20 --batch-size 2Best checkpoint:
results/checkpoints/best.pt
python scripts/evaluate.py --checkpoint results/checkpoints/best.pt --images data/images --masks data/maskspython scripts/predict.py --checkpoint results/checkpoints/best.pt --input data/images/001.png --output results/predictions/001.pngpytestThe test verifies that a 572x572 input produces a 388x388 segmentation output.
The original paper describes a U-shaped network with a contracting path and symmetric expanding path, using skip connections to combine high-resolution encoder features with decoder features.
The paper also used Caffe, SGD with momentum 0.99, batch size 1, extensive data augmentation, and a weighted pixel-wise loss. This repository focuses first on understanding and implementing the architecture in modern PyTorch rather than claiming an exact reproduction of every experimental detail.
Do not claim the paper's reported benchmark scores unless you reproduce the corresponding experiment and dataset.
@article{ronneberger2015unet,
title={U-Net: Convolutional Networks for Biomedical Image Segmentation},
author={Ronneberger, Olaf and Fischer, Philipp and Brox, Thomas},
journal={arXiv preprint arXiv:1505.04597},
year={2015}
}Educational/research implementation.