Distract Your Attention: Multi-head Cross Attention Network for Facial Expression Recognition
- Publication
- Biomimetics 2023Journal
- First submitted to arXiv
- arXiv:2109.07270
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{faceanalysisdan2021,
title = {{Distract Your Attention: Multi-head Cross Attention Network for Facial Expression Recognition}},
author = {Zhengyao Wen and Wenzhong Lin and Tao Wang and Ge Xu},
year = {2021},
eprint = {2109.07270},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2109.07270}
}
Surprise and fear look a lot alike. Both have wide-open eyes and an open mouth, and the difference is in the details. In surprise the whole eyebrow lifts and the mouth falls open with the jaw; in fear the eyebrows lift while also drawing together, and the corners of the mouth stretch sideways. To tell them apart, you have to look at the eyebrows and the mouth at the same time.
A model that looks at only one place easily gets this wrong. The DAN authors drew heat maps for an expression model with a single attention head, and what lights up is usually a large patch around the mouth or nose; with the authors' four heads, each of the four maps lights up a different spot, and the eyes, the bridge of the nose, the mouth and the chin all have a head looking at them.
DAN (Distract your Attention Network) comes from Zhengyao Wen, Wenzhong Lin, Tao Wang and Ge Xu of Minjiang University in Fuzhou. The "distraction" in the name is meant literally. The authors require in the loss that the four attention heads differ from one another, forcing them to spread out to different places on the face, and then combine the four heads' results to classify. One more loss term pushes the features of different expressions farther apart.
The paper reports 89.70% on RAF-DB, 62.09% on AffectNet-8 and 65.69% on AffectNet-7, each the top of its table at the time. The arXiv version went through six revisions from September 2021 to May 2023, the last in sync with the journal version, and a few numbers changed along the way, which comes up later.
Defining the Problem
The exams and the scores
Facial expression recognition takes a cropped face and picks one of seven classes (surprise, fear, disgust, happiness, sadness, anger, neutral) or eight (adding contempt). DAN sits three exams.
- RAF-DB: faces collected from the web and annotated by multiple voters, aligned to . The basic-expression subset has 12,271 training and 3,068 test images, reporting overall accuracy. The test set is very uneven, with 1,185 happiness images (38.6%) and only 74 for fear.
- AffectNet: about 280,000 hand-annotated training images out of a million web pictures. The seven-class version is called AffectNet-7, and adding contempt gives AffectNet-8. Testing uses the official validation set, with 500 images per class, so overall accuracy equals the mean per-class accuracy.
- SFEW 2.0: still frames taken from AFEW, a database of movie clips; seven classes, 958 training, 436 validation and 372 test images, a very small set.
Classes too much alike
In general image classification, cats and trucks have wildly different features. Expression classes all share the same face, differing only in a few muscles, so their features naturally crowd together.
Face recognition ran into a similar problem long ago, and the common fix is center loss. It keeps a center for each class and pulls each image's feature toward the center of its own class, packing each class tightly. Expression recognition has plenty of variants too, such as Island loss, DDA loss, and DACL's attention version. The authors point out two drawbacks. These variants mostly need extra parameters or computation; and center loss itself only packs each class tightly, without requiring different class centers to stay apart.
One attention head isn't enough
The second observation is that an expression shows up in several places on the face at once. A single attention module produces only one weight map, which usually lands on one fairly large region.

These are Grad-CAM++ heat maps on the RAF-DB test set, marking which locations in the image the model relies on most when it decides. On the left is DACL, one head, lighting up a large area around the mouth or nose. The four columns on the right are DAN's four heads; on the same face, each lights up its own spot.
Compare row by row and you'll notice that the same head doesn't look at a fixed place across faces. The first head looks at the eye on the right side of the image in the first row, the mouth in the second, and between the eyes in the third. DAN never specifies which head is in charge of which part; it only requires that the four heads differ from one another on the same face.
So the problem becomes two things. How do you push the class centers apart without adding hyperparameters? And with no part annotations at all, how do you get several attention heads to spread out on their own?
Solving the Problem
The big picture

