## 2 The Proposed Method  
### Overall Framework  
As shown in Figure 1, the proposed railway image anomaly detection framework comprises three sequential stages: data preprocessing, model training, and inference validation. In the initial phase, input railway images are processed through a YOLOv5 model for fastener region detection and localization. The extracted individual fastener images subsequently undergo size normalization and data augmentation operations. These pre-processed samples are then channeled into the unsupervised teacher-student network architecture for either the training phase or inference verification process. During the training phase, normal fastener samples drive a teacher-student dual-path generative adversarial network: the teacher network extracts multi-scale features through its encoder to guide feature alignment in the student network via knowledge distillation, while adversarial training optimizes the discriminator's capability to evaluate reconstruction quality. In the inference stage, test images are directly fed into the student network's encoder-generator module for sample reconstruction. The discriminator calculates anomaly scores and generates Grad-CAM heatmaps to localize anomalous regions, with final detection outcomes (normal/abnormal) determined by preset thresholds. This modularly designed workflow explicitly illustrates data flow paths and functional interdependencies, effectively balancing algorithmic robustness with result interpretability. Rail fasteners, as core components of railway rail systems, typically exhibit small physical sizes. In this study, the YOLOv5 is first utilized for preprocessing the fastener dataset to extract the fastener regions [42; 43; 44]. Subsequently, multi-scale feature information is integrated through a teacher-student network architecture to enhance the detection capability for anomalies with complex shapes. The overall structure of the proposed method is illustrated in Figure 2. During training, the model only requires normal images, enabling it to automatically capture the feature distribution of normal samples. In the testing phase, the model receives both normal and abnormal images, and identifies anomaly regions by comparing differences between input and reconstructed images. The teacher network comprises two main components: the encoder (E) and the teacher generator (G\({}_{\text{T}}\)). A MSAC module is introduced into both the encoder of the teacher network and the generator. This module fuses features at multiple scales, significantly enhancing the network's ability to perceive local details and global semantic information, thereby enabling a more comprehensive understanding of the input data. Furthermore, to improve the accurate expression of abnormal position information in rail fasteners under complex background environments, a coordinate channel attention mechanism (CASE) mechanism is specifically designed within the MSAC at the skip connection step. The CASE effectively suppresses background noise interference, enabling the teacher network to more accurately locate and express the abnormal positions of rail fasteners, thereby reducing errors caused by background interference. Figure 1: The flow chart of the proposed method. The student network architecture comprises an encoder (E), a student generator (\(\rm G_{s}\)), and an enhanced anomaly discriminator (D). Notably, an in-painting block [38] is incorporated between the encoder and the student generator. This block is designed to restore the image and enhance the quality of restoration during the upsampling process, ensuring that detailed information is more accurately recovered, thus providing higher-quality image data for subsequent processing. Additionally, the Enhanced Joint Pyramid Up Sampling Module (E-JPU) is introduced into the enhanced anomaly discriminator to enhance the model's ability to detect fine details of rail fasteners, improving its capacity to capture small-scale anomalies and increasing the overall accuracy of the network in detecting rail fastener anomalies.