Construct Informative Triplet with Two-stage Hard-sample Generation
Abstract
In this paper, we propose a robust sample generation scheme to construct informative triplets. The proposed hard sample generation is a two-stage synthesis framework that produces hard samples through effective positive and negative sample generators in two stages, respectively. The first stage stretches the anchor-positive pairs with piecewise linear manipulation and enhances the quality of generated samples by skillfully designing a conditional generative adversarial network to lower the risk of mode collapse. The second stage utilizes an adaptive reverse metric constraint to generate the final hard samples. Extensive experiments on several benchmark datasets verify that our method achieves superior performance than the existing hard-sample generation algorithms. Besides, we also find that our proposed hard sample generation method combining the existing triplet mining strategies can further boost the deep metric learning performance.
keywords:
Deep metric learning, triplet , hard sample generation , adversarial network , mode collapse.1 Introduction
Many applications, such as mobile augmented reality (MAR) and social life based on image websites, are becoming more and more popular with the increase of digital cameras and intelligent mobile devices [1]. Millions of images are produced every day from these types of digital equipment, and thus how to accurately search images from a large dataset poses a great challenge. To address this problem, the content-based image retrieval (CBIR) technique is proposed to search for images representing the same object or scene as the one depicted in a query image [2, 3].
The key for CBIR is how to build compact and robust image representations. The traditional hand-crafted features suffer large memory size consumption and will lower the searching efficiency [2, 4]. Many compact image representations [5, 6, 7, 8, 9] are thus proposed to address the above problem. Among these studies, the deep features can produce better performance than the traditional Fisher compact features (such as work [5] and [6]), and they have become the predominant stream of features used for CBIR. Generally, the Convolutional Neural Networks (CNN) layer activations are directly adopted as the off-the-shelf deep features [7, 8, 9]. However, these features are usually trained for image classification tasks. They should be further optimized by transferring the CNN model to the image retrieval task through the deep metric learning (DML) architecture [10, 11].
In deep metric learning, the matching/non-matching training pairs or triplets are first carefully constructed and then passed to the Siamese [12] or triplet architectures [13]. The off-the-shelf deep feature is thus fine-tuned by decreasing the distances of matching pairs and increasing the distances of non-matching pairs. How to construct the informative matching/non-matching pairs or triplets plays a vital role in the fine-tuning process. Typically, there are two different lines of recent research regarding this issue: hard example mining and hard example generation.
In the first category, the researchers applied hard example mining to build the pairs or triplets, which targets selecting samples that can provide informative supervision to make the model training more efficient [14, 15, 16, 17]. The hard mining strategy has been proved to be effective in accelerating the convergence of the network. However, this training strategy may lead to training a biased network [18] because only a few samples are selected for training, while most of the non-selected easy samples are considered invalid.

The second category tries to construct informative triplets through hard example generation. These studies focus on digging the potential information of easy samples by generating hard samples with one-stage adversarial model, instead of just mining existing samples [19, 20]. This kind of research utilizes both the original and the generated hard samples to construct the informative pairs or triplets, and then optimizes the embedding learning. However, the positive and negative samples play different roles in the deep metric learning process, which have different distributions and characteristics. It is difficult to exploit the full potential capability of all the positive and negative samples at the same time just through one adversarial model.
Approach and Contributions. In this work, we propose a novel hard sample generation for deep embedding learning. To exploit the full potential ability of all the simple samples, we propose a two-stage generator to perform the hard positive and negative generation, separately. Moreover, to avoid generating random vectors, the existing hard sample generation schemes adopt label preserving synthesis, which just requires the generated samples to have the same label with their original samples. However, the generator can only produce samples from a subset of modes to meet the above simple constraint. This will ruin the diversity of the generated hard samples and thus cause the mode collapse problem [21]. We propose to build more powerful conditional synthesis with stronger constraints to alleviate this problem. The main contributions of our work are summarized as follows:
-
1.
A two-stage adversarial learning architecture, as shown in Fig 1, is proposed to produce hard positives and hard negatives at different stages, respectively. In this architecture, the hard anchor-positive pair is first produced and then the hard negative generation is performed in the second stage. Through our two-stage generation, the potential ability of both the simple positives and negatives is mined.
-
2.
In the hard anchor-positive generation, we perform a stronger conditional synthesis scheme than the recent label-preserving method. Different from the previous works, we use a conditional generative adversarial network that introduces intermediate embeddings as discriminatory conditions to generate samples that can be as close as the original samples and lower the risk of mode collapse. Besides, a piecewise linear manipulation function is also proposed to properly control the hard level of the generated samples.
-
3.
In the hard negative generation, we propose an adaptive reverse triplet loss (ART-loss). We impose more stringent restrictions on reverse triplet loss with a larger threshold when the sample generation is getting better. With ART-loss we can gradually generate harder and harder negative samples.
-
4.
We experimentally demonstrate that our proposed two-stage hard sample generation can be directly combined with other triplet mining methods. Our method can generate informative hard samples, which can be used as a complement to previous hard mining strategies to further boost the DML model performance.
Outline. The paper is organized as follows. In Section 2, we review techniques that are related to our work. In Section 3, we show some preliminaries about deep metric learning. In Section 4, we build the whole deep embedding learning framework. After that, we present the proposed two-stage hard sample generation scheme in Section 5. Then, we report the experimental results in Section 6. Finally, the conclusions of this paper are summarized in Section 7.
2 Related Works
Hard Sample Mining. Because not all image pairs or triplets are equally informative, many works proposed schemes mining hard samples to train the deep metric models [14, 15, 22, 23, 24]. Hard sample mining is a technique for selecting informative sample pairs in deep metric learning. Considering that the number of triplets grows cubically with the size of training data, triplet selection is quite necessary for training with triplet loss. These triplets can be selected not only offline from the entire training set [23], but also from each batch of training using an online approach [14, 24, 25]. Hard negative pair mining [23] proposed the selection of the hardest positive and negative within a batch to construct triplets that produce useful gradients and therefore help triplet loss networks converge quickly. While it speeds up convergence, it also could lead to a bad local minimum and a biased model due to the hardest positive and negative may often be noise in data. In work [25], the authors proposed a more relaxed strategy, semi-hard negative pair mining, which chooses an anchor-negative pair that is farther than the anchor-positive pair, but within a margin. Based on this idea, soft-hard mining [24] restricting the sampling set using moderate negatives and positives is proposed to avoid too confusing samples that are highly likely to be noisy data. Given that the previous method only focuses on a small number of hard triplets and ignores a large number of simple samples, distance-weighted tuple mining [14] comprehensively considers samples in the whole spectrum of difficulties by introducing a sampling distribution over the range of distances between anchor and negatives.
Hard Sample Generation. The more recent researches seek approaches that can generate hard sample pairs or triplets to optimize the deep network, such as work [19], work [20], work [26] and [27].
In work [19], the authors proposed an adversarial learning algorithm to jointly optimize a hard triplet generator and an embedding network. The hard triplet generation is realized by using an inverse triplet loss function, and the generator is constrained by keeping label consistency to avoid random output. However, in metric learning, just the generated hard triplets are adopted and the original triplets are ignored. Besides, performing hard triplet generation only through one adversarial stage fails to dig the full potential information for both the simple positive and negative samples at the same time. Both work [20] and [26] focus on hard negative generation. Compared with work [20], the authors in [26] proposed an adaptive hardness-aware augmentation method to control the hard levels of the generated samples and thus achieved some improvement. The hard level here denotes the “hard degree” of the image matching pair or non-matching pair used in the deep metric learning: the hard level is high when the embedding distance is big for the matching pair or the embedding distance is small for the non-matching pair, and vice versa. However, these two studies just digging the potential information of easy negatives while ignoring the easy positives. All the above hard sample generation studies just use the simple label preserving technique to avoid the failure of generation. In work [27], the authors proposed a method of synthetic hard sample generation, which generates symmetrical synthetic embeddings with each other as an axis of symmetry and then selects the hardest negative pair within the original and synthetic embeddings. Although this scheme is computationally inexpensive, it can only synthesise samples at corresponding symmetric positions in the embedding space, and the diversity of the synthesised samples is very dependent on the number of original samples.
3 Preliminaries
In this paper, the boldface uppercase letter denotes a set (pair) of input images, embeddings, or labels; the boldface lowercase letter or the uppercase letter denotes a specific image, embedding, or label. The main notations used in this paper are listed in Table 1.
Notations | Descriptions |
---|---|
the input images | |
, | the ground truth labels and predicted labels |
, | original samples and the generated hard samples |
, , | a triplet embeddings of the anchor, positive and negative |
, , | generated hard triplet embeddings |
,, | a triplet input images |
, | a ground truth label and a predicted label |
Deep metric learning aims to learn an embedding that can decrease the distance between matching pairs and increase the distance between non-matching pairs [3, 28]. To learn such an embedding, many works are proposed, such as the traditional contrastive loss [12, 29] under Siamese network and triplet loss [13, 25].

