A deep learning approach for detecting and segmenting crack propagation in materials under electromechanical stress using U-Net architecture with ResNet backbones. This project applies computer vision techniques to analyze phase-field and electrical potential patterns from FEM simulations.
This repository implements semantic segmentation models to detect crack patterns in materials subjected to coupled mechanical and electrical stresses. The computer vision pipeline processes grayscale images from FEM simulations to identify and classify different regions:
- Phase Field (PHI): Background, cracks, and holes
- Electrical Potential (V): Left side, right side, and holes
Below is an example illustrating the model's input, the ground truth segmentation, and the model's predicted segmentation mask:
- U-Net with skip connections for precise localization
- ResNet backbones (34, 50, 101, 152) for feature extraction
- Transfer learning from ImageNet pre-trained weights
- Multi-class semantic segmentation (3 classes)
- Pixel-level crack detection with IoU > 0.95
- Comparison between phase-field and electrical potential visualization methods
- Automated hyperparameter tuning using Keras Tuner
- 10,000 FEM simulations generated using ABAQUS
- Image dimensions: 512×512 pixels
- Data split: 70% training, 20% validation, 10% test
- Classes:
- Class 0: Background
- Class 1: Minor defects/features
- Class 2: Major cracks/boundaries
Traditional crack detection methods rely on edge detection algorithms (Canny, Sobel) or threshold-based approaches, which fail to:
- Distinguish between different severity levels of defects
- Handle varying lighting conditions and surface textures
- Provide pixel-accurate segmentation masks
- Generalize across different materials and imaging conditions
We employ semantic segmentation - a computer vision technique that classifies each pixel in an image into predefined categories. Unlike object detection (bounding boxes) or image classification (whole image labels), semantic segmentation provides:
- Pixel-level precision: Every pixel gets a class label
- Multi-class support: Distinguish between defect severities
- Spatial understanding: Preserve exact shape and location of defects
- End-to-end learning: No manual feature engineering required
U-Net is a fully convolutional network (FCN) specifically designed for biomedical image segmentation but has proven highly effective for industrial defect detection.
Input (512×512×3)
│
┌──────────────────────┴──────────────────────┐
│ ENCODER │
│ ┌─────────────────────────────────────┐ │
│ │ Conv Block 1: 512×512×64 │ │ ──┐ Skip Connection
│ │ MaxPool 2×2 ↓ │ │ │
│ │ Conv Block 2: 256×256×128 │ │ ──┤
│ │ MaxPool 2×2 ↓ │ │ │
│ │ Conv Block 3: 128×128×256 │ │ ──┤
│ │ MaxPool 2×2 ↓ │ │ │
│ │ Conv Block 4: 64×64×512 │ │ ──┤
│ │ MaxPool 2×2 ↓ │ │ │
│ └─────────────────────────────────────┘ │ │
│ │ │
│ Bottleneck │ │
│ 32×32×1024 │ │
│ │ │
│ DECODER │ │
│ ┌─────────────────────────────────────┐ │ │
│ │ UpConv 2×2 + Skip ← 64×64×512 │←──┼───┘
│ │ Conv Block │ │
│ │ UpConv 2×2 + Skip ← 128×128×256 │←──┤
│ │ Conv Block │ │
│ │ UpConv 2×2 + Skip ← 256×256×128 │←──┤
│ │ Conv Block │ │
│ │ UpConv 2×2 + Skip ← 512×512×64 │←──┘
│ │ Conv Block │ │
│ └─────────────────────────────────────┘ │
└──────────────────────┴──────────────────────┘
│
Output Conv 1×1
│
Segmentation Map (512×512×3)
Follow these steps to set up your environment and execute the scripts:
- Python: Ensure you have Python 3.8 or newer installed.
- Git: Required for cloning the repository.
- (Optional) ABAQUS: Needed only if you intend to regenerate the primary dataset from FEM simulations as described in the thesis.
-
Clone the Repository: Open your terminal or command prompt and run:
git clone [https://github.com/BBahtiri/Computer-Vision-Crack-Detection.git](https://github.com/BBahtiri/Computer-Vision-Crack-Detection.git) cd Computer-Vision-Crack-Detection
-
Create and Activate a Virtual Environment (Recommended):
python -m venv venv
Activate the environment:
- On Windows:
venv\Scripts\activate
- On macOS/Linux:
source venv/bin/activate
- On Windows:
-
Install Dependencies: Install all required Python libraries using the
requirements.txt
file:pip install -r requirements.txt
-
Dataset Source: The primary dataset for this project was generated from 10,000 Finite Element Method (FEM) simulations using ABAQUS, as detailed in the accompanying Master Thesis. If you are not regenerating this dataset, you will need to have your set of images (e.g.,
*_frame_0.png
) and their corresponding label masks (e.g.,*_frame_last.png
). -
Organize Your Data: Before splitting, place your raw images and labels into separate directories. For example:
data_root/ ├── all_images/ │ ├── sim0001_frame_0.png │ └── ... └── all_labels/ ├── sim0001_frame_last.png └── ...
-
Split into Train/Validation/Test Sets: Use the
src/split_data.py
script to organize your dataset into the required structure for training and evaluation.python src/split_data.py path/to/your/all_images path/to/your/all_labels --output-dir ./name_of_processed_data_directory
Replace
path/to/your/all_images
andpath/to/your/all_labels
with the actual paths to your data. This command will create subdirectories liketrain_images/
,train_labels/
,val_images/
,val_labels/
,test_images/
, andtest_labels/
inside the specified--output-dir
(e.g.,./data_dir_PHI
or./data_dir_V
). -
Configure Script Paths: Before running any training or inference script, you must update the
DATA_DIR
variable (and potentially other path-related variables) within theConfig
class at the beginning of that specific Python script (e.g., insrc/hyper.py
,src/phi.py
,src/prediction_new_model.py
). This path should point to your processed data directory created in the previous step.For example, in
src/phi.py
, you might change:class Config: DATA_DIR = "./name_of_processed_data_directory" # Or, e.g., "data_dir_PHI" # ... other configurations
Ensure your virtual environment is activated and you are in the root directory of the project (Computer-Vision-Crack-Detection
) in your terminal.
-
Hyperparameter Tuning (Recommended to find optimal model parameters):
- Verify/update the
Config
class insrc/hyper.py
(especiallyDATA_DIR
). - Run the script:
python src/hyper.py
- This will initiate the Keras Tuner search. The best model found will be saved (e.g.,
best_unet_hyper_tuned_corrected.h5
), and logs will be stored in a directory likehyperband_tuning_output_corrected/
.
- Verify/update the
-
Training a Specific Model Configuration:
- To train a model using, for example, the custom U-Net defined in
src/phi.py
:- Verify/update the
Config
class insrc/phi.py
. - Run the script:
python src/phi.py
- Verify/update the
- Similarly, you can run
src/seg_model_train.py
after configuring itsConfig
class. - These scripts will save the trained model (e.g.,
best_model_phi_corrected.h5
) and create training logs.
- To train a model using, for example, the custom U-Net defined in
-
Inference (Running Predictions on New Data):
- Modify the
Config
class insrc/prediction_new_model.py
:- Set
MODEL_PATH
to the path of your trained.h5
model file. - Ensure
BACKBONE
matches the architecture of the loaded model. - Set
TEST_IMAGES_DIR
andTEST_LABELS_DIR
to your test dataset paths.
- Set
- Execute the script:
python src/prediction_new_model.py
- This will load the specified model, perform predictions on images from your test set, and visualize the results. Predictions might be saved in an
predictions_output/
directory if configured.
- Modify the
- IoU (Intersection over Union): Primary metric for segmentation accuracy
- F1-Score: Harmonic mean of precision and recall
- Confusion Matrix: Per-class classification accuracy
This work is done in collaboration with Jaykumar Mavani