This repository demonstrates semantic image segmentation using the U-Net architecture.
Two segmentation tasks are implemented:
- Binary Segmentation
- Multi-class Segmentation
The repository illustrates how encoder-decoder networks can perform pixel-wise classification by assigning a class label to every pixel in an image.
Image segmentation is the task of classifying every pixel in an image.
Instead of predicting a single label for the entire image, the network predicts a segmentation mask where each pixel belongs to one semantic class.
Each training sample consists of:
- Input image
- Corresponding segmentation mask
U-Net is one of the most successful encoder-decoder architectures for image segmentation.
The encoder extracts increasingly abstract image features through downsampling, while the decoder reconstructs a full-resolution segmentation mask through upsampling.
Skip connections transfer high-resolution spatial information from the encoder directly to the decoder, significantly improving boundary localization and segmentation accuracy.
2018 Data Science Bowl (Kaggle)
The segmentation masks contain only two values:
- Background
- Object
The masks are normalized from:
- 0 → 0
- 255 → 1
Input images are also normalized before training.
The output layer uses a sigmoid activation.
Binary Cross-Entropy is used as the loss function because each pixel represents a binary classification problem.
People Clothing Dataset (Kaggle)
The dataset contains 59 semantic classes.
Images can be loaded in either RGB or BGR format. The choice of color format is not critical during training, but the same format must be used during inference to obtain consistent predictions.
The final layer uses the Softmax activation function to predict the probability of each class for every pixel.
Sparse Categorical Cross-Entropy is used because the segmentation masks already contain integer class labels.
Although Categorical Cross-Entropy could also be used, it requires converting every mask into one-hot encoded labels. With 59 classes, one-hot encoding significantly increases memory usage, making Sparse Categorical Cross-Entropy the more efficient choice.
The implemented U-Net consists of:
- Encoder
- Bottleneck
- Decoder
- Skip Connections
Skip connections preserve fine spatial details that are often lost during downsampling, resulting in more accurate object boundaries.
- Python
- TensorFlow
- Keras
- OpenCV
- NumPy
- Matplotlib