# Defect Detection for Wear Debris Based on Few-Shot Contrastive Learning
## 3 Method
### Aligned Contrastive Loss  
The features of wear debris defect are too analogous to distinguish them, which is a burden on the detector of the model, as shown in Figure 2. Traditional methods fuse query features and support features by channel-wise multiplication [7], element-wise subtraction [6], without treating support features as a supervision information. Figure 1: The overall architecture. Inspired by the pretext task of unsupervised contrastive model MoCo [27], instance discrimination, we introduce _Aligned Contrastive Loss_, which is based on _InfoNCE Loss_[28] and is parallel to the RoIPooling, to the primary model, as shown in Equation (1). \[L_{Aligned\ Contrastive}=-\log\frac{\exp(f(Aligned(q,k_{+})*q,k_{+})/\tau)}{ \sum_{1}^{k}\exp(f(Aligned(q,k_{i})*q,k_{i})/\tau))} \tag{1}\]  
Specifically, the model learns to match Pf (Proposal of foreground) and Pb (Proposal of background) generated by RPN in the query image with the Sp (Support positive) object and Sn (Support negative) object in the support image. Thus, the model learns to not only match the objects in positive pair (Pf, Sp) but also distinguish objects in negative pairs (Pf, Sn), (Pb, Sp) and (Pb, Sn). The ratio of the positive and negative pairs is 1:3. The _InfoNCE Loss_ is shown in Equation (2). \[InfoNCE\ Loss=-\log\frac{\exp(pos)}{\exp(pos+neg)} \tag{2}\]  
Referring to SimCLR [29] and MoCo, the features from query and support branch were regularized by L2 norm and projected by a MLP projector. Another form of _InfoNCE Loss_ is shown in Equation (3). \[InfoNCE\ Loss=-\log\frac{\exp(f(q\cdot k_{+})/\tau)}{\sum_{1}^{k}\exp(f(q\cdot k _{i})/\tau)} \tag{3}\]  
In Equation (3): \(c\) is the total number of positive and negative pairs within the current episode; \(q\) is the query feature of the proposal; \(k_{+}\) is the support feature of the positive pairs; \(k_{i}\) is the support feature of the positive/negative pairs; \(i\) is the \(i\)-th pair in this batch; \(f\) is a distance metric function, such as cosine distance, Euclidean distance, and so on. The hyper-parameter \(\tau\) is a hyper-parameter that controls the concentration level of the distribution. We perform a hyper-parameter searching for \(\tau\), and select 0.2 as the default value of \(\tau\) in this paper. However, due to the inaccurate localization of proposals generated by RPN, spatial misalignment between proposals and support examples has a negative effect on the matching. Instance discrimination is a classification task, so there is no misaligned problem. FSCE [3] filters out proposals that are heavily misaligned using IoU threshold. Inspired by Meta-FasterRCNN [8], we design an alignment module that establishes soft correspondences between query features and support features. Then the regions of suppression and highlight are deduced. So, the misaligned problem is alleviated in a certain extent. In alignment module, _Aligned Mask_ is calculated by multiplication and sigmoid operation. _Aligned Mask_ is calculated as Equation (4):  
\[Aligned\ Mask=Aligned(Q,K)=\text{sigmoid}\bigg{(}\frac{QK^{T}}{\sqrt{d}} \bigg{)} \tag{4}\]  
Figure 2: The features of wear debris defect have small inter-category variance, between fatigue and copper. In Equation (4): \(Q\) is the query feature of the proposal; \(K^{T}\) is the transpose of support feature in the positive/negative pairs; \(Q\) is the scaling factor of features. _InfoNCE Loss_ with alignment module(_Aligned Contrastive Loss_) is shown in Figure 3. The value in the _Aligned Mask_ means the soft correspondences between each position in query and support feature maps. Specifically, in Faster RCNN frameworks, RPN takes backbone feature maps as inputs and generates region proposals, RoI head feature extractor first pools the region proposals to fixed size and then encodes them as vector embeddings known as the RoI features. Then, alignment module adjust the RoI features to handle the misaligned problem. Finally, we utilize the _InfoNCE Loss_ function to optimize model parameter.