Advanced Detection System for Casting Part Surface Defects

In modern manufacturing industries, the quality of casting parts is paramount, as defects can lead to catastrophic failures in critical applications such as automotive, aerospace, and metallurgical sectors. Traditional inspection methods for casting part surfaces, including manual visual checks and non-destructive testing techniques, often suffer from inefficiencies, high costs, and limited accuracy, especially in real-time production environments. With the advent of deep learning and computer vision, automated defect detection systems have emerged as a promising solution. In this work, we propose an enhanced object detection algorithm based on YOLOv8n, termed SPI, which integrates structural improvements and channel-wise knowledge distillation (CWD) to achieve superior performance in casting part surface defect detection. We further develop a comprehensive detection system that combines this algorithm with a database for real-time analysis and management of defect data. This article details our methodology, experimental validation, and system implementation, emphasizing the importance of casting part quality control and the advancements our approach brings.

The casting process involves transforming molten metal into solid shapes, and surface defects such as gas holes, sand inclusions, shrinkage cavities, and misruns can arise due to factors like improper pouring temperatures, mold issues, or material impurities. Detecting these defects early is crucial for ensuring the reliability and safety of casting parts. However, challenges persist in achieving high precision and speed, particularly for small or subtle defects. Existing machine learning-based methods, including convolutional neural networks (CNNs) and YOLO variants, have shown potential but often face trade-offs between accuracy, computational complexity, and real-time performance. For instance, while models like YOLOv5 or YOLOv7 offer improvements, they may not adequately address the unique characteristics of casting part defects, such as varied scales and textures. Therefore, we aim to refine these approaches by introducing a lightweight yet powerful network architecture and leveraging knowledge distillation to enhance detection capabilities.

Our proposed SPI algorithm builds upon the YOLOv8n framework, incorporating three key modifications: the S-FasterNet backbone network, the P-C2f module, and the Inner-CIoU loss function. The S-FasterNet replaces the original CSPDarknet backbone to reduce redundant computations and memory access while capturing rich contextual information in both horizontal and vertical dimensions. This is particularly beneficial for casting part defects, which can exhibit elongated or irregular shapes. The P-C2f module integrates polarized self-attention mechanisms to enhance feature representation with minimal information loss, and the Inner-CIoU loss improves localization accuracy by adapting to varying intersection-over-union (IoU) scenarios. Subsequently, we apply channel-wise knowledge distillation (CWD) to distill knowledge from a larger teacher model (SPIm) to the student SPI model, further boosting performance without increasing model size. Below, we elaborate on each component and present experimental results demonstrating the effectiveness of our approach for casting part inspection.

The S-FasterNet backbone is designed to optimize feature extraction for casting part surface defects. It utilizes partial convolutions (PConv) to selectively process channels, significantly reducing floating-point operations (FLOPs) and memory access costs. For an input feature map of size $$H \times W \times C$$, where $$H$$ is height, $$W$$ is width, and $$C$$ is channels, a conventional convolution with kernel size $$k$$ produces an output of size $$H’ \times W’ \times C’$$, with FLOPs computed as:

$$F_{SC} = H’ \times W’ \times C’ \times C \times k^2$$

In contrast, PConv operates on a subset of $$C_p$$ continuous channels, typically set to $$C_p = C/4$$, leading to reduced FLOPs:

$$F_{PC} = H’ \times W’ \times C_p^2 \times k^2$$

This results in approximately 1/16 of the FLOPs compared to standard convolutions. Additionally, the memory access cost (MAC) is minimized:

$$G_{PC} = H’ \times W’ \times 2C_p + k^2 \times C_p^2$$

To further enhance spatial context, we replace global pooling with strip pooling in the S-FasterNet, which employs horizontal and vertical pooling kernels of size $$H \times 1$$ and $$1 \times W$$, respectively. This allows the network to capture long-range dependencies along both axes, preserving fine details in casting part defects. The output feature map $$z$$ is computed as:

$$z = S(x, \sigma(f(y)))$$

where $$x$$ is the input feature map, $$S(\cdot)$$ denotes element-wise multiplication, $$\sigma(\cdot)$$ is the Sigmoid activation function, and $$f(\cdot)$$ represents a $$1 \times 1$$ convolution layer. The intermediate feature $$y$$ is derived from horizontal and vertical pooling operations: $$y_{c,i,j} = y_{c,i}^h + y_{c,j}^v$$, with $$y_{c,i}^h \in \mathbb{R}^{C \times H}$$ and $$y_{c,j}^v \in \mathbb{R}^{C \times W}$$. This design ensures efficient and effective feature extraction tailored for casting part surfaces.

