One Loss for All: Deep Hashing with a Single Cosine Similarity based Learning Objective

1 Introduction . A key building block of a real-world large-scale image retrieval system is hashing . The objective of image hashing is to represent the content of an image using a binary code for efficient storage and accurate retrieval . Recently , deep hashing methods [ 48 , 23 ] have shown great improvements over conventional hashing methods [ 46 , 15 , 16 , 22 , 36 , 37 , 19 ] . Furthermore , deep hashing methods can be grouped by how the similarity of the learned hashing codes are measured , namely pointwise [ 49 , 54 , 40 , 12 , 50 ] , pairwise [ 25 , 23 , 5 , 4 ] , triplet-wise [ 45 , 32 ] , or listwise [ 52 ] . Among them , pointwise methods have a O ( N ) computational complexity , whilst the complexity of the others are of at least O ( N2 ) for N data points . This means that for large-scale problems , only the pointwise methods are tractable [ 49 ] . They are thus the focus of most recent studies . A deep hashing neural network naturally has multiple learning objectives . Specifically , given an image input , the network outputs a continuous code ( feature vector ) which is then converted into a binary ∗equal contribution . †corresponding author ( cs.chan @ um.edu.my ) . 35th Conference on Neural Information Processing Systems ( NeurIPS 2021 ) . hash code using a quantization layer ( usually a sign function ) . There are thus two main objectives . First , the final model output , i.e. , the binary codes must be discriminative , meaning the intra-class hamming distances are small , while the inter-class ones are big . Second , a quantization error minimization objective is needed to regularize the continuous codes . But the learning is constrained by the vanishing gradient problem caused by the quantization layer . Although the problem can be avoided by deploying some relaxation schemes [ 5 , 23 , 25 ] , these schemes often produce sub-optimal hash codes due to the introduction of quantization error ( see Figure 1 ) . Hence , most recently deep hashing methods [ 41 , 25 , 4 , 53 , 50 ] has an explicit quantization error minimization learning objective . Having these two main objectives/losses are still not enough . In particular , to ensure the quality of hash codes , many other losses are employed by existing methods . These include bit balance loss [ 53 , 49 , 40 ] , weights constraints to maximize Hamming distance [ 54 ] , code orthogonality [ 31 , 32 ] . Further , losses are designed to address the vanishing gradient problem caused by the sign function used to obtain binary codes from the continuous ones [ 41 , 40 , 27 ] . As a result , the state-of-the-art hashing models typically have a large number ( > 4 ) losses . This means difficulties in optimization which in turn hamper their effectiveness . In this work , for the first time , a deep hashing model with a single loss is developed which removes any needs for loss weight tuning and is thus much easier to optimize . As mentioned earlier , a deep hashing model needs to be trained with at least two objectives , namely binary code discriminativenss and quantization error minimization . So how could one use one loss only ? The answer lies in the fact that the two objectives are closely related and can be unified into one . More concretely , we show that both objectives can be satisfied by maximizing the cosine similarity between the continuous codes and their corresponding binary orthogonal target , which can be formulated as a cross-entropy ( CE ) loss . Our model , dubbed OrthoHash has one loss only which maximizes the cosine similarity between the L2-normalized continuous codes and binary orthogonal target to maximize inter-class Hamming distance and minimize quantization error simultaneously . We show that this single unifying loss has a number of additional benefits . First , we can leverage the benefit of margin [ 42 , 10 ] to further improve the intra-class variance . Second , since conventional CE loss only works for single-label classification , we can easily leverage Label Smoothing [ 38 ] to modify the CE loss to tackle multilabels classification . Finally , we show that code balancing can now be enforced by introducing a batch normalization [ 17 ] ( BN ) layer rather than requiring a different loss . Extensive experiment results suggest that on conventional category-level retrieval tasks using ImageNet100 , NUS-WIDE and MS-COCO , our model is on par with the SOTA . More importantly , on the large-scale instance-level retrieval tasks , our method achieves the new SOTA , beating the best results obtained so far on GLDv2 , ROxf andRParis by 0.6 % , 9.1 % and 17.1 % respectively . 2 Related Work . Hashing methods . Conventional hashing methods can be categorized into many streams . Dataindependent methods such as Locality-sensitive Hashing ( LsH ) [ 16 , 13 ] , and its kernelized version ( KLsH ) [ 22 ] have contributed many of the fundamental concepts for hashing such as the requirement of code balance , uncorrelated bit , and similarity preserving . In contrast , data-dependent methods [ 46 , 21 , 15 , 19 , 36 , 37 ] aim to learn hash codes that are more compact yet more dataset-specific [ 7 ] . Recently , deep learning based hashing methods [ 25 , 48 , 23 ] dominated the hashing research due to the superior learning ability of DNN . Various learning objectives are developed to learn hash codes using a training dataset . The objective functions include i ) task learning objective which can be further categorized into pointwise [ 49 , 54 , 40 , 41 , 12 , 50 ] , pairwise [ 5 , 25 , 23 ] , triplet-wise [ 45 , 32 ] , listwise [ 52 ] and unsupervised [ 27 , 14 ] ; ii ) quantization error minimization such as the loss designed to minimize the p-norm ( usually p = 2 ) between continuous codes and hash codes ; iii ) code balancing [ 27 , 40 ] . We refer readers to learning to hashing surveys [ 44 , 43 , 11 ] for more detailed review . Binary optimization . Hashing is a NP-hard binary optimization problem [ 46 ] , and is prone to the vanishing gradient problem due to the discrete and non-differentiable binary hash functions . Early methods solved the problem by discarding the discrete constraints ( e.g. , designing a penalty loss term to generate feature as binary as possible [ 25 , 23 ] ; solve with continuous relaxation , i.e. , to optimize in a continuous space using sigmoid or tanh for approximation [ 5 ] ) . Some methods also utilized coordinate descent method in the training [ 28 , 24 ] . Nevertheless , these methods have increased the complexity of learning due to need for tuning of hyper-parameters balancing different learning objectives . Bypassing vanishing gradient . Greedy Hash [ 41 ] designed a new coding layer which uses the sign function in the forward pass to generate binary codes , and gradients are backpropagated using straight-through estimator [ 1 ] during optimization . [ 27 ] designed a parameter-free coding layer – Bi-half , to maximize the bit capacity by shifting the network output by median ( each bit can have a 50 % chance of being +1 or−1 ) . These methods typically requires the modification of computational graphs , in the sense that the original graph is no longer end-to-end trained , hence further complicates the original optimization objective . Ours on the other hand incorporates a neat one-loss design that removes all such complications . Learning hash codes with pre-defined target . Deep Polarized Network ( DPN ) [ 12 ] used a random assignment scheme to generate target vectors with maximal inter-class distance , then optimized with hinge-like polarized loss . Central Similarity Quantization ( CSQ ) [ 50 ] uses Hadamard matrix as `` hash centers '' , then optimized with binary cross entropy . Both methods have similar overall objective , i.e. , the continuous codes are learned to be as similar as the target vectors ( or `` hash centers '' ) . Our model also employs a hash target , but uniquely it is used in a single cosine similarity based single objective . Cosine similarity . According to [ 6 ] , which is a theoretical analysis for Locality-sensitive Hashing ( LsH ) [ 16 , 13 ] , if two samples have high angular similarity , then we have high probability of obtaining the same hash codes as well . Hence , while most works focus on hashing images with various constraints , we reformulate the problem of deep hashing in the lens of cosine similarity . By following the same principle , a similar work is done by [ 2 ] which described the hashing problem under pairwise constraint while our work describes the problem under pointwise constraint . As inspired by [ 51 , 14 ] which utilize cosine similarity to find closest approximate binary or ternary representation , we also interpret the quantization error in terms of cosine similarity . Moreover , deep hypersphere embedding learning methods ( e.g. , SphereFace [ 35 ] , CosFace [ 42 ] and ArcFace [ 10 ] ) imposed discriminative constraints on a hypersphere manifold and proposed to improve decision boundary by cosine or angular margin . Inspired by them , we also leverage the benefit of margin to improve intra-class variance . 3 OrthoHash : One Loss for All . In Section 3.1 , we reformulate the problem of deep hashing in the lens of cosine similarity , i.e. , interpreting both Hamming distance retrieval and quantization error in cosine similarity . In Section 3.2 , we propose to maximize cosine similarity between the continuous codes and binary orthogonal target under a single classification objective ( for both single-label and multi-labels classification ) . Finally , we describe why adding a batch normalization layer after the continuous codes will achieve code balance in Section 3.2.3 . Our method is illustrated in Figure 2 . Let us first formally define the deep hashing problem . Let d-dimensional data , X = { xn } Nn=1 ∈ RN×d where N is the number of training samples , and Y = { yn } Nn=1 ∈ { 0 , 1 } N×C as onehot training labels of C classes ( for multi-labels , yn , yni = [ yn1 , · · · , ynC ] , whose yni = 1 if any i-th class are assigned to the n-th sample and 0 otherwise ) . Our objective is to learn a set of K-bit binary codes B = { bn } Nn=1 ∈ { −1 , 1 } N×K for each training point xn , which is converted from the continuous codes vn through a sgn function . v can be computed by a latent layer H ( x ) = Wφ ( x ) ∈ RK , φ ( · ) is a deep neural network ( backbone network ) to compute q-dimensional nonlinear feature representation f = φ ( x ) ∈ Rq , W ∈ RK×q is the weights of the latent layer and sgn ( vnk ) = 1 if k-th bit of vn ≥ 0 and −1 otherwise . In our work , binary orthogonal targets oyn ∈ [ o1 , · · · , oC ] ᵀ = O ∈ { −1 , +1 } C×K , where oi denotes a column vector belongs to i-th class . Ideally , for any two rows , 1 ≤ i , j ≤ C , oi and oj are orthogonal to each other . We use a or A to represent scalar , a to represent column vector , and A to represent matrix . Both i and j are often used as index .

Summary: In this paper, authors propose a novel deep hashing model with only a single learning objective and claim that state of the art papers generally use a large number (>4) of losses. Specifically, authors show that maximizing the cosine similarity between the continuous codes and their corresponding binary orthogonal codes can ensure both hashcode discriminativeness and quantization error minimization. Authors show significance of their method using extensive experiments.