NormFace: L2 Hypersphere Embedding for Face Verification
- Publication
- ACM Multimedia 2017Conference
- First submitted to arXiv
- arXiv:1704.06369
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{facerecognitionnormface2017,
title = {{NormFace: L2 Hypersphere Embedding for Face Verification}},
author = {Feng Wang and Xiang Xiang and Jian Cheng and Alan L. Yuille},
year = {2017},
eprint = {1704.06369},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1704.06369}
}
In 2016 Wen et al. released a face model trained with softmax plus center loss. Use it for face verification on LFW, taking the inner product of two faces' features directly as the similarity, and accuracy is 98.27%; first divide each feature by its own length, then take the inner product, and it becomes 98.98%.
| Similarity | Before | After |
|---|---|---|
| Inner product | 98.27 | 98.98 |
| Euclidean distance | 98.35 | 98.95 |
Compiled from Table 1 of the paper (the model released by Wen et al., LFW, %).
Same model, same features, one extra division before comparing, and the error rate drops from 1.73% to 1.02%. LFW's standard protocol has 6,000 photo pairs, half the same person and half different people, split into 10 folds that take turns: 9 to pick the threshold, the remaining 1 to test. In terms of pairs, that is forty-odd fewer mistakes out of 6,000.
This task of "given two faces, are they the same person?" is called face verification, or 1:1 matching. At the time almost every method compared this way at test time: L2-normalize the features first, then compute the cosine similarity. Yet nobody did this during training; the softmax classification layer always computed unnormalized inner products.
The authors of this paper are Feng Wang of the University of Electronic Science and Technology of China (then visiting Alan L. Yuille's lab at Johns Hopkins University), Xiang Xiang of Johns Hopkins University, Jian Cheng of the University of Electronic Science and Technology of China, and Alan L. Yuille. They wanted to move the test-time normalization into training; since normalization is itself differentiable, it looked like a matter of adding one more layer. But once both the features and the last layer's weights were normalized, the network would not train.
The paper traces back from there and answers four questions. Why is normalization at test time so useful? Why does optimizing the cosine directly with softmax fail to converge? What has to change to make it converge? And besides softmax, are there other losses suited to normalized features?
Defining the Problem
Train on inner products, test on angles
Start with how classification-based face recognition trains. The backbone turns an aligned face into a feature vector , say 512-dimensional; during training it is followed by a fully connected layer whose weight matrix has columns, one per identity. The -th column can be seen as the class center of the -th identity; its inner product with the feature gives a logit, which softmax turns into a probability, the paper's Eq. (1):
is the number of samples, is the identity of the -th face, and is the bias. After training the fully connected layer is discarded and only the features are kept. None of the test identities appeared in the training set, so the classification layer is no help; all that is left is comparing how alike two features are.
The inner product can be split into three factors:
is the angle between the feature and the class center. The cosine similarity used at test time keeps only the last factor. Divide both vectors by their own lengths, call them and , and the squared Euclidean distance can be written directly in terms of the cosine:
That is why, in the opening table, the normalized inner product and Euclidean distance score about the same: they are two sets of markings on the same ruler. During training, though, softmax looks at the product of all three factors.
Softmax likes to stretch features
The authors first ran a toy experiment: reduce the feature dimension of a LeNet on MNIST to 2 and plot the features of ten thousand training samples directly on the plane. Each of the ten digits grows outward from the origin as a spoke, like the spokes of a bicycle wheel. In such a distribution, two points on the same spoke, one near the origin and one far out, can be farther apart in Euclidean distance than points on two neighboring spokes; the angle, meanwhile, keeps them cleanly apart.
Why spokes? The authors state it as Proposition 1. Remove the bias and let be the probability that is assigned to class . If class already has the highest score, scaling the feature up by can only make larger, never smaller.
The proof takes one step. Let and divide numerator and denominator by :
Class has the highest score, so the difference in parentheses ; every exponent is smaller than before, the denominator shrinks, and .

The top plot works it out with three classes. With the feature at 20° and length 1, the correct-class probability is 0.66; keep the direction and make the length 2, 3 and 5, and the probability becomes 0.88, 0.96 and 0.996. The loss falls accordingly from 0.41 to 0.004. The easiest way for the network to lower the loss is to stretch features that are already classified correctly, so well-classified samples keep running outward and grow into spokes.
The bottom plot shows the trouble this distribution causes. and both belong to class one, but their lengths differ by a factor of 4; belongs to class two and has the same length as . By Euclidean distance, is only 0.77 from but 3.08 from ; by angle, is 20° from and 45° from . From start to finish softmax only cares about the class decided by direction; length is a by-product it inflates along the way. Divide out the length at test time, and what you compare is the part softmax actually learned. That answers the first question.
Biases grab the territory near the origin
Proposition 1 has one precondition, that there is no bias. With a bias, two classes can have identical weight directions and be told apart by the bias alone. The authors actually saw this on MNIST: one or two classes had all their points squeezed near the origin. Close to the origin, every class's inner product is small, and whichever bias is larger wins.
Once these points are normalized, they get projected onto every direction of the unit circle and mix with other classes. So this paper adds no bias before softmax. The later SphereFace, CosFace and ArcFace all set the bias to 0 as well.
After normalization, the loss gets stuck
Now normalize both the features and every column of the weights, and the logit is just , the paper's Eq. (5). The trouble is the range, since can only fall within . The authors note that with an ordinary inner-product layer plus softmax, logits usually range between and .
Work out the most ideal case. The correct class has cosine 1, and every other class sits on the far side of the hypersphere with cosine :
With 10 classes this is 0.45; with 1,000 classes only 0.007 is left.
Here, however well the classes are separated, the correct-class probability cannot rise. The softmax gradient on the correct-class logit is , so perfectly classified samples still get gradients close to 1, about the same as hard samples, and hard samples get no relative extra attention. The authors actually trained on CASIA-WebFace (10,575 identities): the loss fell from about 9.27 to about 8.50 and then would not budge, however long they trained and however small they made the learning rate.
So the question becomes how low the loss can go in this setting.
Solving the Problem
Proposition 2: a lower bound on the loss
The authors' answer is Proposition 2, and we will derive it step by step.
First assume the best case. Every class has the same number of samples, and every sample's feature is exactly equal to its class weight. In addition, normalize both features and weights to the same length instead of 1. Each class then only needs to look at its own weight , and the loss is:
. Divide numerator and denominator by ; the numerator becomes 1, and the term in the denominator also becomes 1:
Next, apply Jensen's inequality twice. The first time is on . is convex, so the average of the terms is at least raised to "the mean of the "; hence the sum in parentheses is at least , where is the mean of over all . The second time is on , which is also convex in , so when averaging over the average can likewise be moved inside. Together:
is the average inner product between any two different class centers, minus . The remaining question is how low the average pairwise inner product of these class centers can go.
Here a simple fact helps. The vector obtained by adding up all the class centers cannot have a negative squared length:
So the pairwise inner products sum to at least , and their mean is at least . Try three classes on a plane: three unit vectors 120° apart have pairwise cosines of , exactly . With more classes this lower limit approaches 0; with ten thousand classes it is about . On average, the other classes can at best be perpendicular to you.
Substituting back, , which gives Proposition 2:
For equality to hold, all pairwise inner products of the class centers must be equal and the centers must sum to the zero vector, that is, a regular simplex (the family of the equilateral triangle and the regular tetrahedron). A -dimensional space can hold at most such points, and the number of face classes far exceeds the feature dimension, so in practice this bound is never reached; the loss always stays above it.
Plug in CASIA-WebFace's numbers. With and , , and the bound is . Compare that with the starting point: having learned nothing, with every class equally probable, the loss is . In other words, even if the model classified every face perfectly, the loss could only fall from 9.27 to 8.27; the measured loss stalled at 8.50, just 0.23 above the bound, from which the authors conclude the bound is quite tight.
The fix: make the sphere bigger
The only thing in the bound that can move is . Normalize features and weights to a larger length and the logit becomes , with its range scaled up accordingly. In practice there is no need to actually change the lengths on both sides: put a scale layer with a single parameter after the cosine layer, let , and you get the paper's Eq. (6):
The tilde marks normalized vectors. How large does need to be? Plot Proposition 2 against .

The four curves look much alike. At the bound is , the loss when every class is equally probable; the more classes, the higher the starting point and the later the curve reaches 0. On CASIA-WebFace's orange curve, the bound is down to 0.39 at and just 0.001 at ; with MS1MV2's 85,742 classes, it is still 1.59 at .
You can also ask it the other way around. If you want a perfectly classified sample to reach a correct-class probability of at least , the bound must not exceed , which solves to:
For , CASIA-WebFace needs 11.5 and MS1MV2 needs 13.6; for , 13.9 and 16.0 respectively. Eight times as many classes need only about 2 more in , because the number of classes sits inside a logarithm.
The paper suggests that a fixed value be 20 or 30 depending on the number of classes; the authors themselves prefer to let be learned through backpropagation, because they do not want an extra hyperparameter. The official scaled_cosine_softmax.prototxt is set up exactly this way: the scale layer starts at 10, is learnable and has a lower limit of 0.01; the inner-product layer before it has no bias; and there are 10,572 classes (CASIA-WebFace minus the 3 identities that overlap with LFW).
The gradient of the normalization layer
The paper also derives how the normalization layer itself backpropagates. Let , where can be a feature or a column of the weights (in practice a tiny is also added to the denominator to avoid dividing by zero). The partial derivative of with respect to has two parts: the numerator contributes only when , while the length in the denominator depends on every component:
is 1 when and 0 otherwise. Sum over all with the chain rule and tidy it into vector form, and you get the paper's Eq. (4):
The parentheses subtract the component of the upstream gradient along , keeping only the part perpendicular to , which is then divided by the length.

Walk through it with numbers. , and suppose the upstream gradient is . ; subtract the radial component and is left, whose inner product with is , so it is indeed perpendicular, with length 0.8. This has two consequences.
First, the gradient always lies along the tangent, so a step along it always carries the point off the circle and outward. A point of length 1 that takes one step along the tangent, say with learning rate 0.75, moves 0.6, and its new length is ; that is just the Pythagorean theorem. As the weights keep getting updated, their lengths keep growing, which is why the authors say normalized vectors need weight decay to hold their lengths in check. The official code uses another trick, rewriting the weights to unit length directly in the forward pass (Feng Wang's explanation in issue #4) and setting that layer's weight decay to 0.
Second, the gradient is divided by the length. For the same upstream gradient, a feature of length 0.5 gets 1.6 and a feature of length 2 gets only 0.4, a factor of 4 apart. Feng Wang later filled in the other side of this in the AM-Softmax paper. Low-quality faces usually have smaller feature norms, so after normalization they receive larger gradients, a bit like hard example mining; when the norm is very small, the gradient can also become large enough to be unstable.
Another loss: give each class an agent
The fourth question is whether, besides softmax, the losses of the metric learning school can take normalized features directly.
The two most common metric learning losses are contrastive and triplet. Contrastive takes two faces and pulls them together if they are the same person, or pushes them beyond a margin if not (Eq. (7)); triplet takes three faces and requires the anchor's distance to the same person plus to still be smaller than its distance to the other person (Eq. (8)). Both are already commonly used on normalized features; the trouble is sampling. With faces in the training set there are pairs and triplets; CASIA-WebFace has about 490,000 images, so the pairs alone exceed a hundred billion, and the only way through is sampling and hard example mining, which is slow and hard to tune. A classification loss only needs to look at each of the faces once per epoch.
The authors first rewrite the normalized softmax. Substitute into Eq. (6); every exponent picks up a common , which cancels between numerator and denominator, leaving distances (Eq. (9)):
Normalized softmax is in fact also comparing the Euclidean distance between the feature and each class center. Following this idea, replace "the other face" in contrastive and triplet with the weight of the class that face belongs to; the authors call it the class's agent, and it is learned by backpropagation just like the weights of a fully connected layer. This gives C-contrastive, Eq. (10):
and C-triplet, Eq. (11):
is the identity of the -th face; in C-triplet, is its own class and another class. Each face is compared only against the agents, with no pairs to assemble.
Where do the agents settle? With , C-contrastive keeps only the term that pulls features toward their own agent, and the agent ends up near the mean of its class's features, which is exactly what center loss does (in the official repo, the authors reproduce center loss precisely by setting the margin to 0). With , features of other classes inside the margin also push the agent, so agents at the border between two classes get pushed apart; and hard samples stuck at the border receive larger gradients.
The cost is an approximation. What used to be the distance between two real samples now has an agent on one side, and some features near the margin that the original loss would handle end up outside the margin once the agent stands in, and are no longer optimized. Proposition 3 bounds this distortion with the triangle inequality. Using the agent in place of every sample in the class, the average squared error in distance is no more than the average squared distance from the class's features to the agent. This quantity can be computed on the fly during training with a moving average; the authors observed it to be about 0.5 to 0.6, and so recommend a somewhat larger margin: 1 for C-contrastive and 0.8 for C-triplet.
These distances are all squared distances after normalization, ranging from 0 to 4. In terms of angles it is easier to picture: a squared distance of 1 corresponds to a cosine of 0.5, that is, 60°. C-contrastive's means pushing every other class's agent beyond 60°. C-triplet's is relative instead: if a face is 30° from its own agent, a squared distance of , then the agents of other classes must be beyond a squared distance of 1.07, about 62°.
The old practice was to stop every few epochs and search for the margin again, because the feature lengths kept changing. Normalization pins the length down, so the margin can stay fixed too.
Discussion
Experimental setup
The authors picked two public models as baselines and fine-tuned them with the new losses rather than training from scratch. The two baselines are:
- Wu et al.'s Light CNN: a plain 10-layer CNN with Maxout activations;
- Wen et al.'s ResNet: 28 layers, trained with softmax plus center loss, which is the model from the opening.
Neither model uses feature or weight normalization, and both are trained only on CASIA-WebFace. The loss is attached after the penultimate layer's features, with features and weights first normalized to 1. Wu's model uses a learning rate of 0.0001 and Wen's 0.001, with SGD, momentum 0.9 and batch size 256. After convergence they train another 5,000 iterations, saving every 1,000, and evaluate each of the five checkpoints and average.
At evaluation, features are extracted once from the original image and once from its horizontal flip and added element-wise; PCA is then fitted on the training folds of the evaluation set, and finally the cosine after PCA is used as the similarity. LFW has 13,233 images of 5,749 people; besides the standard 6,000-pair protocol, BLUFR is also used, covered below. CASIA-WebFace shares identities with LFW; the paper says it removed 3, while the official README later updated this, saying 17 actually overlap.
Normalization is the real star
First, LFW's 6,000 pairs with Wen et al.'s model:
| Loss | Normalized | LFW |
|---|---|---|
| softmax | None | 98.28 |
| softmax + center | None | 99.03 |
| softmax | Feature only | 98.72 |
| softmax | Weight only | 98.95 |
| softmax | Both | 99.16 |
| C-contrastive | Both | 99.15 |
| C-triplet | Both | 99.11 |
| softmax + C-contrastive | Both | 99.19 |
Compiled from Table 2 of the paper (%); the second row is the baseline model itself.
With both features and weights normalized, the four losses land between 99.11 and 99.19, only 0.08 percentage points apart; the authors conclude that the choice of loss matters little and normalization is the key. Compared with the baseline, 99.03 to 99.19, the error rate falls from 0.97% to 0.81%, about 10 fewer mistakes out of 6,000 pairs. By this time LFW was already squeezed above 99%, and that is all the room there was.
The two experiments that normalize only one side are more intriguing. Normalizing only the weights gives 98.95, about the same as the baseline; normalizing only the features gives 98.72, actually worse than the baseline. The authors also found that normalizing only the features requires a scale layer, or the network collapses, while normalizing only the weights does not; the paper says flatly that they cannot explain this for now. Training from scratch is different again: normalizing only the weights collapses, while normalizing only the features reaches 98.45%, better than plain softmax but far short of the best losses of the time.
How the two losses are weighted when mixed also matters. For softmax plus C-contrastive, with the C-contrastive weight varied from 0.001 to 100, accuracy stays between 99.17% and 99.19%; for softmax plus center loss, accuracy drops to about 99.0% once the weight reaches 1, below the baseline. The authors' explanation is that C-contrastive can train a model on its own, while center loss only shrinks intra-class distance and has to be propped up by another loss.
Switching to Wu et al.'s Light CNN, the baseline with flip fusion is 98.41%, normalized softmax is 98.75% and C-contrastive is 98.78%, a gain of 0.37 percentage points.
Lower the threshold, widen the gap
LFW's 6,000 pairs are too few to measure performance at low false-accept rates. The BLUFR protocol uses all 13,233 LFW images over 10 trials and reports two numbers:
- TPR@FAR=0.1%: 1:1 verification; with the threshold set so that only one in every thousand different-person pairs is falsely accepted, the share of same-person pairs that pass;
- DIR@FAR=1%: open-set identification, that is, 1:N. A face is compared against a database, and the closest match must be the right person and also score above the threshold; the threshold is set so that only 1% of strangers not in the database are misidentified.
| Model | Loss | 0.1% | DIR |
|---|---|---|---|
| ResNet | Baseline | 93.35 | 67.86 |
| ResNet | softmax | 95.77 | 73.92 |
| ResNet | softmax + C-contrastive | 95.83 | 77.18 |
| Light CNN | Baseline | 89.12 | 61.79 |
| Light CNN | softmax | 90.64 | 65.22 |
| Light CNN | C-contrastive | 90.32 | 68.14 |
Compiled from Table 3 of the paper (%); "Baseline" is the original model without normalization, and all others are normalized; the "0.1%" column is TPR@FAR=0.1% and the "DIR" column is DIR@FAR=1%.
Two models only 0.16 percentage points apart on the 6,000 pairs differ by 9.3 percentage points on the 1:N DIR. The agent loss also shows its use here: with normalization in both cases, DIR goes further from 73.92% to 77.18%, and Light CNN from 65.22% to 68.14%; on LFW's standard protocol these differences are nearly invisible.
Video: beyond the average
YouTube Faces (YTF) has 1,595 people and 3,425 video clips, and tests whether 5,000 pairs of clips show the same person. The usual approach computes the cosine between every frame of one clip and every frame of the other, giving a similarity matrix, and takes the mean as the pair's score.
The authors instead take a 100-bin histogram of the whole matrix as the pair's feature and hand it to an SVM with a histogram intersection kernel to decide whether it is the same person; the table calls this HIK-SVM. The histogram keeps "how many frame pairs are very alike and how many are not alike at all," while the mean leaves just one number.
| Loss | Normalized | YTF |
|---|---|---|
| softmax + center | No | 93.74 |
| softmax | Yes | 94.24 |
| softmax + HIK-SVM | Yes | 94.56 |
| softmax + C-contrastive | Yes | 94.34 |
| softmax + C-contrastive + HIK-SVM | Yes | 94.72 |
Compiled from Table 5 of the paper (Wen et al.'s model, %).
Normalization brings 0.5 percentage points, and the histogram adds another 0.3 to 0.4.
All of it is fine-tuned
In the tables above, every number with normalization comes from fine-tuning an existing model (climbing a little higher from someone else's summit). The paper's own conclusion admits that for now this is the only way to use it: trained from scratch with C-contrastive, the results are as good as center loss, and only fine-tuning goes higher.
The official repo's issues are more specific. In issue #1 (April 2017), Feng Wang replied that training the scaled softmax from scratch "is similar with softmax," and that training C-contrastive from scratch can reach 98.9%, close to center loss but not beyond it. In issue #14 (July 2017), a user trained a feature-only-normalized version with a fixed scale from scratch and got around 99%; Feng Wang said that C-contrastive from scratch could also reach 99% for them, but that they "fail to get good results using normalized softmax loss." Step 4 of the README's training instructions says it outright too: fine-tune from the original model with a small learning rate.
When someone asked in issue #22 why there was no comparison with triplet, the author's answer was that they could not get reasonable triplet results themselves, nor find code that could, and pointed the asker to the triplet numbers in the SphereFace paper.
The author looking back
In September 2017, someone asked in issue #21 whether NormFace could be combined with SphereFace. Feng Wang replied the same day that simply merging the two only makes things worse and that the right way to do it was being written up as a paper; in November they said that route had failed: the new method only helped on BLUFR, was about the same as SphereFace on MegaFace, and was not good enough for a top conference. The next day, Feng Wang said something very candid about NormFace itself. The gist was that NormFace actually has no theoretical basis; it rests only on "a methodology that we should make training and testing consistent," and whether features should be normalized at all is still doubtful.
The author also offered their later understanding. Feature normalization lets us control the temperature of softmax directly, which is the scale in the paper; ordinary softmax cannot, because the temperature gets absorbed into the preceding layers. Put another way, without normalization the network can tune the temperature itself by stretching the features, and Proposition 1 shows it has a motive to do so; normalization takes this knob out of the network's hands and gives it to .
The same reply also listed the drawbacks. Normalized features can only move along the surface of the sphere and cannot pass through the inside; it is also unstable near the origin, where a small disturbance can carry a feature across the origin and, after normalization, flip it to the other side of the sphere. This is the same thing as the gradient being divided by the length earlier.
How the scale was settled later
Around the same time as NormFace, several other papers were working on the same knob.
L2-softmax (Ranjan, Castillo, Chellappa) appeared on arXiv a little over three weeks before NormFace. It normalizes only the features, to length , leaving the weights alone, and also derives a lower bound , assuming the class centers are pairwise at least 90° apart. Its experimental conclusion is the opposite of NormFace's preference: when the network learns itself, the learned value comes out large and the constraint loose, and fixing it to a smaller constant works better. On 13,403 identities, results stabilize once exceeds 12, close to the 11.7 computed for ; on the final 58,000-person dataset they use .
AM-Softmax is Feng Wang's own next paper, posted to arXiv in January 2018, which subtracts a margin from the cosine. The paper says NormFace lets be learned through backpropagation, but once the margin is added, does not grow and the network converges very slowly, so they fix at 30. The same paper's comparison table includes NormFace too (fine-tuned from the center loss model), with a rank-1 of 65.03% on MegaFace with a million distractors, against 72.47% for AM-Softmax (with different backbones).
CosFace came 12 days later and also uses a subtractive margin. Its Eq. (6) gives a lower bound on , ; the derivation in its supplementary material likewise relies on Jensen's inequality and "the squared length of the sum of class centers is non-negative," and the result is exactly the formula solved above from "pushing the bound below "; in its experiments, though, is set empirically to 64. ArcFace's first version also uses 64, citing L2-softmax's lower bound and Tencent's FaceCNN as the basis.
Conclusion
NormFace explains test-time normalization as a side effect of softmax. Without a bias, well-classified features get stretched further and further, and dividing out the length at matching time leaves just the direction to compare. Direct normalization fails to train because the logits are locked inside , and Proposition 2 computes that on CASIA-WebFace the loss can go no lower than 8.27; adding a scale unlocks it. The agents, meanwhile, free contrastive and triplet from assembling pairs. The experiments are all fine-tuning on two CASIA-WebFace models, with LFW gains of at most 0.16 and 0.37 percentage points and BLUFR's 1:N metric improving by up to 9.3 percentage points.
When training your own cosine classification head, first remove the bias from the last layer, then estimate from the number of classes. with gives about 11.5 for ten thousand classes and about 13.6 for eighty-odd thousand, so the common 64 leaves plenty of headroom; if you add a margin, follow AM-Softmax's experience and fix rather than letting it learn. The same applies when matching with someone else's model: normalize the features before computing the similarity.
CosFace, ArcFace and CurricularFace all later wrote as a flat 64; not one of them let it learn on its own.