Hard negative mining

Overview schema of hard negative mining from data. In the example above, the model is trained to classify gray cats from anything else. The "Hard Negative Selector" prioritizes other images of cats with a different fur color, since they are the most difficult negatives to distinguish from the positive samples.

Hard negative mining, also referred to as hard negative sampling, is a family of techniques in machine learning for selecting or constructing negative samples more difficult to distinguish from a corresponding positive example with respect to uniformly-sampled negatives, leading to a more informative training signal for discriminative classifiers and similarity models.

The technique is used when the pool of possible negative examples is far larger than the set of positives and most randomly chosen negatives are easy to classify by the model, contributing little to learning. Concentrating training on the small fraction of difficult negatives is intended to provide stronger gradient signal and faster convergence than uniform sampling.

Hard negative mining originated in object detection in the 1990s, where it was referred to as bootstrapping (not to be confused with statistical bootstrap resampling), and was later formalized for support vector machine models. Closely related ideas appear in deep metric learning, information retrieval, recommender systems, self-supervised contrastive learning, and vision-language models. Surveys distinguish hard negative mining from the broader practice of negative sampling, in which negatives are drawn at random or in proportion to frequency rather than by difficulty.

Background

In many learning problems the number of negative examples vastly exceeds the number of positives. In sliding-window object detection, a single image yields on the order of 105 candidate windows, almost all of which are background. Online hard example mining work reports that the imbalance can reach roughly 100,000 background regions per object for sliding-window detectors and remain about 70:1 even for proposal-based detectors. Using all negative examples is both computationally infeasible and statistically harmful, because the large number of easy negatives dominates the loss and gradient.

A parallel motivation arises in similarity learning. When training examples are grouped into pairs or triplets, the number of candidate tuples grows quadratically or cubically with the number of images, and once a model has converged most such tuples already satisfy the training objective and produce little or no gradient. In high-dimensional embedding spaces, randomly drawn negatives also tend to be uninformative because a significant percentage of them might already be distant from positive samples in the latent space. Selecting the most informative ("hard") examples is therefore presented as a way to keep the training signal steadier and informative throughout the whole training process.

Definition and terminology

Across application areas, a hard negative is consistently defined relative to the model being trained: it is a negative (an example whose label differs from that of the anchor or query), that the current model nonetheless classifies as a positive. For instance, in a margin-based detector, like a SVM a hard negative is a background example that is misclassified or that falls within the classifier's margin. In embedding-based methods, it is a differently-labelled example that lies close to the anchor in the learned feature space.

Several finer distinctions are commonly utilized:

Techniques

Object detection

In classical detectors, hard negative mining is an iterative, cache-based procedure layered on top of an SVM or other classifier: the model is trained on the positives and a small set of negatives, the trained model is run over background data to collect new false positives, easy examples are discarded, and the model is retrained, repeating a few times. A negative is treated as hard when its margin score indicates misclassification or a within-margin position.

Online hard example mining (OHEM) adapts this idea to the training of deep region-based detectors. Rather than alternating offline mining and retraining, OHEM forms each SGD mini-batch by scoring all candidate regions of interest with a read-only forward pass, and then back-propagating only through the highest-loss regions. An efficient implementation keeps two copies of the region network: one read-only copy that scores all regions and one trainable copy that processes only the selected hard examples. Hard negative mining has likewise been applied to convolutional face detectors by harvesting false positives from a first-round Faster R-CNN face detector and retraining on them, labelling a detected region a hard negative when its maximum intersection-over-union with any ground-truth face falls below a certain threshold.

Deep metric learning

Negative sampling examples for triplet loss. Easy negatives falls outside the selected margin, semi-hard negatives are within the margin, hard negatives are closer than the positive match.

In deep metric learning, models are trained with pairwise or triplet losses so that examples of the same class map to nearby points and examples of different classes map to distant points. Because most randomly sampled triplets quickly satisfy the loss margin and stop contributing gradient, the selection of hard pairs or triplets is widely regarded as essential. The triplet loss, in fact, penalizes triplets in which the anchor–positive distance is not smaller than the anchor–negative distance by at least a margin .