For the neck and head parts of the network, we introduce the P-C2f module, which combines the C2f structure with polarized self-attention (PSA). PSA includes channel polarized self-attention (CPSA) and spatial polarized self-attention (SPSA), enabling the model to focus on discriminative features in both channel and spatial domains. Given an input feature map $$X$$, CPSA computes channel attention weights as:

$$A_{ch}(X) = F_{SG}[W_Z(\sigma_1(W_V(X)) \otimes F_{SM}(\sigma_2(W_Q(X)))]$$

where $$W_Q$$, $$W_V$$, and $$W_Z$$ are independent $$1 \times 1$$ convolutions, $$\sigma_1$$ and $$\sigma_2$$ are reshaping operations, $$F_{SG}$$ and $$F_{SM}$$ are Sigmoid and Softmax functions, and $$\otimes$$ denotes tensor multiplication. Similarly, SPSA computes spatial attention:

$$A_{sp}(X) = F_{SG}[\sigma_3(F_{SM}(\sigma_1(F_{GP}(W_Q(X)))) \otimes \sigma_2(W_V(X)))]$$

where $$F_{GP}$$ is global average pooling. By integrating PSA into the C2f module, the P-C2f enhances feature representation while minimizing information loss, crucial for detecting subtle defects in casting parts.

To improve localization accuracy, we adopt the Inner-CIoU loss function for bounding box regression. Traditional CIoU loss incorporates center distance and aspect ratio, but it can suffer from gradient saturation in partially overlapping cases. Inner-CIoU introduces a scale factor $$s_{ratio}$$ to adjust the prediction box dimensions, with auxiliary box coordinates computed as:

$$B_l = x_c – \frac{w \cdot s_{ratio}}{2}, \quad B_r = x_c + \frac{w \cdot s_{ratio}}{2}$$
$$B_t = y_c – \frac{h \cdot s_{ratio}}{2}, \quad B_b = y_c + \frac{h \cdot s_{ratio}}{2}$$

where $$(x_c, y_c)$$ is the center of the anchor box, and $$w$$ and $$h$$ are its width and height. The inner IoU is then calculated as:

$$L_{IoU_{inner}} = \frac{\text{Inter}}{\text{Union}}$$

with $$\text{Inter}$$ and $$\text{Union}$$ representing the intersection and union areas of the adjusted boxes. The overall Inner-CIoU loss is defined as:

$$L_{Inner-CIoU} = L_{CIoU} + L_{IoU} – L_{IoU_{inner}}$$

This dynamic scaling mechanism, with $$s_{ratio}$$ ranging from 0.5 to 1.5, adapts to different IoU levels, enhancing regression performance for casting part defects of varying sizes and overlaps.

After developing the SPI algorithm, we employ channel-wise knowledge distillation (CWD) to further refine its performance. CWD transfers knowledge from a teacher model (SPIm, an expanded version of SPI) to the student model (SPI) by aligning their channel-wise probability distributions. The distillation loss is based on Kullback-Leibler (KL) divergence:

$$L_{distill}(y^T, y^S) = T^2 \sum_{c=1}^{C} \sum_{j=1}^{W \cdot H} \phi(y_{c,j}^T) \ln \frac{\phi(y_{c,j}^T)}{\phi(y_{c,j}^S)}$$

where $$T$$ is a temperature parameter (set to 2 in our experiments), $$y^T$$ and $$y^S$$ are activation maps from teacher and student networks, and $$\phi(\cdot)$$ is a Softmax function that converts activations to probabilities: $$\phi(y_c) = \exp(y_{c,j}/T) / \sum_{j=1}^{W \cdot H} \exp(y_{c,j}/T)$$. The total loss during training combines the standard training loss and the distillation loss:

$$L_{total} = L_{train} + \alpha L_{distill}$$

with $$\alpha = 0.25$$. This approach allows the student model to learn rich feature representations from the teacher, improving accuracy without increasing model complexity, which is vital for real-time casting part defect detection.

To validate our method, we conducted extensive experiments on a dataset of casting part surface defects, including gas holes, sand inclusions, shrinkage cavities, and misruns. The dataset comprises 2,319 images, augmented with techniques like random noise addition, Gaussian filtering, and spatial transformations to enhance diversity. We split the data into training and testing sets in an 8:2 ratio. Our experimental platform includes an NVIDIA GeForce RTX 4060 GPU, Intel Core i5-12400F CPU, and PyTorch framework. Key evaluation metrics are mean average precision (mAP), precision (P), recall (R), frames per second (FPS), and model size (S). The mAP is calculated as:

$$\text{mAP} = \frac{\sum_{i=0}^{n} AP(i)}{n}$$

where $$n$$ is the number of classes (here, $$n=4$$), and $$AP$$ is the average precision for each class, derived from the precision-recall curve: $$AP = \int_0^1 p(R) dR$$. Precision and recall are defined as:

$$P = \frac{TP}{TP + FP}, \quad R = \frac{TP}{TP + FN}$$

with $$TP$$, $$FP$$, and $$FN$$ being true positives, false positives, and false negatives, respectively. FPS measures real-time performance: $$FPS = FR / ET$$, where $$FR$$ is the number of frames processed and $$ET$$ is the elapsed time.

We performed ablation studies to assess the contribution of each component in the SPI algorithm. The results are summarized in the table below, showing improvements over the baseline YOLOv8n model.

Method Precision (P) Recall (R) mAP@0.5 mAP@0.95 FPS Model Size (MB)
YOLOv8n (Baseline) 85.1% 72.4% 80.0% 46.9% 200 5.98
+ S-FasterNet 85.6% 76.9% 82.9% 48.3% 211 ~6.0
+ P-C2f 84.6% 76.4% 82.4% 47.7% 191 ~6.5
+ Inner-CIoU 86.7% 74.2% 82.0% 47.6% 220 5.98
SPI (Full) 90.0% 79.0% 84.8% 48.8% 213 9.3
CWD-SPI 90.1% ~79.0% 85.9% 50.8% ~210 9.3

The ablation results indicate that each modification contributes to performance gains. Specifically, the S-FasterNet backbone improves mAP by 2.9% and FPS by 11, demonstrating its efficiency for casting part defect detection. The P-C2f module enhances feature representation, albeit with a slight FPS drop, while Inner-CIoU boosts precision and localization. The full SPI model achieves an mAP@0.5 of 84.8%, a 4.8% improvement over the baseline, with maintained real-time performance. After applying CWD, the CWD-SPI model reaches an mAP@0.5 of 85.9%, further validating the effectiveness of knowledge distillation for casting part applications.

We also compared our CWD-SPI algorithm with other state-of-the-art detection models, as shown in the following table. The comparison highlights the superiority of our approach in balancing accuracy and speed for casting part surface defect detection.

Model mAP@0.5 FPS Model Size (MB)
Faster R-CNN 75.0% 31.2 100
SSD 61.3% 73.2 80
YOLOv5s 80.0% 166 17.7
YOLOv7-tiny 73.3% 193 11.7
YOLOv8n 80.0% 200 5.98
YOLOv10n 76.7% 225 5.51
CWD-SPI (Ours) 85.9% ~213 9.3

Our CWD-SPI model outperforms all competitors in mAP@0.5, with a significant margin over YOLOv8n (5.9% higher) and YOLOv10n (9.2% higher), while maintaining competitive FPS and model size. This makes it highly suitable for industrial deployment in casting part production lines, where both accuracy and real-time processing are critical.

To provide deeper insights, we analyzed the precision-recall curves and heatmaps generated by our model. The SPI algorithm shows improved focus on defect regions compared to the baseline, with higher activation intensities around casting part anomalies like gas holes and sand inclusions. This enhanced feature extraction capability stems from the S-FasterNet’s strip pooling and PSA mechanisms, which better capture contextual information. For instance, in cases of misrun defects, which often appear as elongated patterns, our model successfully identifies them with high confidence, reducing false negatives and misclassifications.

Building on the algorithmic advancements, we developed a casting part surface defect detection system that integrates the CWD-SPI model with a MySQL database for real-time monitoring and data management. The system is implemented using PyCharm as the IDE, PyQt5 for the graphical user interface (GUI), and Python for backend logic. It supports multiple functionalities, including image input, defect detection, result visualization, and statistical analysis. The GUI features two main interfaces: a real-time detection interface and a casting part information management interface. In the detection interface, users can upload images or videos of casting parts, run the CWD-SPI model to identify defects, and view bounding boxes with confidence scores. The results are displayed alongside detailed data, such as defect type, location, and size, enabling quick quality assessment. The management interface allows users to log and query casting part information, including production IDs, defect grades, and inspection timestamps, facilitating traceability and process optimization. By storing detection results in the MySQL database, the system enables historical analysis and trend identification, helping manufacturers improve casting part quality over time.

In conclusion, we have presented a comprehensive approach for casting part surface defect detection, centered on the SPI algorithm enhanced with channel-wise knowledge distillation. Our method addresses key challenges in accuracy and speed by incorporating a lightweight S-FasterNet backbone, a feature-enhancing P-C2f module, and an adaptive Inner-CIoU loss function. The CWD training strategy further boosts performance, achieving an mAP@0.5 of 85.9% on a casting part defect dataset, which surpasses existing models while maintaining real-time capabilities. The developed detection system leverages this algorithm to provide a practical solution for industrial quality control, with user-friendly interfaces and database integration. Future work may explore extending the model to other defect types or adapting it for 3D casting part inspection. Overall, this research underscores the potential of deep learning and knowledge distillation in advancing casting part manufacturing, ensuring higher reliability and efficiency in defect detection processes.

Scroll to Top