Paper commentary
CosFace: Large Margin Cosine Loss for Deep Face Recognition
Large cosine margins make softmax face features more discriminative
- Problem Softmax-trained face features lack the discriminative power face recognition needs. Earlier margin losses either need costly pair or triplet mining, or define the margin in angle space.
- Method CosFace trains with LMCL, which normalizes features and class weights so only cosine values matter. It then subtracts a fixed margin m from the target-class cosine inside a softmax.
- Results CosFace reaches 99.73 on LFW and 97.6 on YTF with a model trained on about 5M images. It also reaches 77.06 Rank 1 on MegaFace Challenge 2 with the 3-patch ensemble.
Why softmax features need a margin
Deep CNNs usually use a multilayer perceptron followed by a softmax loss for face recognition, yet recent studies found the plain softmax insufficient for discriminating faces. Softmax logits depend on both the norm of each weight vector and the angle between weight and feature, so the angle that face scoring uses is only one of two factors in training.
Testing compares features by cosine similarity, which means the training objective and the scoring rule do not match when norms still vary.
Many earlier methods share one goal: enlarge inter-class variance and shrink intra-class variance. Center loss reduces intra-class variance but ignores inter-class variance, and contrastive or triplet style losses need careful mining of pairs or triplets, which is very time-consuming. A-Softmax moves the problem to angular space and adds an angular margin, but the decision margin depends on the angle, and optimization needs an ad-hoc piecewise function.
The gap addressed here is a margin that is defined directly in cosine space, the quantity the matching step actually compares.
Large margin cosine loss in one formula
The softmax activation of class j is the inner product of its weight vector and the feature, which equals the product of the two norms and the cosine of the angle between them, with the bias fixed at zero. Fixing each weight norm to 1 and the feature norm to a scale s removes the radial terms, so the posterior depends only on the cosine of the angle, which gives the normalized softmax NSL.
LMCL keeps this normalized form and subtracts m from the target-class cosine, so the loss is \\( L_{lmc} = \\frac{1}{N}\\sum_i -\\log \\frac{e^{s(\\cos(\\theta_{y_i,i})-m)}}{e^{s(\\cos(\\theta_{y_i,i})-m)}+\\sum_{j\\neq y_i} e^{s\\cos(\\theta_{j,i})}} \\), with m at least zero and s fixed. For two classes, the requirement becomes cos(θ1) − m > cos(θ2) for C1 and the symmetric condition for C2, so the margin makes the decision stricter than the NSL condition.
The experiments fix s at 64 and the margin at 0.35 in the later runs, and the weight and feature normalization are applied during training.
At test time, features of the original and horizontally flipped image are concatenated, and the cosine distance between two such features gives the similarity score used for verification and identification.
Cosine margin versus angular margin
Without any margin, the NSL boundary cos(θ1) = cos(θ2) separates the classes but has no room for noise, since any small perturbation around the boundary can flip a decision. A-Softmax instead requires cos(mθ1) ≥ cos(θ2) for C1, so its margin is measured in angle and shrinks as θ decreases, vanishing at θ = 0. That angle dependence means classes with a small angle between their weights get a smaller margin, and the non-monotonic cosine forces a piecewise workaround.
LMCL requires cos(θ1) ≥ cos(θ2) + m for C1, which applies the same cosine margin to every class regardless of the angles between weight vectors. The authors describe the resulting decision margin as a clear gap of √2 m in the distribution of cosine values, which they argue makes LMCL more robust than NSL.
Why fix the feature norm in advance?
Plain softmax learns the feature norm together with the angle, and the norm of easy samples grows much larger than that of hard samples, which weakens the cosine constraint. This approach instead requires every feature to share the same norm, so that learning depends only on cosine values and features of one class cluster on the hypersphere.
Without feature normalization, the LMCL constraint becomes an inequality on ‖x‖ times (cos θi − m) against ‖x‖ times cos θj, so when cos θi − m is smaller than cos θj early in training the norm must shrink, which slows optimization. Feature normalization is therefore presented as critical under LMCL, especially when networks are trained from scratch, and fixing the scale is preferred to learning it.
The scale s should be large enough: NSL loss keeps decreasing as s grows, and too small an s leaves training insufficiently converged or not converging at all. Under the assumption that normalized features lie on the hypersphere around their class weights, the authors derive the lower bound s ≥ \\( \\frac{C-1}{C}\\log\\frac{(C-1)P_W}{1-P_W} \\) , where P_W is the expected minimum posterior of a class center and C the number of classes.
Bounds on the margin m
The admissible margin is derived from a uniform spread of the weight vectors on the unit hypersphere, where the margin is bounded by the gap between class weights. For K = 2 the bound is 0 ≤ m ≤ 1 − cos(2π/C), where C is the number of training classes and K the feature dimension. For C at most K + 1 the bound is 0 ≤ m ≤ C/(C − 1), and for C larger than K + 1 the upper bound is strict and smaller, written as m much less than C/(C − 1).
The admissible upper bound therefore drops as the number of classes grows relative to the feature dimension. Reaching the bound in practice is not possible, because feature vectors gather around their class centers and the feature space collapses, and very large m makes the cosine constraint hard to satisfy and more sensitive to noisy data.
Data, architecture and training settings
MTCNN detects the faces and five facial points, which are aligned by a similarity transform and resized to 112 × 96 with pixels normalized by subtracting 127.5 and dividing by 128. The small training set is CASIA-WebFace, with 0.49M images from 10,575 subjects, while the large set mixes several public datasets and a private dataset of about 5M images from more than 90K identities.
Training faces are horizontally flipped for augmentation, and identities that appear in the test datasets are removed from training.
The network has 64 convolutional layers with residual units, matching the architecture of the SphereFace comparison, and the scale s is set to 64 in Equation (4). Models are trained with SGD and a batch size of 64 on 8 GPUs with weight decay 0.0005; on the small set the learning rate starts at 0.1 and is divided by 10 at 16K, 24K and 28K iterations, ending at 30K, while the large-set run starts at 0.05 with drops at 80K, 140K and 200K and ends at 240K.
Effect of the cosine margin
The margin is swept from 0 to 0.45 on CASIA-WebFace models evaluated on LFW and YTF, and the m = 0 model gives the lowest accuracy on both. Accuracy improves consistently as m rises and levels off at m = 0.35, which the authors take as the point where the margin's benefit saturates. Beyond 0.45 the model fails to converge in this setting, and 0.35 is then fixed for the subsequent experiments.
Effect of feature normalization
CosFace models trained on CASIA-WebFace with and without feature normalization are compared, with m fixed at 0.35, on LFW, YTF and MF1. The model without normalization is initialized from softmax and then supervised by LMCL. Without normalization the scores are 99.10 on LFW, 93.1 on YTF, 75.10 Rank 1 and 88.65 MF1 verification, and with normalization they are 99.33, 96.1, 77.11 and 89.88, so the normalized model is higher on every reported column.
The authors attribute the gain to feature normalization removing radial variance, which makes the features more discriminative in angular space.
Comparison with other loss functions
The comparison follows the SphereFace setup, with the 64-layer ResNet-like CNN and CASIA-WebFace training, so the losses differ only in their objective. Table 2 lists softmax, contrastive, triplet, L-Softmax, center loss, A-Softmax, A-Softmax with normalized features and LMCL. LMCL reaches 99.33 on LFW, 96.1 on YTF, 77.11 Rank 1 and 89.88 MF1 verification, while A-Softmax-NormFea reaches 99.32, 95.4, 75.42 and 88.82.
The authors describe LMCL as competitive across the three datasets, with clear gains over the other losses on YTF and MF1, and the table gives one value per method.
Benchmarks on LFW and YTF
LFW is evaluated under the unrestricted protocol with labeled outside data on 6,000 pair testing images, and YTF contains 3,425 videos of 1,595 people, evaluated on 5,000 video pairs. The CosFace model trained on 5M images reaches 99.73 on LFW and 97.6 on YTF, against 99.63 and 95.1 for FaceNet trained on 200M images and 99.47 and 93.2 for DeepID2+ with 25 models.
The authors note that FaceNet is the runner-up on LFW and that CosFace is first on YTF among the listed methods.
MegaFace identification and verification
MegaFace uses a gallery of more than one million images from 690K individuals and a Facescrub probe set of 106,863 images of 530 celebrities. On MF1 the small protocol uses training sets under 0.5 million images, and the CosFace single-patch model reaches 77.11 Rank 1 and 89.88 verification, with the 3-patch ensemble at 79.54 and 92.22.
Challenge 2 uses MegaFace-provided training data of 4.7 million faces and 672K identities, with a gallery of one million images. The CosFace 3-patch ensemble reaches 77.06 Rank 1 and 90.30 verification, against 74.11 and 86.77 for the single-patch model and 75.77 and 74.84 for GRCCV. The text reports margins of 1.39% on rank-1 identification and 5.46% on verification for the Challenge 2 result.
Limitations
Training did not converge for margins above 0.45 in the CASIA-WebFace setting, and the admissible margin under the theory was not reached in practice. The margin and normalization sweeps, and the comparison with other losses, use CASIA-WebFace and the 64-layer architecture only. The large-data benchmarks use a training set that combines public and private data, so those numbers cannot be reproduced from public data alone.
On the MF1 large protocol, the table lists Vocord deepVo V3 above CosFace in rank-1 accuracy, which does not match the text's claim of superiority on both protocols. The bound on m assumes uniform weight distribution and well-separated features, and its proofs are given in the supplementary material.
Paper info
- Authors
- Hao Wang, Yitong Wang, Zheng Zhou, Xing Ji, Dihong Gong, Jingchao Zhou, Zhifeng Li, Wei Liu
- Venue
- arXiv · Preprint
- First published
- 2018-1-29
- Added
- 2026-11-12
- Updated
- 2018-4-3
- Version
- v2
- arXiv
- 1801.09414
- Category
- cs.CV
- Original paper terms
- arXiv non-exclusive 1.0
- What we show
- Paper info and commentary
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Quote from the paper
Hao Wang, Yitong Wang, Zheng Zhou, Xing Ji, Dihong Gong, Jingchao Zhou, Zhifeng Li, Wei Liu (2018). CosFace: Large Margin Cosine Loss for Deep Face Recognition. arXiv:1801.09414v2. https://arxiv.org/abs/1801.09414v2
BibTeX
@misc{wang2018cosface,
title = {{CosFace: Large Margin Cosine Loss for Deep Face Recognition}},
author = {Hao Wang and Yitong Wang and Zheng Zhou and Xing Ji and Dihong Gong and Jingchao Zhou and Zhifeng Li and Wei Liu},
year = {2018},
eprint = {1801.09414},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1801.09414v2}
}