In this paper, we apply the triplet architecture to learn the embedding for deep metric learning. Triplet loss-based metric learning takes a triplet as the input, where each triplet contains an anchor (), a positive (), and a negative () image. The triplet loss is written as:
(1) |
where are the learned triplet embeddings corresponding to ; denote the learned deep network which maps the input image to the embedding space; the operator is the max(0, ) function, and is a distance margin that makes the anchor-positive pair closer than the anchor-negative pair. The triplet based metric learning is depicted in Fig. 2.
4 Deep Embedding Learning Framework with Hard-sample Generation
4.1 Framework
Our framework is shown in Fig. 3, which mainly consists of three parts: the CNN feature extractor (FE), the hard sample generation (HSG) model, and the embedding learning loss functions. Formally, we denote the feature extraction CNN by , which maps the input images into embedding samples . We then use the proposed HSG model to further produce the hard samples based on the original samples . Note that the produced temporary hidden samples in HSG are also applied in the loss computing process. The input images , the original embedding samples , the hidden samples and the generated hard embedding samples share the same ground truth categories , where . The goal is to train the deep embedding network with parameters . The general metric learning approaches train the parameters through optimizing the following objective function:
(2) |

We enhance the training procedure by utilizing the synthetic hard samples. Our final deep embedding learning metric is denoted as:
(3) |
We rewrite (3) as:
(4) |
where , and are loss terms corresponding to the original deep distance metric loss for , the category loss based on and the deep distance metric loss for the generated hard sample ; , and are three weighting factors corresponding to the three terms. Although the embedding model should be optimized on the whole dataset, we discuss the loss computing in a triplet input images for convenience. In the following, we will detail the objective function (4).
4.2 Objective Function
For the input images , the corresponding triplet embedding samples will be produced by CNN feature extractor . The hard triplet embedding samples are then further generated by passing into HSG model. Besides, the temporary hidden embedding samples are also obtained in HSG. The deep embedding network and the HSG model are simultaneously trained in an end-to-end manner. However, in the early stages of training, the deep model focuses on memorizing the simple training data [30], and thus we can decrease the weight for the generated hard samples. Besides, in the early stage, the embedding space does not have an accurate semantic structure [26], and so the quality of the generated hard embedding samples is very low and they can not provide valid information. Thus the use of the hard generated samples will ruin the learning and damage the embedding space structure. To solve this problem, we designed a weighting parameter adaptively adjusting method by using the training loss of the HSG model. The embedding training objective function is represented as:
(5) |
where and are pre-defined parameters; and are the triplet metric loss and the softmax loss, respectively; , , and are three balancing parameters for the three terms: the original metric loss , the category loss and the generated hard metric loss . In the early stage of training, is very large and thus the weight for is larger than the weight for . As the training proceeds, is decreased and thus high quality hard samples will be produced. The hard synthetics can provide information for training, and higher weight is set to highlight the generated hard samples. Note that is set as in the HSG model, which will be discussed in Section 5. In (5), and are written as (6) and (7).
(6) |
(7) |
The category loss is performed based on the softmax loss,
(8) |
where is the one-hot encoding vector of the correct class for sample ; denotes the predicted probability vector.
In (5), the only unknown elements are the generated samples and . In the following section, we will detail the hard sample generation.
5 Two-stage Hard Sample Generation

