# An Enhanced Contrastive Ensemble Learning Method for Anomaly Sound Detection
## 2 Materials and Methods
### Contrastive-Based Spectrogram Domain Model
#### 2.4.1 Pre-Train Progress  
During the pre-training progress, a multi-task learning approach is used to improve model performance and help the model to converge. The pre-training structure is improved from the Simple Siamese network (Simsiam) [20]. This paper not only calculates the cosine similarity, but also employs a self-supervised classification task to further enhance the model feature representation capabilities. This paper employs GhostnetV2 [21] model as the feature embedding network which can not only accurately capture features but also reduce the number of model parameters, thereby enhancing computational speed. For this backbone network, this paper only uses the front feature extraction convolutional layer without using the last fully connected classification layer. Moreover, to further improve the model generalization ability and data diversity, this paper randomly employs signal transformations such as time shift, pitch shift, fade in/out and add noise. The time shift transformation shifts the audio signal either forward or backward. In this paper, the time shift rate is chosen from the range of 0-56. The pitch shift transformation shifts the pitch of the waveform. In this paper, the step to shift the waveform is set to \(-20\). The fade in/out transformation adds a fade in or fade out to a waveform at its beginning or end. This paper uses two types of fading: exponential and half sinusoidal. And adding noise transformation inserts a white Gaussian noise in the waveform. In this paper, the signal-to-noise ratio (SNR) is chosen in the range of [\(-\)10, 10]. After applying these transformations, the input spectrogram maps are then fed into the Simsiam structure. By maximizing the similarity of the same class samples, the backbone model can efficiently model sample features. Moreover, in order to improve the model performance better, this paper designs a self-supervised classification task for this backbone model.  
Figure 2: Model structure: The model is divided in two sections, the upper section is the pre-trained model and the lower is the fine-tuned model.  
For the contrastive learning pre-trained model, the total cosine similarity can be calculated as follows:  
\[\begin{split} L=\frac{1}{2}D(p_{1},z_{2})+\frac{1}{2}D(p_{2},z_{1}), \\ D(d_{1},d_{2})=-\frac{d_{1}}{\|d_{1}\|_{2}}\cdot\frac{d_{2}}{\|d_{2}\| _{2}},\end{split} \tag{3}\]  
where \(p\) and \(z\) represent two feature vectors, and \(D\) represents the cosine similarity function. However, due to the lack of different class samples, the model will collapse easily during the training progress. Therefore, to avoid this problem, a stop-gradient operation [20] is imposed on one of the feature vectors. So the final cosine similarity loss functions is reformulated as follows:  
\[L=\frac{1}{2}D(p_{1},stopgrad(z_{2}))+\frac{1}{2}D(p_{2},stopgrad(z_{1})), \tag{4}\]  
After the contrastive learning progress, the backbone is trained with an auxiliary classification task. In the auxiliary classification task, the feature vectors of the input spectrograms from the Siamese network are fed into the embedding head, which outputs the predicted label values. Subsequently, the ArcFace [22] loss function is utilized to calculate the loss. The arcface loss functions is as follows:  
\[L=-\frac{1}{N}\sum_{i=1}^{N}\log\frac{e^{s(cos(\theta_{y_{i}}+m))}}{e^{s(cos( \theta_{y_{i}}+m))}+\sum_{k=1,k\
eq y_{i}}^{K}e^{cos\theta_{k}}}, \tag{5}\]  
where \(N\) represents the batch size number, \(s\) represents the scale parameter, \(m\) represents the margin parameter, \(\theta_{y_{i}}\) represents the angle value of the target category corresponding to the sample \(y_{i}\), \(\theta_{k}\) represents the angle value of the non-target category corresponding to sample \(y_{i}\) and \(K\) represents the number of non-target categories.