DAN has three stages.
- FCN (Feature Clustering Network): a ResNet-18 backbone plus an affinity loss that pulls same-class features toward the class center while pushing the class centers apart.
- MAN (Multi-head Attention Network): several parallel attention heads, each a spatial attention (SA) followed by a channel attention (CA).
- AFN (Attention Fusion Network): scales each head's output with log-softmax, requires the heads to differ through a partition loss, sums them, and classifies with a fully connected layer and BN.
The figure doesn't show tensor sizes. Take one RAF-DB face and follow it through the official code networks/dan.py.

networks/dan.py and equations (1)–(6) of the paper.The input is resized to . ResNet-18 with its last pooling and classifier layers removed outputs of ; each cell of the 7×7 grid corresponds to roughly a 32×32-pixel patch of the original image, so the eyes, nose and mouth take up a cell or two each. The backbone is first pretrained on the MS-Celeb-1M face recognition data, the same weights used in papers such as SCN and DACL.
From here splits into two paths. One goes through global average pooling (GAP) to 512 dimensions for the affinity loss; the other goes into the four attention heads at once.
FCN: pushing the class centers apart
The affinity loss is equation (2) of the paper:
is the 512-dimensional feature of image after GAP, is the center of its class, and is the batch size. The numerator is center loss, the squared distance from each image to its own class center. The denominator measures how spread out the class centers are from one another.
In the official code, the denominator is computed by arranging the class centers into a matrix, taking the variance of those numbers in each dimension, and summing over the 512 dimensions. The centers are learnable parameters, updated by the same optimizer along with the network.
A smaller numerator packs each class tighter, and a larger denominator spreads the class centers out; both are written into the same fraction. For example, if the mean squared distance from each image to its own center stays the same and the seven centers spread out twice as far, the variance in each dimension quadruples and the loss drops to a quarter. The authors stress that this denominator is computed directly from the centers during training, with no extra hyperparameter such as a margin to tune.
The authors also projected RAF-DB features trained with the two losses into two dimensions with t-SNE. In the center loss plot several classes' boundaries overlap; in the affinity loss plot the seven clusters are more separated. t-SNE distorts distances, so this serves only as a qualitative reference.
MAN: what happens inside a head
Each attention head first applies spatial attention, equation (3) of the paper:
produces a spatial weight map, and is elementwise multiplication. Then channel attention, equation (4):
, with by default.

