A comprehensive deep learning project for binary image classification that detects and classifies cracked versus non-cracked building surfaces. The project implements and compares multiple neural network architectures including FFNN, CNN, CNN-LSTM, and transfer learning with EfficientNetB0.
- Binary Image Classification: Classifies building images as either "Cracked" or "Non-Cracked"
- Multiple Neural Network Architectures:
- Fully Connected Neural Network (FFNN)
- Convolutional Neural Network (CNN)
- Hybrid CNN-LSTM model
- Transfer Learning with EfficientNetB0
- Data Augmentation: Rotation, zoom, brightness adjustments, and horizontal flipping for improved model generalization
- Advanced Training Techniques:
- Class weight balancing for imbalanced datasets
- Dropout and Batch Normalization for regularization
- L2 regularization on dense layers
- Learning rate scheduling with ReduceLROnPlateau
- Early stopping to prevent overfitting
- Multiple Optimizer Comparisons: Evaluation of Adam and AdamW optimizers
- Comprehensive Evaluation: Confusion matrices, classification reports, accuracy/loss visualizations
- Programming Language: Python 3.7+
- Deep Learning Framework: TensorFlow/Keras
- Image Processing: OpenCV, Pillow
- Data Processing: NumPy, Pandas
- Machine Learning: scikit-learn
- Visualization: Matplotlib, Seaborn
building-crack-classification/
├── NN_Final_notebook___Extra_models1.ipynb # Main Jupyter notebook with all models
├── requirements.txt # Project dependencies
├── README.md # This file
└── split_datase-1/ # Generated dataset splits
├── train/
│ ├── Cracked/
│ └── Non Cracked/
├── val/
│ ├── Cracked/
│ └── Non Cracked/
└── test/
├── Cracked/
└── Non Cracked/
- Python 3.7 or higher
- pip package manager
-
Clone or download the project
cd building-crack-classification -
Create and activate a virtual environment (recommended)
# Windows python -m venv venv venv\Scripts\activate # Linux/macOS python3 -m venv venv source venv/bin/activate
-
Install dependencies
pip install -r requirements.txt
-
Verify installation
python -c "import tensorflow; print(f'TensorFlow {tensorflow.__version__} installed')"
The project expects image data organized as follows:
data/
├── Cracked/ # Directory containing cracked building images
└── Non Cracked/ # Directory containing non-cracked building images
Update the data_dir path in the notebook:
data_dir = r"path/to/your/dataset"Edit these parameters in the notebook to customize training:
- Image Size:
IMG_SIZE = (128, 128)- Input image resolution - Batch Size:
BATCH_SIZE = 32- Number of images per batch - Learning Rate:
- FFNN:
0.0001 - CNN:
0.0005 - CNN-LSTM:
0.0005 - EfficientNetB0:
0.0005
- FFNN:
- Epochs: Varies per model (5-30 epochs)
- Dropout Rates: Range from 0.2 to 0.4
- L2 Regularization:
1e-4to1e-5
-
Launch Jupyter Notebook
jupyter notebook
-
Open the notebook
- Open
NN_Final_notebook___Extra_models1.ipynb
- Open
-
Execute cells sequentially
- The notebook is organized into logical sections:
- Import Libraries
- Load and Explore Data
- Data Preprocessing and Splitting
- Data Augmentation Setup
- Model Architectures and Training
- Evaluation and Visualization
- The notebook is organized into logical sections:
-
Dataset will be automatically split into:
- Training set: 70% of data
- Validation set: 15% of data
- Test set: 15% of data
After running the notebook, you will generate:
-
Trained Models:
ffnn_adam- Fully connected neural networkcnn_adamandcnn_adamw- CNN modelsLSTM_adamw- CNN-LSTM modelmodel- EfficientNetB0 transfer learning model
-
Visualizations:
- Class distribution charts
- Sample images from dataset
- Training/validation accuracy curves
- Training/validation loss curves
- Confusion matrices
- Classification reports
- Images are loaded from the source directories
- Organized into train/validation/test splits (70/15/15)
- Resized to 128×128 pixels
- Normalized to [0, 1] range by scaling pixel values
- Applied only to training data to prevent overfitting:
- Random rotation (±5°)
- Zoom augmentation (±5%)
- Brightness variation (0.9-1.1x)
- Horizontal flipping
- Positional shifts (width/height ±5%)
- Computed class weights for imbalanced datasets
- Ensures minority class receives appropriate training emphasis
- Applied during model training
- Input: Flattened 128×128×3 images
- Dense layers: 256 → 128 → 64 → 32 → 1
- Regularization: L2 regularization, Batch Normalization, Dropout
- Output: Binary classification (sigmoid activation)
- Conv blocks: 32 → 64 → 128 filters
- Kernel size: 3×3 with ReLU activation
- Pooling: MaxPooling2D (2×2)
- Fully connected layers: 256 → 1
- Regularization: L2, Batch Normalization, Dropout
- CNN feature extraction: Same as CNN
- Reshape to 2D sequence: (-1, 128)
- LSTM layers: 128 units → 64 units
- Dense layers: 128 → 1
- Combines spatial and temporal learning
- Pre-trained on ImageNet weights
- Base model frozen (no fine-tuning)
- Added layers: GlobalAveragePooling2D → Dense(256) → Dropout(0.4) → Dense(128) → Dropout(0.3) → Dense(1)
- Leverages pre-learned features for improved performance
- Loss Function: Binary Crossentropy (suitable for binary classification)
- Metrics: Accuracy
- Optimizers: Adam and AdamW with weight decay
- Callbacks:
ReduceLROnPlateau: Reduces learning rate if validation loss plateausEarlyStopping: Stops training if validation loss doesn't improve (patience: 10 epochs)
- Test Set Performance: Accuracy and loss metrics
- Confusion Matrix: TP, TN, FP, FN analysis
- Classification Report: Precision, Recall, F1-Score per class
# The notebook performs the following workflow:
# 1. Load data
cracked_images = load_directory('Cracked')
non_cracked_images = load_directory('Non Cracked')
# 2. Split data (70/15/15)
train_set, val_set, test_set = split_data(all_images)
# 3. Apply augmentation and normalization
train_generator = ImageDataGenerator(augmentation_parameters)
# 4. Build model (e.g., CNN)
model = build_cnn()
model.compile(optimizer=Adam(0.0005), loss='binary_crossentropy', metrics=['accuracy'])
# 5. Train with callbacks
history = model.fit(train_generator, validation_data=val_generator, epochs=30,
callbacks=[reduce_lr, early_stop], class_weight=class_weights)
# 6. Evaluate on test set
test_loss, test_accuracy = model.evaluate(test_generator)
# 7. Generate predictions and visualizations
y_pred = model.predict(test_generator)
confusion_matrix = compute_confusion_matrix(y_true, y_pred)The notebook trains and compares:
| Model | Optimizer | Learning Rate | Epochs | Key Advantage |
|---|---|---|---|---|
| FFNN | Adam | 0.0001 | 5 | Lightweight, fast training |
| CNN | Adam | 0.0005 | 30 | Spatial feature extraction |
| CNN | AdamW | 0.0005 | 10 | Weight decay regularization |
| CNN-LSTM | AdamW | 0.0005 | 5 | Sequence modeling capability |
| EfficientNetB0 | Adam | 0.0005 | 6 | Transfer learning efficiency |
Solution: Verify the data_dir path is correct and matches your system:
data_dir = r"correct_path_to_dataset"Solution: Reduce batch size in the notebook:
BATCH_SIZE = 16 # Instead of 32Solution: Ensure TensorFlow is installed:
pip install --upgrade tensorflowSolution: Verify image formats are supported (JPEG, PNG, BMP):
# Check supported formats
from PIL import Image
test_img = Image.open('path_to_image')
print(f"Format: {test_img.format}")Solution: TensorFlow will automatically use CPU if GPU is unavailable. For GPU support:
pip install tensorflow[and-cuda] # For NVIDIA GPUsNot specified.
- The notebook uses hardcoded paths (
D:\pro\Bangunan Retak) that must be updated to your dataset location - The generated
split_datase-1directory contains the organized train/val/test splits - All models use binary crossentropy loss suitable for 2-class classification
- EfficientNetB0 uses ImageNet pre-trained weights for transfer learning
- Results and visualizations are displayed inline in the notebook; no separate output files are generated
- Training times vary significantly depending on hardware (GPU acceleration recommended for faster training)