Few-Shot Casting Part Fringe Detection: An Enhanced DETR Approach with Attention-Weighted Fusion

The pursuit of full automation in foundry production is a critical industrial objective, aimed at reducing reliance on manual labor in often challenging environments. At the core of this automation lies robust machine vision, particularly for tasks like the precise identification and localization of defects. Among these, fringe or flash—excess material that escapes from the parting line of a mold—is a common defect requiring removal. While deep learning has revolutionized object detection, its success is typically contingent on the availability of vast, annotated datasets. This presents a significant hurdle in industrial settings like casting, where data acquisition is limited due to proprietary part designs and the cost of manual annotation. Consequently, developing accurate models under few-shot conditions—where only a handful of examples for a new class are available—becomes paramount.

To address this challenge for casting part fringe inspection, we introduce an enhanced object detection framework based on the Detection Transformer (DETR). Our method specifically tackles the issues of insufficient feature interaction and weak spatial modeling in few-shot scenarios. The core innovations include a Coordinated Attention-weighted Feature Fusion Pyramid to enhance multi-scale feature representation, the integration of 2D Relative Positional Encoding to bolster spatial reasoning, and an optimized regression loss function for more precise bounding box prediction.

Methodology

The proposed architecture builds upon the end-to-end DETR framework but incorporates several key modifications tailored for few-shot learning on casting part imagery. The overall pipeline consists of a backbone feature extractor, an enhanced encoder-decoder transformer module, and a prediction head.

Overall Architecture

Our model uses a ResNet-101 backbone, modified with our fusion module, to extract multi-scale feature maps from an input image of a casting part. These features, enriched with positional information, are processed by a transformer encoder. A decoder then attends to these encoded features and a set of learned object queries to produce the final set of predictions via feed-forward networks (FFNs). The primary enhancements are injected at the feature extraction and attention computation stages.

Coordinated Attention-Weighted Feature Fusion Network (Conv-FCA)

A fundamental challenge in detecting fine defects like fringe on a casting part is the effective fusion of multi-scale features. Shallow layers possess rich spatial (location) information but poor semantic content, while deep layers have strong semantics but coarse spatial details. Standard feature pyramid networks (FPNs) perform simple element-wise addition, which may not optimally balance these aspects.

We design a Conv-FCA module that integrates Coordinate Attention (CA) as a weighting mechanism within a feature pyramid. The CA mechanism efficiently captures long-range dependencies along both spatial dimensions (height and width) by decomposing the global pooling operation. For an input feature map X, it performs coordinate information embedding:

$$ \mathbf{z}_h = \frac{1}{W} \sum_{0 \leq i < W} \mathbf{X}(h, i), \quad \mathbf{z}_w = \frac{1}{H} \sum_{0 \leq j < H} \mathbf{X}(j, w) $$

These pooled features are then concatenated, transformed via shared convolutions and non-linear activations (e.g., Sigmoid), and split back to produce attention weights gh and gw for height and width respectively. The final output feature Y is computed as:

$$ \mathbf{Y}(h, w) = \mathbf{X}(h, w) \times g_h(h) \times g_w(w) $$

We embed this CA block into the lateral connections of a feature pyramid built from the last residual block of the conv2, conv3, and conv4 stages of ResNet-101. Before fusion, features from a higher level are upsampled and passed through the CA block to generate spatially-aware weights. These weights are then used to perform a weighted fusion with the corresponding lower-level feature, which is first processed by a 1×1 convolution for channel alignment. This process ensures that the fused feature map emphasizes semantically meaningful spatial regions relevant to fringe defects on the casting part.

Integration of 2D Relative Positional Encoding

The original DETR uses fixed, sinusoidal absolute positional encodings added to the input features. While simple, these encodings have limited capacity for modeling relative spatial relationships, which are crucial for understanding the structure of a casting part and its defects. We replace this with a learnable 2D relative positional encoding scheme in the self-attention layers of the transformer.

For two spatial positions i and j with coordinates (xi, yi) and (xj, yj), we compute their relative distance indices. To prevent the encoding from being overly sensitive to large distances while preserving locality, a piecewise indexing function h(x) is employed:

$$ h(x) = \begin{cases}
\left \lfloor x \right \rfloor, & |x| \leq \alpha \\
\text{sign}(x) \times \min\left( \beta, \alpha + \frac{\ln(|x|/\alpha)}{\ln(\gamma/\alpha)} (\beta – \alpha) \right), & |x| > \alpha
\end{cases} $$

Here, α is the segment point, β controls the output range [-β, β], and γ adjusts the curvature. This function maps the continuous relative distances to discrete indices for table lookup. The indices for the x and y directions are computed separately:

$$ I_x(i, j) = h(\tilde{x}_i – \tilde{x}_j), \quad I_y(i, j) = h(\tilde{y}_i – \tilde{y}_j) $$

A learnable embedding table P with (2β+1)2 entries holds the relative positional embeddings. The embedding for the relative position between i and j is fetched as Ri,j = P[Ix(i,j), Iy(i,j)]. This relative encoding is incorporated into the self-attention mechanism. For queries Q, keys K, and values V, the attention weights A are computed as:

$$ \mathbf{A} = \text{Softmax}\left( \frac{\mathbf{Q}\mathbf{K}^T + \mathbf{Q}\mathbf{R}^T}{\sqrt{d_k}} \right) \mathbf{V} $$

This allows the model to more effectively reason about the spatial configuration of fringe relative to the overall casting part geometry.

Optimized Loss Function

To further improve localization accuracy for the often small and irregular fringe on a casting part, we refine the regression loss. The original DETR uses a combination of L1 loss and Generalized Intersection over Union (GIoU) loss. While GIoU helps with scale-invariant alignment, it can have slow convergence when boxes are aligned horizontally or vertically. We supplement it with the Smooth L1 loss, which is less sensitive to outliers than L2 loss and provides more stable gradients than L1 loss near zero. The combined regression loss Lreg for a predicted box bp and ground truth box bg is:

$$ L_{reg}(b_p, b_g) = \lambda_{giou}L_{giou}(b_p, b_g) + \lambda_{smoothl1}L_{smoothl1}(b_p, b_g) $$

where the Smooth L1 loss is defined as:

$$ L_{smoothl1}(x) = \begin{cases} 0.5 x^2, & \text{if } |x| < 1 \\ |x| – 0.5, & \text{otherwise} \end{cases} $$

Here, x represents the element-wise difference between the normalized box parameters (center coordinates, height, width). The hyperparameters λgiou and λsmoothl1 balance the two terms. This hybrid loss encourages both precise coordinate regression and good spatial overlap, which is critical for accurately localizing the fringe on the casting part.

Experimental Setup and Datasets

Our experiments follow a standard few-shot learning protocol. The model is first pre-trained on a base dataset with abundant classes, then fine-tuned on a novel dataset containing only a few examples of the target class—casting part fringe.

Datasets: We use PASCAL VOC 2007+2012 trainval sets (15 classes) for base training. For the novel class, we constructed a custom Casting Part Fringe Dataset. This dataset contains 90 images sourced from two origins: 81 images from real factory castings under varying lighting and background conditions, and 9 images from 3D-printed part models to increase diversity. The dataset includes multiple fringe types: linear, block-like, continuous circular, and discontinuous circular flash. This variety simulates the real-world challenges in inspecting a casting part.

Few-Shot Settings: We evaluate our model under four distinct few-shot scenarios by randomly selecting K instances (shots) of the fringe class from our dataset for fine-tuning: K = 3, 5, 10, and 30. The remaining images are used for testing. This tests the model’s ability to learn from extremely limited data of a novel casting part defect.

Evaluation Metrics: We use the standard Average Precision (AP) at an Intersection-over-Union (IoU) threshold of 0.5 to measure detection accuracy for the fringe class. We also report Precision (P) to assess the model’s ability to avoid false positives, a key concern in automated quality control for a casting part.

Results and Analysis

We conducted extensive experiments to validate the effectiveness of each proposed component and to compare our overall method against state-of-the-art few-shot object detection approaches.

Ablation Study

To dissect the contribution of each module, we performed an ablation study under the 10-shot setting. The baseline is the standard DETR model. The results are summarized in the table below.

Model Configuration FPN Conv-FCA Rel. Pos. Enc. (R) Opt. Loss (GS) mAP (%)
DETR (Baseline) 60.17
DETR + FPN 64.75
DETR + Conv-FCA 65.96
DETR + R 63.01
DETR + R + GS 65.31
Our Full Model 66.00

The analysis reveals several key insights:

  1. Feature Fusion is Crucial: Both the standard FPN and our Conv-FCA module provide a substantial boost over the baseline (+4.58% and +5.79% mAP, respectively). This confirms the importance of multi-scale feature interaction for detecting small defects on a complex casting part.
  2. Attention-Weighted Fusion is Superior: Our Conv-FCA outperforms the standard FPN by 1.21% mAP. This demonstrates that adaptively weighting features with coordinate attention allows for a more intelligent fusion, focusing the model’s capacity on spatially salient regions of the casting part likely to contain fringe.
  3. Relative Positional Encoding Helps: Adding relative positional encoding (R) alone improves the baseline by +2.84% mAP, showing its benefit for spatial reasoning.
  4. Combined Effect: The full integration of all components yields the best performance (66.00% mAP), a significant +5.83% improvement over the baseline DETR. This indicates that the modules are complementary, with Conv-FCA enhancing feature quality, relative encoding improving spatial context modeling, and the optimized loss refining the final output for the casting part fringe task.

