# Real-Time Detection of Hole-Type Defects on Industrial Components Using Raspberry Pi 5
## 4 Results and Real-Time Deployment
### ResNet50  
The ResNet50 architecture was trained and optimized for binary classification of industrial defects using a robust and deployment-aware pipeline. The training strategy incorporated two-stage fine-tuning, initially updating only the classifier head, followed by full-model unfreezing at a lower learning rate combined with advanced augmentations such as random flips, rotations, brightness/contrast adjustments, and Gaussian noise. To mitigate the effects of class imbalance (72% defect, 28% no defect), a dynamically weighted cross-entropy loss function was employed. The model was trained on a grayscale image dataset comprising 43,482 samples and evaluated on a stratified test set of 6523 images. ResNet50 achieved near-perfect test performance, with 99.98% accuracy, a precision of 0.9994, recall of 1.0000, F1-score of 0.9997, and mean average precision (mAP) of 1.0000.  
The training and validation curves for ResNet50 (Figure 6) demonstrate a consistent decline in both training and validation loss, with minimal divergence between the two, indicating effective mitigation of overfitting. Validation accuracy steadily increased and plateaued above 99.9%, closely mirroring training accuracy, which suggests that the model generalized well across unseen data. The low and stable validation loss throughout training further confirms that the optimization strategy, including early stopping and class-weighted loss, contributed to robust convergence.  
Figure 6: Training and validation accuracy/loss curves for ResNet50.  
To enhance interpretability, Grad-CAM visualizations were generated to analyze the spatial attention of the ResNet50 model during inference (Figures 7 and 8). In true positive cases, where both the predicted and actual labels are correct, the model's attention maps consistently highlight relevant structural regions such as holes, edges, and misaligned areas confirming that the model makes decisions based on meaningful visual cues. These heatmaps exhibit tight localization on defect regions for defective samples and diffused, non-anomalous focus for non-defective ones. Additionally, Grad-CAM results for misclassified cases (false positives) reveal that while the samples were defect-free, the model concentrated on regions with minor visual irregularities or artifacts that resembled defects (e.g., dark shadows, non-uniform reflections). This indicates a conservative bias in the model toward erring on the side of caution, an acceptable trade-off in high-stakes industrial contexts where false negatives are more critical than false positives. Together, these visual insights validate the model's attention mechanisms and support its use in real-time quality inspection tasks where explainability is essential for operator trust and certification compliance.  
Figure 7: Grad-CAM visualizations for ResNet50 correct predictions with activation concentrated on irregular or anomalous areas. The color gradient from blue to red represents increasing importance in the model’s decision-making, with red indicating the highest influence.