5.1 Architecture
The detailed two-stage hard sample generation architecture is denoted in Fig. 4. In the first stage, the hard level of the input anchor-positive pair will first be increased by directly manipulating the distances between them. To achieve this, the piecewise linear manipulation (PLM) is designed. Then the generated hard anchor-positive pair will be processed by generator and thus the hard anchor-positive pair is obtained. However, just conducting label-preserving synthesis is not enough and this may result in mode collapse, as shown in Fig. 5(a). In this case, the generator only produces samples from part modes of the distribution and ignores the other modes.

To alleviate the mode collapse issue and guarantee the diversity of the generated samples, we encourage the one-to-one relationship between the original sample and the generated sample by adding an extra conditional constraint to the generation process. The discriminator needs to have the ability to identify the results of combining the intermediate embeddings with the original and generated embeddings respectively. In the second stage, the generated anchor-positive pair and original negative sample are passed into a generator to produce the final hard triplet . To support the adversarial learning, the discriminator is integrated into Stage 2.
In our architecture, the PLM, the conditional synthesis (in Stage 1) and the hard triplet generation (in Stage 2) are three key parts that will be discussed in the following.
5.2 Piecewise Linear Manipulation
In order to make anchor and positive more difficult, we need to increase the distance between them. Here, we first linearly stretch the embeddings of anchor and positive samples, as shown in Stage 1 of Fig. 1, thus generating a harder anchor and positive pair , as depicted in Fig. 4. We propose the piecewise linear manipulation (PLM) scheme as shown in (9),
(9) |
where is a picecwise variable which controls the hard level of the generated anchor-positive pair . The picecwise variable plays the key role in our PLM scheme and we will discuss it in the following.
During the stretching process, if the value of is too large, the anchor-positive samples may be stretched into different classes. Even if the distance is increased, the stretched samples will no longer be anchor-positive pair and thus become meaningless. In order to avoid this issue, we need to limit the scope and size of the . Thus in our PLM, we set the piecewise variable as:
(10) |
where denotes the distance between the anchor and positive samples; is an adaptive threshold. When is already large enough to be greater than , we use a exponent function . At this situation, the maximum value of is , and the minimum value is about 0. When the distance between them is less than , we use a linear function . At this situation, the maximum value of is and the minimum value is . In the calculation of , is related to the distance distribution of samples in different datasets, so it is difficult to manually adjust in various datasets. In order to automatically select the best hyperparameters in each dataset, we proposed an adaptive method by choosing as:
(11) |
where is the number of anchor-positive pairs, and represents the average distance of anchor-positive pairs in the previous epoch.
After piecewise linearly manipulating the input anchor-positive pair , we obtain hard anchor-positive pair . However, there is no guarantee that share the same label with . Next, we will conduct conditional synthesis based on the generated to produce valid hard anchor-positive pair .
5.3 Conditional Synthesis
After piecewise linearly manipulating anchor-positive pairs, in order to ensure the generated pairs are still in the same category domain as the original samples, the recent method requires label consistency to avoid generating invalid embeddings [20]. However, the simple constraint may result in a mode collapse problem, which means the generated samples suffering from insufficient diversity, as shown in Fig 5. Thus except for the label-preserving requirement, we try to enhance the relationship between the original sample and the generated sample by adding an extra restriction. Specifically, we require that the generated samples based on have the same distribution as the original samples . Unlike an unconditional generative adversarial network, both the generator and discriminator consider the information of the linear-manipulated anchor-positive pair . To summarize, we encourage the one-to-one relationship between the original sample and the generated sample for our conditional synthesis, and thus the diversity is ensured.
When passed , they are mapped to . On the one hand, we synthesize anchor-positive pairs that are further apart based on ; On the other hand, no matter where the input of lies in its category space, the output of is located in the same category space as the original one by the constraint of label consistency. Our conditional synthesis is achieved by training with the following loss function:
(12) |
where is the balance factor between softmax and adversarial loss; the first term makes sure the generated embeddings have the same labels with the original ones; the second term is adversarial loss with discriminator which aims to let the generated embeddings look real, not the fake ones; the third term guarantees the generated embeddings can maintain the same hard level as the input embedings. In (12), the first term is denoted as:
(13) |
where denotes embeddings from and is the ground truth label of ; the denotes the softmax loss; is a shared fully connected layer which is used to classify the generated pair. In (12), the second term is denoted as:
(14) |
where denotes embeddings from , denotes embeddings from and ”1” represents the real samples. The discriminator takes the result of concatenating and along the last dimension as the input. This adversarial loss is used to fool the discriminator by generating real-like samples; the third term in (12) is reconstruction loss between and , which is calculated as:
(15) |
With the reconstruction loss , the relative hardness of anchor-positive synthetic pairs in triplets will be guaranteed. In this way, our generative loss enables generated anchor-positive pairs to have a greater separation distance while remaining within their original category space.
The discriminator is a two-category classifier for identifying whether a given embedding is a real one or a generated one. The training loss function for is formulated as:
(16) |
where , and denote embeddings from , and , respectively. and , the input embedding of the discriminator , are concatenated of and corresponding embedding in the last dimension of the same labeled instance. ”1” and ”0” denote the real sample and the fake sample respectively.
After the adversarial training of , our hard anchor-positive pairs have met the requirements of label consistency. At the same time, conditional discriminator ensures that the generated pairs do not have mode collapse with random generation. Next, we will use the hard anchor-positive pair to generate the hard negative, and produce the final hard samples .
5.4 Hard Triplet Synthesis
We propose a new hard triplet generator to generate hard samples. Generator is able to map its input and to . While generating a negative sample, the hard negative sample is not only related to the information of negative sample itself, but also to the anchor-positive pair in the triplet. Therefore the inputs of are negative sample and hard anchor-positive pair generated in Stage 1. For our hard triplet synthesis, we propose the loss function to train as follows:
(17) |
where and are the balancing factors. The overall objective loss function of is composed of four parts: the adaptive reverse triplet loss (ART-loss) , reconstruction loss , softmax loss and adversarial loss . The ART-loss is used to generate the hard negative; the reconstruction loss is applied to keep hard anchor-positive pair from being affected by reverse triplet loss; the softmax loss is used to ensure label consistency for the generated samples; adversarial loss with discriminator is performed to generate samples like the real ones. These four terms are detailed in the following.
The proposed ART-loss is defined as follows:
(18) |
The proposed ART-loss is obtained from the triplet loss by reversing the sign in front of the first two terms in (1). The purpose of ART-loss is to make the distance between and as small as possible until it is less than the distance between and with a parameter . When satisfies this condition, it will be a hard negative sample. In this paper, we propose to use an adaptive parameter which increases with the training of the network. We impose more stringent restrictions on reverse triplet loss with larger when the performance of is getting better. Thus, we can gradually generate harder and harder negative samples. We set as a parameter that changes with as:
(19) |
where and are constant parameters. As trains better, the will get smaller, and will increase adaptively to enhance the difficulty of hard negative.
The reconstruction loss aims to keep the synthetic anchor-positive pair with a consistent hard-level by decreasing the distances between and , which is denoted as:
(20) |
The softmax loss makes the generated embeddings have the same labels with the original samples, which is calculated as:
(21) |
where denotes embeddings from , and denotes the ground truth labels of .
The adversarial loss with discriminator is
(22) |
Different with the previous adversarial loss, such as (14), we use the ground truth label to represent the “real” samples. Thus the adversarial loss (22) is used to fool the discriminator by classifying these generated embeddings as the real samples with ground truth . Actually, our adopted discriminator is a -class classifier where denotes the number of ground truth categories for the real samples and the left one class represents the generated samples. When an embedding is given, can discriminate it into the categories [31] and thus the real-like sample generation and label consistency are achieved at the same time. The training loss for is denoted as:
(23) |
where denotes embeddings from or , denotes the ground truth labels of , and denotes embeddings from .
6 Experiment
In this section, extensive experiments are performed to demonstrate the superior performance of our proposed hard sample generation. Besides, an ablation study is further developed to show the contributions of each component of our proposed algorithm.
6.1 Dataset
We evaluated our method under the three datasets below. In order to better compare with other methods, we split the datasets according to [20][26][32][33], and the training set and test set contain image classes with no intersection.
-
1.
The CUB-200-2011 dataset [34] consists of 200 bird species with 11,788 images. We use the first 100 species (5,864 images) for training and the rest 100 species (5,924 images) for testing.
-
2.
The Cars196 dataset [35] consists of 196 car makes and models with 16,185 images. We use the first 98 models (8,054 images) for training and the rest 100 models (8,131 images) for testing.
-
3.
The Stanford Online Products dataset (SOP) [32] consists of 22,634 online products from eBay.com with 120,053 images. We use the first 11,318 products (59,551 images) for training and the rest 11,316 products (60,502 images) for testing.
6.2 Implementation
Configuration. The proposed method is implemented with Python3.7 and PyTorch 1.6.0, using the GoogleNet [36] and ResNet50 [37] architectures, pre-trained on ImageNet ILSVRC dataset [38]. All experiments are performed on individual NVIDIA Tesla T4 GPUs. We trained networks with standard back-propagation, which is performed by Adam optimizer with 4e-4 weight decay. We set the initial learning rate 1e-5 for the feature extractor, 1e-3 for the classifier, 1e-4 for the discriminators, and 1e-3 for the generators. Each generator is composed of two fully connected layers with dimensions 128 and 512. Note that all output embedding vectors of the feature extractor and generators are under normalization. Each training is run over 150 epochs for CUB200-2011/CARS196 and 100 epochs for Stanford Online Products. For a fair comparison with previous methods on deep metric learning, we used GoogLeNet [36] architecture as the CNN feature extractor with output embedding size of 512 for all the three datasets similar to [26] and [20]. While training, we set the batch size to 128, and all the training images are resized to . During the training process, we will update all discriminators, generators, and the embedding extraction network according to Algorithm 1. We set triplet margin and set the the maximum value of reverse triple margin as well. The other related parameters of this work are summarized in Table 2.
Cub200 | 0.2 | 0.8 | 0.3 | 0.5 | 0.3 | 0.5 |
---|---|---|---|---|---|---|
Cars196 | 0.2 | 0.8 | 0.3 | 0.5 | 0.3 | 0.25 |
SOP | 0.2 | 0.8 | 0.3 | 0.5 | 0.3 | 0.2 |
Training Flow. We alternately train and the other models, and mini-batch SGD is applied in the training process. Specifically, we first initialize all the models and then pre-train model . After that, we will iteratively train all the models for times. In each epoch, these models are updated for times. For a specific batch, embeddings are first produced by , and then and can be obtained by using PLM and . Then model can be updated with (16). After that, the generator are updated with (12) accordingly. However, for the training of and , we will use the samples produced by the latest generative networks. For this reason, we will re-extract embeddings after the model updating of Stage 1 for the training of and in Stage 2. Then we update them with (23) and (17) accordingly. Finally, we update model with the re-extracted , and the original by using (5). With the above training flow, the final embedding model and the other models related to hard sample generation are obtained at the same time. When our network training flow is over, there is no additional computational effort involved in the final feature extraction for image retrieval. We summarize the whole embedding training flow as Algorithm 1.
Input: model , , , , , images , labels ,
Parameter: ,
Output: trained
6.3 Evaluation
We designed experiments to prove the effectiveness of our hard-sample generation.
Evaluation Criteria. We report the performance for both retrieval and clustering tasks. The mAP is widely used in state-of-the-art image retrieval works and it reveals the position-sensitive ranking precision. The definition of mAP is denoted as:
(24) |
where is the query index and is the number of total queries [3]. is the number of relevant images corresponding to the query, is the rank and is the precision at the cut-off rank of . The employed Recall@K metric is determined by the existence of at least one correct retrieved sample in the K nearest neighbors [26].
For the clustering task, we employed NMI and as performance metrics. The normalized mutual information (NMI) [32] is defined by the ratio of the mutual information of clusters and ground truth labels and the arithmetic mean of their entropy: , where is a set of clusters and is the ground truth classes. Here and denotes mutual information and entropy respectively. score is deined as the harmonic mean of precision and recall [32].
Qualitative Analysis. We visualize our image retrieval performance in several selected cases and visually show the advantage of our algorithm. Besides, we depict the learned embeddings for different datasets and qualitatively verify the effectiveness of the proposed scheme.
6.4 Objective Comparison
Method | R@1 | R@2 | R@4 | R@8 | NMI | F1 | mAP |
---|---|---|---|---|---|---|---|
Triplet[26] | 35.9 | 47.7 | 59.1 | 70.0 | 49.8 | 15.0 | 19.2 |
MAC | 40.3 | 51.9 | 63.6 | 74.1 | - | - | 20.1 |
R-MAC | 41.7 | 53.7 | 66.3 | 76.3 | - | - | 20.8 |
GeM | 42.2 | 54.3 | 67.1 | 77.4 | - | - | 21.4 |
DAML[20] | 37.6 | 49.3 | 61.3 | 74.4 | 51.3 | 17.6 | 21.7 |
HDML[26] | 43.6 | 55.8 | 67.7 | 78.3 | 55.1 | 21.9 | 22.8 |
SS[27] | 51.4 | 63.0 | 74.4 | 84.1 | 59.6 | 26.2 | - |
OURS128 | 55.3 | 66.6 | 77.3 | 86.0 | 62.6 | 31.5 | 26.8 |
OURS | 57.0 | 68.8 | 79.2 | 86.7 | 64.1 | 33.0 | 27.9 |
Method | R@1 | R@2 | R@4 | R@8 | NMI | F1 | mAP |
---|---|---|---|---|---|---|---|
Triplet[26] | 45.1 | 57.4 | 69.7 | 79.2 | 52.9 | 17.9 | 20.8 |
MAC | 56.5 | 69.3 | 79.1 | 87.2 | - | - | 17.0 |
R-MAC | 60.0 | 71.8 | 81.1 | 88.2 | - | - | 20.5 |
GeM | 60.4 | 71.9 | 81.2 | 88.1 | - | - | 21.5 |
DAML[20] | 60.6 | 72.5 | 82.5 | 89.9 | 56.5 | 22.9 | 20.7 |
HDML[26] | 61.0 | 72.6 | 80.7 | 88.5 | 59.4 | 27.2 | 22.5 |
SS[27] | 69.7 | 78.7 | 86.1 | 91.4 | 62.4 | 31.8 | - |
OURS128 | 79.3 | 86.9 | 92.2 | 95.7 | 65.5 | 35.4 | 29.6 |
OURS | 82.1 | 88.7 | 93.1 | 96.0 | 66.3 | 35.1 | 31.1 |
Method | R@1 | R@10 | R@100 | NMI | F1 | mAP |
---|---|---|---|---|---|---|
Triplet[26] | 53.9 | 72.1 | 85.7 | 20.2 | 15.0 | 29.7 |
MAC | 54.2 | 72.5 | 85.9 | - | - | 30.5 |
R-MAC | 55.4 | 73.1 | 86.1 | - | - | 31.3 |
GeM | 56.7 | 74.3 | 86.7 | - | - | 32.8 |
DAML[20] | 58.1 | 75.0 | 88.0 | 87.1 | 22.3 | 34.9 |
HDML[26] | 58.5 | 75.5 | 88.3 | 87.2 | 22.5 | 35.7 |
SS[27] | 65.7 | 81.4 | 91.7 | 88.9 | 30.6 | - |
OURS128 | 66.1 | 80.8 | 90.8 | 87.6 | 25.4 | 33.0 |
OURS | 67.7 | 82.2 | 91.6 | 87.7 | 26.1 | 34.4 |

