# An Improved Feature Pyramid Network and Metric Learning Approach for Rail Surface Defect Detection
## 3 Main Results
### Defect Feature Extraction and Location
In order to fit the shape and size of other objects, the convolution kernel changes the sampling points in variable locations according to the image context. Next, based on ResNet50, an improved FPN was designed to obtain the feature maps of rail surfaces with different scales. The specific improvements were as follows: the DC was added to detect the rail defect features with variable dimensions; the CBAM was added to increase the weight of the defect features and reduce the amount of calculation. Region proposal networks (RPN) of Faster RCNN were used to extract the ROI of each feature map with different sizes. Usually, the sizes of the anchors in the feature map are \(16\times 16\), \(32\times 32\), \(64\times 64\), or \(128\times 128\), but the scales of the defects may not be square, and the scales of anchors were set to 1:2, 1:1 and 2:1, respectively. Figure 4 shows the structure of  
Figure 3: CBAM structure. the improved FPN, and the structure can be described as follows: the DC operation was applied to the image, and the defect features were focused by CBAM; then, the feature maps were successively convolved \(1\times 1\), upsampled, and convolved \(3\times 3\) to obtain feature maps (denoted by P2, P3, P4, P5) with different sizes; finally, the ROI was obtained from the RPN and pooled to extract the defects. In dealing with the RPN, it is necessary to keep the prediction frame of the defect detection consistent with the true frame. Therefore, the frames need to be optimized by border regression functions. The position of the box is determined by its center coordinate, width, and height. Let \((x,y,w,h)^{T}\), \((x^{*},y^{*},w^{*},h^{*})^{T}\), and \((x_{a},y_{a},w_{a},h_{a})^{T}\) denote the center coordinates, width, and height of the predicted boundary, the true boundary, and anchor, respectively, where \(T\) denotes the transpose of a matrix. In order to calculate the deviations among the predicted frame, true frame, and anchors, the formula of the offset is established by  
\[\left\{ \begin{array}{ll}t_{x}=(x-x_{a})/w_{a},&t_{y}=(y-y_{a})/h_{a},&t_{w}= \log(w/w_{a}),&t_{h}=\log(h/h_{a}),\\ t_{x}^{*}=(x^{*}-x_{a})/w_{a},&t_{y}^{*}=(y^{*}-y_{a})/h_{a},&t_{w}^{*}=\log( w^{*}/w_{a}),&t_{h}^{*}=\log(h^{*}/h_{a}),\end{array} \right. \tag{4}\]  
where \(t_{i}=\{t_{x},t_{y},t_{w},t_{h}\}^{T}\) is the offset predicted by the anchor, and \(t_{i}^{*}=\{t_{x^{*}}^{*}t_{y^{*}}^{*}t_{y^{*}}^{*}t_{h}^{*}\}^{T}\) denotes the offset between the anchor and the true border. The loss function of the regression used in RPN is given by  
\[L_{r} =\sum_{i}P_{i}^{*}L(t_{i},t_{i}^{*})/N_{anc}, \tag{5}\] \[smooth_{L1}(t_{i}-t_{i}^{*}) =\left\{ \begin{array}{ll}0.5(t_{i}-t_{i}^{*})^{2}/\beta,&|t_{i}- t_{i}^{*}|<1/\beta,\\ |t_{i}-t_{i}^{*}|-0.5*\beta,&Otherwise,\end{array} \right. \tag{6}\]  
where \(N_{anc}\) is the number of anchors, \(P_{i}^{*}\) is the category of each anchor prediction, \(L\) is the \(smooth_{L1}\) function, and \(\beta\) is a parameter that is used to control the transformation of the function. During the training, we set the intersection over union \((IoU)\) to be the overlap rate between the predicted frame and the true frame of the ROI, and if the \(IoU>0.7\), then, the ROI is a positive sample, and \(p_{i}^{*}=1\); if the \(IoU<0.3\), then the ROI is a negative sample, and \(p_{i}^{*}=0\). There are few samples for rails surface defect detection; thus, there are not enough samples for network training, which results in the phenomenon of overfitting. In order to avoid overfitting and obtain better network parameters, an improved FPN was pretrained by using the MS COCO dataset, and the learned parameters and model of the FPN were transferred to the rail surface detection model using transfer learning. In our work, the FPN was used for the optimization detection model; so, all the convolutional blocks of ResNet50 were used as the backbone for transfer learning, so that small-scale anchor boxes  
Figure 4: The structure of an improved FPN.