# Revisiting Low-Resolution Images Retrieval with Attention Mechanism and Contrastive Learning
## 2 Materials and Methods
### Auxiliary Module: Classification Loss and Triplet Loss  
Contrastive loss maximizes agreement between augmented versions derived from the same source without implicitly sampling negative pairs [16]. To achieve a better performance, many negative pairs are sampled to ensure the convergence of the contrastive objective function. For example, [16] used a batch size of 8196. In that case, 16,382 negative samples per positive pair were given from both augmentation views, and the same conditions were applied in [37] with a batch size of 6144. Using a large batch size is a computational burden and hard to train with regular optimizations [38; 39]. In this study, instead of using a large batch size, we leverage the samples' label to implicitly generate pairs of negative and positive samples. We also found that training with implicit labels or supervised training is standard practice to learn embedded vectors for category image retrieval.  
As proposed in previous literature on image retrieval, softmax cross-entropy loss and ranking loss, such as triplet loss, are used for end-to-end training of a CNN backbone [6] or to fine-tune model triplet loss based on a classifier trained with cross-entropy loss [5; 8]. In this study, we train the model with auxiliary classification loss to maximize inter-class distance and utilize triplet loss to rank the embeddings of inter-class pairs over intra-class pairs. We add label smoothing [40] and temperature scaling [41] in the auxiliary cross-entropy loss function to prevent overconfidence and to learn better embedding.  
\[L^{CRE}=-\frac{1}{2N}\sum_{i=1}^{2N}log\frac{\exp\Bigl{(}\frac{c_{i\
eq i}}{ \tau}\Bigr{)}}{\sum_{j}^{M}\exp\Bigl{(}\frac{c_{i\
eq j}}{\tau}\Bigr{)}}, \tag{8}\]  
where \(2N\) is the batch size, including augmented samples, M is the number of classes, and \(\tau\) is the temperature scaling factor. \(c_{i}=W^{\tau}z_{i}+b\) is logits of sample \(i\)th obtained by adding a trainable liner layer over the embedded vector \(e_{i}\).  
Additionally, we add triplet loss to the objective function. Minimizing the triplet loss in the embedding space results in instances with the same label, and its augmentations should be closer together to form well-separated clusters. The version of triplet loss used in this study is online triplet mining using the hard-batch strategy [42]. For each sample \(a\) in the batch, we can select the hardest positive and the hardest negative samples within the batch when forming the triplets to be used for computing the loss.  
\[L^{triplet}=\sum_{i=1}^{P}\sum_{a=1}^{K}\left[m+\max_{\mathrm{p=1...K}}D\big{(} e_{iu},e_{ip}\big{)}-\min_{j=1..P,n=1..K,j\
eq i}D\big{(}e_{iu},e_{jn}\big{)} \right], \tag{9}\]  
where \(m\) is margin and \(P\) and \(K\) are the number of classes and the number of samples in these classes, calculated within a minibatch. The final loss function for end-to-end training of our framework is the weighted summation of a contrastive loss and the two auxiliary losses.  
\[L=\alpha L^{self}+(1-\alpha)L^{sup}+\beta\times L^{CRE}+\gamma L^{triplet}, \tag{10}\]  
where \(\alpha,\beta,\gamma\in\{0,1\}\) are hyperparameters to control the presence of each loss.