We conduct full comparisons between our method and the other image retrieval algorithms. The involved strategies include deep pooling features and the recent deep embeddings with hard generation. For a fair comparison, all the involved algorithms are implemented with triplet loss architecture.
Typical Image Retrieval Methods. To comprehensively demonstrate the superiority of our proposed method over existing methods in the retrieval task, we compared our scheme with several typical baseline image retrieval methods built based on deep pooling features, including the average-pooled convolutional features under triplet architecture (Triplet), maximum activations of convolutions (MAC) [7], regional MAC (R-MAC) and Generalized-Mean (GeM) pooling [17]. For a fair comparison, we evaluated all the methods mentioned above using the same CNN model and trained the deep features with triplet loss.
Deep Embeddings with Hard Generation. Our proposed two-stage hard sample generation method is a novel generation method that enables the network to synthesize informative triplet more efficiently, leading to boosts in performance. The state-of-the-art deep embeddings with hard generation are included in this work for comparisons: the hard-negative generation method DAML [20], HDML [26] and symmetrical synthesis (SS) [27].
Comparison Analysis. Table 3, 4, and 5 show the quantitative results of our method and the comparison methods on the CUB-200-2011, Cars196, and SOP datasets. The feature dimension is 512 by default in the three tables above, except for special cases where the dimension is marked with superscript. Bold numbers indicate the best results and blue color represents the second-best performance. As we can see from the tables, our method achieves the best Recall@K and mAP on the CUB-200-2011, Cars196 datasets (Table 3 and Table 4), while obtaining at least the second-best performance on the Stanford dataset (Table 5). It should be noted that the image retrieval accuracy gain (Table 5) introduced by our method on the Stanford Online Products dataset is smaller than the improvement on the other two datasets (Table 3 and Table 4). The possible reason may come from the fact that the Stanford Online Products dataset [32] consists of more diverse images than the other two datasets. The existing data already contains many hard samples and the left improving space for hard sample generation on Stanford Online Products dataset is limited. For studying the performance of our method with varying embedding sizes, we conducted additional experiments on all three using a small embedding dimension. We can find that the performance of our proposed method can outperform the previous schemes even if the feature dimension is reduced to 128.
Compared with the image retrieval schemes with deep pooling features (MAC, R-MAC, GeM), the methods with hard sample generation (DAML, HDML, SS, and our scheme) produce better results. Compared with the DAML, HDML, and SS which improve performance by generating negative samples, our scheme can further boost the retrieval performance. In addition, our scheme can also obtain at least the second-best performance in the result of F1 and NMI. In summary, our proposed two-stage hard sample generation method is highly competitive in both retrieval and clustering tasks.
6.5 Combine Hard Generation and Hard Mining
The motivation of our hard sample generation approach is to synthesize difficult sample pairs from a large number of simple samples to obtain valid information. In contrast, existing hard sample mining strategies directly select the most informative sample pairs from real samples for training. To further explore whether the generated hard samples and real hard samples are complementary, we design to combine the generated samples with the mined samples for training. We perform experiments to analyze the effect of combining our proposed method with a variety of hard sample mining strategies.
Hard Sample Mining Methods. Triplet loss needs to mine training tuples from the available mini-batch. In our study, all of our experiments are based on triplet architecture, so we choose the four most representative tuple mining strategies for our experiments. These strategies include random tuple mining (R) [12], semihard triplet mining (Semi)[25], softhard triplet mining (Soft) [24] and distance-weighted tuple mining (D) [14]. In this experiment, our proposed algorithm combined with random tuple mining fully equivalent to the two-stage hard sample generation method (THSG), which can be regarded as the baseline of our methods.
Method | R@1 | R@2 | R@4 | R@8 | NMI | F1 | MAP |
---|---|---|---|---|---|---|---|
Triplet(R) | 58.0 | 70.1 | 80.3 | 86.0 | 64.4 | 32.6 | 29.9 |
Triplet(Semi) | 59.2 | 70.9 | 80.8 | 86.4 | 64.8 | 33.3 | 30.8 |
Triplet(Soft) | 60.3 | 72.1 | 81.8 | 87.2 | 65.9 | 34.6 | 31.6 |
Triplet(D) | 62.5 | 72.9 | 82.0 | 88.7 | 66.3 | 34.8 | 32.3 |
THSG | 61.3 | 72.8 | 82.3 | 89.1 | 65.6 | 33.8 | 31.5 |
THSG(Semi) | 61.3 | 72.5 | 81.8 | 88.2 | 64.9 | 32.7 | 31.8 |
THSG(Soft) | 62.4 | 74.0 | 82.8 | 89.2 | 66.3 | 34.5 | 32.4 |
THSG(D) | 63.2 | 74.6 | 83.5 | 89.5 | 66.7 | 35.9 | 33.0 |
Implementation Details. In order to warrant unbiased comparability in Table 6, 7, and 8, our training protocol follows settings of [39]. The difference from the previous experimental setting is that we resize images to and utilize a ResNet50 architecture with frozen Batch-Normalization layers and embedding dimensionality 128 . When combining two different hard triplet for training, we only replace the original deep distance metric loss with the hard mining deep distance metric loss and keep the other training steps of ours method unchanged. Therefore, the final deep embedding learning (4) is rewritten as:
(25) |
Analysis of Results. We can find that experimental results in all evaluation criteria are further boosted by combining our method with a hard sample mining. Both in the retrieval task and in the clustering task, our method combined with hard sample mining results in better performance compared to the individual method. The performance improvement implies there is complementary information in the mined and generated hard samples. In particular, our generation method combined with simple mining strategies, such as random tuple mining or semihard triplet mining, yields an even more obvious improvement in results. Thus, our method does generate hard samples with information that is difficult to mine in the original data.
Method | R@1 | R@2 | R@4 | R@8 | NMI | F1 | MAP |
---|---|---|---|---|---|---|---|
Triplet(R) | 67.8 | 78.2 | 85.8 | 91.2 | 60.1 | 27.2 | 26.9 |
Triplet(Semi) | 70.6 | 80.1 | 86.7 | 91.5 | 61.3 | 29.7 | 28.8 |
Triplet(Soft) | 76.9 | 84.6 | 90.3 | 94.1 | 62.8 | 30.6 | 31.3 |
Triplet(D) | 77.7 | 85.7 | 90.8 | 94.0 | 64.4 | 33.1 | 31.9 |
THSG | 80.2 | 87.2 | 91.7 | 95.0 | 66.2 | 34.8 | 31.9 |
THSG(Semi) | 79.8 | 86.4 | 91.5 | 95.0 | 66.0 | 35.4 | 34.2 |
THSG(Soft) | 80.3 | 87.0 | 91.8 | 95.2 | 66.4 | 35.6 | 34.2 |
THSG(D) | 81.6 | 88.4 | 92.5 | 95.6 | 67.5 | 36.7 | 34.6 |
Method | R@1 | R@10 | R@100 | NMI | F1 | MAP |
---|---|---|---|---|---|---|
Triplet(R) | 71.0 | 85.3 | 93.6 | 88.8 | 30.9 | 38.7 |
Triplet(Semi) | 76.2 | 88.8 | 95.4 | 89.9 | 36.2 | 44.3 |
Triplet(Soft) | 76.3 | 89.1 | 95.6 | 89.8 | 35.8 | 44.1 |
Triplet(D) | 77.8 | 90.2 | 95.9 | 90.1 | 36.9 | 45.8 |
THSG | 76.2 | 88.7 | 95.2 | 89.6 | 34.7 | 43.4 |
THSG(Semi) | 78.0 | 89.9 | 95.8 | 90.1 | 37.3 | 45.9 |
THSG(Soft) | 77.7 | 89.8 | 95.7 | 90.0 | 36.6 | 45.5 |
THSG(D) | 78.5 | 90.3 | 95.9 | 90.1 | 37.1 | 46.3 |
6.6 Subjective Comparison
Visualized Retrieval Cases. We selected several queries from the utilized dataset and depicted the retrieval results in Fig. 6. The depicted methods are our work, the HDML of work [26] and the basic triplet scheme. The visualized results show that our method achieves better search accuracy. It should be noted that for some cases, such as the last two queries in Fig. 6, almost all the methods have many error results.