The FaceNet system introduced semi-hard negative mining, in which the chosen negative is farther from the anchor than the matching positive yet still falls within the margin, after finding that always selecting the hardest negatives could drive the embedding toward a collapsed (degenerate) solution; FaceNet also computed hard positives and negatives online from within each training batch rather than searching the whole dataset.

An early example of in-batch top-K selection appears in unsupervised video representation learning, where the negatives incurring the highest triplet loss within a mini-batch are selected. A widely cited variant is batch-hard mining, where a batch is built by sampling a fixed number of classes and a fixed number of examples per class, and for each anchor only the hardest positive and hardest negative within that batch are used; because these are the hardest within a small random subset rather than the whole dataset, the authors argued they act as "moderate" triplets and avoid the cost and instability of offline mining.

Several methods reduce the cost of mining or change what is mined:

Information retrieval and recommendation

In recommender systems and neural information retrieval, training pairs each observed (relevant) item against non-observed items as implicit negatives. Bayesian Personalized Ranking constructs training triples in which an observed item is treated as a positive and a non-observed item as an implicit negative, and trains a pairwise objective by drawing such triples uniformly at random; it does not use hard or dynamic negative selection, and reviews describe it as the uniform-sampling baseline that later hard-negative methods improved upon. In neural dense retrieval, a dual encoder is trained so that relevant query-document pairs have higher similarity than irrelevant ones, and the choice of negatives is decisive.

Several strategies are in common use:

Contrastive self-supervised learning

Contrastive self-supervised learning methods learn representations by pulling together different views of the same instance (positives) and pushing apart unrelated ones (negatives). The widely used InfoNCE objective, introduced in contrastive predictive coding, scores one positive against negatives drawn from a proposal distribution:

In its original form, contrastive predictive coding draws its negatives at random and recommends using more negatives because the objective bounds a mutual information quantity that tightens as grows. Several influential methods similarly increase the supply of negatives rather than their difficulty: SimCLR treats all other augmented examples in a large training batch as negatives and argues that its temperature-scaled cross-entropy loss implicitly weights negatives by hardness, whereas margin- and triplet-style losses require explicit semi-hard mining to remain competitive; and Momentum Contrast (MoCo) maintains negatives in a queue decoupled from the batch size, encoded by a slowly updated momentum encoder to keep them consistent.

Explicit hard-negative methods have nonetheless been adapted to the unsupervised setting.

Vision-language models

Mined hard negative images and crafted hard negative captions from NegCLIP.

In CLIP-style vision-language models, hard negatives are used to address the "bag of words" behavior of such models, ignoring word order and object-attribute binding. Methods in this area differ in whether they select, construct, or reweight negatives:

Theoretical aspects

For SVM detectors, hard negative mining has a convergence guarantee. An algorithm which alternately mines margin-violating examples into a cache and discards easy ones converges to the model that training on the entire dataset would produce. A complementary line of analysis treats the choice of negatives as an importance sampling problem in which the optimal negative-sampling distribution is proportional to the per-example gradient norm, so that harder (higher-loss) negatives should be sampled more often; this motivates mining the model's current top-ranked negatives.

At the same time, analyses of deep embedding training caution that the hardest negatives can be counterproductive. As the anchor-negative distance shrinks, the gradient direction becomes dominated by noise, yielding high-variance updates that can collapse the embedding. A 2023 theoretical study modelled hard-negative sampling as tilting the negative distribution toward more anchor-aligned negatives and proved that such hardening never decreases the contrastive loss while leaving the globally optimal representation geometry unchanged in the supervised case, while, in some settings harming the unsupervised case.

Limitations and challenges

Embedding collapse from over-hard negatives

Several authors report that mining only the single hardest negative destabilizes embedding training. FaceNet found that always selecting the hardest negatives could drive the embedding to a collapsed solution, and adopted semi-hard mining instead. Lifted structured embedding similarly observed that selecting only the hardest negative converges to a poor local optimum, motivating a smoothed objective over all in-margin negatives. Distance-weighted sampling diagnoses the mechanism as noise-dominated, high-variance gradients, and an empirical comparison in hierarchical-triplet-loss work found naive hardest-negative mining giving little improvement over random sampling, with a semi-hard variant performing better, partly because mining within a mini-batch cannot capture the global data distribution.

False negatives

Hard negative mining solely based on a similarity metric may include false positives in unsupervised settings.

