A S IMPLE AND E FFECTIVE F RAMEWORK FOR P AIRWISE D EEP M ETRIC L EARNING
Anonymous authors
Paper under double-blind review
A BSTRACT
Deep metric learning (DML) has received much attention in deep learning due to its wide applications in computer vision.
Previous studies have focused on designing complicated losses and hard example mining methods, which are mostly heuristic and lack of theoretical understanding.
In this paper, we cast DML as a simple pairwise binary classiﬁcation problem that classiﬁes a pair of examples as similar or dissimilar.
It identiﬁes the most critical issue in this problem— imbalanced data pairs.
To tackle this issue, we propose a simple and effective framework to sample pairs in a batch of data for updating the model.
The key to this framework is to deﬁne a robust loss for all pairs over a mini-batch of data, which is formulated by distributionally robust optimization.
The ﬂexibility in constructing the uncertainty decision set of the dual variable allows us to recover state-of-the-art complicated losses and also to induce novel variants.
Empirical studies on several benchmark data sets demonstrate that our simple and effective method outperforms the state-of-the-art results.
I NTRODUCTION
Metric Learning aims to learn a metric to measure the distance between examples that captures certain notion of human-deﬁned similarity between examples.
Deep metric learning (DML) has emerged as an effective approach for learning a metric by training a deep neural network.
Simply speaking, a deep neural network can induce new feature embedding of examples and it is trained in such a way that the Euclidean distance between the induced feature embeddings of two similar examples shall be small and that between the induced feature embeddings of two dissimilar pairs shall be large.
DML has been widely used in many tasks such as face recognition (Fan et al.
(2017)), image retrieval (Chen & Deng (2019)), and classiﬁcation (Qian et al.
; Li et al.
However, unlike training a deep neural network by minimizing the classiﬁcation error, training a deep neural network for metric learning is notoriously more difﬁcult (Qian et al.
Wang et al.
Many studies have attempted to address this challenge by focusing on several issues.
The ﬁrst issue is how to deﬁne a loss function over pairs of examples.
A variety of loss functions have been proposed such as contrastive loss (Hadsell et al.
(2006)), binomial deviance loss (Yi et al.)
, margin loss (Wu et al.
(2017)), lifted-structure (LS) loss (Oh Song et al.
, N-pair loss (Sohn (2016)), triplet loss (Schroff et al. (2015)), multi-similarity (MS) loss (Wang et al.
The major difference between these pair-based losses lies at how the pairs interact with each other in a mini-batch.
In simple pairwise loss such as binomial deviance loss, contrastive loss, and margin loss, pairs are regarded as independent of each other.
In triplet loss, a positive pair only interacts with one negative pair.
In N-pair loss, a positive pair interacts with all negative pairs.
In LS loss and
MS loss, a positive pair interacts with all positive pairs and all negative pairs.
The trend is that the loss functions become increasingly complicated but are difﬁcult to understand.
In parallel with the loss function, how to select informative pairs to construct the loss function has also received great attention.
Traditional approaches that construct pairs or triplets over all examples before training suffer from prohibitive O ( N 2 ) or O ( N 3 ) sample complexity, where N is the total number of examples.
To tackle this issue, constructing pairs within a mini-batch is widely used in practice.
Although it helps mitigate the computational and storage burden, slow convergence and model degeneration with inferior performance still commonly exist when using all pairs in a minibatch to update the model.
To combat this issue, various pair mining methods have been proposed to complement the design of loss function, such as hard (semi-hard) mining for triplet loss (Schroff et al.
, distance weighted sampling (DWS) for margin loss (Wu et al.
MS sampling for MS loss (Wang et al.
These sampling methods usually keep all positive (similar) pairs and select roughly the same order of negative (dissimilar) pairs according to some criterion.
Regardless of these great efforts, existing studies either fail to explain the most fundamental problem in DML or fail to propose most effective approach towards addressing the fundamental challenge.
It is evident that the loss functions become more and more complicated.
But it is still unclear why these complicated losses are effective and how does the pair mining methods affect the overall loss within a mini-batch.
In this paper, we propose a novel effective solution to DML and bring new insights from the perspective of learning theory that can guide the discovery of new methods.
Our philosophy is simple: casting the problem of DML into a simple pairwise classiﬁcation problem and focusing on addressing the most critical issue, i.e., the sheer imbalance between positive pairs and negative pairs.
To this end, we employ simple pairwise loss functions (e.g., margin loss, binomial deviance loss) and propose a ﬂexible distributionally robust optimization (DRO) framework for deﬁning a robust loss over pairs within a mini-batch.
The idea of DRO is to assign different weights to different pairs that are optimized by maximizing the weighted loss over an uncertainty set for the distributional variable.
The model is updated by stochastic gradient descent with stochastic gradients computed based on the sampled pairs according to the found optimal distributional variable.
The DRO framework allows us to (i) connect to advanced learning theories that already exhibit their power for imbalanced data, hence providing theoretical explanation for the proposed framework; (ii) to unify pair sampling and loss-based methods to provide a uniﬁed perspective for existing solutions; (iii) to induce simple and effective methods for DML, leading to state-of-the-art performance on several benchmark datasets.
The contributions of our work are summarized as follows:
• We propose a general solution framework for DML, i.e., by deﬁning a robust overall loss based on the DRO formulation and updating the model based on pairs sampled according to the optimized sampling probabilities.
We provide theoretical justiﬁcation of the proposed framework from the perspective of advanced learning theories.
• We show that the general DRO framework can recover existing methods based on complicated pair-based losses: LS loss and MS loss by specifying different uncertainty sets for the distributional variable in DRO.
It veriﬁes that our method is general and brings a uniﬁed perspective regarding pair sampling and complicated loss over all pairs within a batch.
• We also propose simple solutions under the general DRO framework for tackling DML.
Experimental results show that our proposed variants of DRO framework outperform state-of-the-art methods on several benchmark datasets.
R ELATED W ORK
Loss Design.
The loss function is usually deﬁned over the similarities or distances between the induced feature embeddings of pairs.
There are simple pairwise losses that simply regard DML as binary classiﬁcation problem using averaged loss over pairs, e.g., contrastive loss, binomial loss, margin loss.
It is notable that the binomial loss proposed in (Yi et al.) assigns asymmetric weights for positive and negative pairs, which can mitigate the issue of imbalance to certain degree.
The principal of the newly designed complicated pair-based losses can be summarized as heuristically discovering speciﬁc kinds of relevant information between groups of pairs to boost the training.
The key difference between these complicated losses lies at how to group the pairs.
N-pair loss put one positive pair and all negative pairs together, Lifted-structure loss and MS-loss group all positive pairs together and all negative pairs together for each example.
In contrast, our DRO framework employs simple pairwise loss but induce complicated overall loss in a systematic and interpretable way.
Pair Mining/Pair Weighting.
Wu et al.
(2017) points out that pair mining plays an important role in distance metric learning.
Different pair mining methods have been proposed, including semihard sampling for triplet loss, distance weighted sampling (DWS) for margin loss, MS mining for
MS losses.
These pair mining methods aim to select the hard positive and negative pairs for each anchor.
For instance, Schroff et al.
(2015) selects the hard negative pairs whose distance is smaller than that between the positive pairs in triplets, Shi et al.
(2016) selects the hardest positive pair whose distance is smaller than that of the nearest negative pair in a batch, and MS mining (Wang et al.
(2019)) selects hard negative pairs whose distance is smaller than the largest distance between positive pairs and hard positive pairs whose distance is larger than the smallest distance between negative pairs at the same time.
DWS method keeps all positive pairs but samples negative pairs according to their distance distribution within a batch.
The proposed DRO framework induce a pair sampling method by using the optimal distributional variables that deﬁnes the robust loss over pairs within a mini-batch.
As a result, the sampling probabilities induced by our DRO framework is automatically adaptive to the pair-based losses .
There are other works that study the problem from the perspective of pair weighting instead of pair sampling.
For example, Yu et al.
(2018) heuristically design exponential weights for the different pairs in a triplet, which is a special case of our DRO framework.
Details are provided in the supplementary.
However, since the quality of anchors varies very much, it may not be reasonable to sample the same number of pairs from all anchors.
Imbalance Data Classiﬁcation.
There are many studies in machine learning which have tackled the imbalanced issue.
Commonly used tricks include over-sampling, under-sampling and cost-sensitive learning.
However, these approaches do not take the differences between examples into account.
Other effective approaches grounded on advanced learning theories include minimizing maximal losses (Shalev-Shwartz & Wexler, 2016), minimizing top-k losses (Fan et al., 2017) and minimizing variance-regularized losses (Namkoong & Duchi, 2017).
However, these approaches are not efﬁcient for deep learning with big data, which is a severe issue in DML.
In contrast, the proposed
DRO formulation is deﬁned over a mini-batch of examples, which inherits the theoretical explanation from the literature and is much more efﬁcient for DML.
In addition, the induced loss by our
DRO formulation include maximal loss, top-k loss and variance-regularized loss as special cases by specifying different uncertainty sets of the distributional variable.
DML A S A DRO-B ASED B INARY C LASSIFICATION P ROBLEM
In this section, we will ﬁrst present a general framework for DML based on DRO with theoretical justiﬁcation.
We will then discuss three simple variants of the proposed framework and also show how the proposed framework recover existing complicated losses for DML.
Preliminaries.
Let x ∈ R D denote an input data (e.g., image) and f ( · ; θ ) :
R D → R d denote the feature embedding function deﬁned by a deep neural network parameterized by θ .
The central task in DML is to update the model parameter θ by leveraging pairs of similar and dissimilar examples.
Following most existing works, at each iteration we will sample a mini-batch of examples denoted by { x 1 , ..., x B } .
We can construct B 2 pairs between these examples 1 , and let y ij denote the label of pairs, i.e., y ij = 1 if the pair is similar (positive), and y ij =
if the pair is dissimilar (negative).
The label of pairs can be either deﬁned by users or derived from the class label of individual examples.
Existing works of DML follow the same paradigm for learning the deep neural network i.e., a loss function F ( θ ) is ﬁrst deﬁned over the pairs within a mini-batch and the model parameter θ is updated by gradient-based methods.
Various gradient-based methods can be used, including stochastic gradient descent (SGD), stochastic momentum methods and adaptive gradient methods (e.g. Adam).
Taking SGD as an example, the model parameter θ can be updated by θ ← θ − η ∇ F ( θ ) , where η denotes the learning rate.
The focus here is to how to deﬁne the loss function F ( θ ) over all pairs within a mini-batch.
As mentioned earlier, we will cast the problem as simple binary classiﬁcation problem, i.e., classifying a pair into positive or negative.
To this end, we use l ij ( θ )
=
l ( f ( x i ; θ ) , f ( x j ; θ ) , y ij ) denote the pairwise classiﬁcation loss between x i and x j in the embedding space (e.g., margin loss Wu et al.
(2017), binomial loss Yi et al.).
A naive approach for DML is to use the averaged loss over all pairs, i.e., F avg ( θ ) =
1 B 2 (cid:80) Bi
=1 (cid:80)
Bj =1 l ij ( θ ) .
However, this approach will suffer from the severe imbalanced issue, i.e., most pairs are negative pairs.
The gradient of F avg will be dominated by that of negative pairs.
3. G ENERAL DRO-B ASED F RAMEWORK
To address the imbalanced pair issue, we propose a general DRO formulation to compute a robust loss.
The formulation of our DRO-based loss over all pairs within a mini-batch is given below:
[Equation]
where p ∈ R B 2 + is a non-negative vector with each element p ij representing a weight for an individual pair.
U ⊆ R B 2 denotes the decision set of p , which encodes some prior knowledge about p .
In 1 For simplicity, we consider all pairs including self-pair.
the literature of DRO Namkoong & Duchi (2017), p is interpreted as a probability vector such that (cid:80) ij p ij = 1 called the distributional variable and U denotes the uncertainty set that speciﬁes how p deviates from the uniform probabilities (1 /B 2 , . . .
, 1 /B 2 ) .
In next subsection, we will propose simple variants of the above general framework by specifying different constraints or regularizations for p .
Below, we will provide some theoretical evidences to justify the above framework.
To theoretically justify the above loss, we connect (1) to exiting works in machine learning by considering three different uncertainty sets for p .
First, we can consider a simple constraint U = ∆ = { p ij ≥ 0 , (cid:80) ij p ij = 1 } .
As a result, F ( θ ) = max ij l ij ( θ ) becomes the maximal loss over all pairs.
Shalev-Shwartz & Wexler (2016) shows that minimizing maximum loss is robust to imbalanced data distributions and also derives better generalization error for imbalanced data with a rare class.
However, the maximal loss is more sensitive to outliers (Zhu et al., 2019).
To address this issue, top- K loss (Fan et al., 2017) and variance-regularized loss (Namkoong & Duchi, 2017) are proposed, which can be induced by the above DRO framework.
If we set U = { (cid:80) ij p ij = 1 , 0 ≤ p ij ≤ 1 /K } , F will become the top- K loss F ( θ )
=
1 K (cid:80) Ki
=1 l [ i ] ( θ ) , where l [ i ] ( θ ) denotes the i -th largest loss over all pairs.
If we set U φ = { p ∈ ∆ , D φ ( p (cid:107) 1 /B 2 ) ≤ ρB 2 } , where D φ ( p (cid:107) p (cid:48) )
= (cid:82) φ ( d p d p (cid:48) )
d p (cid:48) is the φ -divergence between two distributions p and p (cid:48) with φ ( t ) = 12 ( t − 1) 2 , then
the
DRO-based loss becomes the variance-regularized loss under certain condition about the variance of the random loss, i.e., for a set of i.i.d random losses { (cid:96) 1 , ..., (cid:96) n } ( n = B 2 ) we could have
[Equation]
where Var n ( (cid:96) ) denotes the empirical variance of (cid:96) 1 , ..., (cid:96) n .
We can see that the second term in R.H.S of the above equation involves the variance, which can play a role of regularization.
The varianceregularized loss has been justiﬁed from advanced learning theory by Namkoong & Duchi (2017), and its promising performance for imbalanced data has been observed as well.
Before ending this subsection, we will discuss how to update the model parameter θ based on the robust loss F ( θ ) deﬁned by (1).
A simple approach is to ﬁnd an optimal distributional variable p ∗ to (1) and then update θ according to the subgradient of weighted loss by ∂ θ g ( θ, p ∗ )
= (cid:80) ij p ∗ ij ∇ l ij ( θ ) , which is justiﬁed by the following lemma.
Lemma 1 Assume that g is proper, lower-semicontinuous in θ and level-bounded in p locally uniformly in θ .
Then the subgradient ∂F ( θ ) ⊂ (cid:83) p ∗ ∈
P ∗ ( θ ) ∂ θ g
( θ, p ∗ ) , where P ∗ ( θ ) denotes the optimal solution set of the maximization problem in (1).
Furthermore, when l ij ( θ ) is smooth in θ and P ∗ ( θ ) is a singleton, i.e., p ∗ = arg max p g ( θ, p ) is unique, we have ∂F ( θ ) =
∂ θ g ( θ, p ∗ ) .
Remark
The above lemma can be proved by Theorem 10.13 in Rockafellar & Wets (2009).
It shows that even if we may not directly compute ∂F ( θ ) , our framework can at least obtain its superset ∂ θ g
( θ, p ∗ ) .
Particularly, if we have additional conditions, i.e., l ij ( θ ) is smooth in θ and the optimal solution p ∗ is unique (considering our regularized formulation below), it theoretically guarantees that our framework exactly computes ∂F ( θ ) .
3. P ROPOSED T HREE V ARIANTS OF O UR F RAMEWORK
In this subsection, we present three variants of our general framework.
In order to contrast to other variants recovering existing complicated losses presented in next subsection, we introduce some notations and make some simpliﬁcations.
For each example x i that serves as an anchor data, let
P i = { j | y ij = 1 , j ∈
[ B ] } and N i = { j | y ij = 0 , j ∈ [ B ] } denote the index sets of positive and negative pairs, respectively.
Let P =
(cid:83) Bi
=1 P i and N = (cid:83) Bi
=1 N i .
We denote the cardinality of a set by P = |P| .
For simplicity, we let P i
=
|P i | , N i
= |N
i | , P = |P| and N = |N| .
Since zero losses usually do not contribute to the computation of the subgradient for updating the model, we can simply eliminate those examples for consideration.
The ﬁrst variant is to simply select the top- K pairs with K -largest losses, which is equivalent to the following DRO formulation:
[Equation]
where K is a hyper-parameter.
The gradient of the robust loss can be simply computed by sorting the pairwise losses and computing the average of top- K losses.
The second variant is a variant of the variance-regularized loss.
Instead of specifying the uncertainty set U φ , we use a regularization term for the ease of computation, which is deﬁned by
[Equation]
where γ > 0 is a hyper-parameter and D KL denotes the KL divergence between two probabilities.
The optimal solution to p can be easily computed following Namkoong & Duchi (2016).
It is notable that the optimal solution p ∗ is not necessarily sparse.
Hence, computing (cid:80) ij p ∗ ij ∇ l ij ( θ ) needs to compute the gradient of pairwise loss for all pairs, which could be prohibitive in practice when the mini-batch size is large.
To alleviate this issue, we can simply sample a subset of pairs according to probabilities in p ∗ and the compute the averaged gradient of these sampled pairs.
The third variant of our DRO framework is explicitly balancing the number of positive pairs and negative pairs by choosing top K/ 2 pairs for each class, which is denoted by DRO-TopK-PN:
DRO-TopK-PN:
[Equation]
For implementation, we can simply select K/ 2 positive pairs with largest losses and K/ 2 negative pairs with largest loss respectively, and compute averaged gradient of the pairwise losses of the selected pairs for updating the model parameter.
3. R ECOVERING THE M ETHOD BASED ON SOTA P AIR -B ASED L OSS
Next we show that proposed framework can recover the method based on SOTA complicated losses.
With the induced feature vector f ( x ; θ ) normalized to have unit norm, we deﬁne the similarity of two samples as S ij := (cid:104) f
( x i ; θ ) , f ( x j ; θ ) (cid:105) , where (cid:104)· , ·(cid:105) denotes dot product.
Speciﬁcally, we consider two SOTA loss functions, LS and MS loss, which are deﬁned below:
[Equation]
where α, β, λ are hyper-parameters of these losses.
The key to our argument is that the gradient computed based on these losses can be exactly computed according to our DRO framework by choosing appropriate constrained set U and setting the pairwise loss as the margin loss.
To this end, we ﬁrst show the gradient based on the LS loss, which can be computed by (Wang et al., 2019):
[Equation]
which can be alternatively written as
[Equation]
It can be shown that for LS loss, derivations are provided in the supplementary, we have
[Equation]
To recover the gradient of the LS loss under our DRO framework, we employ the pairwise margin loss for l ij ( θ ) , i.e., l ij ( θ ) =
[ m + y ij ( λ − S ij )] + , where m and λ are two hyper-parameters and [ a ] + = max { 0 , a } .
Assume that the margin parameter m is sufﬁciently large such that l ij ( θ ) >for all pairs.
The key to deriving the same gradient of the LS loss under our framework is to group distributional variables in p for the positive and negative pairs according to the anchor data.
Let p + i ∈ R P
i and p − i ∈
R N i denote the corresponding vectors of positive and negative pairs for the anchor x i , respectively.
Let us consider the following DRO formulation:
[Equation]
where γ + i ≥ 0 and γ − i ≥ 0 for i ∈
[ B ] are hyper-parameters.
we can easily derive the closed-form solution for p ∗ , i.e., p + ∗ ij =
[Equation]
Then computing the gradient of the robust loss with respect to θ by using the above optimal p ∗ , we have:
[Equation]
which exactly recover the gradient in (6) by setting γ + i = γ − i = 1 .
Finally, we can recover the gradient based on the MS loss in a very similar way.
The difference is to add a pseudo positive pair and pseudo negative pair with 0 loss for each anchor x i , and augment each p + i and p − i by one additional dimension.
The details are provided in the supplementary.
E XPERIMENTS
Our methods was implemented by Pytorch and using BN-Inception network (Ioffe & Szegedy (2015)) pre-trained on ImageNet ILSVRC (Russakovsky et al.
(2015)) to fairly compare with other works.
The same as (Wang et al.
, a FC layer on the top of the model structure following the global pooling layer was added with randomly initialization for our task.
Adam Optimizer with 1 e − 5 learning rate was used for all our experiments.
We verify our DRO framework on image retrieval task with three standard datasets, Cub-200-2011,
Cars-196 and In-Shop.
These three datasets are split according to the standard protocol.
For Cub200-2011, the ﬁrst 100 classes with 5864 images are used for training, and the the other 100 classes with 5924 images are saved for testing.
Cars-196 consists of 196 car models with 16,185 images.
We use the ﬁrst 98 classes with 8054 images for training and the remaining 98 classes with 8, images for testing.
For In-Shop, 997 classes with 25882 images are used for training.
The test set is further partitioned to a query set with 14218 images of 3985 classes, and a gallery set containing 3985 classes with 12612 images.
Batches are constructed with the following rule: we ﬁrst sample a certain number of classes and then randomly sample M instances for each class.
The standard recall @
k evaluation metric is used in all our experiments, where k = { 1 , 2 , 4 , 8 , 16 , 32 } on Cub200-2011 and Car-196, and k = { 1 , 10 , 20 , 30 , 40 , 50 } on In-Shop.
We apply margin loss ( L M ) and binomial loss ( L B , Yi et al.)
as base losses for our DRO framework.
m is the margin in L M .
λ is the threshold for both L M and L B .
α and β are hyperparameters in L B .
4. Q UANTITATIVE R ESULTS
In this experiment, we compare our DRO framework with other SOTA baselines on Cub-200-2011,
Cars-196 and In-Shop, which includes Wang et al.
; Yu et al.
; Kim et al.
; Opitz et al.
Ge (2018); Harwood et al.
; Wu et al.
Oh Song et al.
Among them, mining-based methods are Clusetring, HDC, Margin, Smart Mining and HDL.
ABIER and
ABE are ensemble methods.
HAP2S E and MS are sampling-based methods, which are highly related to our methods.
For our DRO framework, we test all three variants which are proposed in section 3.
We apply two loss functions, margin loss and binomial loss, respectively.
Since
DRO p -sampling works on all pairs in a batch, the binomial variant may not directly apply to psampling.
Thus, it makes totally ﬁve variants of our DRO framework, denoted by DRO-TopK M ,
DRO-TopK B , DRO-TopK-PN M , DRO-TopK-PN B and DRO-KL M , where the subscript M and
B represent the variants of our framework using margin loss and binomial loss, respectively.
We set embedding space dimension d = 1024 .
The batchsize is set B = 80 on Cub-200-2011 and
Cars-196, B = 640 on In-Shop.
γ is tuned from the range =
9 } on all three
[Table]
datasets and K is tuned from { 160 , 200 , 240 , 280 } on Cub-200-2011 and Cars-196, and selected from { 640 , 960 , 1280 , 1600 , 1920 } on In-Shop.
Table 1 and 3 report the experiment results.
We mark the best performer in bold in the corresponding evaluation measure on each column.
For our framework, particularly, we mark those who outperform all other SOTA methods in bold.
We can see that our ﬁve variants outperform other SOTA methods on recall @1 on all three datasets.
Particularly on Cars-196, our ﬁve variants outperforms other SOTA methods on all recall @
k measures.
On Cub-200-2011, DRO-TopK B achieves a higher recall @1 (improving 2 . 4 in recall @1 ) than the best SOTA, MS.
On Cars-196, DRO-KL M has the best performance, which improves 2 . 3 and 1 . 2 in recall @1 compared to the best non-ensemble SOTA,
MS, and the best ensemble SOTA, ABE.
On In-Shop, DRO-TopK-PN M improves 1 . 6 in recall @
compared to the best results among SOTA, MS.
The above results verify 1) the effectiveness of our
DRO sampling methods and 2) the ﬂexibility of our DRO framework to adopt different losses.
4. A BLATION S TUDY 4.2. C OMPARISON WITH LS LOSS AND MS LOSS
[Table] [Table]
In Section 3.
[Table]
we theoretically show that LS loss and MS loss can be viewed as special cases of our DRO framework.
In this experiment
[Table]
we aim to empirically demonstrate that our framework is general enough and recovers LS loss.
Speciﬁcally, we would show 1) when γ = 1 , our framework performs similarly to LS loss, as stated in Section 3.3, 2) our framework can be seen as
a generalized
LS loss by treating γ as a hyper-parameter, and 3) our generalized LS loss outperforms MS loss, even though the performance of the ordinary LS loss is inferior to that of MS loss.
We adopt the set up of embedding dimension and batchsize in the ablation study of Wang et al.
(2019), i.e., d = 64 and B = 80 .
Therefore, we report the existing results of MS and LS loss presented in Wang et al.
(2019) on Cars-196.
For Cub-200-2011 and In-Shop, we implement MS and LS loss according to Wang et al.
Following Wang et al.
(2019), we set α = 2 , β
=for MS loss.
For our DRO framework, we apply grouping to p by equation (7), and denote this variant of DRO framework as DRO-KL-G. We set γ + i = γ − i =
γ = { 1 , 0 . 1 , 0 . 01 , 0 .
001 } , i ∈ [ B ] for DRO-KL-G , m = 0 .
2 for the margin loss, and λ = 0 .
5 for all three losses (MS, LS and margin loss).
As the pairs with zero loss will not contribute to the updates of model but affect the calculation of p in DRO framework, we remove the pairs with zero loss to further promotes training.
Table 2 and 3 show experiment results on Cub-200-2011, Cars-196 and In-Shop, respectively.
As can be seen, the performance of MS loss is better than LS loss on three datasets, particularly on Cars196, which also veriﬁes the results of ablation study in Wang et al.
When γ = 1 , our method performs similarly to LS loss, which veriﬁes that our method recovers LS loss.
Furthermore, when we treat γ as a hyper-parameter (especially γ = 0 . 001 ) and regard our framework as generalized
LS loss
[Figure]
our method obtain improved performance compared to the ordinary LS loss.
Lastly, even if
MS loss exploits pseudo positive and negative pairs, our generalized LS loss outperforms MS loss.
[Figure] [Figure]
[Table] [Table]
4.2. C APACITY TO H ANDLE P AIR I MBALANCE .
In this experiment, we compare our DRO framework with different sampling methods, i.e., semihard (SH) and DWS, in terms of sensitivity to the imbalance ratio.
By setting different batchsizes B ∈ { 80 , 160 , 320 , 480 , 640 } , we have different positive-negative pair ratios |P| : |N| ∈ { 0 .
For all methods, we apply margin loss and set M = 5 for each class and embedding space dimension d = 1024 .
SH mining is originally designed for triplet loss.
Since there is no straightforward choice for the positive pair, we use λ as the upper bound to simulate the similarity of the positive pair in triplet loss.
For DWS, we follow the parameter setting in the original paper (Wu et al.
We apply margin loss in the proposed three variants our DRO framework, which are denoted by DRO-TopK M , DRO-TopK-PN M and DRO-KL M , respectively.
We set K = 2 ∗ B both for DRO-TopK M , DRO-TopK-PN M .
We evaluate recall @1 of all methods and report results in Figure 1.
Figure 1 shows that the DWS has better performance when the positive-negative pair ratio is relatively large, and encounters a sharp decrease in recall @1 when the ratio decreases.
Other four methods obtain better performance when the positive-negative ratio increases.
Among them, DROTopK M and DRO-KL M have similar performance, with SH on all positive-negative pair ratios, while they perform slightly better than SH when the positive-negative ratios are small.
DRO-TopK-PN M constantly outperforms all other methods.
The reason why DWS performs poorly when the positivenegative pair ratio is small may be that DWS aims to sample pairs uniformly in terms of distance (Wu et al.
(2017)), while our DRO framework and SH focus more on hard pairs.
To sum up, our framework achieves very competitive performance against SOTA methods, and maintains increasing recall @1 as the positive-negative ratio increases.
These two observations together demonstrate the effectiveness of our DRO framework to handle pair imbalance.
4.2. S ENSITIVITY OF K IN T OP -K
As we mentioned in section 1, selecting too many pairs within a batch will leads to poor performance of the model.
On the other hand, when the number of selected pairs is too small, the model would be sensitive to outliers.
In this experiment, we study the sensitivity of K in our DRO framework–how the performance of our DRO framework is affected by the value of K .
We set the batchsize B =and M = 5 , which makes the number of positive pairs |P| = 1280 and the number of negative pairs |N| = 198080 .
We set K from the range { 640 , 960 , 1280 , 1600 , 1920 , 2560 } and evaluate recall @ k for models trained by different K .
We choose the above range of K according to the number of pairs selected by DWS and SH in Section 4.2.2 (both selects 2560 pairs roughly).
Figure 2 illustrates how different values of K affect recall @
k on In-Shop.
We can see that, DROTopK M performs best when K = 1280 and recall @
k is stable on the entire range of K .
Our DRO framework is not sensitive to K when K is in a reasonably large range.
[Figure]
[Figure] [Figure]
R UNTIME C OMPARISON
Next, we compare the running time of our proposed three variants of our DRO framework with different pair mining methods, MS and LS losses on In-shop.
Our experiments conducted on eight
GTX1080Ti GPU.
The embedding dimension d = 1024 , and results are compared under different batchsize B = { 80 , 160 , 320 , 480 , 640 } .
The same as previous experiments, we set K = 2 ∗ B both for DRO-TopK M and DRO-TopK-PN M .
γ = 0 .
1 for DRO-KL M .
SH is implemented according to the paper Schroff et al.
(2015), Wu et al.
DWS and MS are implemented based on the code provided by the author.
LS loss is implemented following the code provided by Wang et al.
Figure 3 reports the average running time of each iteration on 200 epochs.
We can see that all of three proposed variants of DRO framework run faster than other anchor-based mining methods and losses.
For all of our three variants, pairs are selected directly from all the pairs, while additional cost is required to select pairs anchor by anchor in other methods.
LS loss is slower than MS loss, because MS mining is applied to MS loss, which would reduce the number of pairs for computing subgradients when updating the model.
For DWS, the distance distribution of negative pairs is only calculated once for each iteration.
It thus only needs to select pairs according to the pre-computed distribution for each anchor.
In contrast, SH requires to compare negative pairs with the lower and upper bound of an interval at each iteration for each anchor, which increases the computational burden.
It can be the reason why SH is slower than DWS.
C ONCLUSION
In this paper we cast DML as a simple pairwise binary classiﬁcation problem and formulate it as a DRO framework.
Compared to existing DML methods that leverage all pairs in a batch or employ heuristic approaches to sample pairs, our DRO framework constructs a robust loss to sample informative pairs, which also comes with theoretical justiﬁcation from the perspective of learning theory.
Our framework is general since it can include many novel designs in its uncertainty decision set.
Its ﬂexibility allows us to recover the state-of-the-art loss functions.
Experiments show that our framework outperforms the state-of-the-art DML methods on benchmark datasets.
We also empirically demonstrate that our framework is efﬁcient, general and ﬂexible.
S UPPLEMENTARY 6. D ERIVATION OF R ECOVER SOTA LOSSES
In this section we show how our DRO framework recovers SOTA loss functions, LS loss and MS loss.
Their deﬁnitions are as follows, respectively.
[Equation]
) where λ is the margin hyper-parameter.
[Equation]
where λ is the margin hyper-parameter and α and β are coefﬁcient hyper-parameters.
6.1. LS L OSS UNDER O UR F RAMEWORK
Recall that the objective function is decomposable in terms of p i =
[ p + i , p − i ] ∈ R P i + N i .
We denote the p ij = p + ij when j ∈ P i , and p ij = p − ij when j ∈ N i for simplicity.
The Lagrangian function of (7) can be represented as:
[Equation]
where
L ( p i
[Equation]
According to KKT conditions, v + ∗ i and v −∗ i are the optimal solutions of the dual function, and p ∗ i is the optimal solution of the primal problem (7), if and only if
[Equation]
We ﬁrst derive p ∗ i in terms of v + i , v − i using equation (12), i.e.:
[Equation]
where l i ( θ ) =
{ l i 1 ( θ ) , · · · , l i | p
+ i ∪ N + i
| ( θ ) } , 1 + ∈ R
|P i | , 1 − ∈ R |N
i | .
Then the closed form of p ∗ i for positive pairs and negative pairs can be written as follows
[Equation]
Substitute p + ∗ ij , p −∗ ij into equation (13), which means v + ∗ i and v −∗ i need to satisfy:
[Equation]
Even though equal (16) also equals to 0 when v + ∗ i = 0 or v −∗ i = 0 , or v + ∗ i =
v −∗ i = 0 , but the corresponding optimal solution p ∗ i will not meet the equality constraints, i.e., (cid:80)
j ∈P i p + ∗ ij = 1 and (cid:80) j ∈P i p + ∗ ij = 1 , in the original formulation (7).
Therefore, we only have
[Equation]
Then from equation (17), we can get
[Equation]
[Equation]
Plugging them into (15) and apply margin loss as the base loss function for each pair, l ij ( θ ) =
[ m + y ij ( λ − S ij )] + , we successfully derive the weighting representation of LS loss:
[Equation]
Thus, when updating the model parameter θ , we are going to minimize the following objective function:
[Equation]
Taking the gradients to equation 20 in terms of θ , we can get:
[Equation]
Similarly, we take gradients to the L LS loss function (8):
[Equation]
By substituting equation (19) into equation (26), we can see that equation (22) and (26) are equivalent.
This shows our DRO framework successfully recovers the LS loss by setting the uncertainty decision set U in equation (7).
6.1. MS L OSS UNDER O UR F RAMEWORK
MS loss, a combination of binomial loss and LS loss, can also be formulated into our DRO framework.
LS loss only considers the lifted structure between pairs, while binomial loss focusing on the intrinsic property of an independent pair while encoding the pair class information.
To recover MS loss under our framework, we re-deﬁne p ∈ [0 , 1] P + + N + +2 B by adding one more element to P i and p − i .
Therefore, now we have p + i ∈ [0 , 1] P + i +
and p − i ∈ [0 , 1] N + i +1 , where the newly added element corresponds to a zero loss, and thus does not contribute to the computation of overall loss.
Then based on the formulation of LS loss, the formulation of MS loss can be written as:
[Equation]
Note that
[Equation]
As the analysis of LS loss
[Equation]
we can also obtain the representation of MS loss under our DRO framework from formulation (23),
i
[Equation]
e.,
[Equation] [Equation]
where c + , c − , γ + i , γ − i are hyperpatmeters.
Similar to LS loss, waking the gradients to ∂g ( θ, p ∗ ) in terms of θ , we can get:
[Equation]
[Equation]
Similarly, we take gradients to the L MS loss function (9):
[Equation]
By substituting (25) into (21),and set c + = λ + m, c − = λ − m, γ + i = 1 α , γ − i = 1 β , i ∈ [ B ] , it is obvious to show equation (21) and (27) are the same.
As a result, our DRO framework also recovers the MS loss.
6.1. R ECOVERING OF HAP2S E IN Y U ET AL . (2018)
Yu et al.
(2018) provides an a hardaware point to set (HAP2S) triplet loss with an adaptive hard mining scheme to address the sensitive issue caused by mining the hardest positive pair and negative pair in Hermans et al.
The key of HAP2S loss is to assign different weights to the points in
P i and N i for each anchor x i .
We show that our DRO framework is able to recover HAP2S with exponential weighting scheme by exactly using the DRO formulation equation (7) for LS loss.
The triplet loss is deﬁned as follows:
[Equation]
where [ x ] + = max { 0 , x } , S ij and S ik denote the similarity of positive pair { x i , x j } , j ∈ P i , negative pair { x i , x k } , k ∈ N i for the same anchor x i , N t is the number of all possible triplets in the mini-batch.
Without causing ambiguity, S + ij = S ij , when j ∈ P i , S − ij = S ij , when j ∈ N i .
Similar to pair losses, such as margin loss and binomial loss, there exist a huge amount of triplets in a batch that have no contribution to the L trp .
As a result, pair mining is critical to improve the performance of the model.
Hermans et al.
(2017) provide a variant of triplet loss by selecting the hardest positive pair and the hardest negative pair for each anchor.
The formulation can be written as:
[Equation]
The state-of-the-art results on two large-scale datasets has been reported based on this variant of triplet loss.
However, equation (29) is sensitive to outliers which usually serve as the hardest sample.
To increase the robustness of the model, HAP2S triplet loss has been proposed in Yu et al.
[Equation]
where
[Equation]
where q ij is the weights designed to each pair, for which they propose two variants of weighting schemes for HAP2S, i.e., exponential weighting and polynomial weighting.
Here we show that our
DRO formulation is able to recover the HAP2S with exponential weighting scheme (denoted by
HAP2S E), i.e. the weight q ij of each pair is an exponential function over its similarity:
[Equation]
Note that q + ij and q − ij are constant scalars that not involved in the gradient of L HAP 2 S w.r.t to θ .
Substitute equation (31) and (32) into (30)
[Equation]
where D absorbs all constants.
Since S ij − S ik , S ik − S ij in the exponential function is derived from q + ij and q − ij , thus they are also a constant scalars when taking derivative to θ .
By taking derivative to L HAP 2 S
[Figure]
we get: ∂ L HAP 2 S ∂θ =
B
B (cid:88) i = (cid:16) (cid:88)
j ∈P i ( −(cid:80) k ∈P i exp( S ij − S ik γ ))
∂S
ij ∂θ + (cid:88)
j ∈N i ( (cid:80) k ∈N
i exp( S ik − S ij γ ))
∂S ij ∂θ (cid:17) , (34)
We can see that the subgradients of L HAP 2 E in (34) are the same as the subgradients of LS loss in (22).
As a result, L HAP 2 S is a special case of our DRO framework
[Figure]
6. S UPPLEMENTARY E XPERIMENTS
[Figure] [Figure]
To investigate the effect of randomness of the stochastic algorithms and evaluate the robustness of our DRO framework, we report the average mean and standard variance of recall @
k on Cub-2002011 in Figure 4.
We do not plot recall @4 and recall @16 for better visualization.
The experimental setting is the same as the experiments of the SOTA quantitative results we reported in Table 1 (section 4.1) but with ﬁve runs.
The gray bars are the recall @
k of best performer among SOTA baselines, i.e., MS.
It is clear to see that all our DRO variants outperform MS in terms of the average recall @ k over all different values of k .
Speciﬁcally, the average recall @1 of DRO-TopK B is 67 .
9% , which has a signiﬁcant improvement over the baselines, i.e., 65 . 7% of MS.
In addition, the small standard deviation error bars imply that our DRO framework is robust enough to have a better performance than SOTA methods.
To show the effect of the network architecture to our DRO framework and its robustness, we additionally repeat the experiments of Recall/Imbalance Ratio in section 4.2.2, but with the embedding space dimensions 512 (rather than 1024 in Section 4.2.2).
The results are illustrated in Figure 5.
In comparison with the performance of recall @1 on embedding space dimensions 1024 in Figure 1, we can see that the ﬂuctuations of recall @1 on different PN-Ratios are sublet when the feature embedding changes from 1024 to 512.
For example, the recall @1 only changes, from 0.9046 to 0.9058 for
DRO-TopK M , from 0.9086 to 0.9073 for DRO-TopK-PN M , from 0.9018 to 0.9021 for DRO-KL M , when PN-Ratio is 0.006.
Further, the trends of different methods imply that our DRO framework can consistently achieve competitive even best results compared with other SOTA methods (SH and
DWS) under different embedding space dimensions.
Since different P-N Ratios in corresponding to batchsizes { 80 , 160 , 320 , 480 , 640 } , the above experimental results verify that our methods are not too sensitive to the embedding space dimensions in different batchsizes, and also outperform other SOTA mining methods in different embedding space dimensions.
In contrast, it shows that the baseline DWS is relatively more sensitive to the embedding space dimension.
In Figure 5 where embedding space dimension is set to 512 , DWS has a smaller performance drop from P-N ration = 0 .
012 to 0 .
006 , while in Figure 1 where the embedding space dimension is 1024 , DWS encounters a much larger performance decrease.