Skip to content

About

PyTorch from-scratch U-Net for binary semantic segmentation with training, evaluation, prediction, metrics, and test coverage.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

U-Net From Scratch in PyTorch

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.

What is implemented

  • Contracting encoder path
  • Expanding decoder path
  • Valid 3x3 convolutions
  • ReLU activations
  • 2x2 max pooling
  • 2x2 transposed convolutions
  • Cropped skip connections
  • Final 1x1 classification 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.

Repository

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

Installation

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.txt

Windows:

python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txt

Dataset

Place 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.

Train

python scripts/train.py

Or override settings:

python scripts/train.py --epochs 20 --batch-size 2

Best checkpoint:

results/checkpoints/best.pt

Evaluate

python scripts/evaluate.py   --checkpoint results/checkpoints/best.pt   --images data/images   --masks data/masks

Predict

python scripts/predict.py   --checkpoint results/checkpoints/best.pt   --input data/images/001.png   --output results/predictions/001.png

Test

pytest

The test verifies that a 572x572 input produces a 388x388 segmentation output.

Relation to the paper

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.

Citation

@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}
}

License

Educational/research implementation.

About

PyTorch from-scratch U-Net for binary semantic segmentation with training, evaluation, prediction, metrics, and test coverage.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages