A prototype-aligned multi-scale attention network for few-shot industrial surface defect detection Rubing Huang, Lianyi Zhao, Xiaowei Li Abstract Few-shot industrial surface defect detection remains challenging because annotated defect images are scarce, defect scales vary considerably, and textured backgrounds can cause false responses. This study proposes a Prototype-Aligned Multi-Scale Attention Network (PA-MSAN) that integrates multi-receptive-field feature extraction, spatial-channel attention, and region of interest (RoI)-level prototype matching within a two-stage detector. Experiments are conducted on NEU-DET under 1-shot, 3-shot, and 5-shot episode settings, with 5-shot cross-domain evaluation on GC10-DET using target-domain support prototypes. Multi-scale feature-pyramid fusion represents local textures and broader defect morphology, while attention weighting enhances defect-sensitive regions and channels before region proposal network (RPN) proposal generation and RoI Align. Few-shot classification is performed by cosine similarity between RoI features and class prototypes, and training jointly optimizes detection classification, bounding-box regression, region proposal, and Prototype Alignment Loss (PAL). Under the 5-shot NEU-DET setting, PA-MSAN achieves 92.2 ± 0.4% mean average precision (mAP)@0.5, 88.7 ± 0.4% small-object average precision (APsmall), 0.89 ± 0.01 average intersection over union (IoU), and a 4.1 ± 0.2% background false response rate, with 23.4 ms detection time and 18.7 × 106 parameters. Compared with Faster R-CNN, PA-MSAN improves mAP@0.5 by 2.9 percentage points and APsmall by 3.9 percentage points while reducing the background false response rate by 1.7 percentage points. Under the 5-shot target-domain support setting, PA-MSAN achieves 86.4 ± 0.4% mAP@0.5 on GC10-DET, 5.8 percentage points (6.3%) below the corresponding NEU-DET result. The results support region-level prototype alignment for limited-label metallic surface inspection under the evaluated datasets and two-stage configuration. 1. Introduction Surface defect detection is an important part of ensuring product quality and production efficiency in industrial production. With the improvement of intelligent manufacturing and automation level, the demand for high-precision and real-time defect detection is increasing day by day. In complex industrial scenarios, there are numerous types of defects with significant differences in size and shape. At the same time, the background environment is complex, and traditional manual detection is not only inefficient, but also subject to subjective judgment errors []. Therefore, building intelligent systems that can adapt to diverse defect features and achieve reliable classification and detection under few-shot conditions has important academic value and application significance for improving the level of industrial automation []. In industrial production, surface defect detection is challenged by three closely related factors: limited annotated defect samples, large defect-scale variation, and strong background texture interference [,]. Scarce samples restrict deep networks from learning stable category-level representations, while scale variation requires multi-scale perception to capture both fine linear defects and larger defect regions [,]. In addition, small and irregular defect shapes together with mottled material textures can reduce localization accuracy if the detector relies only on single-level features or shallow feature representations [,]. Existing studies address these challenges from complementary directions. Convolutional neural network (CNN)-based detectors provide a basic framework for automated defect localization but remain sensitive to scarce samples and small defects [,]. Multi-scale feature fusion and feature-pyramid designs improve sensitivity to defects of different sizes [,], while recent few-shot industrial detectors use bidirectional feature fusion and multi-stage proposal refinement to improve localization under limited supervision []. Attention-based methods strengthen defect-related responses and suppress background textures [,], and attention-guided few-shot frameworks further use channel-spatial weighting and support-query semantic alignment []. Prototype learning aggregates scarce class features into compact representations and improves inter-class separability [,]. Representative few-shot detectors adopt different transfer mechanisms: TFA performs classifier-level fine-tuning [], FSRW reweights query features using support information [], and Meta R-CNN introduces RoI-level meta features for low-shot detection []. PA-MSAN differs by constructing explicit class prototypes from attention-enhanced support RoI features, comparing RoI embeddings with those prototypes through cosine similarity, and regularizing the shared RoI embedding with Prototype Alignment Loss. This study formulates few-shot industrial surface defect detection as a region-level prototype-alignment problem. PA-MSAN first constructs multi-receptive-field pyramid features to preserve defect information across scales and then applies spatial-channel attention to the fused representation before proposal generation. Support-region features obtained after RoI Align are aggregated into class prototypes, and RoI features are matched to those prototypes in the same cosine-similarity space. RPN proposal learning and bounding-box regression optimize localization, while prototype matching and Prototype Alignment Loss optimize few-shot class discrimination on the shared RoI representation. The resulting architecture links multi-scale representation, attention refinement, region localization, and metric-based classification within one training objective. isolates the contributions of the multi-scale, attention, prototype-learning, and PAL components. The main contributions of this paper are as follows: - (1) PA-MSAN is proposed to formulate few-shot industrial surface defect detection as an RoI-level prototype-alignment task. Support-set prototypes are constructed from attention-enhanced region features and directly guide RoI classification. Localization is optimized through the shared RoI representation together with RPN and bounding-box regression objectives, thereby coupling region-level metric learning with two-stage detection. - (2) A hierarchical feature representation module is developed for the defect detection task by combining feature-pyramid fusion with convolutions of multiple receptive fields. With this design, the detector can describe subtle local textures, medium-scale defect structures, and higher-level semantic information even when only a few support samples are available. - (3) A defect-sensitive prototype matching strategy is introduced at the RoI level by using spatial-channel attention together with RoI Align. The attention mechanism helps filter out irrelevant background textures, and the cosine similarity between region features and class prototypes allows the detector to distinguish defect categories more effectively when only a few labeled examples are available. - (4) A joint training objective is designed by optimizing detection classification loss, bounding-box regression loss, RPN loss, and Prototype Alignment Loss together. Through this objective, the model learns more accurate region localization and more discriminative feature distributions, with samples from the same class becoming closer and different classes remaining better separated. This helps improve the stability of few-shot detection and supports cross-domain adaptation with limited target-domain supervision. 2. Network design and method implementation This paper proposes PA-MSAN for surface defect detection with scarce labeled samples and substantial scale variation. The network contains three interacting components: multi-scale feature extraction, attention enhancement, and prototype-based region classification, which are optimized jointly with the two-stage detection objectives. summarizes the overall processing flow. shows the complete processing flow of the proposed method from input image to defect classification. The input image is first preprocessed and normalized, and is then processed by a multi-scale feature extraction (MSFE) module to generate hierarchical features under different receptive fields. Then, the multi-scale attention mechanism applies adaptive weights to spatially significant regions and key channels, and outputs attention-enhanced features. The RPN then generates candidate defect regions from the enhanced features, and region features are extracted through RoI Align. Finally, cosine similarity matching is performed with prototype vectors of various categories in the prototype learning module, combined with bounding-box regression and PAL to complete defect detection and localization under few-shot conditions. 2.1. Data augmentation and preprocessing Industrial surface images are affected by illumination variation, background texture, and sensor noise, which can further limit feature learning when only a few labeled defect images are available []. The preprocessing pipeline therefore combines geometric and photometric augmentation with consistent grayscale normalization while preserving the annotated defect structure [, ]. NEU-DET [,] contains 200 × 200 grayscale steel-surface images, whereas GC10-DET [] contains grayscale metallic surface-defect images with variable resolutions in commonly used releases. All images are read or converted as single-channel grayscale inputs, resized to 416 × 416, and normalized to the 0–1 range. During training, rotation, flipping, illumination perturbation, scaling, and Gaussian noise are applied according to the ranges in ; validation and test images use deterministic resizing and normalization only. The geometric and photometric augmentation sequence is defined as follows: In , [pone.0359400.e002.webp]I denotes the original image, [pone.0359400.e003.webp]I′ denotes the clipped geometrically and photometrically augmented image, [pone.0359400.e004.webp]Ageo(·) includes rotation, scaling, and horizontal/vertical flipping, and [pone.0359400.e005.webp]Aphoto(·) applies illumination gain and offset. The continuous parameters [pone.0359400.e006.webp]θr, [pone.0359400.e007.webp]θs, [pone.0359400.e008.webp]α, and [pone.0359400.e009.webp]β follow the ranges in . The illumination-adjusted intensities are saturated to the [0,255] grayscale range in before min-max normalization. The clipped photometrically augmented image is min-max normalized before Gaussian noise is added in the normalized 0–1 intensity space: In , [pone.0359400.e011.webp]Inorm is the min-max normalized image, [pone.0359400.e012.webp]εn is zero-mean Gaussian noise with standard deviation [pone.0359400.e013.webp]σn, and the final input is clipped to the 0–1 range. This ordering keeps the illumination transformation before normalization and defines the Gaussian-noise magnitude in the same normalized intensity space used in . The minimum and maximum intensities used for min-max normalization are computed from the clipped output of . Each defect class is augmented after the 70%/15%/15% dataset split. Geometric augmentation applies rotation, horizontal and vertical flipping, and scaling together with the corresponding bounding-box transformation. Illumination gain and offset are then applied and the resulting intensities are clipped to the [0,255] grayscale range before min-max normalization, followed by Gaussian noise in the normalized 0–1 intensity space. Every augmented view inherits the subset assignment of its source image, so transformations derived from one original image remain within the same training, validation, or test subset. shows some typical original and augmented industrial surface-defect samples. Augmentation operations include rotation, flipping, scaling, illumination gain and offset, and Gaussian noise injection; at the same time, the main defect texture and contour structure are retained for follow-up multi-scale feature extraction. As shown in , the upper row contains original defect samples and the lower row shows their augmented counterparts. These transformations simulate changes in viewing geometry, illumination, and sensor noise while retaining the defect texture and contour needed for subsequent feature extraction and prototype learning. A unified set of data-augmentation parameters is used, and the specific numerical ranges of rotation angle, scale ratio, lighting gain coefficient, lighting offset and Gaussian noise standard deviation are listed in . The parameters in are sampled from continuous intervals for rotation, scaling, illumination, and Gaussian noise. Illumination gain and offset are applied, and the resulting intensities are clipped to [0,255], before min-max normalization, and Gaussian noise is added afterward in normalized intensity units. Random augmentation is restricted to support-image training views; validation and test images undergo deterministic resizing to 416 × 416 and min-max normalization. Transformed versions of the same source image remain within a single data subset. 2.2. Multi-scale feature extraction module To represent industrial defects with substantial scale variation, PA-MSAN combines multi-receptive-field convolution with a feature-pyramid pathway containing top-down fusion and lateral connections [–]. The module produces hierarchical feature maps that preserve fine texture details, intermediate defect structures, and broader semantic context [,]. Lateral projections align channel dimensions before feature addition, and the resulting pyramid features provide multi-scale inputs for attention refinement, proposal generation, prototype matching, classification, and box regression. The convolution operation can be expressed as: In , F(l) denotes the feature map of the l-th convolutional layer, F(l−1) denotes the input feature map from the previous layer, W(l) is the convolution kernel, b(l) is the bias term, * denotes convolution, and δ(·) denotes the rectified linear unit (ReLU) activation function. Convolution kernels of 3 × 3, 5 × 5, and 7 × 7 are used at different levels to balance the responses to small and large defects [,]. Multi-scale features are integrated through the feature-pyramid fusion mechanism. To make element-wise fusion dimensionally valid when adjacent feature levels have different channel numbers, the lateral feature is first projected to the target channel dimension: In , [pone.0359400.e016.webp]Pl denotes the fused pyramid feature at level l, [pone.0359400.e017.webp]Fl denotes the lateral feature at level l, [pone.0359400.e018.webp]φl(·) denotes a lateral projection, typically implemented by a 1 × 1 convolution, and Up(·) denotes the upsampling operation used to align spatial resolution. This projection avoids channel-dimensional mismatch before feature addition. In , Dl−1 denotes the downsampled feature map and Down(·) denotes the downsampling operation with a stride of 2. Thus, it can build a multi-scale feature pyramid and maintain spatial consistency among the feature maps at different scales [,]. shows the response of the multi-scale feature extraction module. Fine-scale features retain local edge and texture details; mid-scale features are structural defect patterns; and global feature maps focus on large-scale semantic regions. The above responses serve as hierarchical inputs for the following attention and prototype-learning modules. As shown in , the normalized input is converted into fine-grained, medium-grained and global feature representations. High-response defect regions are more concentrated at higher levels of the hierarchy, and multi-scale convolution and feature-pyramid fusion improve the feature representation by incorporating local texture details and global defect morphology. summarizes the kernel sizes, output channels, and implementation-level computational costs of the multi-scale blocks. Explicit 5 × 5 and 7 × 7 kernels provide direct medium- and large-receptive-field observations. compares this multi-kernel configuration with a stacked-3 × 3 variant under matched channel settings and the same feature pyramid network (FPN), attention, prototype branch, training budget, and evaluation protocol. The proposed configuration improves mAP@0.5 by 1.8 percentage points and APsmall by 2.8 percentage points while using comparable feature-extractor parameters and lower reported floating-point operations (FLOPs). defines the kernel sizes and channel allocation used in the multi-scale feature extractor. The resulting pyramid features provide inputs to the attention module and the downstream proposal, RoI, prototype-classification, and box-regression stages. For FPN fusion, lateral features from different levels are projected to a common feature dimension before element-wise addition, consistent with the projection operation defined in . 2.3. Attention mechanism and feature enhancement The hierarchical feature map generated by the multi-scale feature extraction module contains local and global information for defects of different sizes. The attention module applies channel weighting first and spatial weighting second to the fused pyramid feature. The input [pone.0359400.e020.webp]Fp is used to generate channel weights [pone.0359400.e021.webp]Mc(Fp); channel-weighted features [pone.0359400.e022.webp]Fc are then used to generate spatial weights [pone.0359400.e023.webp]Ms(Fc). The resulting attention-enhanced feature [pone.0359400.e024.webp]Fa is shared by the RPN, RoI Align, prototype branch, classification head, and box-regression branch [,]. Channel attention uses global average pooling and global maximum pooling to summarize the fused pyramid feature and maps the resulting descriptors through a two-layer fully connected network: In , Mc(Fp) denotes the channel attention weight generated from the pyramid feature Fp. Global average pooling (GAP) and global maximum pooling (GMP) are denoted by GAP(·) and GMP(·), respectively; W1 and W2 are the weight matrices of the two fully connected layers, δ(·) is the ReLU activation function, and σ(·) is the Sigmoid function. Channel attention strengthens defect-related channels and suppresses background-noise channels. After channel weighting, spatial attention averages and maximizes [pone.0359400.e026.webp]Fc along the channel dimension and applies a 7 × 7 convolution to generate the spatial attention map []: In , [pone.0359400.e028.webp]Ms(Fc) denotes the spatial attention weight generated from [pone.0359400.e029.webp]Fc, [·;·] denotes channel-wise concatenation, [pone.0359400.e030.webp]f7×7(·) denotes a 7 × 7 convolution, and AvgPool(·) and MaxPool(·) denote channel-wise average and maximum pooling, respectively []. Feature weighting combines channel and spatial attention to generate the final enhanced feature Fa, which is represented as: In , [pone.0359400.e032.webp]Fc is the channel-weighted intermediate feature and [pone.0359400.e033.webp]Fa is the final channel-spatial attention output. The sequential formulation makes the spatial mask depend on the channel-refined representation and matches the channel-to-spatial processing flow used in . is the qualitative feature-response visualization after attention enhancement. Warmer or brighter regions indicate stronger defect-related responses, showing that the attention module strengthens defect-sensitive regions while suppressing irrelevant background texture responses. The attention enhancement module performs channel and spatial weighting on the input multi-scale feature map and outputs defect-sensitive feature responses. The qualitative visualization indicates that small Crazing and Scratch regions are more clearly reinforced in complex backgrounds, supporting subsequent RPN proposal generation, RoI Align feature extraction, and detection-head localization. 2.4. Prototype learning and few-shot classification Under few-shot conditions, a detector must distinguish defect categories from a small number of labeled support images. PA-MSAN constructs class representations from fixed-size RoI features at the region level. Candidate regions are aligned through RoI Align, projected to the prototype embedding space, and aggregated by class. For each class k, the prototype pk is the arithmetic mean of its support-region features and is defined as: In , [pone.0359400.e035.webp]pk denotes the prototype vector of category k, [pone.0359400.e036.webp]Sk denotes the support feature set of category k, |Sk| is the number of support samples in that category, and [pone.0359400.e037.webp]fi denotes the region feature vector extracted by RoI Align. RoI Align reduces boundary quantization errors and improves prototype consistency for small defects by producing fixed-size region representations before projection into the embedding space. The matching between features and prototypes is measured using cosine similarity, defined as: In , si,k ∈ [−1, 1] denotes the cosine similarity between the input region feature fi and the class prototype pk, ‖·‖2 denotes the L2 norm, and fi Tpk denotes the inner product. The similarity distribution over all class prototypes is used for few-shot defect classification. The cosine-similarity scores are converted to class probabilities by a softmax over the [pone.0359400.e039.webp]K defect prototypes: In , the cosine-similarity scores are normalized across the [pone.0359400.e041.webp]K defect prototypes to obtain the prototype-based class probability for each RoI feature. The resulting probability distribution is used for few-shot class prediction and in the Prototype Alignment Loss. During optimization, PAL increases the similarity of each labeled training RoI embedding derived from the K-shot support images and their augmented views to its ground-truth class prototype while suppressing similarities to competing prototypes [,]. Related few-shot metal-defect work has explored adaptive support-feature correlation and hierarchical multi-scale fusion for cross-domain generalization []. In , [pone.0359400.e043.webp]LPAL denotes the Prototype Alignment Loss, [pone.0359400.e044.webp]N is the number of labeled training RoI embeddings included in the loss, and [pone.0359400.e045.webp]yi is the ground-truth class of embedding [pone.0359400.e046.webp]fi. The loss is computed from RoIs derived from the [pone.0359400.e047.webp]K-shot support images and their augmented training views. visualizes prototype-learning effects by projecting high-dimensional region features into a two-dimensional space using t-distributed stochastic neighbor embedding (t-SNE). Different colors denote different defect categories, and the cosine-similarity heatmap summarizes the similarity relationship among class prototypes. compares attention-enhanced RoI features before and after prototype learning. The projected features become more compact within classes and more separated between classes after prototype alignment, and the prototype cosine-similarity heatmap summarizes the remaining inter-class relationships. In PA-MSAN, the prototypes are constructed from RoI-Aligned support-region features, so the metric representation is defined at the same region level used by the detection head. 2.5. Joint optimization and training strategy PA-MSAN uses a joint objective to optimize region classification, box regression, proposal generation, and prototype-space structure within the same training procedure. The total loss is defined as: In , [pone.0359400.e049.webp]Ljoint denotes the total joint loss, [pone.0359400.e050.webp]Ldet is the detection classification loss, [pone.0359400.e051.webp]Lreg is the bounding-box regression loss, [pone.0359400.e052.webp]LRPN is the region proposal loss, and [pone.0359400.e053.webp]LPAL is the Prototype Alignment Loss. The coefficients [pone.0359400.e054.webp]λd, [pone.0359400.e055.webp]λr, [pone.0359400.e056.webp]λRPN, and [pone.0359400.e057.webp]λp weight the four objectives. The detection classification loss [pone.0359400.e058.webp]Ldet is cross-entropy on the linear classification logits, [pone.0359400.e059.webp]Lreg is Smooth L1 loss for bounding-box regression, and [pone.0359400.e060.webp]LRPN combines RPN objectness and proposal-regression losses. [pone.0359400.e061.webp]LPAL structures the prototype embedding while sharing the upstream RoI representation with the auxiliary linear classifier. separates the complete prototype-learning branch from PAL: removing the full prototype-learning branch lowers mAP@0.5 by 7.9 percentage points, while removing PAL with the prototype classifier retained lowers mAP@0.5 by 3.3 points. During inference, the prototype metric head provides the few-shot class decision and the linear classification head remains an auxiliary training branch. The detection classification head computes linear classification logits from the RoI feature as: In , [pone.0359400.e063.webp]fi denotes the RoI feature vector for candidate region [pone.0359400.e064.webp]i, [pone.0359400.e065.webp]W and [pone.0359400.e066.webp]b are the learnable parameters of the auxiliary linear classification head, and [pone.0359400.e067.webp]zi contains the logits supervised by [pone.0359400.e068.webp]Ldet. The prototype metric head uses and to compute cosine similarities and normalized prototype class probabilities. The network parameters are updated using Adam optimizer, and the optimization formula is as follows: In , [pone.0359400.e070.webp]θt denotes the network parameters at iteration t, [pone.0359400.e071.webp]η is the learning rate, [pone.0359400.e072.webp]m^t and [pone.0359400.e073.webp]v^t are the bias-corrected first- and second-moment estimates, and [pone.0359400.e074.webp]ε is the numerical-stability constant used by Adam. During the training process, the epoch-wise step learning-rate decay strategy is used to control the parameter update amplitude, consistent with the setting that the learning rate decays to 0.95 times its previous value every 10 epochs: In , [pone.0359400.e076.webp]ηe denotes the learning rate at epoch [pone.0359400.e077.webp]e, [pone.0359400.e078.webp]η0 denotes the initial learning rate, [pone.0359400.e079.webp]γ is the decay coefficient, and [pone.0359400.e080.webp]ne is the number of completed 10-epoch decay intervals. The experiments use [pone.0359400.e081.webp]γ = 0.95. During the training process, the input features extracted through data augmentation and multi-scale attention modules are simultaneously subjected to detection classification loss, bounding box regression loss, RPN candidate region loss, and PAL constraints. Joint optimization makes the features close to the category prototype in high-dimensional space and ensures consistency among the samples. summarizes the optimizer, learning rate, batch size, joint loss weights, training budget, regularization, learning-rate decay, Adam parameters, and data-augmentation settings used in the few-shot experiments. During training, Adam uses an initial learning rate of 0.001 with a decay factor of 0.95 every 10 epochs. Each optimizer step processes a mini-batch of 16 training views derived from the K-shot support images. The K-shot count refers to distinct annotated source images; augmented views do not increase the shot count. The joint loss weights for detection classification, bounding-box regression, RPN proposal loss, and prototype alignment are 1.0, 1.0, 0.5, and 0.5, respectively, and Dropout is set to 0.3. The prototype-alignment weight [pone.0359400.e082.webp]λp is selected by validation-set grid search over {0.1, 0.3, 0.5, 0.8, 1.0}, with the remaining weights fixed as in . Section reports the corresponding sensitivity results. 3. Experimental design and data preparation 3.1. Experimental datasets and acquisition This experiment used two publicly available industrial surface defect detection datasets, NEU-DET [,] and GC10-DET [], to train and evaluate the proposed model. NEU-DET is a steel surface defect detection dataset released by Northeastern University and contains six typical defect categories, including Crazing, Scratch, Pitted Surface, Patch, Rolled-in Scale, and Inclusion, with bounding-box annotations. GC10-DET contains ten defect categories (punching, weld line, crescent gap, water spot, oil spot, silk spot, inclusion, rolled pit, crease, and waist folding) with bounding-box annotations and is used to evaluate cross-domain adaptation with limited target-domain supervision in more complex industrial scenarios. NEU-DET images are grayscale steel surface images with a resolution of 200 × 200, whereas GC10-DET contains grayscale metallic surface-defect images with variable resolutions in commonly used releases. For a consistent input setting, all images from both datasets are read or converted into a single-channel grayscale format, resized to 416 × 416, and normalized to the 0–1 interval. Random augmentation, including rotation, horizontal/vertical flipping, scaling, illumination gain and offset, and Gaussian noise injection, is applied only to training and support-set images; validation and test images undergo deterministic resizing and normalization only. Each dataset is divided once into 70% training, 15% validation, and 15% test subsets. For a given run, the 70% training subset is the candidate pool from which exactly K annotated support images per class are sampled. Parameter updates use only these support images and their augmented views. The validation and test subsets remain held out from parameter learning. In , [pone.0359400.e084.webp]pi denotes the proportion of samples from class [pone.0359400.e085.webp]i, [pone.0359400.e086.webp]Ni denotes the number of samples in that class, and [pone.0359400.e087.webp]Ntotal is the total sample count. This quantity is used to check category balance before K-shot support sampling. Image normalization and bounding-box transformations are applied consistently with the geometric augmentation operations so that defect annotations remain aligned with the transformed images. The resulting inputs are then used by the multi-scale feature extractor, attention modules, prototype branch, and detection head. presents the statistical information of the NEU-DET and GC10-DET datasets, including the number of defect categories, annotation forms, image types and application scenarios. shows the datasets used in this experiment, as well as the number of categories, annotation types, original image formats, original resolutions and preprocessing strategies. NEU-DET is used mainly for steel surface defect detection and few-shot evaluation, while GC10-DET is used to evaluate cross-domain adaptation with limited target-domain supervision under more complex industrial imaging conditions. 3.1.1. Few-shot episode construction and evaluation protocol. For each defect category, [pone.0359400.e088.webp]K annotated images are randomly sampled from the fixed 70% training subset as the support set, with [pone.0359400.e089.webp]K ∈ {1, 3, 5}. These K source images and their stochastic augmented views provide the labeled training material for that run. The dedicated 15% validation and 15% test subsets remain non-overlapping with the support pool and are used only for model selection and final evaluation, respectively. For the cross-domain experiment reported in , five labeled GC10-DET support images per class are selected from the target-domain training pool. PA-MSAN constructs target-domain class prototypes from these 5-shot support samples, and performance is evaluated on the fixed non-overlapping GC10-DET test subset. Each shot setting is repeated five times with random seeds {0, 1, 2, 3, 4}. PA-MSAN and all baselines use the same seed-specific support selections, preprocessing pipeline, confidence threshold, IoU threshold, and non-maximum suppression (NMS) threshold. Across seeds, support sampling, augmentation draws, mini-batch order, and Dropout masks are re-randomized while the underlying 70%/15%/15% split remains fixed. Each repeated run starts from the architecture-specific initialization or pretrained state listed in . Results are summarized as mean ± standard deviation over the five runs. For example, 92.2 ± 0.4% mAP@0.5 in the 5-shot setting denotes the mean and standard deviation across the five seed-specific runs. This reporting separates average performance from variability caused by support selection and other stochastic training factors. summarizes the 1-shot, 3-shot, and 5-shot episode settings. Each configuration uses balanced support counts and identical seed-specific support selections across compared methods, allowing performance changes with increasing support size to be evaluated under the same data partition and detection thresholds. 3.2. Experimental configuration and training settings Experiments are conducted on Ubuntu 20.04 using an NVIDIA GeForce RTX 3090 graphics processing unit (GPU) with 24 GB memory and an Intel Xeon Gold 6338 central processing unit (CPU). CUDA acceleration is used for training and inference. All images are resized to 416 × 416 and normalized to the 0–1 interval before entering the network. Inference-time measurements use batch size 1 with 416 × 416 inputs on the RTX 3090. Detection time includes network forward inference and detection post-processing/NMS and excludes disk input/output (I/O) and input preprocessing. PA-MSAN consists of a multi-scale convolutional backbone with feature-pyramid fusion, spatial and channel attention, an RoI-level prototype branch, RPN proposal generation, and a detection head. The attention-enhanced pyramid representation is used by the RPN and RoI Align stages. The resulting RoI features are processed by the prototype metric head for few-shot class prediction and by the box-regression branch for localization; the auxiliary linear classification head provides the Ldet supervision during training. Training uses mini-batch stochastic optimization with batch size 16. Adam is initialized with a learning rate of 1 × 10−3, which is multiplied by 0.95 every 10 epochs. The optimizer uses adaptive first- and second-moment estimates as specified in . During inference, the prototype class probability from is combined with the RPN objectness probability to form the final detection confidence: In , [pone.0359400.e091.webp]sconf denotes the final detection confidence score, [pone.0359400.e092.webp]P(obj) is the candidate-region objectness probability, and [pone.0359400.e093.webp]P(cls∣obj) is the prototype-based conditional class probability. During NMS, IoU is computed between predicted boxes; evaluation IoU is computed between matched predicted and ground-truth boxes. During validation and testing, detections are evaluated at an IoU threshold of 0.5, with an NMS threshold of 0.5 and a confidence threshold of 0.25. Each training run uses a 200-epoch budget, and the checkpoint with the highest validation mAP is used for final test evaluation. A Dropout rate of 0.3 is applied to the fully connected layer during training. reports the seed-aggregated epoch at which the best validation mAP is reached for each prototype-alignment weight. 3.3. Model comparison and ablation settings Comparative and ablation experiments use a shared target-dataset partition, input resolution, support-set protocol, augmentation policy, random seeds, and evaluation thresholds. Each baseline retains its prescribed model-specific optimization procedure while using the same target-dataset support selections and evaluation conditions. The comparison includes three in-house reference variants (CNN Detector, Proto CNN, and Attention CNN), two general-purpose detectors (YOLOv5 [] and Faster R-CNN []), and three established few-shot object detectors (TFA [], Meta R-CNN [], and FSRW []). CNN Detector uses a conventional CNN-FPN detection pipeline without attention or prototype learning; Proto CNN adds prototype constraints; and Attention CNN adds spatial-channel attention without the prototype branch. TFA, Meta R-CNN, and FSRW provide few-shot-specific references with classifier fine-tuning, RoI-level meta features, and feature reweighting, respectively. PA-MSAN is evaluated under the same episode and detection-evaluation protocol as these baselines. All models use the same dataset split, 5-shot GC10-DET support selections, 416 × 416 input resolution, confidence threshold of 0.25, NMS threshold of 0.5, and random seeds. CNN Detector, Proto CNN, Attention CNN, and PA-MSAN use Adam for 200 epochs with an initial learning rate of 1 × 10−3 and batch size 16. YOLOv5, Faster R-CNN, TFA, Meta R-CNN, and FSRW follow their method-specific training procedures within the same 200-epoch budget. For cross-domain evaluation, PA-MSAN constructs target-domain prototypes from the support set, while each baseline uses the same labeled support set according to its method-specific adaptation procedure. For target-domain evaluation, every compared method uses the same five labeled GC10-DET support images per class for its method-specific adaptation stage and is evaluated on the same fixed non-overlapping GC10-DET test subset. 3.3.1. Baseline implementation details. summarizes the implementation and evaluation settings used for the compared detectors. Each repeated run starts independently from the initialization or pretraining configuration listed in . The shared split, support episodes, preprocessing, thresholds, and random seeds align the target-dataset evaluation conditions across methods. Reported differences are considered together with each detector’s architecture and prescribed optimization procedure. The ablation study removes the spatial attention module, channel attention module, prototype-learning branch, Prototype Alignment Loss, and multi-scale feature extraction structure in controlled variants. All variants use the same data split, random seeds, detection head, training budget, optimizer settings, augmentation policy, and evaluation thresholds. summarizes the corresponding configurations. Detection performance is evaluated with average precision (AP) and mAP@0.5, with mAP@0.5 used as the primary metric for combined localization and classification performance. For an ablation variant, the mAP decrease relative to the complete model is defined as: In , [pone.0359400.e095.webp]ΔmAP denotes the performance decrease associated with removing the corresponding component from the full model, [pone.0359400.e096.webp]mAPfull is the full-model performance, and [pone.0359400.e097.webp]mAPablated is the performance of the ablated variant. 4. Result analysis 4.1. Detection performance under multi-scale and attention mechanisms 4.1.1. Multi-scale defect detection performance evaluation. PA-MSAN is evaluated on six NEU-DET defect classes using average precision at an IoU threshold of 0.5 (AP50), scale-specific AP, and mAP@0.5. Object-scale groups are defined after resizing to the 416 × 416 network input. If [pone.0359400.e098.webp]A denotes the resized ground-truth bounding-box area in pixels, small objects satisfy [pone.0359400.e099.webp]A<322, medium objects satisfy [pone.0359400.e100.webp]322≤A<642, and large objects satisfy [pone.0359400.e101.webp]A≥642. mAP@0.5 is the mean AP50 over the six classes, and [pone.0359400.e102.webp]APsmall denotes AP at IoU = 0.5 evaluated only on the small-object group. In , P(R) denotes precision as a function of recall R, AP is the area under the precision-recall curve, K denotes the number of defect categories, and [pone.0359400.e104.webp]mAP0.5 denotes the mean AP across categories when the IoU threshold is 0.5. Representative multi-scale responses are shown in , and category-wise AP50 results are summarized in . illustrates complementary responses of the MSFE module across defect scales. Inclusion retains a concentrated response in the fine-scale representation, Pitted Surface is represented more continuously at the intermediate scale, and Rolled-in Scale receives a broader response from the large-receptive-field branch. The examples are consistent with the intended role of the multi-scale pathway: preserving local texture details while incorporating larger spatial context for extended defects. reports the mean category-wise AP50 values over five repeated few-shot runs together with the overall mAP@0.5 of 92.2%. Scratch and Rolled-in Scale reach 94.3% and 93.7% AP50, respectively, while Crazing and Inclusion show lower category-wise AP50 under the same evaluation protocol. 4.1.2. Attention focus detection effect. The contribution of SA and CA is evaluated using no-attention, SA-only, CA-only, and dual-attention variants on the six NEU-DET classes. Localization quality is measured from matched-box IoU. also reports boundary integrity, background false response rate, and target/non-target channel-response intensity as implementation-level diagnostic quantities. The calculation method of IoU is as follows: In , [pone.0359400.e106.webp]Bpred denotes the predicted bounding box and [pone.0359400.e107.webp]Bgt denotes the ground-truth bounding box. A larger IoU indicates more accurate target localization. The quantitative results are shown in . For localization-quality reporting, predictions are sorted by confidence and greedily matched to ground-truth boxes at IoU ≥ 0.5, with each ground-truth instance matched at most once. Each ground-truth instance contributes the IoU of its matched prediction, or zero if it is missed. Extra unmatched predictions are counted as false positives for precision/AP computation but are not added as separate terms to the Average-IoU denominator. For images with multiple objects, these ground-truth-level IoU contributions are pooled across the full test set and averaged as in . In , [pone.0359400.e109.webp]Nm denotes the total number of ground-truth instances included in the Average-IoU calculation. For each instance [pone.0359400.e110.webp]i, [pone.0359400.e111.webp]Bp,i is the matched prediction when one exists and [pone.0359400.e112.webp]Bg,i is the ground-truth box; missed instances contribute an IoU value of zero. IoU(·,·) follows . compares category-wise Average IoU across the four attention settings. reports boundary integrity, reports the overall background false response rate, and compares target and non-target channel-response intensity. Boundary integrity is reported as a boundary-agreement score; background false response rate records background detections above the evaluation confidence threshold; and target/non-target response intensity summarizes normalized activation in target and non-target regions. Across these diagnostics, the dual-attention configuration shows higher localization-related scores and a lower background false response rate than the no-attention reference. Boundary integrity score is used as an auxiliary localization diagnostic. Background false response rate is calculated as the number of retained background false-positive detections divided by the total number of retained detections after confidence filtering and NMS. A retained prediction is counted as a background false positive when it cannot be matched to any ground-truth defect at IoU ≥ 0.5. 4.2. Few-shot and cross-domain adaptation performance 4.2.1. Few-shot detection performance. Few-shot performance is evaluated on NEU-DET under the 1-shot, 3-shot, and 5-shot settings defined in . During test evaluation, RoI features are classified by cosine similarity to prototypes constructed from the corresponding support set. PA-MSAN and all baselines use the same seed-specific support selections and evaluation thresholds. The corresponding few-shot performance and prototype-discriminability results are summarized in . Five seed-level runs are summarized as mean ± standard deviation in the result tables; the corresponding seed-level values are provided in . reports mean mAP@0.5 under the three shot settings. PA-MSAN reaches 84.6 ± 0.3%, 89.3 ± 0.3%, and 92.2 ± 0.4% mAP@0.5 for 1-shot, 3-shot, and 5-shot, respectively. presents category-wise 5-shot AP50, and shows lower intra-class standard deviation and larger inter-class distance as the number of support samples increases. These measurements are consistent with more stable class prototypes when additional support examples are available. The detailed quantitative results of PA-MSAN under different few-shot settings are summarized in . The comparative results between PA-MSAN and baseline detectors under the 5-shot NEU-DET setting are summarized in . also includes three few-shot-specific detectors. TFA [], Meta R-CNN [], and FSRW [] reach 88.4 ± 0.4%, 86.8 ± 0.5%, and 85.9 ± 0.7% mAP@0.5, respectively, compared with 92.2 ± 0.4% for PA-MSAN. PA-MSAN also records the highest APsmall in this comparison, exceeding TFA by 4.7 percentage points under the shared 5-shot evaluation protocol. 4.2.2. Cross-domain adaptation with limited target-domain supervision. Cross-domain evaluation uses NEU-DET as the source-domain training dataset and GC10-DET as the target dataset. The reported experiment uses five labeled GC10-DET support images per class for target-domain adaptation and evaluates each method on the fixed non-overlapping GC10-DET test subset. reports the 5-shot target-domain mAP@0.5 together with the corresponding cross-dataset differences relative to the 5-shot NEU-DET results. Under the 5-shot target-domain support setting, PA-MSAN reaches 86.4 ± 0.4% mAP@0.5 on GC10-DET, compared with 92.2 ± 0.4% on NEU-DET. The corresponding difference is 5.8 percentage points (6.3%). The relative differences for Faster R-CNN, Attention CNN, YOLOv5, Proto CNN, and CNN Detector are 8.6%, 10.0%, 11.2%, 13.6%, and 17.5%, respectively. The experiment evaluates adaptation under a change in dataset, target-class composition, image distribution, and object scale using limited labeled target-domain support. presents four representative GC10-DET failure cases: two false positives and two false negatives. The examples show errors associated with defect-like background texture, illumination boundaries, low-contrast linear structure, and fine localized defect patterns. shows two recurring error patterns in the target-domain evaluation. False positives occur when periodic textures or illumination boundaries produce RoI features close to defect prototypes. False negatives occur when low-contrast or fine-scale defect regions yield weak region responses and reduced feature-prototype similarity at the detection threshold. These cases motivate adaptive or multi-prototype representations for classes with larger appearance variation. 4.3. Small-defect and comprehensive ablation analysis 4.3.1. Small defect detection capability. Small-defect performance is evaluated on representative NEU-DET categories including Crazing, Pitted Surface, and Inclusion. The comparison uses APsmall, localization error, boundary integrity score, and detection time together with attention-response and box-overlay visualizations. The same scale definition as Section is used throughout: at the resized 416 × 416 input, small objects satisfy A < 322, medium objects satisfy 322 ≤ A < 642, and large objects satisfy A ≥ 642. Localization error is computed over matched small-defect instances as the Euclidean distance, in pixels, between the centers of predicted and ground-truth bounding boxes for pairs matched at IoU ≥ 0.5. The reported localization error is the instance-level mean over the evaluated subset. Boundary integrity score is reported as an auxiliary localization diagnostic. Representative qualitative cases are shown in . shows representative Crazing, Pitted Surface, and Inclusion cases with small or fragmented regions. The attention-response overlays concentrate more strongly around the annotated defect areas, and the detection-box overlays show the corresponding predicted locations. These qualitative examples complement the quantitative small-object results reported in . reports 88.7 ± 0.4% APsmall for PA-MSAN, 5.8 percentage points above YOLOv5 and 16.3 percentage points above CNN Detector. PA-MSAN also records a localization error of 2.1 pixels, a boundary integrity score of 93.4%, and a detection time of 23.4 ms. reports category-wise localization error for representative small defects. These measurements summarize the accuracy-localization-computation profile of the complete model on the evaluated small-defect subset. 4.3.2. Comprehensive performance and ablation analysis. Controlled 5-shot ablations quantify the contributions of the multi-scale feature extractor, prototype-learning branch, Prototype Alignment Loss, spatial attention, and channel attention on NEU-DET. All variants use the same data split, seeds, detection head, training budget, optimizer parameters, augmentation policy, confidence threshold, and NMS threshold. The quantitative ablation results under the 5-shot NEU-DET setting are summarized in . The corresponding overall performance and controlled-ablation visualization is shown in . compares parameter count, detection time, and mAP@0.5 for the evaluated methods. PA-MSAN reaches 92.2% mAP@0.5 with 18.7 × 106 parameters and a detection time of 23.4 ms. and summarize the controlled ablations: removing MSFE produces the largest mAP decrease (10.7 percentage points), removing the complete prototype-learning branch lowers mAP by 7.9 points, removing PAL alone lowers mAP by 3.3 points, and removing spatial or channel attention lowers mAP by 5.0 and 3.8 points, respectively. separately evaluates the explicit multi-receptive-field configuration against the stacked-3 × 3 variant. Attention placement is evaluated with three alternatives under the same 5-shot NEU-DET protocol: attention applied separately at each FPN level before fusion, attention applied after RoI Align, and attention applied only inside the RPN. compares these variants with the proposed post-fusion, pre-RPN placement. The post-fusion, pre-RPN placement reaches 92.2 ± 0.4% mAP@0.5, compared with 90.8 ± 0.4% for per-level pre-fusion attention, 91.3 ± 0.4% for post-RoI attention, and 90.2 ± 0.5% for RPN-only attention. Among the four evaluated placements, post-fusion pre-RPN attention achieves the highest mAP@0.5. A stacked-3 × 3 variant is evaluated against the explicit 3 × 3/5 × 5/7 × 7 configuration using matched channel settings and the same FPN, attention modules, prototype branch, training budget, and evaluation protocol. The stacked-3 × 3 variant reaches 90.4 ± 0.4% mAP@0.5 and 85.9 ± 0.5% APsmall, while the proposed multi-kernel design reaches 92.2 ± 0.4% and 88.7 ± 0.4%, respectively. The corresponding feature-extractor parameter counts are 3.8 M and 4.0 M, and the reported FLOPs are 27.9 G and 25.7 G. 4.3.3. Hyperparameter sensitivity analysis. A sensitivity analysis evaluates the Prototype Alignment Loss weight [pone.0359400.e113.webp]λp over {0.1, 0.3, 0.5, 0.8, 1.0}, with all other settings fixed and each configuration repeated for seeds 0–4. reports 1-shot, 3-shot, and 5-shot mAP@0.5 together with the mean ± standard deviation of the best-validation epoch across repeated runs, and visualizes the corresponding trends. shows that mAP@0.5 increases as [pone.0359400.e120.webp]λp rises from 0.1 to 0.5 and decreases at 0.8 and 1.0. The [pone.0359400.e121.webp]λp = 0.5 setting reaches 92.2 ± 0.4% in the 5-shot condition and 84.6 ± 0.3% in the 1-shot condition. Across seeds, the best-validation epoch is 158 ± 3.1 for [pone.0359400.e122.webp]λp = 0.5, while larger weights reach their best validation scores later within the 200-epoch budget. 5. Discussion The results characterize PA-MSAN under three coupled conditions in few-shot industrial surface inspection: limited labeled samples, substantial defect-scale variation, and background texture interference. The complete architecture combines multi-scale feature extraction, spatial-channel attention, and RoI-level prototype matching, and the ablations quantify the contribution of each component under the shared 5-shot protocol. The multi-scale feature extractor combines feature-pyramid fusion with 3 × 3, 5 × 5, and 7 × 7 kernels to represent fine textures, intermediate defect structures, and broader spatial context. PA-MSAN reaches 88.7 ± 0.4% APsmall in the 5-shot NEU-DET setting, and removing MSFE lowers mAP@0.5 by 10.7 percentage points. The stacked-3 × 3 comparison in further shows that the multi-kernel configuration yields higher mAP@0.5 and APsmall with comparable feature-extractor parameters and lower reported FLOPs. Spatial-channel attention refines the fused feature representation before proposal generation. The complete model reaches 0.89 ± 0.01 average IoU and a 4.1 ± 0.2% background false response rate. Removing spatial attention lowers mAP@0.5 by 5.0 percentage points, and removing channel attention lowers it by 3.8 percentage points. These ablations are consistent with complementary roles for spatial localization and channel-wise feature selection in textured industrial backgrounds. Prototype learning provides the few-shot class representation used by the final metric classifier. Support-region features are averaged into class prototypes and evaluation RoIs are compared with these prototypes by cosine similarity, while PAL regularizes the shared embedding. Removing the complete prototype-learning branch lowers mAP@0.5 by 7.9 percentage points, and removing PAL alone lowers it by 3.3 points. PA-MSAN also records higher 5-shot mAP@0.5 than TFA, Meta R-CNN, and FSRW under the shared evaluation protocol, indicating that the proposed RoI-level prototype formulation is competitive with the evaluated few-shot detectors on NEU-DET. The general-purpose and few-shot-specific baselines provide complementary reference points. Under the 5-shot NEU-DET setting, PA-MSAN reaches 92.2 ± 0.4% mAP@0.5 with a reported detection time of 23.4 ms, compared with 87.6 ± 0.3% for YOLOv5, 89.3 ± 0.4% for Faster R-CNN, 88.4 ± 0.4% for TFA, 86.8 ± 0.5% for Meta R-CNN, and 85.9 ± 0.7% for FSRW. These results describe the accuracy-computation trade-off under the tested implementation and evaluation conditions. Cross-domain evaluation assesses PA-MSAN under a change in dataset and target-class composition. With 5-shot GC10-DET support prototypes, the model reaches 86.4 ± 0.4% mAP@0.5, compared with 92.2 ± 0.4% on NEU-DET. The 5.8-point (6.3% relative) difference reflects the combined change in class semantics, class count, object scale, image distribution, and target-domain adaptation under limited labeled support. The two-stage RPN/RoI Align configuration requires 23.4 ms per image and 18.7 × 106 parameters on the reported platform. Structured pruning [], knowledge distillation [], and lightweight backbones such as MobileNetV3 [] provide direct routes toward lower deployment cost. Evaluation on NEU-DET and GC10-DET covers steel and metallic surface defects; broader validation can extend the analysis to composite surfaces, semiconductor wafers, highly reflective components, and other production environments. Each class is represented by a single arithmetic-mean prototype, while adaptive support-feature correlation [] and multi-prototype modeling [] provide relevant directions for representing stronger intra-class morphological variation. Recent industrial defect research has highlighted information-density imbalance and heterogeneous local defect characteristics in practical surface inspection scenarios []. Self-supervised pre-training and parameter-efficient adaptation provide additional directions for improving data efficiency []. 6. Conclusion This study evaluates PA-MSAN for few-shot industrial surface defect detection with limited labeled samples, large defect-scale variation, and textured backgrounds. PA-MSAN combines multi-receptive-field feature-pyramid fusion, spatial-channel attention, RoI-level prototype matching, RPN proposal generation, and joint detection/prototype optimization. The evaluation covers controlled 1-shot, 3-shot, and 5-shot NEU-DET experiments together with limited-supervision cross-domain adaptation on GC10-DET. The method constructs class prototypes from attention-enhanced support RoI features and uses cosine similarity for final few-shot classification. RPN and bounding-box regression optimize localization through the same RoI representation, while the auxiliary linear classification head and Prototype Alignment Loss provide classifier-space and metric-space supervision during training. The ablations separately evaluate multi-scale feature extraction, prototype learning, PAL, attention components, attention placement, and the explicit multi-kernel design. Under the 5-shot NEU-DET protocol, PA-MSAN reaches 92.2 ± 0.4% mAP@0.5, 88.7 ± 0.4% APsmall, 0.89 ± 0.01 average IoU, and a 4.1 ± 0.2% background false response rate. Under the same evaluation protocol, it records higher mAP@0.5 than CNN Detector, Proto CNN, Attention CNN, YOLOv5, Faster R-CNN, TFA, Meta R-CNN, and FSRW. The controlled ablations show the largest mAP decrease after removing MSFE, followed by removal of the complete prototype-learning branch, spatial attention, channel attention, and PAL. With 5-shot target-domain GC10-DET support, PA-MSAN reaches 86.4 ± 0.4% mAP@0.5, compared with 92.2 ± 0.4% on NEU-DET. The 5.8-percentage-point (6.3% relative) difference summarizes the cross-dataset result under limited target-domain supervision. The evaluation covers NEU-DET and GC10-DET with the two-stage PA-MSAN configuration. Future work will examine adaptive and multi-prototype aggregation, lighter deployment architectures, and broader validation across industrial materials and imaging conditions.