# An Improved Feature Pyramid Network and Metric Learning Approach for Rail Surface Defect Detection
## 3 Main Results  
### Defect Feature Extraction and Location  
In this subsection, we first describe the detection of the defect of the ROI and then the pooling of the ROI and the corresponding feature map of the ROI to extract defect features. In the process of detecting defects, it is difficult to extract defects because the sizes of the defects vary. FPN is a useful tool to detect defects with different sizes, especially for small defects. Based on ResNet, the FPN extracts feature maps from different convolutional layers and then superimposes and fuses the feature maps of the previous layer and the feature maps of the current layer by twofold upsampling, which realizes the fusion of information in shallow and deep feature maps. Therefore, the FPN is improved to improve the accuracy of the model's detection of small defects. Adding attention mechanisms to deep neural networks not only makes the network pay more attention to specific inputs but also increases the attention of important features and reduces the influence of unimportant features through weight allocation. The convolutional block attention module (CBAM) is a lightweight module that works with CNN for end-to-end training. In order to increase the weight of defective features in the network training, the features of the defect are extracted by adding a CBAM, and the CBAM structure is shown in Figure 3; see [46]. Figure 3 shows that the CBAM includes two separate submodules, i.e., the channel attention module (CAM) and the spatial attention module (SAM). The output of the CAM can be obtained using the max-pooling and avg-pooling of the shared network and the output of the SAM derived from the pool, which is  
Figure 2: The structure for detecting defects on the surface of rails with few samples. transmitted between convolution layers via the channel axis. The CAM and SAM focus on the important features of the images in the channel dimension and the spatial dimension, respectively, and the two modules are connected in series. In order to obtain the attention map, the channel information of the feature map is summarized twice and connected and convolved through the standard convolution layer. The CBAM can be described by  
\[F^{\prime} =M_{\mathrm{c}}(F)\otimes F, \tag{1}\] \[F^{\prime\prime} =M_{\mathrm{s}}(F^{\prime})\otimes F^{\prime},\]  
where \(F\in R^{\mathrm{C}\times H\times W}\) is the input feature map, \(M_{\mathrm{c}}\in R^{\mathrm{C}\times 1\times 1}\) is the 1D channel attention map, \(M_{\mathrm{s}}\in R^{1\times H\times W}\) is the 2D spatial attention map, \(F^{\prime\prime}\)is the final refined output, \(\otimes\) denotes the element-wise multiplication, \(R^{\mathrm{C}\times H\times W}\) is the feature map, and \(C\), \(H\), and \(W\) are the number of channels and the height and width of the feature map, respectively. Next, the ROI extraction in the Faster RCNN was improved to extract the defective features by adding the FPN. Meanwhile, ResNet50 is a basic network, and the traditional convolution operation used in the basic ResNet50 can be defined as  
\[y(P_{0})=\sum_{P_{n}\in R}w(P_{n})\times x(P_{0}+P_{n}), \tag{2}\]  
where \(y(P_{0})\) is the convolution value of point \(P_{0}\) in the image, \(P_{n}\) is all positions in \(R\), \(R\) is the feature space, \(x\) is the input, and \(w\) is the weight of features. From Equation (2), it can be seen that the traditional convolution has the same receptive field at any position of the image and can extract an image with a fixed size. The generalization ability of traditional convolution is limited when the scales of defects change. For the varying scale of rail defects, the DC is introduced into the ResNet50, and the DC can be defined as  
\[y(P_{0})=\sum_{P_{n}\in R}w(P_{n})\times x(P_{0}+P_{n}+\Delta P_{n}), \tag{3}\]  
where \(\Delta P_{n}\) is the offset variable. When the offset is added to the DC, the magnitude and location of the DC kernel are adjusted based on the current object content after learning.