When labels are unavailable or incomplete, the hardest negatives are also the most likely to be false negatives: examples that are mined as negatives but actually share the anchor's class or relevance. The problem has been documented in unsupervised contrastive learning, where it is called sampling bias (not to be confused with sampling bias in statistics), and in dense retrieval, where one study estimated that a large fraction of top-retrieved but unlabelled passages were in fact relevant, so that mining them as negatives without denoising degraded retrieval quality. Proposed remedies include debiasing corrections, cross-encoder denoising, uncertainty- and diversity-aware selection, and, in multi-modal settings, using a third modality to filter likely false negatives before reweighting.

See also

References

  1. 1 2 3 4 5 6 Yang, Zhen; Ding, Ming; Huang, Tinglin; Cen, Yukuo; Song, Junshuai; Xu, Bin; Dong, Yuxiao; Tang, Jie (August 2024). "Does Negative Sampling Matter? a Review With Insights Into its Theory and Applications". IEEE Transactions on Pattern Analysis and Machine Intelligence. 46 (8): 5692–5711. arXiv:2402.17238. Bibcode:2024ITPAM..46.5692Y. doi:10.1109/TPAMI.2024.3371473. ISSN 1939-3539. PMID 38421844.
  2. 1 2 3 4 5 6 7 Felzenszwalb, Pedro F.; Girshick, Ross B.; McAllester, David; Ramanan, Deva (2010). "Object Detection with Discriminatively Trained Part Based Models". IEEE Transactions on Pattern Analysis and Machine Intelligence. 32 (9). IEEE: 1627–1645. Bibcode:2010ITPAM..32.1627F. doi:10.1109/TPAMI.2009.167. PMID 20634557.
  3. 1 2 3 4 5 6 Schroff, Florian; Kalenichenko, Dmitry; Philbin, James (2015). FaceNet: A Unified Embedding for Face Recognition and Clustering. 2015 IEEE Conference on Computer Vision and Pattern Recognition (CVPR). IEEE. pp. 815–823. arXiv:1503.03832. doi:10.1109/CVPR.2015.7298682.
  4. 1 2 3 4 Harwood, Ben; Kumar B G, Vijay; Carneiro, Gustavo; Reid, Ian; Drummond, Tom (2017). Smart Mining for Deep Metric Learning. 2017 IEEE International Conference on Computer Vision (ICCV). pp. 2821–2829. arXiv:1704.01285. doi:10.1109/ICCV.2017.307.
  5. 1 2 Sung, K.-K.; Poggio, T. (January 1998). "Example-based learning for view-based human face detection". IEEE Transactions on Pattern Analysis and Machine Intelligence. 20 (1): 39–51. Bibcode:1998ITPAM..20...39S. doi:10.1109/34.655648. hdl:1721.1/7193. ISSN 1939-3539.
  6. 1 2 3 Zou, Zhengxia; Chen, Keyan; Shi, Zhenwei; Guo, Yuhong; Ye, Jieping (2023). "Object Detection in 20 Years: A Survey". Proceedings of the IEEE. 111 (3): 257–276. arXiv:1905.05055. doi:10.1109/JPROC.2023.3238524.
  7. ↑ Desai, Shasvat; Ghose, Debasmita; Chakraborty, Deep (2025). "A Survey on Data Curation for Visual Contrastive Learning: Why Crafting Effective Positive and Negative Pairs Matters". arXiv:2502.08134 [cs.CV].
  8. 1 2 Oksuz, Kemal; Cam, Baris Can; Kalkan, Sinan; Akbas, Emre (2021). "Imbalance Problems in Object Detection: A Review". IEEE Transactions on Pattern Analysis and Machine Intelligence. 43 (10): 3388–3415. arXiv:1909.00169. Bibcode:2021ITPAM..43.3388O. doi:10.1109/TPAMI.2020.2981890. PMID 32191882.
  9. 1 2 3 4 Shrivastava, Abhinav; Gupta, Abhinav; Girshick, Ross (2016). Training Region-based Object Detectors with Online Hard Example Mining. 2016 IEEE Conference on Computer Vision and Pattern Recognition (CVPR). pp. 761–769. arXiv:1604.03540. doi:10.1109/CVPR.2016.89.
  10. ↑ Lin, Tsung-Yi; Goyal, Priya; Girshick, Ross; He, Kaiming; Dollár, Piotr (2017). Focal Loss for Dense Object Detection. 2017 IEEE International Conference on Computer Vision (ICCV). IEEE. pp. 2980–2988. arXiv:1708.02002. doi:10.1109/ICCV.2017.324.
  11. 1 2 3 Ge, Weifeng; Huang, Weilin; Dong, Dengke; Scott, Matthew R. (2018). Deep Metric Learning with Hierarchical Triplet Loss. European Conference on Computer Vision (ECCV) 2018. Lecture Notes in Computer Science. Vol. 11210. pp. 272–288. arXiv:1810.06951. doi:10.1007/978-3-030-01231-1_17.
  12. 1 2 3 Yuan, Yuhui; Yang, Kuiyuan; Zhang, Chao (2017). Hard-Aware Deeply Cascaded Embedding. 2017 IEEE International Conference on Computer Vision (ICCV). IEEE. pp. 814–823. arXiv:1611.05720. doi:10.1109/ICCV.2017.94.
  13. 1 2 3 Wu, Chao-Yuan; Manmatha, R.; Smola, Alexander J.; Krähenbühl, Philipp (2017). Sampling Matters in Deep Embedding Learning. 2017 IEEE International Conference on Computer Vision (ICCV). IEEE. arXiv:1706.07567. doi:10.1109/ICCV.2017.309.
  14. 1 2 Kaya, Mahmut; Bilge, Hasan Şakir (2019). "Deep Metric Learning: A Survey". Symmetry. 11 (9). MDPI: 1066. Bibcode:2019Symm...11.1066K. doi:10.3390/sym11091066.
  15. 1 2 Musgrave, Kevin; Belongie, Serge; Lim, Ser-Nam (2020). "A Metric Learning Reality Check". Computer Vision – ECCV 2020. Lecture Notes in Computer Science. Vol. 12370. Springer. pp. 681–699. arXiv:2003.08505. doi:10.1007/978-3-030-58595-2_41.
  16. 1 2 3 Robinson, Joshua; Chuang, Ching-Yao; Sra, Suvrit; Jegelka, Stefanie (2020). Contrastive Learning with Hard Negative Samples. International Conference on Learning Representations (ICLR). arXiv:2010.04592.
  17. 1 2 Suh, Yumin; Han, Bohyung; Kim, Wonsik; Lee, Kyoung Mu (June 2019). "Stochastic Class-Based Hard Example Mining for Deep Metric Learning". 2019 IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR). pp. 7244–7252. Bibcode:2019cvpr.conf..742S. doi:10.1109/CVPR.2019.00742. ISBN 978-1-7281-3293-8.
  18. 1 2 Xuan, Hong; Stylianou, Abby; Pless, Robert (March 2020). "Improved Embeddings with Easy Positive Triplet Mining". 2020 IEEE Winter Conference on Applications of Computer Vision (WACV). pp. 2463–2471. Bibcode:2020wacv.conf..260X. doi:10.1109/WACV45572.2020.9093432. ISBN 978-1-7281-6553-0.
  19. ↑ Girshick, Ross; Donahue, Jeff; Darrell, Trevor; Malik, Jitendra (June 2014). "Rich Feature Hierarchies for Accurate Object Detection and Semantic Segmentation". 2014 IEEE Conference on Computer Vision and Pattern Recognition. pp. 580–587. Bibcode:2014cvpr.conf...82G. doi:10.1109/CVPR.2014.81. ISBN 978-1-4799-5118-5.
  20. ↑ Wan, Shaohua; Chen, Zhijun; Zhang, Tao; Zhang, Bo; Wong, Kong-kat (2016). "Bootstrapping Face Detection with Hard Negative Examples". arXiv:1608.02236 [cs.CV].
  21. 1 2 Hermans, Alexander; Beyer, Lucas; Leibe, Bastian (21 November 2017), In Defense of the Triplet Loss for Person Re-Identification, arXiv:1703.07737
  22. ↑ Wang, Xiaolong; Gupta, Abhinav (December 2015). "Unsupervised Learning of Visual Representations Using Videos". 2015 IEEE International Conference on Computer Vision (ICCV). pp. 2794–2802. Bibcode:2015iccv.conf..321W. doi:10.1109/ICCV.2015.320. ISBN 978-1-4673-8391-2.
  23. ↑ Sohn, Kihyuk (2016). "Improved Deep Metric Learning with Multi-class N-pair Loss Objective". Advances in Neural Information Processing Systems. Vol. 29. Barcelona, Spain: Curran Associates, Inc. pp. 1857–1865.
  24. 1 2 Song, Hyun Oh; Xiang, Yu; Jegelka, Stefanie; Savarese, Silvio (June 2016). "Deep Metric Learning via Lifted Structured Feature Embedding". 2016 IEEE Conference on Computer Vision and Pattern Recognition (CVPR). pp. 4004–4012. arXiv:1511.06452. Bibcode:2016cvpr.conf..434S. doi:10.1109/CVPR.2016.434. ISBN 978-1-4673-8851-1.
  25. ↑ Rendle, Steffen; Freudenthaler, Christoph; Gantner, Zeno; Schmidt-Thieme, Lars (2009). "BPR: Bayesian Personalized Ranking from Implicit Feedback". Proceedings of the Twenty-Fifth Conference on Uncertainty in Artificial Intelligence (UAI 2009). AUAI Press. pp. 452–461. arXiv:1205.2618.
  26. ↑ Zhao, Wayne Xin; Liu, Jing; Ren, Ruiyang; Wen, Ji-Rong (2024). "Dense Text Retrieval Based on Pretrained Language Models: A Survey". ACM Transactions on Information Systems. 42 (4): 1–60. arXiv:2211.14876. doi:10.1145/3637870.
  27. ↑ Karpukhin, Vladimir; Oğuz, Barlas; Min, Sewon; Lewis, Patrick; Wu, Ledell; Edunov, Sergey; Chen, Danqi; Yih, Wen-tau (2020). "Dense Passage Retrieval for Open-Domain Question Answering". Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP). Association for Computational Linguistics. pp. 6769–6781. arXiv:2004.04906. doi:10.18653/v1/2020.emnlp-main.550.
  28. 1 2 Xiong, Lee; Xiong, Chenyan; Li, Ye; Tang, Kwok-Fung; Liu, Jialin; Bennett, Paul; Ahmed, Junaid; Overwijk, Arnold (2021). Approximate Nearest Neighbor Negative Contrastive Learning for Dense Text Retrieval. International Conference on Learning Representations (ICLR). arXiv:2007.00808.
  29. 1 2 3 Qu, Yingqi; Ding, Yuchen; Liu, Jing; Liu, Kai; Ren, Ruiyang; Zhao, Wayne Xin; Dong, Daxiang; Wu, Hua; Wang, Haifeng (2021). "RocketQA: An Optimized Training Approach to Dense Passage Retrieval for Open-Domain Question Answering". Proceedings of the 2021 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies. Association for Computational Linguistics. pp. 5835–5847. arXiv:2010.08191. doi:10.18653/v1/2021.naacl-main.466.
  30. ↑ Zhan, Jingtao; Mao, Jiaxin; Liu, Yiqun; Guo, Jiafeng; Zhang, Min; Ma, Shaoping (2021). Optimizing Dense Retrieval Model Training with Hard Negatives. Proceedings of the 44th International ACM SIGIR Conference on Research and Development in Information Retrieval (SIGIR '21). Association for Computing Machinery. arXiv:2104.08051.
  31. ↑ Le-Khac, Phuc H.; Healy, Graham; Smeaton, Alan F. (2020). "Contrastive Representation Learning: A Framework and Review". IEEE Access. 8: 193907–193934. arXiv:2010.05113. Bibcode:2020IEEEA...8s3907L. doi:10.1109/ACCESS.2020.3031549.
  32. ↑ van den Oord, Aaron; Li, Yazhe; Vinyals, Oriol (2018). "Representation Learning with Contrastive Predictive Coding". arXiv:1807.03748 [cs.LG].
  33. ↑ Chen, Ting; Kornblith, Simon; Norouzi, Mohammad; Hinton, Geoffrey (2020). A Simple Framework for Contrastive Learning of Visual Representations. Proceedings of the 37th International Conference on Machine Learning (ICML). Vol. 119. PMLR. pp. 1597–1607. arXiv:2002.05709.
  34. ↑ He, Kaiming; Fan, Haoqi; Wu, Yuxin; Xie, Saining; Girshick, Ross (2020). Momentum Contrast for Unsupervised Visual Representation Learning. 2020 IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR). IEEE. pp. 9726–9735. arXiv:1911.05722. doi:10.1109/CVPR42600.2020.00975.
  35. ↑ Kalantidis, Yannis; Sariyildiz, Mert Bulent; Pion, Noe; Weinzaepfel, Philippe; Larlus, Diane (2020). "Hard Negative Mixing for Contrastive Learning". Advances in Neural Information Processing Systems 33 (NeurIPS 2020). Curran Associates, Inc. arXiv:2010.01028.
  36. ↑ Ho, Chih-Hui; Vasconcelos, Nuno (2020). "Contrastive Learning with Adversarial Examples". Advances in Neural Information Processing Systems 33 (NeurIPS 2020). arXiv:2010.12050.
  37. 1 2 3 Yuksekgonul, Mert; Bianchi, Federico; Kalluri, Pratyusha; Jurafsky, Dan; Zou, James (2023). When and Why Vision-Language Models Behave like Bags-of-Words, and What to Do About It?. International Conference on Learning Representations (ICLR). arXiv:2210.01936.
  38. ↑ Patel, Maitreya; Kusumba, Abhiram; Cheng, Sheng; Kim, Changhoon; Gokhale, Tejas; Baral, Chitta; Yang, Yezhou (2024). TripletCLIP: Improving Compositional Reasoning of CLIP via Synthetic Vision-Language Negatives. Advances in Neural Information Processing Systems 37 (NeurIPS 2024). arXiv:2411.02545.
  39. ↑ Radenovic, Filip; Dubey, Abhimanyu; Kadian, Abhishek; Mihaylov, Todor; Vandenhende, Simon; Patel, Yash; Wen, Yi; Ramanathan, Vignesh; Mahajan, Dhruv (2023). Filtering, Distillation, and Hard Negatives for Vision-Language Pre-Training. Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR). pp. 6967–6977. arXiv:2301.02280.
  40. ↑ Wang, Haonan; Huang, Minbin; Huang, Runhui; Hong, Lanqing; Xu, Hang; Hu, Tianyang; Liang, Xiaodan; Li, Zhenguo; Cheng, Hong; Kawaguchi, Kenji (April 2025). "Getting More Juice Out of Your Data: Hard Pair Refinement Enhances Visual-Language Models Without Extra Data". In Chiruzzo, Luis; Ritter, Alan; Wang, Lu (eds.). Proceedings of the 2025 Conference of the Nations of the Americas Chapter of the Association for Computational Linguistics: Human Language Technologies (Volume 1: Long Papers). Albuquerque, New Mexico: Association for Computational Linguistics. pp. 7854–7873. doi:10.18653/v1/2025.naacl-long.399. ISBN 979-8-89176-189-6.
  41. ↑ Jiang, Ruijie; Nguyen, Thuan; Aeron, Shuchin; Ishwar, Prakash (2025). "Hard-Negative Sampling for Contrastive Learning: Optimal Representation Geometry and Neural- vs Dimensional-Collapse". arXiv:2311.05139 [cs.LG].
  42. 1 2 3 Chuang, Ching-Yao; Robinson, Joshua; Lin, Yen-Chen; Torralba, Antonio; Jegelka, Stefanie (2020). "Debiased Contrastive Learning". Advances in Neural Information Processing Systems 33 (NeurIPS 2020). arXiv:2007.00224.
  43. ↑ Tabassum, Afrina; Wahed, Muntasir; Eldardiry, Hoda; Lourentzou, Ismini (2022). "Hard Negative Sampling Strategies for Contrastive Representation Learning". arXiv:2206.01197 [cs.LG].
  44. ↑ Li, Guangping; Gao, Yanan; Huang, Xianhui; Ling, Bingo Wing-Kuen (2025). "A Hard Negatives Mining and Enhancing Method for Multi-Modal Contrastive Learning". Electronics. 14 (4). MDPI: 767. doi:10.3390/electronics14040767.