Comparison with State-of-the-Art Methods

We compare our method against leading few-shot object detectors from different paradigms: metric learning (PNPDet, FSCE), transfer learning (TFA), data augmentation (TIP), and meta-learning (FSRW, Meta R-CNN, Meta-DETR, MPSR). All models are evaluated on our custom casting part fringe dataset under the same few-shot settings.

Method Paradigm Backbone 3-Shot 5-Shot 10-Shot 30-Shot
AP(%) P(%) AP(%) P(%) AP(%) P(%) AP(%) P(%)
PNPDet Metric Learning CenterNet 29.53 58.48 37.86 62.56 46.81 66.29 55.11 69.95
FSCE Metric Learning Faster R-CNN, R-101 31.84 60.73 35.82 60.61 45.00 64.74 55.77 68.51
TFA Transfer Learning Faster R-CNN, R-101 31.92 60.96 36.69 61.45 45.97 65.82 55.92 68.29
TIP Data Augmentation Faster R-CNN, R-101 32.08 61.09 39.91 65.17 46.34 66.07 57.21 70.16
Meta R-CNN Meta-Learning Faster R-CNN, R-101 32.46 61.84 37.43 62.24 50.26 69.31 55.98 68.72
MPSR Meta-Learning Faster R-CNN, R-101 32.17 61.52 39.08 64.98 49.18 68.96 58.18 70.34
Our Method Enhanced DETR DETR, R-101 47.50 76.67 54.06 79.66 66.00 84.93 79.32 90.48

The results are compelling. Our proposed method consistently and significantly outperforms all compared state-of-the-art approaches across all few-shot settings on the casting part fringe dataset. For instance, in the challenging 3-shot scenario, our model achieves an AP of 47.50%, which is over 15 percentage points higher than the best competitor (Meta R-CNN at 32.46%). This advantage grows with more shots, reaching an AP of 79.32% in the 30-shot setting—a massive 21.14 percentage point lead over MPSR (58.18%). Similarly, our Precision scores are markedly higher, indicating a superior ability to minimize false positives, a critical factor for reliable automated inspection of a casting part.

Qualitative Analysis and Robustness

The efficacy of our model is further evidenced in challenging real-world conditions typical of a foundry. Visualizations of attention maps reveal that our model, equipped with Conv-FCA, focuses more precisely and comprehensively on the fringe regions compared to the baseline, which often shows diffused or weak attention. The model demonstrates robustness across diverse scenarios:

  • Strong Glare: It successfully detects thin, low-contrast linear fringe even under harsh lighting reflections from a polished casting part surface.
  • Cluttered Backgrounds: In environments with occlusions and complex backgrounds, our model maintains detection capability where the baseline fails, particularly for heavily occluded fringe.
  • Low-Light Conditions: Simulating dim factory lighting on dark casting part material, our method avoids missed detections for subtle fringe, showcasing its robust feature representation.

These results validate that the proposed enhancements confer strong generalization and robustness, making the system viable for practical deployment in variable industrial settings for casting part inspection.

Conclusion

This work addresses the critical problem of few-shot object detection in the specific and industrially relevant domain of casting part quality inspection. We have presented a significantly enhanced DETR-based framework tailored to overcome the limitations of data scarcity and the need for precise spatial modeling. The integration of a Coordinate Attention-weighted Feature Fusion Pyramid (Conv-FCA) enables intelligent, multi-scale feature recombination that prioritizes defect-relevant regions. The incorporation of 2D Relative Positional Encoding strengthens the model’s innate ability to understand the spatial layout of a casting part and its defects. Finally, the optimized hybrid regression loss drives more accurate bounding box localization.

Comprehensive experiments on a custom-built casting part fringe dataset demonstrate the substantial individual and synergistic contributions of each component. Our full model establishes a new state-of-the-art for this task, outperforming a wide range of existing few-shot detection methods by a large margin across all experimental settings (3, 5, 10, and 30 shots). The model also exhibits commendable robustness to common industrial challenges like glare, occlusion, and poor lighting. This research provides a powerful and practical solution for automating visual inspection in casting and other manufacturing sectors where labeled defect data is inherently limited, paving the way for more intelligent and adaptive industrial quality control systems.

Scroll to Top