Deep Learning for Casting Defect Recognition

In modern manufacturing, casting defects remain one of the most critical factors affecting product reliability. The aerospace sector in particular relies on light-alloy castings, and even small casting defects can lead to catastrophic consequences. In this work, I investigate an automated method for casting defect recognition using deep neural networks. The core objective is to detect and localize multiple categories of casting defects from X-ray inspection images.

The data used throughout this work were collected from an X-ray computed tomography system on an aerospace light-alloy casting production line. Manual inspection of radiographic films is still prevalent in many foundries, but it suffers from low throughput, high cost, and subjective variability. I therefore designed a deep learning framework that can automatically identify casting defects and meet the speed requirements of modern automated casting lines.

Problem Context and Data Analysis

Light-alloy castings are widely used in aerospace, marine, and automotive industries because of their excellent strength-to-weight ratio. In the casting process, the solidification of molten metal is affected by gas evolution, mould conditions, cooling rates, and chemical segregation. As a result, multiple forms of casting defects can appear inside the product. The most common categories observed in this work are gas porosity, slag inclusion, shrinkage, cracks, and segregation.

I obtained 1,451 X-ray images with a resolution of 2176×1792 pixels. Among these images, 1,137 images were defect-free, while 314 images contained 421 labeled casting defects. All bounding-box annotations were validated with experienced quality inspectors. I divided the defect images into a training set and a test set using an 8:2 ratio. The training set contained 252 images, and the test set contained 62 images. This limited amount of data is one of the major challenges for training a deep detector.

The distribution of casting defects is highly irregular. Some defects such as cracks and segregation occupy large elongated regions, while gas porosity and slag inclusions are often very small. To quantify this observation, I define defect size from the pixel area of the annotated bounding box. The definition is shown in the following table.

Defect size Minimum pixel area Maximum pixel area
Micro defect 0 6400
Medium defect 6400 40000
Large defect 40000 Infinity

Micro defects accounted for nearly 47% of all defects, while large defects accounted for a substantial proportion as well. The defects also showed a large variation in aspect ratio. Most defects had an aspect ratio below 2, but a small number of cracks and shrinkage defects were elongated with an aspect ratio greater than 5. This multi-scale and multi-shape property makes casting defect recognition particularly difficult.

In addition to the geometric variation, class imbalance is severe. The ratio between the most frequent and least frequent defect categories approached 4:1. If the model is trained without appropriate data augmentation, it tends to bias towards categories with more samples. Therefore, data augmentation must be designed carefully before training the detection network.

Evaluation Metrics

To evaluate the performance of casting defect recognition, I used the standard object detection metrics based on intersection over union. A predicted bounding box is considered correct if it has the same class label as the ground-truth box and the IoU value is at least 0.5. The IoU between a ground-truth box B_truth and a predicted box B_pred is

$$IoU(B^{truth}, B^{pred}) = \frac{B^{truth} \cap B^{pred}}{B^{truth} \cup B^{pred}}$$

Precision and recall are defined from the confusion matrix of each defect category. True positive predictions are denoted by TP, false positives by FP, and false negatives by FN. I computed precision and recall as

$$\text{Precision} = \frac{TP}{TP + FP}$$

$$\text{Recall} = \frac{TP}{TP + FN}$$

The precision-recall curve is constructed by varying the confidence threshold. The average precision for a single class is the area under the precision-recall curve,

$$AP = \int_{0}^{1} p(r) \, dr$$

where p(r) is precision as a function of recall r. The mean average precision over all defect classes is

$$mAP = \frac{1}{C} \sum_{c=1}^{C} AP_c$$

In addition to AP and mAP, I also report the miss rate for each class, which is defined as 1 minus recall. This metric is important for quality inspection because missing a defect can be more dangerous than producing a false alarm.

Data Augmentation for Micro Casting Defects

Since the original X-ray images are large and the number of casting defects is limited, I first applied an overlap-based cropping method. The images were cut into sub-images with a sliding window of size 300×300 and an overlap rate of 15%. The overlap criterion ensures that micro defects can appear completely in at least one sub-image. After cropping, a sub-image was retained when it contained a sufficient portion of an annotated defect. The retained ratio r is defined as

$$r = \frac{R(gt \cap sub)}{R(gt)}$$

Here, gt denotes the ground-truth bounding box, sub denotes the sub-image, R represents the area function, and r is the percentage of the original defect bounding box that falls inside the sub-image. I empirically kept a sub-image when r > 0.1. The selected sub-images were resized to 448×448 pixels. For images containing large casting defects, I also resized the entire original image to 448×448 to preserve contextual information. After geometric transformations including rotation, horizontal flipping, and vertical flipping, the training set was expanded from 252 original images to 2,779 training samples.