Embedding Visualization. We visualized the learned embeddings for the CUB-200-2011 and Cars196 datasets by using t-SNE [40], as denoted by Fig. 7 and Fig. 8. In each figure, several selected specific regions are enlarged to highlight the representative classes for easy observation. In each specific region, although the images in the same class suffer from large variations in poses, colors, and backgrounds, our proposed method still can group similar objects. The visualization results denote our learned embeddings have strong representation ability and thus can achieve better retrieval performance in an intuitive manner.

6.7 Ablation Study
We conducted the ablation study of the proposed method on the Cars196 dataset. In Fig. 10, we show the results based on the Recall@1 criterion of different model settings, including our proposed method (OURS), the proposed method combined with distance-weighted hard mining (OURS∗), the proposed method without using the second stage of synthesis (w/o ), the proposed method without using the second stage of synthesis and category loss (w/o ( & )), and the proposed method without both the generated hard sample loss and the category loss (w/o ( & )), which is the baseline of using triplet architecture.
We can find that the Recall@1 is further improved (80.2% 81.6%) by combining our method with a hard sample mining. The performance improvement implies there is complementary information in the mined and generated hard samples. We remove the Generator and only use the first stage of generation to synthesize the anchor-positive pairs in hard triplets, then some performance loss is introduced (80.2% 76.5%), which reveals the importance of hard sample generation which can extract the potential information from simple samples for embedding training. By further removing category loss , the performance suffers an obvious degradation again (76.5% 71.9%), which tells that the category loss still can provide useful information under the metric learning architecture. Moreover, because we train the embedding model and the other hard sample generation models alternately, the absence of will ruin the original obtained sample and in turn impairs the generation of hard samples. Thus, the category loss also plays a great role in our scheme. When the generated hard sample loss is totally removed (w/o ), we see the performance decreases a lot (71.9% 67.8%), which verifies the effectiveness of the proposed stronger conditional synthesis scheme.
To further verify the contributions of each component in our method, we perform the similar experiment on all quantitative criteria including Recall@K, NMI, F1, and mAP, which are depicted as Fig. 10. The quantitative results in Fig. 10 also confirm the analysis as presented above, and the specific performance reduction brought to the model can be observed when each corresponding component is removed.


