Paper commentary
Class-Balanced Loss Based on Effective Number of Samples
Effective number of samples gives a better class weight for long-tailed loss
- Problem Class-balanced losses weighted by inverse class frequency perform poorly on large real-world long-tailed datasets. Nobody has a principled weight to replace it, because extra samples add less as overlap grows.
- Method The authors replace raw class counts with an effective number of samples, (1 − β^n)/(1 − β), which discounts overlap between samples. Each class weight is its inverse, and the weight multiplies softmax, sigmoid or focal loss.
- Results On ILSVRC 2012 with ResNet-50, class-balanced focal loss reached 22.71 top-1 error, against 23.92 for softmax cross-entropy at 224×224 input.
Why raw sample counts fall short
Many large real-world datasets have a long tail: a handful of dominant classes hold most of the examples, and the remaining classes have only a few each. Networks trained on such data tend to do poorly on the weakly represented classes.
A common response is a class-balanced loss that gives each sample a weight inversely proportional to its class frequency. On large real-world long-tailed sets that strategy has been reported to perform poorly, and a smoothed variant that uses weights inversely proportional to the square root of class frequency has been used in its place.
The question this study takes up is what a better class weight should be built from so that it works across many kinds of datasets. Its starting point is sample size: extra data helps less and less once new samples overlap with what the model has already seen.
How overlap turns sample counts into an effective number
The derivation treats sampling as a simplified random covering problem: each sample is a unit-volume subset of a feature region of volume N, and samples may overlap. Under this view the effective number of samples is defined as the expected volume that the drawn samples cover.
To keep the calculation tractable, a newly drawn sample is assumed to lie either entirely inside the previously covered set, with probability p, or entirely outside it, with probability 1 minus p; partial overlap is ignored. With E_{n-1} as the expected covered volume after n−1 draws, the overlap probability is \(p = E_{n-1}/N\). This gives the recurrence \(E_n = 1 + \frac{N-1}{N} E_{n-1}\).
Setting \(\beta = (N-1)/N\) and unrolling the recurrence gives the closed form \(E_n = (1-\beta^n)/(1-\beta)\), which equals \(\sum_{j=1}^{n} \beta^{j-1}\). The j-th sample therefore contributes β^(j−1), so each later sample adds a factor of β less than the one before, and β controls how quickly the effective number flattens. Letting n grow without bound gives a total volume of \(1/(1-\beta)\), which equals N, so the definition of β is consistent.
At β = 0 the effective number is 1, which corresponds to N = 1, where every sample is the same prototype. As β approaches 1 the effective number approaches the raw count n, which corresponds to a large N with almost no overlap between samples.
The authors tie N to visual data: near-duplicate images and data augmentation make many samples cover the same region. They read N as the number of unique prototypes in a class, so stronger augmentation would mean a smaller N.
Turning the effective number into a loss weight
Each class i receives a weight inversely proportional to its effective number \(E_{n_i}\), and the weights are normalized so that they sum to C. Choosing a separate N for each class would need information the study does not have, so N is treated as a dataset-level value and β = (N−1)/N is shared by all classes in a dataset.
For a sample whose ground-truth class y has n_y training samples, the class-balanced term is \((1-\beta)/(1-\beta^{n_y})\), which multiplies the base loss L(p, y). The resulting class-balanced loss is written \(CB(p,y) = \frac{1-\beta}{1-\beta^{n_y}} L(p,y)\). With β = 0 the factor equals 1 and nothing is re-weighted, while as β approaches 1 the factor tends toward re-weighing by inverse class frequency.
The term does not depend on the chosen loss or on the predicted probabilities, so it can be attached to any loss. The authors apply it to softmax cross-entropy, sigmoid cross-entropy and focal loss.
Attaching the weight to softmax, sigmoid and focal losses
Softmax cross-entropy treats classes as mutually exclusive, and its class-balanced form multiplies the negative log of the softmax probability of the true class by the same factor. The form is \(CB_{softmax}(z,y) = -\frac{1-\beta}{1-\beta^{n_y}} \log\left(\frac{\exp(z_y)}{\sum_{j=1}^{C}\exp(z_j)}\right)\).
Sigmoid cross-entropy instead gives each class its own one-vs-all binary prediction, so the loss is a sum over all classes. For the true class the logit keeps its sign, and for every other class the sign is flipped, which lets one expression cover both cases. The class-balanced version multiplies that whole sum by the same factor, fixed by the ground-truth class count n_y.
The authors argue that this suits real-world data, where some classes are very similar and many images carry more than one label.
Focal loss multiplies each term by \((1-p_t)^{\gamma}\) to reduce the loss of well-classified samples. The class-balanced focal loss keeps this modulating factor and applies the class-balanced weight outside the sum. Focal loss also has an alpha-balanced variant, and the class-balanced focal loss matches it when α_t is set to \((1-\beta)/(1-\beta^{n_y})\), so the term can be read as an explicit choice of α_t based on the effective number.
Datasets and training choices
Long-tailed CIFAR datasets reduce the training images of class i by an exponential profile, \(n = n_i \mu^i\), with μ between 0 and 1, and the test set stays unchanged. The imbalance factor is the number of training images in the largest class divided by the number in the smallest. Table 2 reports imbalance factors of 200, 100, 50, 20, 10 and 1 on CIFAR-10 and CIFAR-100.
iNaturalist 2017 has 579,184 training images from 5,089 classes, and iNaturalist 2018 has 437,513 training images from 8,142 classes. The ILSVRC 2012 split has 1,281,167 training images and 50,000 validation images. Table 1 lists imbalance factors of 435.44 for iNaturalist 2017, 500.00 for iNaturalist 2018 and 1.78 for ILSVRC 2012.
All networks are trained from scratch with stochastic gradient descent with momentum, implemented in TensorFlow. CIFAR models are ResNet-32 with batch size 128 for 200 epochs, an initial learning rate of 0.1, decay by 0.01 at epochs 160 and again at 180, and linear warm-up over the first 5 epochs. The authors report that setting the decay factor to 0.01 instead of 0.1 removed a rise in loss and validation error after the learning-rate drop.
Focal loss uses a learning rate 2× larger on ILSVRC 2012 and 4× larger on iNaturalist, because its scale is smaller than that of softmax and sigmoid cross-entropy.
For sigmoid-based losses the bias of the last layer is initialized from a class prior π = 1/C as \(b = -\log((1-\pi)/\pi)\), instead of zero. With zero bias every class probability starts near 0.5 and the initial loss is large, which is why the prior-based initialization is used. Weight decay is also removed for that bias.
Hyperparameters are chosen by cross-validation over loss type (softmax, sigmoid or focal), β in {0.9, 0.99, 0.999, 0.9999}, and γ in {0.5, 1.0, 2.0} for focal loss.
Long-tailed CIFAR error rates against the original losses
At imbalance factor 200 on Long-Tailed CIFAR-10, the class-balanced loss with softmax at β = 0.9999 reached a classification error rate of 31.11, against 34.32 for softmax cross-entropy. The class-balanced row is the lowest entry in each of the other CIFAR-10 columns. At imbalance factor 1 the class-balanced term equals 1, so the table marks that entry with an asterisk.
On Long-Tailed CIFAR-100 at imbalance factor 10, the class-balanced row gives 42.01, against 44.29 for softmax cross-entropy, using focal loss with β = 0.999 and γ = 0.5.
Under the training strategy described for sigmoid and focal losses, sigmoid cross-entropy and focal loss outperform softmax cross-entropy in most cases. On CIFAR-10 the best β is 0.9999 in every case the authors report, while on CIFAR-100 the optimal β differs between imbalance factors and is smaller.
Why the best β differs between CIFAR-10 and CIFAR-100
At imbalance factor 50, the class-balanced term improves the original loss on CIFAR-10 at each β tested, and the gain grows as β increases toward 0.9999. On CIFAR-100 only β of 0.99 and 0.999 improve the original loss, and a larger β degrades it.
At β = 0.9999 on CIFAR-10 the effective numbers are close to the raw counts, so the weights resemble inverse class frequency. When β is small the effective numbers are similar across classes, so the weights are smoother. The authors take the poor large-β results on CIFAR-100 as evidence that inverse class frequency is not a good choice there, and that a smaller β is needed.
The authors connect β to N: since β = (N−1)/N, a larger β means a larger N, that is, more unique prototypes. They argue that CIFAR-100 classes are finer-grained than CIFAR-10 classes and so should have a smaller N, and they offer this as the reason for the observed effect of β.
Full β and γ grids and CIFAR-20
The appendix adds CIFAR-20, which uses the CIFAR-100 images with 20 coarse labels. It trains ResNet-32 with softmax, sigmoid and focal losses, each in original form and in class-balanced form with β of 0.9, 0.99, 0.999 and 0.9999, and with γ of 0.5, 1.0 and 2.0 for focal loss.
On CIFAR-10 higher β gives better performance in general. On CIFAR-20 and CIFAR-100 a lower β is needed for good performance, which the authors read as evidence that weights should follow the effective number rather than inverse class frequency.
ResNet results on iNaturalist and ILSVRC 2012
The large-scale runs use class-balanced focal loss with β = 0.999 and γ = 0.5, chosen because the authors found those values reasonably good on all datasets. The comparison covers ResNet-50, ResNet-101 and ResNet-152 with softmax cross-entropy, and the same depths with class-balanced focal loss.
On ILSVRC 2012 at 224×224 input, ResNet-50 top-1 error falls from 23.92 with softmax cross-entropy to 22.71 with class-balanced focal loss, and top-5 error from 7.03 to 6.72. ResNet-50 with the class-balanced loss lands close to ResNet-101 with softmax, which reached 22.65 top-1.
On iNaturalist 2017 ResNet-50 top-1 error goes from 45.38 with softmax to 41.92 with class-balanced focal loss, and on iNaturalist 2018 from 42.86 to 38.88. ResNet-50 with the class-balanced focal loss at 41.92 is close to ResNet-152 with softmax at 41.42 on iNaturalist 2017.
The 320×320 rows vary the input size and are reported separately from the 224×224 rows, giving ResNet-50 top-1 error of 38.16 on iNaturalist 2017, 35.84 on iNaturalist 2018 and 21.99 on ILSVRC 2012. Training curves for ResNet-50 on ILSVRC 2012 and iNaturalist 2018 show the class-balanced focal loss ahead of softmax cross-entropy only after about 60 epochs.
Limits of the evidence and the framework
The framework assumes no particular data distribution, and the authors say a better estimate of the effective number would likely need one. Each dataset uses a single N, so β is the same for all classes in it, and partial overlap between samples is not modelled.
The best β on CIFAR-100 changes with imbalance factor, so the value is chosen by cross-validation for each setting rather than fixed in advance. On the large-scale datasets the study reports only softmax cross-entropy and class-balanced focal loss at β = 0.999 and γ = 0.5, so other β values and the sigmoid variant are not tested there. The authors list adaptive methods and assumptions about the data distribution as future work.
Paper info
- Authors
- Yin Cui, Menglin Jia, Tsung-Yi Lin, Yang Song, Serge Belongie
- Venue
- arXiv · Preprint
- First published
- 2019-1-16
- Added
- 2026-11-12
- Updated
- 2019-1-16
- Version
- v1
- arXiv
- 1901.05555
- 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
Yin Cui, Menglin Jia, Tsung-Yi Lin, Yang Song, Serge Belongie (2019). Class-Balanced Loss Based on Effective Number of Samples. arXiv:1901.05555v1. https://arxiv.org/abs/1901.05555v1
BibTeX
@misc{cui2019class,
title = {{Class-Balanced Loss Based on Effective Number of Samples}},
author = {Yin Cui and Menglin Jia and Tsung-Yi Lin and Yang Song and Serge Belongie},
year = {2019},
eprint = {1901.05555},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/1901.05555v1}
}