In addition, I used a simplified Mosaic augmentation strategy. The original Mosaic method combines four images into one new image by random cropping, scaling, color jitter, and spatial arrangement. Since X-ray images have physical intensity values, I avoided color-domain distortions. In the simplified method, four images are arranged in four quadrants. Two random split lines are generated in the ranges [0.4w, 0.6w] and [0.4h, 0.6h], where w and h define the final template size. The template size was set to 608×608 pixels. Each quadrant is cropped and the four cropped regions are stitched together. This operation increases the number of defects per image and improves the complexity of the background, which is beneficial for training a robust detector.

YOLO-Based Casting Defect Detection

After data augmentation, I designed a one-stage detection network based on the YOLO detection philosophy. The goal is to predict the class and bounding box of casting defects directly from the image in a single forward pass. In the YOLO framework, the image is divided into an S×S grid. If the center of a casting defect falls inside a grid cell, that cell is responsible for predicting the defect. Each cell produces several bounding boxes, and each predicted box contains the center coordinates, width, height, and a confidence score.

The confidence score is defined as

$$\text{Score} = Pr(class_i \mid object) \cdot Pr(object) \cdot IoU_{pred}^{truth}$$

The network also uses anchor boxes. Given an anchor with width p_w and height p_h, the predicted bounding box coordinates are computed as

$$b_x = \sigma(t_x) + c_x$$

$$b_y = \sigma(t_y) + c_y$$

$$b_w = p_w e^{t_w}$$

$$b_h = p_h e^{t_h}$$

where c_x and c_y are the top-left coordinates of the grid cell, and sigma is the sigmoid activation function that keeps the predicted center inside the cell.

For feature extraction, I adopted a Darknet-53 architecture. The network contains a series of convolutional blocks and residual blocks. A convolutional block consists of convolution, batch normalization, and LeakyReLU activation. The residual structure helps to train deeper networks without vanishing gradients. To handle the multi-scale property of casting defects, I connected the feature extraction network to a feature pyramid network. The feature pyramid produces three prediction scales at different spatial resolutions, allowing the network to detect both micro defects and large defects.

Backbone stage Output spatial size Output channels
Stage 1 608×608 32
Stage 2 304×304 64
Stage 3 152×152 128
Stage 4 76×76 256
Stage 5 38×38 512
Stage 6 19×19 1024

The three detection heads receive feature maps from the three deepest stages of the feature pyramid and produce prediction tensors of shape 76×76×33, 38×38×33, and 19×19×33, respectively. The number 33 is calculated from (5 + number of classes) × 3 anchors per scale, and with 6 classes this yields 11 values per anchor.

Anchor Design Using k-Means++

Choosing appropriate anchor sizes is critical for casting defect detection. I used the k-means++ algorithm to cluster the width and height of all annotated defect bounding boxes. Instead of Euclidean distance, I used the IoU distance because it directly reflects the overlap quality between a candidate anchor and a real ground-truth box. The distance is defined as

$$d(box_i, box_j) = 1 – IoU(box_i, box_j)$$

In k-means++, the initial cluster centers are selected in a probabilistic way. The probability of selecting a sample box_i as the next cluster center is

$$p_{ij} = \frac{D^2(box_i, c_j)}{\sum_{box \in X} D^2(box, c_j)}$$

After initial centers are chosen, each sample is assigned to the nearest center, and the centers are updated as the mean of the samples in each cluster,

$$c_j = \frac{1}{|X_j|} \sum_{box \in X_j} box$$

To determine the number of anchors, I computed the average IoU between all ground-truth boxes and their closest anchor. The results are presented in the following table.

Number of anchors k 1 2 3 4 5 6 7 8 9
Average IoU 0.357 0.581 0.679 0.724 0.768 0.796 0.807 0.812 0.820

The average IoU grows quickly before k=6 but saturates afterwards. Therefore, I chose six anchors, which were assigned to the three detection heads according to the scale. This design improves the localization ability of the model for casting defects of different sizes.

Test-Image Detection with Bounding-Box Suppression

Because the network was trained with sub-images, I could not feed an entire high-resolution X-ray image directly into the model. During testing, I used a sliding-window strategy similar to training. A window of size 407×407 was moved over the test image and then resized to 608×608. In addition, the whole test image was resized to 608×608 and sent to the network. All detection results from these sub-images were transformed back to their original positions and concatenated into a single output map.

The overlapping sliding windows often produced multiple bounding boxes for the same casting defect. To solve this issue, I proposed a bounding-box suppression method. For two boxes A and B, the overlap ratios are defined as

$$r_A = \frac{R(A \cap B)}{R(A)}$$

$$r_B = \frac{R(A \cap B)}{R(B)}$$

