Deep Metric Learning with Hierarchical Triplet Loss

Weifeng Ge, Weilin Huang, Dengke Dong, Matthew R. Scott

Abstract

We present a novel hierarchical triplet loss (HTL) capable of automatically collecting informative training samples (triplets) via a defined hierarchical tree that encodes global context information. This allows us to cope with the main limitation of random sampling in training a conventional triplet loss, which is a central issue for deep metric learning. Our main contributions are two-fold. (i) we construct a hierarchical class-level tree where neighboring classes are merged recursively. The hierarchical structure naturally captures the intrinsic data distribution over the whole database. (ii) we formulate the problem of triplet collection by introducing a new violate margin, which is computed dynamically based on the designed hierarchical tree. This allows it to automatically select meaningful hard samples with the guide of global context. It encourages the model to learn more discriminative features from visual similar classes, leading to faster convergence and better performance. Our method is evaluated on the tasks of image retrieval and face recognition, where it outperforms the standard triplet loss substantially by 1%-18%. It achieves new state-of-the-art performance on a number of benchmarks, with much fewer learning iterations.

Introduction

Deep Metric Learning (DML) is fundamental to tasks like image retrieval and face recognition, where the goal is to learn a mapping that places semantically similar images close together in a feature space. The triplet loss is a cornerstone of DML, yet it suffers from significant inefficiencies. Specifically, the number of possible triplets grows cubically ($O(N^3)$) with dataset size, and random sampling often yields "easy" triplets that provide negligible gradients. Furthermore, standard mini-batch training lacks a global perspective on class relationships, often leading to suboptimal local convergence.

Methodology

To address these challenges, the authors propose Hierarchical Triplet Loss (HTL), which integrates global dataset structure into the local training process.

Hierarchical Class Tree

The core of HTL is a global hierarchical tree constructed by recursively merging classes based on their average distance in the feature space. This structure captures the intrinsic data distribution across the entire database. For instance, in a fine-grained dataset, visually similar species are grouped at lower levels of the tree, while distinct categories merge at higher levels.

Visualization of class-level distributions in the feature space, illustrating the global context captured by the hierarchical structure.

Anchor-Neighbor (A-N) Sampling and Dynamic Margin

HTL utilizes the hierarchical tree to guide sample selection and loss calculation:

  • A-N Sampling: Mini-batches are constructed by selecting an anchor class and its "neighbor" classes from the tree, ensuring the model consistently encounters challenging, visually similar negatives.
  • Dynamic Violate Margin: Unlike standard triplet loss which uses a fixed margin, HTL employs a dynamic margin ($\alpha_z$). This margin is calculated based on the hierarchical level at which the anchor and negative classes diverge, forcing the model to prioritize separation of globally similar classes.

Experimental Setup

The effectiveness of HTL was evaluated on several benchmark datasets:

  • In-Shop Clothes Retrieval: ~4,000 classes of clothing.
  • CUB-200-2011: 200 fine-grained bird species.
  • Cars-196: 196 car types.
  • Stanford Online Products: High intra-class variability e-commerce images.
  • Labeled Faces in the Wild (LFW): Face verification benchmark.
Sample images from the CUB-200-2011 dataset, highlighting the challenge of distinguishing between visually similar fine-grained bird species.

Results and Discussion

HTL demonstrates significant improvements in both accuracy and training efficiency. On the In-Shop Clothes dataset, HTL achieved a Recall@1 of 80.9%, a substantial increase over the 62.3% baseline. Similar gains were observed on Cars-196 (81.4% Recall@1) and LFW (99.2% accuracy).

Crucially, HTL acts as a form of automated curriculum learning. By focusing on the most informative samples guided by the global tree, the model converges approximately twice as fast as methods relying on random sampling. This suggests that incorporating global context is vital for efficient deep metric learning.

Conclusion

Hierarchical Triplet Loss successfully mitigates the sampling bottleneck in DML by leveraging global class hierarchies. While the method requires periodic tree updates and shows sensitivity to label noise in face datasets, its ability to achieve state-of-the-art results with fewer iterations marks a significant advancement. Future research may explore the integration of HTL with angular-based loss functions to further push the boundaries of face recognition and retrieval performance.

Summary updated

Abstract

We present a novel hierarchical triplet loss (HTL) capable of automatically collecting informative training samples (triplets) via a defined hierarchical tree that encodes global context information. This allows us to cope with the main limitation of random sampling in training a conventional triplet loss, which is a central issue for deep metric learning. Our main contributions are two-fold. (i) we construct a hierarchical class-level tree where neighboring classes are merged recursively. The hierarchical structure naturally captures the intrinsic data distribution over the whole database. (ii) we formulate the problem of triplet collection by introducing a new violate margin, which is computed dynamically based on the designed hierarchical tree. This allows it to automatically select meaningful hard samples with the guide of global context. It encourages the model to learn more discriminative features from visual similar classes, leading to faster convergence and better performance. Our method is evaluated on the tasks of image retrieval and face recognition, where it outperforms the standard triplet loss substantially by 1%-18%. It achieves new state-of-the-art performance on a number of benchmarks, with much fewer learning iterations.
BibTeX
@inproceedings{ge2018deep,
  author = {Weifeng Ge and Weilin Huang and Dengke Dong and Matthew R. Scott},
  title = {{Deep Metric Learning with Hierarchical Triplet Loss}},
  booktitle = {European Conference on Computer Vision},
  year = {2018},
  doi = {10.1007/978-3-030-01231-1\_17},
}
Figure