## 3 Background
### Offline and Online Triplet Mining  
Preparing triplets can be offline or online which is referred to as offline/online triplet mining. In the offline triplet mining method, triplets are prepared before the training begins, e.g., at the start of each epoch, by computing all the possible triplets on the training data, and then the hardest of them are selected. However, this approach is very inefficient since computing all possible triplets and choosing hard triplets at the start of each epoch is very time-consuming. In the online triplet mining, also known as batch-wise approach or technique of in-batch negative [62], the idea is to prepare triplets during the training step within a mini-batch of data [60; 63], where for each anchor in a batch, other in-batch positives and negatives are taken as negatives. There are several contrastive loss functions based on online triplet mining in the literature. Here, we introduce five of them. The first two are based on margin and the rest are based on cross-entropy. **BatchAll and BatchHard:** Ref. [39] proposed two online (batch-wise) margin-based triplet losses (BatchNormHard and BatchAll) which only deal with positive samples from each class. The way they work is to accept a batch with \(P\times K\) samples (\(P\) classes with \(K\) positive instances per each class). Therefore, for each class in the batch, the other classes' positives act as negatives samples. BatchAll loss function is in the simple margin-based triplet loss format, is summed over all the possible triplets in the mini-batch, and was formulated as:  
\[L_{BA}=\overbrace{\sum_{i=1}^{P}\sum_{a=1}^{K}}^{all\ anchors}\overbrace{ \sum_{p=1}^{K}}^{all\ positives\ all\ negatives}\overbrace{\sum_{j=1}^{P}\sum_{n=1}^{K}}^{ \sum_{p=1}^{K}}\ \ \left[D\left(x_{a}^{i},x_{p}^{i}\right)-D(x_{a}^{i},x_{n}^{j})+m\right]_{+} \tag{2}\]  
where \(m\) is the margin, \(D(\cdot)\) is the metric distance function, and \([\cdot]_{+}:=max(\cdot,0)\) is the standard hinge loss [64]. In the BatchHard loss function, for each anchor \(x_{a}^{i}\) the Hardest Positive and Hardest Negative (HPHN), which are the farthest positive and nearest negative in the mini-batch, are selected. Hence, its loss function is:  
\[L_{BH}=\overbrace{\sum_{i=1}^{P}\sum_{a=1}^{K}}^{all\ anchors} \left(\overbrace{\underbrace{\max_{p=1\dots K}D\left(x_{a}^{i},x_{p}^{i} \right)}_{\underbrace{ \begin{subarray}{c}j=1\dots P\\ n=1\dots K\\ j\
eq i\end{subarray} }_{ \begin{subarray}{c}j=1\dots P\\ n=1\dots K\\ j\
eq i\end{subarray} }D(x_{a}^{i},x_{n}^{j})}^{\text{hardest positive}}+m \right)_{+} \tag{3}\]  
**Normalized Temperature-Scaled Cross-Entropy loss (NT-Xent)**: Ref. [38] proposed a batch-wise self-supervised contrastive loss which is a modification of the multi-class N-pair loss [40] with addition of the temperature parameter \(\tau\) to scale the cosine similarities. This loss function only accepts positive pairs in the form of \((x_{i},x_{i}^{+})\). That is, each sample in the batch belongs to just one positive pair and all other possible pairs with \(x_{i}\) are negative pairs (denominator). Therefore, for a positive pair of \((x_{i},x_{i}^{+})\) within a mini-batch of N pairs, the loss function is defined as:  
\[L_{NT-Xent}=-\log\frac{e^{sim(x_{i},x_{i}^{+})/\tau}}{\sum_{j=1}^{N}e^{sim(x_{ i},x_{j}^{+})/\tau}} \tag{4}\]  
where \(sim(\cdot)\) is the standard cosine similarity. **Supervised Contrastive loss (SupCon):** Ref. [32] extended the self-supervised NT-Xent loss to the fully supervised setting by adding multiple positives. The self-supervised NT-Xent loss pulls the anchor and the positive sample closer together and pushes the anchor away from many negative samples (other positive samples in batch), while the SupCon usesadditional information by adding multiple hard positives for each class. Hence, SupCon generalizes the NT-Xent loss to use multiple positive samples from the same class for each anchor.