7 Conclusion
The existing mining-based triplet constructing methods directly learn the deep embeddings based on mining the hard samples, which will ignore the potential information of the easy samples. Although the recent hard sample generation schemes have tried to dig the potential information of the easy samples with the one-stage adversarial model, it is ignored the fact that the positives and negatives have different distributions and characteristics. In this study, we designed a two-stage hard sample generation scheme to achieve better embedding learning.
We found that our proposed scheme achieves the best overall image retrieval and clustering performance on three large datasets (see Table 3, 4, and 5), indicating the effectiveness of the two-stage hard sample generation scheme. Specifically, our proposed method can conduct better results than the existing metric learning with hard sample generation. We also found that both the anchor-positive pairs generated by the conditional synthesis in the first stage and the negative samples generated in the second stage are crucial. Besides, by combining the generated hard samples and real hard samples selected by hard sample mining strategies, the performance can be further boosted (see Fig. 10 and Fig. 10).
Our results provide compelling performance for image retrieval and clustering tasks through the proposed hard sample generation scheme. In this paper, the usage of generated samples in our experiments is limited to the triplet metric learning architecture. In the future, we can consider exploring more efficient usage strategies for generated samples and we will conduct more hard sample generation research on the other loss architectures, such as the n-pair loss structure. In addition, there is a general problem in current hard sample generation schemes is that the generated samples are discarded after being used once. In the future, if schemes can be designed to efficiently reuse high-quality generated samples, it is possible to save computational resources and further accelerate network training.
8 Acknowledgement
This work was supported in part by the 111 project (NO.B17007), Shandong Key Laboratory of Intelligent Buildings Technology (Grant No.SDIBT202006).
References
- [1] Z. Li, J. Tang, Weakly supervised deep metric learning for community-contributed image retrieval, IEEE Transactions on Multimedia 17 (11) (2015) 1989–1999.
- [2] B. Girod, V. Chandrasekhar, D. M. Chen, N.-M. Cheung, R. Grzeszczuk, Y. Reznik, G. Takacs, S. S. Tsai, R. Vedantham, Mobile visual search, IEEE signal processing magazine 28 (4) (2011) 61–76.
- [3] C. Zhu, H. Dong, S. Zhang, Feature fusion for image retrieval with adaptive bitrate allocation and hard negative mining, IEEE Access 7 (2019) 161858–161870.
- [4] D. G. Lowe, Distinctive image features from scale-invariant keypoints, International journal of computer vision 60 (2) (2004) 91–110.
- [5] F. Perronnin, Y. Liu, J. Sánchez, H. Poirier, Large-scale image retrieval with compressed fisher vectors, in: 2010 IEEE Computer Society Conference on Computer Vision and Pattern Recognition, IEEE, 2010, pp. 3384–3391.
- [6] H. Jegou, F. Perronnin, M. Douze, J. Sánchez, P. Perez, C. Schmid, Aggregating local image descriptors into compact codes, IEEE transactions on pattern analysis and machine intelligence 34 (9) (2011) 1704–1716.
- [7] H. Azizpour, A. Sharif Razavian, J. Sullivan, A. Maki, S. Carlsson, From generic to specific deep representations for visual recognition, in: Proceedings of the IEEE conference on computer vision and pattern recognition workshops, 2015, pp. 36–45.
- [8] A. Babenko, V. Lempitsky, Aggregating local deep features for image retrieval, in: Proceedings of the IEEE international conference on computer vision, 2015, pp. 1269–1277.
- [9] G. Tolias, R. Sicre, H. Jégou, Particular object retrieval with integral max-pooling of cnn activations, arXiv preprint arXiv:1511.05879.
- [10] R. Arandjelovic, P. Gronat, A. Torii, T. Pajdla, J. Sivic, Netvlad: Cnn architecture for weakly supervised place recognition, in: Proceedings of the IEEE conference on computer vision and pattern recognition, 2016, pp. 5297–5307.
- [11] F. Radenović, G. Tolias, O. Chum, Cnn image retrieval learns from bow: Unsupervised fine-tuning with hard examples, in: European conference on computer vision, Springer, 2016, pp. 3–20.
- [12] J. Hu, J. Lu, Y.-P. Tan, Discriminative deep metric learning for face verification in the wild, in: Proceedings of the IEEE conference on computer vision and pattern recognition, 2014, pp. 1875–1882.
- [13] E. Hoffer, N. Ailon, Deep metric learning using triplet network, in: International Workshop on Similarity-Based Pattern Recognition, Springer, 2015, pp. 84–92.
- [14] C.-Y. Wu, R. Manmatha, A. J. Smola, P. Krahenbuhl, Sampling matters in deep embedding learning, in: Proceedings of the IEEE International Conference on Computer Vision, 2017, pp. 2840–2848.
- [15] Y. Yuan, K. Yang, C. Zhang, Hard-aware deeply cascaded embedding, in: Proceedings of the IEEE international conference on computer vision, 2017, pp. 814–823.
- [16] F. Radenović, A. Iscen, G. Tolias, Y. Avrithis, O. Chum, Revisiting oxford and paris: Large-scale image retrieval benchmarking, in: Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, 2018, pp. 5706–5715.
- [17] F. Radenović, G. Tolias, O. Chum, Fine-tuning cnn image retrieval with no human annotation, IEEE transactions on pattern analysis and machine intelligence 41 (7) (2018) 1655–1668.
- [18] B. Yu, T. Liu, M. Gong, C. Ding, D. Tao, Correcting the triplet selection bias for triplet loss, in: Proceedings of the European Conference on Computer Vision (ECCV), 2018, pp. 71–87.
- [19] Y. Zhao, Z. Jin, G.-j. Qi, H. Lu, X.-s. Hua, An adversarial approach to hard triplet generation, in: Proceedings of the European Conference on Computer Vision (ECCV), 2018, pp. 501–517.
- [20] Y. Duan, W. Zheng, X. Lin, J. Lu, J. Zhou, Deep adversarial metric learning, in: Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, 2018, pp. 2780–2789.
- [21] Y. Guo, D. An, X. Qi, Z. Luo, S.-T. Yau, X. Gu, et al., Mode collapse and regularity of optimal transportation maps, arXiv preprint arXiv:1902.02934.
- [22] Y. Cui, F. Zhou, Y. Lin, S. Belongie, Fine-grained categorization and dataset bootstrapping using deep metric learning with humans in the loop, in: Proceedings of the IEEE conference on computer vision and pattern recognition, 2016, pp. 1153–1162.
- [23] A. Hermans, L. Beyer, B. Leibe, In defense of the triplet loss for person re-identification, arXiv preprint arXiv:1703.07737.
- [24] R. Yu, Z. Dou, S. Bai, Z. Zhang, Y. Xu, X. Bai, Hard-aware point-to-set deep metric for person re-identification, in: Proceedings of the European conference on computer vision (ECCV), 2018, pp. 188–204.
- [25] F. Schroff, D. Kalenichenko, J. Philbin, Facenet: A unified embedding for face recognition and clustering, in: Proceedings of the IEEE conference on computer vision and pattern recognition, 2015, pp. 815–823.
- [26] W. Zheng, Z. Chen, J. Lu, J. Zhou, Hardness-aware deep metric learning, in: Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, 2019, pp. 72–81.
- [27] G. Gu, B. Ko, Symmetrical synthesis for deep metric learning, arXiv preprint arXiv:2001.11658.
- [28] K. Sohn, Improved deep metric learning with multi-class n-pair loss objective, in: Advances in Neural Information Processing Systems, 2016, pp. 1857–1865.
- [29] S. Chopra, R. Hadsell, Y. LeCun, et al., Learning a similarity metric discriminatively, with application to face verification, in: CVPR (1), 2005, pp. 539–546.
- [30] C. Zhang, S. Bengio, M. Hardt, B. Recht, O. Vinyals, Understanding deep learning requires rethinking generalization, arXiv preprint arXiv:1611.03530.
- [31] M. Mirza, S. Osindero, Conditional generative adversarial nets, arXiv preprint arXiv:1411.1784.
- [32] H. Oh Song, Y. Xiang, S. Jegelka, S. Savarese, Deep metric learning via lifted structured feature embedding, in: Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, 2016, pp. 4004–4012.
- [33] H. Oh Song, S. Jegelka, V. Rathod, K. Murphy, Deep metric learning via facility location, in: Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, 2017, pp. 5382–5390.
- [34] C. Wah, S. Branson, P. Welinder, P. Perona, S. Belongie, The caltech-ucsd birds-200-2011 dataset.
- [35] J. Krause, M. Stark, J. Deng, L. Fei-Fei, 3d object representations for fine-grained categorization, in: Proceedings of the IEEE International Conference on Computer Vision Workshops, 2013, pp. 554–561.
- [36] C. Szegedy, W. Liu, Y. Jia, P. Sermanet, S. Reed, D. Anguelov, D. Erhan, V. Vanhoucke, A. Rabinovich, Going deeper with convolutions, in: Proceedings of the IEEE conference on computer vision and pattern recognition, 2015, pp. 1–9.
- [37] K. He, X. Zhang, S. Ren, J. Sun, Deep residual learning for image recognition, in: Proceedings of the IEEE conference on computer vision and pattern recognition, 2016, pp. 770–778.
- [38] O. Russakovsky, J. Deng, H. Su, J. Krause, S. Satheesh, S. Ma, Z. Huang, A. Karpathy, A. Khosla, M. Bernstein, et al., Imagenet large scale visual recognition challenge, International journal of computer vision 115 (3) (2015) 211–252.
- [39] K. Roth, T. Milbich, S. Sinha, P. Gupta, B. Ommer, J. P. Cohen, Revisiting training strategies and generalization performance in deep metric learning, arXiv preprint arXiv:2002.08473.
- [40] L. Van Der Maaten, Accelerating t-sne using tree-based algorithms, The Journal of Machine Learning Research 15 (1) (2014) 3221–3245.