# A Study on Small-Scale Snake Image Classification Based on Improved SimCLR
## 2 Materials and Methods
### Related Network
#### 2.3.3 Introduction of Supervised Loss Function  
In SimCLR, the loss function is unsupervised and uses positive sample pairs generated based on data enhancement with unknown positive and negative sample labels. Positive sample pairs are generated through data enhancement, and feature representation is optimized with the help of the contrastive learning loss function. The formula is  
\[\ell_{ij}=-\log\frac{exp\bigg{(}\frac{sim\big{(}z_{i}z_{j}\big{)}}{\tau}\bigg{)} }{\sum_{k=1}^{2N}1_{[k\
eq i]}exp\bigg{(}\frac{sim\big{(}z_{i}z_{j}\big{)}}{ \tau}\bigg{)}} \tag{4}\]  
However, for small-sample, high-similarity category tasks, reliance on unsupervised positive and negative sample partitioning may be inadequate due to the absence of intra-class features and an insufficient number of negative samples. The lack of explicit category information may result in samples of the same category being classified as negative. In contrast, the paucity of negative samples in a small dataset may compromise the efficacy of comparative learning.  
In addressing these limitations, this paper proposes utilizing sample labeling information to ensure the aggregation of similar samples within the same category while promoting the separation of samples from different categories. This approach is formalized as a supervised loss function [32], the specific steps of which are outlined below:  
1. Feature normalization: The initial step involves the L2 normalization of the input feature vector to eliminate the effect of feature length. This ensures that the subsequent similarity calculation is contingent on the direction of the feature as opposed to the magnitude. Consequently, this process guarantees that the calculated similarity is consistent with the relative relationship between the samples: \[features=\frac{features}{\|features\|}\] (5)
2. Similarity calculation: The similarity matrix is then generated by calculating the cosine similarity between features: \[similarity_{matrix}=features*features^{T}\] (6)
3. Positive and Negative Sample Distinction: Within the framework of the loss function, labeling information is employed to explicitly differentiate between positive and negative sample pairs. Samples from the same category are designated positive sample pairs, whereas samples from different categories are classified as antagonistic sample pairs. A positive sample mask is constructed from the label information to ensure that the computation prioritizes the similarity between pairs of samples from the same category: \[mask=labels=labels^{T}\] (7)
4. Removal of the self-comparison effect: To avoid the effect of the similarity between the self-sample and itself in the calculation, we exclude the diagonal elements, i.e., the similarity between the sample and itself, by modifying the similarity matrix: \[logits=similarity_{matrix}-eye(n)*le12\] (8)
5. Loss function calculation: Finally, the contrast loss is optimized by calculating the ratio of the similarity of the positive sample pair to the similarity of all sample pairs. Weuse a temperature parameter to adjust the similarity scale and calculate the final loss through a logarithmic function and weighted average. The goal of this loss function is to minimize the similarity of sample pairs of the same category and maximize the similarity of sample pairs of different categories:  
\[loss=-\frac{1}{N}\sum log\bigg{(}\frac{\exp{(logits)*mask}}{ecp(logits)}\bigg{)} \tag{9}\]  
In actual model training, a fixed batch size of N = 64 was used, and each sample was generated into a positive view pair through two independent random enhancements. The same class samples within the batch were used as positive sample pairs using category labels. In contrast, samples of different categories in the batch were automatically treated as negative samples. The temperature hyperparameter was set to 0.07, referring to the default value in the SupCon Loss paper [32].