In defence of metric learning for speaker recognition
Joon Son Chung Jaesung Huh Seongkyu Mun Minjae Lee Hee Soo Heo Soyeon Choe Chiheon Ham Sunghwan Jung Bong-Jin Lee Icksang Han
eess.AS cs.SD


Georg Mohr's “Euclides Danicus” – Preliminary Version
    Ricardo Bianconi (translation and commentary)
    2020-03-25
=====================================================




The objective of this paper is `open-set' speaker recognition of unseen speakers, where ideal embeddings should be able to condense information into a compact utterance-level
representation that has small intra-speaker and large inter-speaker distance. 

A popular belief in speaker recognition is that networks trained with classification objectives outperform metric learning methods. 
In this paper, we present an extensive evaluation of most popular loss functions for speaker recognition on the VoxCeleb dataset. We demonstrate that the vanilla triplet loss shows competitive performance compared to classification-based losses, and those trained with our proposed metric learning objective outperform state-of-the-art methods. 




Index Terms: speaker recognition, speaker verification, metric learning.








§ INTRODUCTION

The code for this paper can be found at:
 <https://github.com/clovaai/voxceleb_trainer>
Research on speaker recognition has a long history and has received
an increasing amount of attention in recent years.
Large-scale datasets for speaker recognition such as the VoxCeleb <cit.> and Speakers in the Wild <cit.> have become freely available, facilitating fast progress in the field.

Speaker recognition can be categorised into closed-set or open-set settings. For closed-set setting, all testing identities are predefined in training set, therefore can be addressed as a classification problem. For open-set setting, the testing identities are not seen during training, which is close to practice. This is a metric learning problem in which voices must be mapped to a discriminative embedding space. The focus of this research, and most others, are on the latter problem.

Pioneering work on speaker recognition using deep neural networks have learnt speaker embeddings via the classification loss <cit.>.
Since then, the prevailing method has been to use softmax classifiers to train the embeddings <cit.>. 
While the softmax loss can learn separable embeddings, they are not discriminative enough since it is not explicitly designed to optimise embedding similarity.  
Therefore, softmax-trained models have often been combined with PLDA <cit.> back-ends to generate scoring functions <cit.>. 

This weakness has been addressed by <cit.> who have proposed angular softmax (A-Softmax) where cosine similarity is used as logit input to the softmax layer, and a number of works have demonstrated its superiority over vanilla softmax in speaker recognition <cit.>. 
Additive margin variants, AM-Softmax <cit.> and AAM-Softmax <cit.>, have been proposed to increase inter-class variance by introducing a cosine margin penalty to the target logit, and these have been very popular due to their ease of implementation and good performance <cit.>. However, training with AM-Softmax and AAM-Softmax has proven to be challenging since they are sensitive to the value of scale and margin in the loss function.

Metric learning objectives present strong alternatives to the prevailing classification-based methods, by learning embeddings directly.
Since open-set speaker recognition is essentially a metric learning problem, 
the key is to learn features that have small intra-class and large inter-class distance. 
Contrastive loss <cit.> and triplet loss <cit.> have been demonstrated promising performance on speaker recognition <cit.> by optimising the distance metrics directly, but these methods require careful pair or triplet selection which can be time consuming and performance sensitive.

Of closest relevance to our work is
prototypical networks <cit.> that learn a metric space in which open-set classification can be performed by computing distances to prototype representations of each class, with a training procedure that mimics the test scenario. The use of multiple negatives helps to stabilise learning since loss functions can enforce that an embedding is far from all negatives in a batch, rather than one particular negative in the case of triplet loss. <cit.> have adopted the prototypical framework for speaker recognition.
Generalised end-to-end loss <cit.>, originally proposed for speaker recognition, is also closely related to this setup.

Comparing different loss functions from prior works can be challenging and unreliable, since speaker recognition systems can vary widely in their design. Popular trunk architectures include TDNN-based systems such as x-vector <cit.> and its deeper counterparts <cit.>, as well as network architectures from the computer vision community such as the ResNet <cit.>. A range of encoders have been proposed to aggregate frame-level informations into utterance-level embeddings, from simple averaging <cit.> to statistical pooling <cit.> and dictionary-based encodings <cit.>. 
<cit.> has proven that data augmentation can significantly boost speaker recognition performance, but the augmentation methods can range from adding noise <cit.> to room impulse response (RIR) simulation <cit.>.

Therefore, in order to directly compare a range of loss functions, we conduct over 20,000 GPU-hours of careful experiments while keeping other training details constant. Against popular belief, we demonstrate that the networks trained with vanilla triplet loss show competitive performance compared to most AM-Softmax and AAM-Softmax trained networks, and those trained with our proposed angular objective outperform all comparable methods.











§ TRAINING FUNCTIONS



This section describes the loss functions used in our experiments, including a new angular variant of the prototypical loss. 




 §.§ Classification objectives


The VoxCeleb2 development set contains C=5,994 speakers or classes. During training, each mini-batch contains N utterances each from different speakers, whose embeddings are 𝐱_i and the corresponding speaker labels are y_i where 1 ≤ i ≤ N and 1 ≤ y ≤ C.

Softmax.
The softmax loss consists of a softmax function followed by a multi-class cross-entropy loss. It is formulated as:

    L_S=-1/N∑_i=1^Nloge^𝐖^T_y_i𝐱_i+b_y_i/∑_j=1^C e^𝐖^T_j𝐱_i+b_j
  
where 𝐖 and b are the weights and bias of the last layer of the trunk architecture, respectively.  
This loss function only penalises classification error, and does not explicitly enforce intra-class compactness and inter-class separation.

AM-Softmax (CosFace).
By normalising the weights and the input vectors, softmax loss can be reformulated such that
the posterior probability only relies on cosine of angle between the weights and the input vectors. This loss function, termed by the authors as Normalised Softmax Loss (NSL), is formulated as:

    L_N=-1/N∑_i=1^Nloge^cos(θ_y_i, i)/∑_j e^cos(θ_j, i)

where cos(θ_j,i) is the dot product of normalised vector 𝐖_j and 𝐱_i.

However, embeddings learned by the NSL are not sufficiently discriminative because the NSL only penalises classification error. In order to mitigate this problem, cosine margin m is incorporated into the equation:

    L_C=-1/N∑_i=1^Nloge^s(cos(θ_y_i, i)-m)/e^s(cos(θ_y_i, i)-m) + ∑_j≠y_i e^s(cos(θ_j, i))

where s is a fixed scale factor to prevent gradient from getting too small in training phase.

AAM-Softmax (ArcFace). This is equivalent to CosFace except that there is additive angular margin penalty m between 𝐱_i and 𝐖_y_i. The additive angular margin penalty is equal to the geodesic distance margin penalty in the normalised hypersphere.

    L_A=-1/N∑_i=1^Nloge^s(cos(θ_y_i, i+m))/e^s(cos(θ_y_i, i+m)) + ∑_j≠y_i e^s(cos(θ_j, i))





 §.§ Metric learning objectives


For metric learning objectives, each mini-batch contains M utterances from each of N different speakers, whose embeddings are 𝐱_j,i where 1 ≤ j ≤ N and 1 ≤ i ≤ M.

Triplet. Triplet loss minimises the L2 distance between an anchor and a positive (same identity), and maximises the distance between an anchor and a negative (different identity). 

    L_T= 1/N∑_j=1^Nmax(0,‖𝐱_j,0 - 𝐱_j,1‖^2_2 - ‖𝐱_j,0 - 𝐱_k≠ j,1‖^2_2 + m)


For our implementation, the negative utterances are sampled from different speakers within the mini-batch and the sample 𝐱_k is selected by the hard negative mining function. This requires M=2 utterances from each speaker.

Prototypical.
Each mini-batch contains a support set S and a query set Q. For simplicity, we will assume that the query is M-th utterance from every speaker. Then the prototype (or centroid) is:

    𝐜_j = 1/M-1∑_m=1^M-1𝐱_j,m

Squared Euclidean distance is used as the distance metric as proposed by the original paper:

    _j,k = ‖𝐱_j,M - 𝐜_k ‖^2_2

During training, each query example is
classified against N speakers based on a softmax over distances to each speaker prototype:

    L_P = -1/N∑_j=1^Nloge^_j,j/∑_k=1^N e^_j,k

Here, _j,j is the squared Euclidean distance between the query and the prototype of the same speaker from the support set. The softmax function effectively serves the purpose of hard negative mining, since the hardest negative would most affect the gradients.
The value of M is typically chosen to match
the expected situation at test-time, 
e.g. M=5+1 for 5-shot learning, so that the prototype is composed of five different utterances.
In this way, the task in training exactly matches the task in test scenario. 

Generalised end-to-end (GE2E).
In GE2E training, every utterance in the batch except the query itself is used to form centroids. As a result, the centroid that is of the same class as the query is computed from one fewer utterance than centroids of other classes. They are defined as:

    𝐜_j   =   1/M∑_m=1^M𝐱_j,m
    𝐜_j^(-i)   =   1/M-1∑_m=1
    m≠ i^M𝐱_j,m

The similarity matrix is defined as scaled cosine similarity between the embeddings and all centroids:

    _j,i,k = 
    
    w·cos(𝐱_j,i, 𝐜_j^(-i))+b    if   k=j 
    
    w·cos(𝐱_j,i, 𝐜_k)+b    otherwise.

where w > 0 and b are learnable scale and bias. 

The final GE2E loss is defined as:

    L_G = -1/N∑_j,iloge^_j,i,j/∑_k=1^N e^_j,i,k


Angular Prototypical.
The angular prototypical loss uses the same batch formation as the original prototypical loss, reserving one utterance from every class as the query. This has advantages over GE2E-like formation since every centroid is made from the same number of utterances in the support set, therefore it is possible to exactly mimic the test scenario during training. 

We use a cosine-based similarity metric with learnable scale and bias, as in the GE2E loss.

    _j,k = 
    w·cos(𝐱_j,M, 𝐜_k)+b

Using the angular loss function introduces scale invariance, improving the robustness of objective against feature variance and demonstrating more stable convergence <cit.>.

The resultant objective is the same as the original prototypical loss, Equation <ref>.







§ EXPERIMENTS



In this section we describe the experimental setup, which is identical across all objectives described in Section <ref>. 




 §.§ Input representations



During training, we use a fixed length 2-second temporal segment, extracted randomly from each utterance.
Spectrograms are extracted with a hamming window of width 25ms and step 10ms. For the Thin ResNet model, the 257-dimensional raw spectrograms are used as the input to the network. For the VGG-M-40 and the Fast ResNet, 40-dimensional Mel filterbanks are used as the input.
Mean and variance normalisation (MVN) is performed by applying instance normalisation <cit.> to the network input.
Since the VoxCeleb dataset consists mostly of continuous speech, voice activity detection (VAD) is not used in training and testing.



 §.§ Trunk architecture



Experiments are performed on the trunk architectures described below. The first two are identical to the models used and described in <cit.>, while the last is a variation of the ResNet model to reduce computation requirement. The architectures are compared in Table <ref>.

VGG-M-40.
The VGG-M model has been proposed for image classification <cit.> and adapted for speaker recognition by <cit.>. The network is known for high efficiency and good classification performance. VGG-M-40 is a modification of the network proposed by <cit.> to take 40-dimensional filterbanks as inputs instead of the 513-dimensional spectrogram. The temporal average pooling (TAP) layer takes the mean of the features along the time domain in order to produce utterance-level representation.

Thin ResNet-34.
Residual networks <cit.> are widely used in image recognition and have recently been applied to speaker recognition <cit.>. Thin ResNet-34 is the same as the original ResNet with 34 layers, except using only one-quarter of the channels in each residual block in order to reduce computational cost. The model only has 1.4 million parameters compared to 22 million of the standard ResNet-34. Self-attentive pooling (SAP) <cit.> is used to aggregate frame-level features into utterance-level representation while paying attention to the frames that are more informative for utterance-level speaker recognition. Thin ResNets of <cit.> and <cit.> differ slightly in their implementation details, but in our experiments we use that of <cit.>.

Fast ResNet-34.
The number and size of filters are identical to the Thin ResNets of <cit.>, but the input dimensions are smaller than <cit.> and the strides are earlier than <cit.> in order to reduce computational requirements. Due to space constraints, the exact specification can be found in the accompanying code. The performance is on par with both Thin ResNet models, while the computation cost is less than half of those models.











 §.§ Implementation details



Datasets.
The network is trained on the development set of VoxCeleb2 <cit.> and evaluated on test set of VoxCeleb1 <cit.>.
Note that the development set of
VoxCeleb2 is completely disjoint from the VoxCeleb1
dataset (i.e. no speakers in common).

Training.
Our implementation is based on the PyTorch framework <cit.> and trained on the NAVER Smart Machine Learning (NSML) platform <cit.>.
The models are trained using a NVIDIA V100 GPU with 32GB memory for 500 epochs. 
For each epoch, we randomly sample a maximum of 100 utterances from each of the 5,994 identities to reduce class imbalance.
We use the Adam optimizer with an initial learning rate of 0.001 decreasing by 5% every 10 epochs. 
For metric learning objectives, we use the largest batch size that fits on a GPU. For classification objectives, we use a fixed batch size of 200.
The training takes approximately one day for the VGG-M-40 model, two days for the Fast ResNet model and five days for the Thin ResNet model.

All experiments were repeated independently three times in order to minimise the effect of random initialisation, and we report mean and standard deviation of the experiments. 

Data augmentation.
No data augmentation is performed during training, apart from the random sampling.

Curriculum learning.
The AAM-Softmax loss function demonstrates unstable convergence from random initialisation with larger values of m such as 0.3. Therefore, we start training the model with m=0.1 and increase it to m=0.3 after 100 epochs. This strategy is labelled Curriculum in Table <ref>. 

Similarly, the triplet loss can cause models to diverge if the triplets are too difficult early in the training. We only enable hard negative mining after 100 epochs, at which point the network only sees the most difficult 1% of the negatives. 











 §.§ Evaluation




Evaluation protocol.
The trained networks are evaluated on the VoxCeleb1 test set. We sample ten 4-second temporal crops at regular intervals from each test segment, and compute the similarities between all possible combinations
(10 × 10 = 100) from every pair of segments. The mean of the 100 similarities is used as the score. This protocol is in line with that used by <cit.>.


Results.
The results are given in Table <ref>. It can be seen that the performance of networks trained with AM-Softmax and AAM-Softmax loss functions can be very sensitive to the value of margin and scale set during training. We iterate over many combinations of m and s to find the optimal value. The model trained with the most common setting (AM-Softmax with m=0.3 and s=30) is outperformed by the vanilla triplet loss.

Generalised end-to-end and prototypical losses show improvements over the triplet loss by using multiple negatives in training. The prototypical networks perform best when the value of M matches the test scenario, removing the necessity for hyperparameter optimisation. The performance of the model trained with the proposed angular objective exceeds that of all classification-based and metric learning methods.

There are a substantial number of recent works on the VoxCeleb2 dataset, but we do not compare to these in the table, since the goal of this work is to compare the performance of different loss functions under identical conditions. However, we are unaware of any work that outperforms our method with a similar number of network parameters.

Batch size.
The effect of batch size on various loss functions is shown in Table <ref>. We observe that a bigger batch size has a positive effect on performance for metric learning methods, which can be explained by the ability to sample harder negatives within the batch. We make no such observation for the network trained with classification loss.










§ CONCLUSIONS


In this paper, we have presented a case for metric learning in speaker recognition. Our extensive experiments indicate that the GE2E and prototypical networks show superior performance to the popular classification-based methods. We also propose an angular variant of the prototypical networks that outperforms all existing training functions. Finally, we release a flexible PyTorch trainer for large-scale speaker recognition that can be used to facilitate further research in the field.



IEEEtran

