ThisCalculator

Everyday

Class Imbalance in Concrete Crack Detection

Updated · Standard formula, shown below

Class Imbalance in Concrete Crack Detection

This guide explains how to handle class imbalance when training a model to detect cracks in concrete images. You’ll learn practical techniques to improve recall for the minority (crack) class, including data-level, algorithm-level, and evaluation adjustments. No prior experience with imbalanced learning is needed, but basic familiarity with Python and a deep learning framework like PyTorch or TensorFlow is assumed.

What You’ll Need

  • A dataset of concrete surface images with binary labels (crack / no-crack).
  • Python environment with a deep learning library (PyTorch, TensorFlow, or Keras).
  • Basic knowledge of training a convolutional neural network (CNN).
  • Optional: GPU for faster training, but not required for the techniques described.

Step-by-Step

  1. Assess the imbalance ratio. Count the number of crack and no-crack images in your training set. Compute the ratio (e.g., 1:10). If the minority class is less than 10% of the total, you’ll need aggressive rebalancing.
  2. Split data preserving the ratio. Use stratified train/validation/test splits so each subset has the same class proportion as the original dataset. This ensures your validation metrics reflect the imbalance.
  3. Apply data augmentation to the minority class. For crack images, apply random rotations (±15°), horizontal/vertical flips, slight brightness/contrast shifts, and small zoom variations. This increases the effective number of crack samples and helps the model generalize. Do not apply heavy geometric distortions that would make cracks unrealistic.
  4. Use class weights in the loss function. In PyTorch, pass a weight tensor to nn.CrossEntropyLoss(weight=class_weights). In TensorFlow, use model.compile(loss=tf.keras.losses.BinaryCrossentropy(class_weight=class_weights)). Compute weights as w_i = total_samples / (n_classes * class_count_i).
  5. Oversample the minority class during training. Use a sampler like WeightedRandomSampler in PyTorch or class_weight in Keras fit(). This makes the model see crack images more frequently per epoch.
  6. Alternatively, undersample the majority class. Randomly drop no-crack images to match the crack count. This is faster but loses data; use only if the majority class is very large (e.g., >100k images).
  7. Train with a suitable metric. Accuracy is misleading with imbalance. Use F1-score, precision, recall, and AUC-ROC as your primary metrics. Monitor recall for the crack class specifically.
  8. Set a decision threshold. After training, adjust the classification threshold (default 0.5) to favor recall if missing cracks is costly. Plot precision-recall curves to find the optimal threshold.
  9. Evaluate on the original test distribution. Do not rebalance the test set. Report metrics on the real-world ratio to reflect actual performance.

Common Mistakes

  • Using accuracy as the main metric. With 90% no-crack images, a model that predicts everything as no-crack gets 90% accuracy but is useless.
  • Applying augmentation to the majority class too. This can overfit the model to the majority class; focus augmentation on the minority.
  • Setting class weights incorrectly. Weights should be inversely proportional to class frequency; if you set them too high, the model may overfit to the minority class.
  • Oversampling by simply duplicating images. Without augmentation, duplicates cause overfitting. Always use augmented copies.
  • Ignoring validation set imbalance. If you randomly split, the validation set may have a different ratio, leading to misleading results.

Tips

  • Start with class weights and augmentation. They are simple and often sufficient. Only move to complex methods if needed.
  • Use focal loss. This loss function down-weights easy examples and focuses on hard ones, which is effective for extreme imbalance.
  • Try ensemble methods. Train multiple models with different rebalancing strategies and average their predictions to improve robustness.
  • Monitor training curves. Watch for divergence between training and validation loss; if training loss decreases but validation doesn’t, you’re overfitting.
  • Use a pretrained backbone. Transfer learning with a model pretrained on ImageNet (e.g., ResNet) often works well even with limited data.

Troubleshooting

  • Model predicts everything as no-crack. This usually means the class weight is too low or the learning rate is too high. Increase the weight for the crack class and reduce learning rate.
  • Validation loss fluctuates wildly. Your batch size might be too small with oversampling, causing noisy gradients. Increase batch size or use a larger validation set.
  • Overfitting to the minority class. If recall is high but precision is very low, you’re overfitting to cracks. Add dropout, increase L2 regularization, or reduce the class weight.
  • Training is too slow. Oversampling can slow down each epoch. Try undersampling or use a smaller dataset first to debug.
  • Augmentation makes cracks look unrealistic. If the model fails on real test images, reduce augmentation intensity. Cracks are thin and directional; heavy rotations may break them.

Frequently Asked Questions

What is a good class weight for a 1:10 imbalance?

Use the inverse frequency: weight for crack = 10, weight for no-crack = 1. You can also use the formula total/(2*class_count) for a softer effect.

Should I use oversampling or undersampling?

Oversampling with augmentation is generally safer because it keeps all majority data. Undersampling is faster but loses information; use it only if the majority class is huge.

Can I use SMOTE for image data?

SMOTE works on feature vectors, not raw images. You could apply it to features extracted from a pretrained CNN, but it’s rarely used because augmentation is simpler and more effective.

How do I choose the decision threshold?

Plot the precision-recall curve and pick the threshold that gives the best trade-off for your application. If missing a crack is dangerous, choose a lower threshold (e.g., 0.3).

Is focal loss better than class weights?

Focal loss often works better for extreme imbalance (e.g., 1:1000) because it focuses on hard examples. For moderate imbalance (1:10), class weights are usually sufficient.

Do I need to rebalance the test set?

No. The test set should reflect the real-world class distribution to give an honest estimate of performance.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.