# A Novel Metric-Learning-Based Method for Multi-Instance Textureless Objects' 6D Pose Estimation
## 2 Methodology
### Masked Circle Loss for Matching Dense Correspondences
Circle loss [33] proposes a unified perspective of view to explain the triplet loss and the softmax cross-entropy loss. Assume there are \(K_{in}\) within-class similarities and \(K_{out}\) between-class similarities, which are denoted by \(s^{i}_{p}(i=1,2,...,K_{in})\) and \(s^{j}_{n}(j=1,2,...,K_{out})\), respectively; \(p\) and \(n\) mean the positive and negative similarity, respectively. In order to minimize \(s^{j}_{n}(\forall j\in 1,2,...,K_{out})\) as well as to maximize \(s^{i}_{p}(\forall i\in 1,2,...,K_{in})\), the unified loss function can be designed as  
\[ \begin{split}\mathcal{L}_{uni}&=log[1+\sum_{i=1}^{ K_{in}}\sum_{j=1}^{K_{out}}exp(\gamma(s^{j}_{n}-s^{i}_{p}+m))]\\ &=log[1+\sum_{j=1}^{K_{out}}exp(\gamma s^{j}_{n})\sum_{i=1}^{K_{ in}}exp(\gamma(-s^{i}_{p}+m))]\\ &=-log\frac{\sum\limits_{i=1}^{K_{in}}exp(\gamma(s^{i}_{p}-m))}{ \sum\limits_{i=1}^{K_{in}}exp(\gamma(s^{i}_{p}-m))+\sum\limits_{j=1}^{K_{out}} exp(\gamma s^{j}_{n})}\end{split}  \tag{7}\]  
where \(\gamma\) is a scale factor. We can find out that if we set \(\gamma=1\), \(m=0\), and \(K_{in}=1\), Equation (7) degenerates to the softmax cross-entropy loss, as shown in Equation (3). The main purpose of the function is to minimize \((s_{n}-s_{p})\), in which reducing \(s_{n}\) is equivalent to increasing \(s_{p}\). Circle loss introduces \((\alpha_{n}s_{n}-\alpha_{p}s_{p})\) instead of \((s_{n}-s_{p})\), where  
\[ \begin{cases}a^{i}_{p}=[O_{p}-s^{i}_{p}]_{+},\\ a^{i}_{n}=[s^{j}_{n}-O_{n}]_{+},\end{cases}  \tag{8}\]  
in which \([. ]_{+}\) is the ReLU function that ensures \(\alpha^{i}_{p}\) and \(\alpha^{j}_{n}\) are non-negative; \(\alpha^{i}_{p}\) and \(\alpha^{j}_{n}\) adjust the weight so the gradient of reducing \(s_{n}\) is equivalent to increasing \(s_{p}\). When \(s_{n}\) approaches zero and \(s_{p}\) approaches one, the gradients drop to a small value according to \(\alpha^{i}_{p}\) and \(\alpha^{j}_{n}\). It intuitively emphasizes the hard examples where \(s_{n}\) is similar to \(s_{p}\). As for the purpose of dense 2D-3D correspondence matching, we need to emphasize the hard examples and pay less attention to the easy case. Thus, the circle loss is more suitable than the softmax cross-entropy loss. Another problem for the 2D-3D correspondence matching is that the ground-truth poses of the objects have measurement errors that lead to the mismatch of the correspondences. To overcome the problem, we assign a neighborhood area \(N\) for each pixel. If the nodes on the 3D mesh model lie in the neighborhood area, they are regarded as positive correspondences. Each pixel has its own neighborhood area to eliminate the influence of the measurement errors of the ground-truth poses. For every neighborhood area, we set a mask on it, and name the overall loss function the masked circle loss. The masked circle loss can be formulated as  
\[ \begin{split}\mathcal{L}_{m\_circle}=\frac{1}{u}\sum_{k=1}^{u}log [1+\sum_{i\
otin N_{l}}exp(\gamma\alpha^{j}_{n}(s^{j}_{n}-\Delta_{n}))\sum_{j \in N_{k}}exp(-\gamma\alpha^{i}_{p}(s^{i}_{p}-\Delta_{p}))]\end{split}  \tag{9}\]  
where \(u\) denotes the number of pixels that belong to the object in the image; \(\Delta_{p}=1-m\) and \(\Delta_{n}=m\) are the margin between the positive pairs and negative pairs; \(N_{k}\) denotes the set of nodes from 3D mesh model that lie in the neighborhood area of pixel \(k\). The final loss of the network is defined as the combination of the segmentation loss and the correspondence matching loss  
\[\mathcal{L}_{all}=\mathcal{L}_{seg}+\zeta\mathcal{L}_{m\_circle} \tag{10}\]  
where \(\zeta\) is a hyperparameter to balance the two parts of the loss; \(\mathcal{L}_{seg}\) is the pixelwise softmax cross-entropy for the semantic segmentation of the objects. After the dense correspondences are obtained, PnP with RANSAC method is used to calculate the final pose of the target.