If either ratio exceeds a threshold of 0.7, the box with the smaller overlap value is suppressed. After suppression, the remaining boxes that are connected by overlapping regions are merged into their minimum enclosing rectangle. This operation creates one final bounding box for each casting defect.

Experimental Validation of the YOLO-Based Method

To validate the proposed detection framework, I trained several model combinations. The first model, called Model 1, used overlap cropping, Mosaic augmentation, and the proposed bounding-box suppression at test time. Model 2 removed Mosaic augmentation. Model 3 and Model 4 directly resized the original images to 608×608 without overlap cropping. Model 4 also removed Mosaic augmentation. The detection results are shown in the following table.

Model Gas AP Slag AP Shrinkage AP Crack AP Segregation AP mAP
Model 1 87.63 85.45 82.13 85.37 83.49 84.81
Model 2 84.89 83.59 81.68 84.36 82.16 83.34
Model 3 43.11 42.81 65.84 80.21 74.26 61.25
Model 4 42.69 44.21 64.75 77.95 73.59 60.64

The results clearly show that overlap cropping is essential for detecting micro casting defects. In Model 3 and Model 4, the AP values for gas porosity and slag inclusions are much lower than for the other classes. This is because these two types of casting defects are usually very small. When the original X-ray image is resized directly to 608×608, tiny defects lose too much information and cannot be recognized.

I also compared the miss rate of the proposed method with a traditional detection method based on a sliding window and a CNN classifier. The comparison is shown below.

Defect class Proposed Model 1 miss rate Traditional SWA+CNN miss rate
Gas porosity 7.14% 17.86%
Slag inclusion 9.09% 18.18%
Shrinkage 15.38% 23.08%
Crack 13.33% 20.00%
Segregation 12.50% 12.50%

Although the proposed method achieved good detection accuracy, the inference speed was still limited. Model 1 and Model 2 required overlap cropping at test time, so their speed was only 0.76 frames per second. This is too slow for practical online inspection. I therefore focused next on a lightweight network design that maintains detection accuracy while substantially improving inference speed.

Lightweight Improvement of the Detection Network

The original detection network used standard convolution operations. Standard convolution performs both spatial filtering and channel fusion in one step. The computational cost of a standard convolution is

$$Cost_{standard} = D_K \cdot D_K \cdot M \cdot N \cdot F_D \cdot F_D$$

where D_K is the kernel size, M is the number of input channels, N is the number of output channels, and F_D is the spatial size of the feature map. To reduce the computation, I replaced standard convolutions with depthwise separable convolutions. A depthwise separable convolution consists of a depthwise convolution and a pointwise convolution. The depthwise convolution applies a single filter to each input channel, and the pointwise convolution uses a 1×1 convolution to combine the channels. The total computational cost is

$$Cost_{depthwise} = D_K \cdot D_K \cdot M \cdot F_D \cdot F_D + M \cdot N \cdot F_D \cdot F_D$$

The ratio between the two costs is

$$\frac{Cost_{depthwise}}{Cost_{standard}} = \frac{1}{N} + \frac{1}{D_K^2}$$

For a 3×3 convolution, this ratio is approximately one eighth when the number of output channels is large. Therefore, depthwise separable convolution can greatly improve inference speed.

Inverse Residual Structure and Modified SE Block

The direct replacement of standard convolutions with depthwise separable convolutions reduced the number of parameters drastically. However, the model became more prone to overfitting and its generalization ability decreased. To solve this problem, I used inverse residual blocks. In a conventional residual block, the number of channels is first reduced, then the depthwise convolution is applied, and finally the number of channels is expanded. In the inverse residual block, the order is reversed: the channel number is first expanded, the depthwise convolution is applied, and then the channel number is reduced. This structure gives the depthwise convolution more channels to work with and improves feature representation.

I also added an enhanced squeeze-and-excitation block to the residual structure. The standard squeeze-and-excitation block first performs global average pooling to produce a channel descriptor,

$$z_c = \frac{1}{H \cdot W} \sum_{i=1}^{H} \sum_{j=1}^{W} X_c(i,j)$$

where X_c is the c-th channel of the input feature map. The descriptor is then passed through an excitation operation to compute the channel attention weights. In the original SE block, the excitation step uses two fully connected layers. To reduce the computational burden, I replaced the fully connected layers with a one-dimensional convolution,

$$s = \sigma(C1d(z))$$

where C1d denotes a one-dimensional convolution and sigma is the sigmoid activation. Finally, the feature map is recalibrated by multiplying each channel by its corresponding weight,

$$\tilde{X}_c = s_c \cdot X_c$$

The improved SE block increases the attention on informative channels and suppresses irrelevant channels. This is particularly useful for casting defect recognition because defects often occupy a small number of channels.

Improved Multi-Task Loss