Walk through this figure with the official code beside it.
Spatial attention. first goes through a convolution that compresses 512 channels to 256. Then three convolutions side by side, , and , each expand 256 channels back to 512, and the three are summed and passed through a ReLU. Using horizontal, vertical and square kernels together, according to the paper, captures local features at multiple scales.
In a step the figure leaves out, the code sums these 512 channels directly into a single map, then multiplies it back onto every channel of . This map never goes through a sigmoid, and its values have no upper bound, being the sum of 512 ReLU outputs. Estimating from the distribution at initialization (this note's estimate), three BN-ed branches summed and passed through a ReLU average about 0.7 per channel, and summing 512 of them gives three hundred-odd.
Channel attention. is pooled by GAP to 512 dimensions, passed through two fully connected layers, 512→32→512 (with BN and ReLU in between), and finally a sigmoid, giving each channel a weight between 0 and 1, which is multiplied back onto the GAP result. The output is a 512-dimensional vector. This is almost exactly SENet's squeeze-and-excitation with a reduction ratio of 16 and an extra BN in the middle; the difference is that the weights multiply the pooled vector instead of going back onto the feature map.
The paper's title says Multi-head Cross Attention, and the class in the code is called CrossAttentionHead too. In a Transformer, cross-attention takes queries from one side and keys and values from the other, then computes the similarity between every pair; inside each DAN head there are no queries or keys, only a spatial weight map and a set of 512-dimensional channel weights. The paper never explains what "cross" refers to; judging from the structure, each head chains spatial and channel attention together, and multi-head means several such modules side by side.
The four heads share the same structure with their own parameters. Counting parameters, each head's convolution has about 130,000, the about 1.18 million, and the and about 390,000 each; with channel attention and BN, one head is about 2.14M and four heads 8.54M. ResNet-18 without its classifier is 11.18M, for a total of 19.72M, the same as the paper reports. The heads all compute on the feature map, so the four together add only 0.41 GFLOPs, taking the whole model from ResNet-18's 1.82 G to 2.23 G.
AFN: scale, require difference, then merge
Each of the four heads gives a 512-dimensional , and they're stacked into . AFN first applies log-softmax along the "head" direction, equation (5) of the paper:
indexes the channel. Each channel gets its own softmax across the four heads, followed by a log. The authors say this puts the different heads' features on a common scale.
Next comes the partition loss, equation (6) of the paper:
is the variance among the four heads' for image and channel , and . The more the four heads differ on this channel, the larger the variance and the smaller this term; when the four heads are identical, the variance is 0 and the term blows up.
Work through one channel. Say the four heads output on this channel; their exponentials are 7.389, 1.649, 1.350 and 1.221, summing to 11.609:
The four are −0.452, −1.952, −2.152 and −2.252, averaging −1.702, with deviations of 1.25, −0.25, −0.45 and −0.55 and a sum of squares of 2.13. The official code uses PyTorch's var, which divides by , for a variance of 0.71, and this channel's loss is . Make the first head stand out a bit more, , and the variance becomes 1.79, with the loss falling to 1.17. If all four heads output 1, the variance is 0; the code adds a tiny eps to the denominator to avoid dividing by 0, and the term comes to about 37.
For the four heads on the same channel, log-softmax subtracts the same number (2.452 here), so the variance of is exactly the variance of , 0.71 in both cases. What the partition loss actually measures is how much the four heads' raw outputs differ, and log-softmax has no effect on it.
The partition loss looks at the pooled 512-dimensional vectors and never touches the spatial maps. If two heads' spatial maps light up in different places, the vectors you get after multiplying by and pooling will naturally differ; the authors rely on this indirect effect to spread the four heads out spatially. It also only requires the four heads on the same image to differ, never that a given head always look at a given part, which matches the heat maps earlier, where "the same head looks at different places on different faces."
Finally the four heads' are summed along the head direction into 512 dimensions, followed by a fully connected layer outputting seven classes, then a BN layer, and cross-entropy. The total loss is equation (7):
; the authors say the results aren't very sensitive to these two values, but give no data.
A BN after the fully connected layer is unusual. In April 2022 someone asked about it in issue #14; the asker had tried it on their own small 4-class dataset, and without the BN the cross-entropy on the first batch was about 313, against a normal 1.94 with it. The author admitted that "the BN put after FC layer is odd," that the model doesn't learn well without it, and that "it's hard for me to explain." Following the estimate above (this note's guess), the spatial map has no upper bound and can reach several hundred, so the resulting are large too, and after log-softmax and summation the features fed into the classifier are large as well; large logits make the cross-entropy explode, and that last BN effectively pulls the scale of the whole path back down.
Training setup
- RAF-DB and AffectNet use the officially aligned images; SFEW 2.0 uses RetinaFace detection followed by manual alignment. The input is .
- ResNet-18 pretrained on MS-Celeb-1M.
- 40 epochs, batch 256, 4 heads, one 12 GB Tesla P100.
- RAF-DB uses SGD with learning rate 0.1 (multiplied by 0.1 every 10 epochs in the code); AffectNet uses Adam with learning rate , upsampling minority classes and downsampling majority classes so the classes are roughly balanced during training.
- Data augmentation is horizontal flips, random rotation and random erasing.
The AffectNet command in the official README trains for 10 epochs, and the released weights stop at epoch 5 (AffectNet-8), epoch 6 (AffectNet-7) and epoch 21 (RAF-DB). The code computes test-set accuracy every epoch and saves only the best one; AffectNet's validation set is its test set, and RAF-DB has no validation set at all. Picking checkpoints this way is common in expression recognition papers, so keep it in mind when you read the numbers.
Discussion
Main results
| Dataset | Baseline | Prior best | DAN |
|---|---|---|---|
| AffectNet-8 | 56.84 | 61.40 | 62.09 |
| AffectNet-7 | 56.97 | 64.57 | 65.69 |
| RAF-DB | 86.25 | 88.98 | 89.70 |
| SFEW 2.0 | 51.29 | 58.50 | 53.18 |
Compiled from Tables 1–4 of the paper, accuracy %. The baseline is a ResNet-18 with the same pretraining. "Prior best" is the highest row other than DAN on each table, which is MViT for both AffectNet rows, PSR for RAF-DB and FaceCaps for SFEW 2.0.
On both large datasets, DAN tops the table. On AffectNet-8 it's 0.69 above MViT and 1.86 above SCN's 60.23; on RAF-DB it's 0.72 above PSR and 2.67 above SCN's 87.03. SFEW 2.0 gets only 53.18, below several methods on the table; the authors' explanation is that multi-head attention needs more data to train, and SFEW's training set has fewer than a thousand images.
RAF-DB's 89.70% needs taking apart. Per-class recall is happiness 95.36, sadness 90.17, surprise 89.97, neutral 88.53, anger 83.95, disgust 70.62, fear 60.81. Happiness makes up nearly 40% of the test set, so accuracy is held up mostly by it; the unweighted mean over the seven classes is 82.77%. As for the surprise and fear from the opening, surprise gets ninety percent right and fear only sixty. AffectNet-8's validation set has the same count per class, and per-class recall ranges from neutral at 56.80 to happiness at 74.60, with contempt at 57.52.
Two numbers across six versions
That "seven-class mean" was actually in the paper at first. In v1 through v5, the RAF-DB table had an extra Avg. Accuracy column, with DAN at 85.32%, and the text said both numbers were the best at the time. In March 2022, the asker in issue #11 found a bug in the code in rafdb.py that computes mean accuracy, and recomputing with the official weights gave only 83.76. The author replied that it was their mistake and would be updated; in April, in issue #18, they said the corrected figure was 82.75, and in June said again that they would update the paper. v5 in November 2022 still had 85.32; v6 in May 2023 dropped the whole column (that's one way to update it).
SFEW 2.0 changed too. v1 through v5 gave 57.88%, with the text calling it "comparable"; v6 changed it to 53.18%, with the text now reading "although not the best," and no explanation of any kind between the two versions. The SFEW training code wasn't released; the author's reason in issue #7 was that the faces had to be cropped by hand, and a different detector would give different results.
RAF-DB's 89.70 and AffectNet's 62.09 and 65.69 never changed across all six versions.
Ablations
| Module | Setting | RAF-DB |
|---|---|---|
| FCN | none | 88.17 |
| FCN | center loss | 88.91 |
| FCN | affinity loss | 89.70 |
| AFN | no partition loss | 88.20 |
| AFN | partition loss | 89.70 |
Compiled from Table 5 of the paper, accuracy %; every setting includes cross-entropy.
Affinity loss is 0.79 above center loss and 1.53 above adding nothing; removing the partition loss costs 1.50.

The number of heads was tried from 1 to 10. One head gives 88.88, four heads 89.70, and after that it wanders between 89.07 and 89.47. The vertical axis of this chart starts at 88.80, so the whole chart spans less than 1 percentage point. The RAF-DB test set has 3,068 images, so one image is 0.033 percentage points, and one head versus four heads is a difference of 25 images.
A gap of this size has to be set beside reproduction noise. In the official issues, people who reran RAF-DB with the code reported 89.0 (#6), 88.98 (#22), and 89.54 and 88.72 (#31). The author's reply in #6 was that the code doesn't fix the random seed and to try a few more times, adding that "the 89.70 is not the best acc of our model." The reproduction spread is 0.2 to 1 percentage point, about as large as the distances between settings in the ablation table; the paper doesn't say how many times each setting was run, nor does it report standard deviations.
The baseline was questioned too. The asker in issue #13 (April 2022) used the same MS-Celeb-1M weights and DAN's training strategy to run a plain ResNet-18 and got 88.75% on RAF-DB, while the paper's table lists the baseline at 86.25%. The author replied that these pretrained weights weren't trained by them, that SCN and DACL use them too, and that "the best performances of all these model are below your stated 88.75%." Put the numbers side by side and DAN is 3.45 percentage points above its own baseline, but only 0.95 above the asker's.
A different dataset
Like most expression papers, DAN tests on the same dataset it trains on. In January 2026, a study from Warsaw University of Technology retrained POSTER++, DMUE and DAN from their official code, with seven classes on all three datasets, and then tested across datasets. In this study DAN gets only 76% on RAF-DB; its authors say some results are slightly below the original authors' numbers, which is "overall negligible" in the context of their later experiments. The RAF-DB described in the study averages , which looks like the unaligned original images rather than the official aligned version DAN uses, so the absolute numbers can't be compared directly with 89.70.
The cross-dataset drops are easier to read. DAN trained on RAF-DB falls to 40% when tested on AffectNet, and only 37% on ExpW, which was collected from the web; trained on AffectNet and tested on RAF-DB, it gets 65%. All three methods drop when the dataset changes; among the three models trained on RAF-DB, DAN has the lowest accuracy on the other two datasets, with POSTER++ at 49% and 52% and DMUE at 45% and 47%. Each dataset has its own annotation habits, and DAN's design doesn't address that problem.
Official code
The official code is at yaoing/DAN, MIT-licensed, with training code for RAF-DB and AffectNet, three sets of weights, and code for single-image inference and Grad-CAM. Besides the class count, the partition loss computation and the final BN mentioned earlier, there are two practical pitfalls.
- MS-Celeb-1M pretrained weights. In October 2022 someone reported that the download link was dead (issue #28), with no reply; the README switched to a new link the same month. These weights decide how high the baseline goes; change the weights and every number has to be rerun.
- AffectNet's file format. The code reads the version with one
*_exp.npylabel file per image. The asker in issue #9 used data in a different format, topping out at 60.94 on AffectNet-8 and getting 60.39 when testing the official weights on their own validation list; the author suggested downloading again through the official channel.
Conclusion
DAN makes an expression classifier look at several places on the face at once. ResNet-18 is followed by four "spatial attention plus channel attention" heads, the partition loss requires the four heads to differ on the same face, and the affinity loss pushes the feature centers of the expression classes apart; the whole model has 19.72M parameters, only 0.41 GFLOPs more than ResNet-18, with 89.70% on RAF-DB and 62.09% on AffectNet-8, both the top of their tables at the time.
The four heads really do spread out, each lighting up its own spot on the heat maps; how much accuracy that buys is less well supported. In the ablations each component brings 0.8 to 1.5 percentage points, community reruns of the official code vary by 0.2 to 1 percentage point, and the paper doesn't report how many times each setting was run; a plain ResNet-18 trained from the same MS-Celeb-1M weights already reaches 88.75%. When you use the official code, change the class count in AffinityLoss to the dataset's real number of classes and keep the BN after the fully connected layer; when you use it as a comparison, run it several times and average, and report the seven-class mean for RAF-DB alongside, which is 82.77% for this same model.
The name says "distract," but what it actually does is make the network pay close attention to several places at once. In the opening puzzle, telling surprise from fear means looking at the eyebrows and the mouth together; DAN spread out where it looks, and on RAF-DB surprise is 89.97% correct while fear is still only 60.81%. Between seeing different places and telling similar expressions apart, there is still a gap.