Concrete Crack Segmentation with UNet

πŸ“‹ Project Overview

A deep learning project for detecting and segmenting concrete cracks in images using an improved UNet architecture. The model achieves exceptional performance with Dice coefficient of 99.77% and IoU of 99.54%.

🎯 Key Features

  • High-Performance UNet: 4-level encoder-decoder with BatchNorm and Dropout
  • Advanced Data Augmentation: 8 different augmentation techniques
  • Automatic Mask Inversion: Detects and handles inverted masks automatically
  • Combined Loss Function: Weighted combination of Dice (60%) + BCE (40%) loss
  • Robust Preprocessing: Automatic dataset path detection (Local/Colab)
  • Comprehensive Validation: Multiple metrics (Dice, IoU, F1, Precision, Recall)
  • Production Ready: Easy inference function for new predictions

πŸ“Š Results

Metric Value
Dice Score 99.77%
IoU Score 99.54%
F1 Score 99.77%
Precision 99.54%
Recall 100.00%
Training Loss 0.0162
Validation Loss 0.0139

πŸ—οΈ Project Structure

concreate-crack-segmentation/
β”œβ”€β”€ Concreate_Crack_Segmentation.ipynb    # Main notebook
β”œβ”€β”€ Dataset/
β”‚   β”œβ”€β”€ images/                           # 800 concrete images (256x256)
β”‚   └── masks/                            # Corresponding crack masks
β”œβ”€β”€ unet_model_weights.pth                # Trained model weights
β”œβ”€β”€ model_config.json                     # Model configuration
β”œβ”€β”€ training_history.json                 # Training metrics history
β”œβ”€β”€ training_history.png                  # Training plots
β”œβ”€β”€ predictions_visualization.png         # Sample predictions
β”œβ”€β”€ requirements.txt                      # Dependencies
β”œβ”€β”€ README.md                             # This file
β”œβ”€β”€ model_card.md                        # Model card for HF
└── inference.py                         # Inference script

πŸ”§ Requirements

torch==2.0.0
torchvision==0.15.0
Pillow==9.5.0
numpy==1.24.3
opencv-python==4.8.0.74
matplotlib==3.7.2
tqdm==4.66.1

πŸš€ Quick Start

Local Usage

from PIL import Image
import torch
from inference import predict_crack_segmentation

# Make prediction
image_path = "path/to/image.jpg"
prediction = predict_crack_segmentation(image_path, threshold=0.5)

Colab Usage

# Mount Google Drive and run notebook cells in order
# Dataset path: /MyDrive/computer vision course/projects/Concreate Crack Segmentation/Dataset/

πŸ“ˆ Model Architecture

ImprovedUNet

  • Input: RGB images (3 channels, 256Γ—256)
  • Encoder: 4 levels with convolution + BatchNorm + ReLU + Dropout
  • Bottleneck: Deep feature extraction
  • Decoder: 4 levels with transposed convolution + skip connections
  • Output: Binary segmentation mask (1 channel)
  • Total Parameters: ~7.8M

Core Components

  • Encoder Filters: [64, 128, 256, 512]
  • Activation: ReLU
  • Normalization: BatchNorm2d
  • Regularization: Dropout2d (rate: 0.2-0.3)

πŸŽ“ Training Configuration

  • Optimizer: AdamW (lr=2e-3, weight_decay=1e-5)
  • Scheduler: CosineAnnealingLR (T_max=50, eta_min=1e-6)
  • Loss Function: CombinedLoss(0.4Γ—BCE + 0.6Γ—Dice)
  • Batch Size: 16
  • Train/Val Split: 80/20 (640 train, 160 val)
  • Epochs: 25 (with early stopping)
  • Data Augmentation:
    • Random flip (H/V)
    • Random rotation (Β±15Β°)
    • Random affine (translate, scale)
    • Color jitter (brightness, contrast, saturation)
    • Gaussian blur

πŸ” Dataset Details

Concrete Crack Dataset

  • Total Images: 800
  • Image Size: 256Γ—256 pixels RGB
  • Mask Format: Binary (black=background, gray/white=cracks)
  • Automatic Features:
    • Mask inversion detection (100% of masks inverted automatically)
    • Image normalization using ImageNet statistics
    • Stratified train/val split

