This repository contains a PyTorch implementation of the U-Net architecture for image segmentation. The project is structured to train and evaluate a U-Net model on the Carvana Image Masking Challenge dataset.
model.py: Contains the U-Net model architecture implementation.train.py: Script for training the U-Net model.utils.py: Utility functions for data loading, checkpointing, and evaluation.config.py: Configuration file with hyperparameters and directory paths.dataset.py: Custom dataset class for loading and preprocessing images and masks.
- Python 3.6+
- PyTorch
- torchvision
- albumentations
- tqdm
- Pillow
- NumPy
-
Clone this repository:
git clone https://github.com/your-username/unet-implementation.git cd unet-implementation -
Install the required packages:
pip install torch torchvision albumentations tqdm Pillow numpy -
Download the Carvana Image Masking Challenge dataset from Kaggle: https://www.kaggle.com/c/carvana-image-masking-challenge
After downloading, organize the data into the following directory structure:
data/ ├── train_images/ ├── train_masks/ ├── val_images/ └── val_masks/
-
Adjust the hyperparameters and paths in
config.pyif necessary. -
Train the model:
python train.py -
The trained model will be saved as
my_checkpoint.pth.tar. -
To use the trained model for inference, load the checkpoint in your script and use the
UNETclass frommodel.py.
The implemented U-Net architecture consists of:
- An encoder path with four double convolution blocks
- A bottleneck layer
- A decoder path with four up-convolution and double convolution blocks
- Skip connections between encoder and decoder blocks
The model uses batch normalization and ReLU activation functions. Optional dropout layers can be added for regularization.
- Modify the
UNETclass inmodel.pyto experiment with different architectures. - Adjust training parameters in
config.pyto optimize performance. - Implement additional data augmentation techniques in
config.pyusing the albumentations library.
This implementation is based on the original U-Net paper: Ronneberger, O., Fischer, P., & Brox, T. (2015). U-Net: Convolutional Networks for Biomedical Image Segmentation. In Medical Image Computing and Computer-Assisted Intervention (MICCAI), Springer, LNCS, Vol.9351: 234--241, 2015.
The dataset used in this project is from the Carvana Image Masking Challenge on Kaggle.