In the YOLO-based detection network, the total loss contains three parts: localization loss, confidence loss, and classification loss. The original localization loss computes the errors of the center coordinates and width-height independently. This ignores the fact that a bounding box should be treated as a whole. To improve this situation, I used the Distance-IoU loss, which is defined as

$$L_{DIoU} = 1 – IoU + \frac{\rho^2(b, b^{gt})}{c^2}$$

where b and b_gt are the predicted and ground-truth centers, rho is the Euclidean distance between these centers, and c is the diagonal length of the smallest enclosing box covering both boxes. This loss function directly penalizes the normalized distance between the centers, and it converges faster than the original independent regression loss.

The confidence loss in the original YOLO framework suffers from severe positive-negative sample imbalance. In a typical training image, only a few grid cells contain casting defects, while the vast majority of cells are negative. To solve this problem, I used the focal loss for the confidence term. The focal loss is defined as

$$FL(p_t) = -\alpha_t (1-p_t)^{\gamma} \log(p_t)$$

where p_t is the model confidence for the true class, alpha_t is a class-balancing weight, and gamma is a focusing parameter. I set alpha_t to 0.22 and gamma to 2 in this work. The focal loss down-weights easy negative samples and forces the network to focus on harder examples.

Results of Lightweight Network and Loss Improvements

I tested four combinations of the baseline network, lightweight network, baseline loss, and improved loss. The baseline network is the original YOLO-style detection network described earlier. The lightweight network uses depthwise separable convolution, inverse residual blocks, and the modified SE block. The improved loss uses DIoU loss and focal loss. The results are shown in the following table.

Model Network type Loss type Gas AP Slag AP Shrinkage AP Crack AP Segregation AP mAP FPS
Model 5 Lightweight Improved 87.84 84.68 82.24 85.51 83.96 84.85 4.96
Model 6 Lightweight Base 86.97 84.36 81.02 84.16 82.98 83.90 4.96
Model 7 Base Improved 88.62 86.53 82.76 86.74 84.38 85.81 0.76
Model 8 Base Base 87.63 85.45 82.13 85.37 83.49 84.81 0.76

The lightweight network improved the inference speed from 0.76 FPS to 4.96 FPS, which is about 6.5 times faster. More importantly, when the lightweight network was combined with the improved loss, the mAP remained competitive at 84.85%, slightly higher than the original baseline model. This demonstrates that the lightweight design can be deployed in real-time production environments without sacrificing detection performance.

To understand the contribution of each modification, I conducted an ablation study. The following table summarizes the key configurations and their mAP and FPS values.

Depthwise convolution Inverse residual + SE Improved loss mAP FPS
No Yes Yes 85.81 0.76
Yes No No 82.75 4.96
Yes Yes No 83.90 4.96
Yes Yes Yes 84.85 4.96

The ablation results show that the depthwise separable convolution is the main reason for the speed improvement. However, using depthwise convolution alone reduces mAP because the model becomes too simple. The inverse residual structure and the modified SE block restore the generalization ability of the network. The improved loss also contributes a positive gain in mAP, although the gain is smaller than the gain from the SE block. The combination of all modifications produces the best overall trade-off between accuracy and speed.

Discussion

The deep learning framework described in this article addresses several challenges that are common in industrial casting defect recognition. First, the use of overlap cropping and Mosaic augmentation mitigates the shortage of training data and improves the detectability of micro casting defects. Second, the YOLO-based multi-scale detector can identify casting defects with widely varying sizes and aspect ratios. Third, the lightweight network improves inference speed sufficiently for practical deployment. Fourth, the improved localization and confidence losses stabilize training and help the network handle the severe sample imbalance encountered in X-ray inspection.

There are still some limitations. The proposed method sometimes confuses a crack with shrinkage because both defects appear as elongated dark regions in X-ray images. The detection of micro gas porosity in areas with strong background clutter remains difficult. In addition, the current test-time overlap cropping strategy is still slower than end-to-end fully convolutional detection on the original image. Future work will explore attention-based feature fusion, self-supervised pre-training, and advanced post-processing algorithms to further improve the accuracy and speed of casting defect recognition.

Conclusion

I proposed a complete pipeline for casting defect recognition based on deep learning. The pipeline includes defect-oriented data augmentation, multi-scale YOLO-style network design, anchor clustering, lightweight network optimization, and improved loss functions. The experimental results show that the proposed method can detect gas porosity, slag inclusion, shrinkage, crack, and segregation defects with an mAP of 84.85% and an inference speed of 4.96 FPS on high-resolution X-ray images. This performance is suitable for many automated foundry inspection applications. The method provides a practical and intelligent solution for replacing manual X-ray film evaluation and for supporting the digital transformation of casting production lines.

Scroll to Top