Key Observations

  • Masks use quantized values: {0.6549 (cracks), 1.0 (background)}
  • Average crack area per image: 0-3% of image
  • Binary nature enables excellent performance

πŸ“š File Descriptions

Main Files

  • Concreate_Crack_Segmentation.ipynb: Complete training notebook
  • unet_model_weights.pth: PyTorch model state dict (recommended for loading)
  • model_config.json: Hyperparameters and training configuration
  • training_history.json: Epoch-by-epoch training metrics

Generated Outputs

  • training_history.png: 4-panel plot showing:
    • Training/Validation loss
    • Validation Dice score
    • Validation IoU
    • Validation F1 score
  • predictions_visualization.png: 6 random predictions with:
    • Original images
    • Ground truth masks
    • Model predictions
    • Binary predictions
    • Error maps
    • Metrics per sample

πŸ”„ Data Processing Pipeline

  1. Loading Phase

    • Load RGB image + grayscale mask
    • Automatic mask inversion check
  2. Preprocessing

    • Resize to 256Γ—256
    • Convert to tensor
  3. Normalization

    • Images: ImageNet normalization
    • Masks: No normalization (raw [0, 1])
  4. Augmentation (train only)

    • Horizontal/vertical flip
    • Rotation Β±15Β°
    • Affine transforms
    • Color jitter
    • Gaussian blur

πŸ’‘ Usage Examples

Load Trained Model

import torch
from model import ImprovedUNet

model = ImprovedUNet(in_channels=3, out_channels=1, depth=4, start_filters=64)
model.load_state_dict(torch.load('unet_model_weights.pth'))
model.eval()

Make Predictions

from PIL import Image
import torchvision.transforms as transforms

image = Image.open('concrete.jpg').convert('RGB')
image = transforms.Resize((256, 256))(image)
image_tensor = transforms.ToTensor()(image).unsqueeze(0)

with torch.no_grad():
    prediction = model(image_tensor)
    
segmentation_mask = prediction.squeeze().numpy()

Batch Processing

from torch.utils.data import DataLoader
from dataset_class import CrackDataset

dataset = CrackDataset(images_path, masks_path, transform=val_transform)
loader = DataLoader(dataset, batch_size=32, shuffle=False)

predictions = []
for images, masks in loader:
    with torch.no_grad():
        preds = model(images)
    predictions.extend(preds.cpu().numpy())

βš™οΈ Advanced Configuration

Custom Training

# Modify these in the notebook:
BATCH_SIZE = 16              # Change batch size
NUM_EPOCHS = 25              # Change max epochs
LEARNING_RATE = 2e-3         # Change learning rate
IMG_WIDTH = IMG_HEIGHT = 256 # Change image size

Loss Function Weights

# In CombinedLoss:
bce_weight = 0.4     # Weight for BCE loss
dice_weight = 0.6    # Weight for Dice loss
# Adjust for different focus (higher dice_weight β†’ more focus on segmentation)

πŸ› Troubleshooting

Issue: DataLoader Error on Windows

Solution: Set num_workers=0 in DataLoader (already implemented)

Issue: Out of Memory (OOM)

Solution:

  • Reduce BATCH_SIZE (16 β†’ 8)
  • Reduce image size (256 β†’ 128)
  • Enable gradient checkpointing

Issue: Model Convergence Issue (loss stays high)

Solution:

  • Check if masks are inverted (auto-detection should handle)
  • Verify dataset loading with diagnostic cells
  • Increase learning rate or change optimizer

πŸ“– Dependencies & Versions

Tested with:

  • Python 3.9+
  • PyTorch 2.0+
  • CUDA 11.8+ (optional, CPU supported)
  • CUDA-enabled GPU (RTX 3060, A100, etc.)

πŸ“„ Model Card

See model_card.md for detailed model information.

πŸ‘€ Author

Created as educational project for concrete crack detection using deep learning.

πŸ“ License

MIT License - Feel free to use for educational and commercial purposes.

πŸ™ Acknowledgments

  • UNet Architecture: [Ronneberger et al., 2015]
  • Dice Loss: [Milletari et al., 2016]
  • Dataset: Concrete Crack Dataset (800 annotated images)

πŸ“§ Support

For issues or questions, please create an issue in the repository.


Last Updated: March 28, 2026
Model Status: Production Ready βœ“

Downloads last month
31
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Space using samir-mohamed/concrete-crack-segmentation 1