CLOC: Contrastive Learning for Ordinal Classification with Multi-Margin N-pair Loss

Dileepa Pitawela, Gustavo Carneiro, Hsiang-Ting Chen

Abstract

Standard ordinal classification methods often assume that all adjacent errors are equally significant. However, in high-stakes domains like medical diagnosis, certain boundaries—such as the threshold between benign and malignant stages—are far more critical than others. We present CLOC (Contrastive Learning for Ordinal Classification), a framework that introduces a novel Multi-Margin N-pair (MMNP) loss. By optimizing learnable, class-specific margins, CLOC allows the model to "stretch" the representation space at critical thresholds. Our results across multiple real-world datasets show that CLOC not only improves overall accuracy but also aligns feature representations with clinical priorities.

The Problem: Non-Uniform Ordinal Boundaries

In tasks like cancer grading or age estimation, labels follow a natural order. Standard classification treats every mistake as equally bad, while traditional ordinal methods assume uniform distances between ranks. In reality, misclassifying a "Stage 1" cancer as "Stage 2" might have minor implications, but misclassifying a "Pre-cancerous" lesion as "Benign" is catastrophic. Existing methods lack the flexibility to prioritize these critical decision boundaries.

Comparison of standard classification, ordinal classification, and CLOC, highlighting how CLOC handles non-equal adjacent errors.

Methodology: Multi-Margin N-pair (MMNP) Loss

CLOC leverages contrastive learning to organize the feature space according to rank. The core innovation is the Multi-Margin N-pair (MMNP) loss, which replaces the single fixed margin of standard contrastive losses with a set of learnable margins ($m_H$) between every pair of consecutive ranks.

The total margin between an anchor and a negative sample is the cumulative sum of all margins between their respective ranks. This forces the model to push distant ranks further away than nearby ranks, preserving the ordinal structure while allowing the model to discover which boundaries require more separation. Training is conducted in two phases: a joint learning phase where margins and the encoder are optimized simultaneously, followed by a margin-guided phase where learned margins are frozen to refine the feature extractor.

Key Results and Representation Geometry

CLOC consistently outperforms traditional ordinal methods and modern contrastive baselines. On the IDRiD clinical grading dataset, CLOC achieved an accuracy of 0.7379, significantly surpassing the next best baseline, MWR (0.6408).

Beyond metrics, CLOC provides unique interpretability. The learned margins often peak at the most clinically significant boundaries. For instance, in diabetic retinopathy grading, the largest margin naturally appeared at the threshold where treatment decisions typically shift. UMAP visualizations confirm that CLOC maintains a clear, ordered trajectory in the embedding space with visible gaps at these difficult boundaries, unlike standard methods that often lose the ordinal flow.

UMAP visualizations showing how CLOC maintains an ordered "string-like" embedding space compared to other methods.

Conclusion

CLOC offers a robust solution for ordinal classification by allowing for flexible, non-uniform class relationships. By enabling both automated margin learning and manual controllability for critical thresholds, it bridges the gap between standard machine learning objectives and real-world clinical needs. While complexity increases with very high class counts, CLOC remains a powerful tool for high-stakes diagnostic and ranking tasks.

Summary updated

BibTeX
@inproceedings{pitawela2025cloc,
  author = {Dileepa Pitawela and Gustavo Carneiro and Hsiang-Ting Chen},
  title = {{CLOC: Contrastive Learning for Ordinal Classification with Multi-Margin N-pair Loss}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2025},
  doi = {10.1109/cvpr52734.2025.01448},
}
Figure