## 2 Methodology
### Discriminator  
Since the detector may obtain incorrect results, removing the wrong results and keeping the correct results is the key to improving the detection accuracy of the entire algorithm, which is exactly what the discriminator can achieve. The det_boxes produced by the detector contain defects and textures, which have the characteristics of a large scale span, diverse forms, and many categories. The essence of the discriminator is to distinguish between defects and textures so that there are significant differences between them. It is necessary to solve feature mapping of defects and textures to obtain a longer feature distance from each other. Therefore, building a discriminant model based on deep metric learning [27] can achieve good results to deal with such problems. The discriminator network structure is shown in Figure 5, including the backbone, neck, and the head. The det_boxes and ref_boxes are linearly interpolated to a resolution of 224 \(\times\) 224, combined with simple preprocessing, and then sent to the network simultaneously. From the comprehensive analysis of the speed and feature representation effect, the backbone is partly modified based on MobileNetV3-Large [28], the layer behind conv2d-1 \(\times\) 1 is removed, and the output is a tensor of 1 \(\times\) 1 \(\times\) 1280. Due to the relatively small area of det_boxes and ref_boxes, too many channels will not significantly improve the feature representation ability. On the contrary, it will reduce the subsequent calculation speed. Therefore, the FC layer is used as a Neck module to map features to 512 dimensions. In order to improve the feature discrimination ability of the discriminator, head is added immediately after the neck, and the Arc-margin [29] function in metric learning is used to maximize the classification boundary in the angular space to achieve the effect of increasing the outer distance of the class and reducing the inner distance. At the same time, the cross-entropy loss function (CE_Loss) is integrated to improve the feature separation ability to prevent the Arc-margin gradient from disappearing. Therefore, the total loss function can be expressed as:  
\[L_{tol}=\alpha L_{ce}+\beta L_{arc} \tag{3}\]  
where \(\alpha\) and \(\beta\) are the weights of Arc-margin and CE_Loss, \(L_{ce}\) and \(L_{arc}\)[28] can be expressed as:  
\[L_{ce}=-\frac{1}{N}\sum_{i=1}^{N}\log\frac{e^{W_{ji}^{T}x_{i}+b_{ji}}}{\sum_{j= 1}^{n}e^{W_{j}^{T}x_{i}+b_{j}}} \tag{4}\]  
\[L_{arc}=-\frac{1}{N}\sum_{i=1}^{N}\log\frac{e^{s(\cos(\theta_{ji}+m))}}{e^{s( \cos(\theta_{ji}+m))}+\sum_{j=1}^{n}j\
eq b_{ji}}e^{s\cos\theta_{ji}} \tag{5}\]  
Figure 5: Network structure diagram of a discriminator. Combined Equations (3)-(5) can be obtained:  
\[L_{tol}=-\frac{1}{N}\Bigg{(}\alpha\sum\limits_{i=1}^{N}\log\frac{e^{W_{y_{i}}^{T }x_{i}+b_{y_{i}}}}{\sum\limits_{j=1}^{n}e^{W_{j}^{T}x_{i}+b_{j}}}+\beta\sum \limits_{i=1}^{N}\log\frac{e^{s(\cos(\theta_{y_{i}}+m))}}{e^{s(\cos(\theta_{y _{i}}+m))}+\sum\
olimits_{j=1,j\
eq y_{i}}^{n}e^{s\cos\theta_{j}}}\Bigg{)} \tag{6}\]  
where batch size and the class number are N and n. \(y_{i}\) and \(x_{i}\), respectively, denote the category label of the \(i\)-th sample and the feature vector, \(x_{i}\) is set to 512 in this paper following. \(b_{j}\) and \(b_{yi}\), respectively, represent the bias term. \(w_{yi}\) and \(w_{j}\) indicate the \(y_{i}\)-th column and the \(j\)-th column of the weight W, respectively. \(S\) is the radius of the hypersphere, \(\theta_{yi}\) and \(\theta_{j}\) are the angles between the weights \(w_{yi}/w_{j}\) and the feature vector \(x_{i}\), and \(m\) is the additional angle penalty factor between \(\theta_{yi}\) and \(x_{i}\). In the inference, the output of the neck part is used as the model feature output. After det_boxes and ref_boxes pass through the discriminator, if it is a proper defect box, the margin of the two feature vectors in the angle space is bigger than zero; otherwise, the margin equals zero. Therefore, the angle cosine distance metric is adopted.