Hotel Recognition via Latent Image Embedding

We approach the problem of hotel recognition with deep metric learning. We overview the existing approaches and propose a modification to Contrastive loss called Contrastive-Triplet loss. We construct a robust pipeline for benchmarking metric learning models and perform experiments on Hotels-50K and CUB200 datasets. Contrastive-Triplet loss is shown to achieve better retrieval on Hotels-50k. We open-source our code.


Introduction
In large-scale image-based hotel recognition, the goal is to retrieve hotels from a large database of hotel images. The input is a hotel photo and the retrieved hotel images are expected to be from the same hotel or hotel chain. We approach this task as a deep metric learning problem. In this setting, we train a deep neural network to project images into an informative low-dimension latent space. This space is used for approximate nearest-neighbor retrieval.
The paper is structured in the following way. First we carefully investigate the available approaches. We investigate failure cases of Contrastive and Triplet losses, where they miss out on potentially important information within a triplet. Based on the hypotheses that Triplet loss and Contrastive loss use different information available in a batch we propose the Contrastive-Triplet loss. We construct a benchmarking pipeline for robust model comparisons, ensuring equal conditions for all approaches. The Mean Average Precision at R (MAP@R) metric is used for evaluation. Finally, we experimentally test the proposed approach by comparing the performance of Contrastive-Triplet loss to individual Contrastive and Triplet losses on the CUB200 and Hotels-50k datasets. We finish with a discussion of experimental results. The code for reproducing our experiments is available on Github. 1 Our contributions are two-fold: We find examples where Contrastive and Triplet losses fail to include important information within a batch and formulate the hypothesis that the classic losses use different aspects of similarity.
2. We propose the Contrastive-Triplet loss function which combines Contrastive loss and Triplet loss with no additional computational overhead. 3. For the first time to our knowledge we evaluate a newly proposed Deep Metric Learning approach against the fair benchmarking pipeline by Musgrave et al. [17] and open-source the code for reproduction.
2 Related work (a) Successful retrieval example.
(b) Unsuccessful retrieval example. The tasks of image retrieval, scene recognition and place recognition can be solved using metric learning. Metric learning attempts to map data to an embedding space, where similar data are close together and dissimilar data are far apart. There are two main approaches to deep metric learning: embedding and classification losses.

Classification
In this setting, a classification model is trained using softmax loss and the embeddings produced by the penultimate layer of the model are used.
The first caveat of this approach is that a model trained for classification does not directly optimize for similarity, which sometimes leads to sub-par results. The second caveat is that image retrieval problems tend to have large numbers of semantic labels, which makes it computationally hard to train a classifier model. Despite that, Zhai et al. [27] modify a softmax loss trained classifier for metric learning and show that such approach is very competitive and can be scaled to large numbers of classes.
Deng et al. [3] address the problem that softmax loss does not directly optimize for similarity. They introduce a new loss function ArcFace loss that distributes the data points on a hypersphere. Unlike a generic softmax loss, this loss function directly enforces an angular margin m between embeddings of different classes. Authors achieve great results on face recognition benchmark datasets, which tend to have high inter-class variance and large numbers of clusters.

Embedding losses
In this approach a model is trained to explicitly learn an embedding of data into a latent space, where the similarity is maximized for semantically similar data points and minimized otherwise. This is usually achieved by considering pairs, triplets or N-tuplets of images.
Contrastive loss [6] is a classic approach that had wide adoption. The idea behind it is to analyze pairs of embeddings and penalize the embedder model for images of the same label being too far apart and for images of different labels being too close. While training with Contrastive loss, the loss is computed over pairs of images within a batch. A pair with the same label is called a positive pair. A pair with different labels is called a negative pair. The loss is calculated as follows: Where d p is the distance for the positive pair, d n is the distance for the negative pair, m pos and m neg are hyperparameters, [x] + = max(0, x) is the hinge function.
Another classic approach is Triplet loss [23]. Triplets of images (a, p, n) are selected from each batch, where x a is an anchor image, p has the same label as the anchor, n has a different label. Triplet loss minimizes the anchor-positive pair distance and maximizes the anchor-negative distance, so that the anchorpositive pair is closer than the anchor-negative pair by some margin m triplet : Both Triplet loss and Contrastive loss share the issue of selecting useful triplets or pairs within a batch. The total number of possible triplets in a dataset is O(n 3 ), but only a tiny fraction of these produce useful gradients. Triplet mining aims to select useful triplets for learning.
Xuan et al. [26] show that sampling the easiest positive and semi-hard negative pairs for triplets leads to better results on datasets with high intra-class variance. An easiest positive image is the closest positive to the anchor in the batch. A semi-hard negative is a negative image that is further from the anchor than the positive image, but within the triplet margin. Such selection pushes each image closer to the most similar image of the same class. This reduces the over-clustering problem, when very different images of the same class are mapped to the same place. Authors indicate that mapping each image close to the closest positive leads to better generalization on unseen data.
Movshovitz et al. [16] argue that it's not enough to select useful triplets from each training batch, but careful sampling of batches is a problem as well. To counter the sensitivity to individual triplets picked into the batch, they propose to learn a small set of proxy points P that approximates the original data distribution. In a supervised setting, where all data points are labelled as belonging to a class, a single proxy is used for each class. Authors extend the NCA loss [4] with proxies, proposing the Proxy-NCA loss. This has the additional advantage over Triplet loss as the margin hyperparameter is no longer needed. This approach was extended [25] to ProxyNCA++ later.
Wang et al. [22] claim that different ranking losses take in account different aspects of similarity. Contrastive loss is based on the similarity between the anchor and the negative example. Triplet loss is based on the difference between the anchor-negative similarity and similarity to positive examples. According to the author each method only uses a specific part of information about a tuple. They propose a multi-similarity loss that attempts to capture all of the information at once and claim that it naturally selects harder pairs.

Other approaches
Kim et al. [9] propose an attention-based ensemble model. Multiple attention masks are used after spatial feature extraction, which focuses the model on different regions of the input image. They utilize a divergence loss that promotes each attention mask to learn a different embedding. Similar methods are used for content-based retrieval [21] and automated content editing techniques [5,12].
There were attempts to combine embedding and classification losses for better performance [13]. Jun et al. [8] combine the two major approaches by pre-training a DCNN using a classification loss and then fine-tuning the network using Triplet loss.
An loss-agnostic way to improve model performance is to provide the model more useful training samples. Lin et al. [11] use a generator network to generate hard negative points from easy negatives, so that the whole distribution of negative samples is covered by the network during training. Ko et al. [10] take a different approach for generating hard points: they generate points by linearly interpolating between negative data points during training and picking the hardest point. Other approaches may directly learn metric space transformation via graph embeddings [14,15].

Reality check
Musgrave et al. [17] benchmark the models proposed by numerous metric learning papers and find out that much of the claimed progress in the field was caused by flawed experiments. They find that most methods perform roughly like the classic Contrastive loss, Triplet loss or softmax loss approaches. Most of the gains of recent approaches are explained by using newer backbone DNN architectures, optimizing hyper-parameters on test subsets and using flawed accuracy metrics.

Proposed improvements
After analysing previous works on this topic, we formulated the following hypothesis that could be experimentally tested: embedding losses use different aspects of similarity and current embedding losses do not use all of the available information within a batch.

Contrastive-Triplet loss
To test the hypotheses that different embedding losses use different information in a batch, we engineer some examples where the classic losses could fail.  Example where Triplet loss fails. Consider Triplet loss with m triplet = 0.1 and two triplets with the following distances: d(a 1 , p 1 ) = 0.9, d(a 1 , n 1 ) = 0.9, and d(a 2 , p 2 ) = 0.1, d(a 2 , n 2 ) = 0.1. For both triplets the loss is equal to 0.1, even though they are different. The information about the positive example being very far from the anchor is ignored in the first triplet. This case is illustrated by Figure 2a.
Another failure case of Triplet loss is when a triplet produces no gradients. For example consider a triplet with d(a, p) = 0.3, d(a, n) = 0.4. This is considered an "easy" triplet and produces zero loss, even though the positive example could be pulled closer and the negative could be pushed further away.
Example where Contrastive loss fails. Consider Contrastive loss m pos = 0.2, m neg = 0.5 and pairs with the following distances: d(p 1,1 , p 1,2 ) = 0.4, d(p 1,1 , n 1,1 ) = 0.4, and d(p 2,1 , p 2,2 ) = 0.1, d(p 2,1 , n 2,1 ) = 0.2. For both cases the resulting loss is 0.3, even though in the first case the distances are equal and the example is therefore harder. The information about the relative distance between the positive and the negative examples is ignored. This is illustrated by Figure 2b.
We attempt to create a loss function for which these failure cases are avoided. We propose a new loss function that combines Contrastive and Triplet loss to use more information about the embedding whilst providing no computational overhead.
Consider a triplet (a, p, n). In addition to Triplet loss, we can also calculate the Contrastive loss for the triplet by taking d p = d(a, p) and d n = d(a, n).  By summing these losses, we obtain the Contrastive-Triplet loss: Where α triplet is a hyperparameter weight for the Triplet loss component. The pairwise distances matrix can be reused when computing both loss components, so there is no computation overhead.

Experiments
We follow the fair comparison procedure proposed by Musgrave et al. [17] and use the proposed benchmarker software [18] for our experiments. We ensure that all approaches are tested using the same backbone network, BatchNorm parameters, image augmentation and other conditions. The hyperparameters for each approach are optimized via Bayesian optimization first. We also use cross-validation and multiple reproductions to obtain robust estimates of model performance as well as confidence intervals. We use 25 Bayesian optimization iterations for CUB200 and 10 for Hotels-50k instead of 50 due to computation resource limits and time constraints.

Experiment setup
Experiments use the PyTorch [1] library with the following procedure: -The trunk model is an ImageNet [2] pre-trained BN-Inception [7] network. -The head model is a 1-layer MLP.
-Trunk BatchNorm parameters are frozen during training.
-Batch size is 32. Batches are constructed by sampling 8 classes and randomly sampling 4 images for each class.
-During training, images are first resized so that the shorter side has length 256, then a random crop is made of size between 40 and 256, and aspect ratio between 3/4 and 4/3. Finally, the crop is resized to 227x227, and flipped horizontally with 50% probability. Within each reproduction, for each CV split, we load the best model based on validation set error. We use the best models to obtain train and test set embeddings. We concatenate these embeddings into 512-dim embeddings and L2-normalize them. Using these embeddings we test retrieval and report the mean accuracy and confidence intervals.
For Hotels-50k the procedure has the following differences: -10 iterations of Bayesian optimization are performed.
-Only 500 batches are processed per epoch.
-For each cross-validation fold, the validation fold only has labels that are present in the training set. This is done to mimic the task of the dataset.
Batch-size can have a significant effect on neural networks, but Musgrave et al. [17] demonstrate that increasing batch size from 32 to 256 has little effect to relative rankings of metric learning approaches.

Accuracy metrics
We compare solutions using Mean Average Precision at R (MAP@R). For a given query it's defined as follows: Where P (i) = precision at i if the i-th retrieval is correct and 0 otherwise. R is the total number of references that have the same label as the query.
This metric takes in account both the number of correct retrievals and their ranking. We also provide the commonly used P (1) metric, also called Top-1 Accuracy, for comparison. However it should be noted that the metric can be very high even for poorly-clustered samples, as demonstrated in [17]. For the best hyperparameters, 5 reproduction iterations are run, following the standard cross-validation scheme. For each reproduction the accuracy score is computed. For each CV partition within a reproduction, the highest-accuracy checkpoint is loaded. The 4 models are used to obtain embeddings. For both train and test sets we concatenate the 128-dim embeddings of the 4 models to get 512-dim embeddings, and then L2 normalize them. We test and report the accuracy of retrieval using these embeddings. In the end, we aggregate the scores of reproductions to obtain mean accuracy metrics and confidence intervals.

CUB200 dataset
We benchmark our approaches on CUB200-2011 [24] dataset. The dataset contains photos of birds taken in the wild, with bird species used as labels. It's a commonly used benchmark in metric learning. Just like Hotels-50k, it contains a lot of hard cases of two types: images of the same class that are very different visually, images of different classes that are very similar visually.
In other words, this dataset is notable for high intra-class variance and high similarity between some classes.
The dataset contains 11,788 images distributed between 200 classes. In accordance with previous literature, we use the first 100 classes for the training dataset, and the last 100 classes for the test dataset.

Hotels-50k dataset
Finally, we benchmark on the Hotels-50k [19] dataset. The dataset contains images taken in hotel rooms. Some images are non-professional photos of hotel rooms taken with the TraffickCam app, the rest are professional photos from hotel booking websites. Each photo is labelled with hotel id, hotel chain id and image source (TraffickCam or not). All test images come from TraffickCam, so the task is to identify a hotel or chain given a poor-quality user submitted photo. The dataset provides two tasks: hotel instance recognition and hotel chain recognition. We only approach the hotel chain recognition task.
Due to the large size of the dataset, time and resource constraints, we use a subset of the dataset. For the training set, we remove all hotel chains that have fewer than 100 images and all hotels that have fewer than 10 images. Among the remaining images, we keep only a sample the non-TraffickCam images, such that their number is equal to the number of TraffickCam images. Finally, we remove all hotels from the test set if they are not in the resulting training set.
In the end, the training subset contains 86,936 images from 30,467 hotels of 90 hotel chains. 43,468 images among these come from TraffickCam. The test subset contains 10,900 images from 3,244 hotels of 85 hotel chains. All images in the testing set come from TraffickCam. The final subset we used, as well as the code for recreating it, can is available on Github 2 .
To speed up training on this large dataset, we convert the test and train sets into Lightning Memory Mapped Database [20] binary files storing the following key value pairs: (image index, (image, chain id, hotel id, image source)). LMDB allows multiple concurrent readers to read images from the file. This significantly increases the speed of loading images and training.

Baseline
For comparison, we reproduce the approach used in the original Hotels 50K paper [19]. A neural network is trained using Triplet loss. Batch-hard mining is used: a miner that samples the hardest positive and hardest negative triplet for each anchor. All losses are averaged, including zero values. All other parameters, including backbone network, batch size, embedding dimension and image augmentations, are the same as for other approaches (described in Section 4), to ensure a fair comparison.

Results
The benchmark results can be found in Table 2 and Table 3. The optimal hyperparameters for the CUB200 dataset can be found in Table 1, and for the Hotels-50k dataset in Table 1.
The hypotheses behind Contrastive-Triplet loss was that the different components of the loss might describe different aspects of similarity. If that was true, combining them with optimized hyperparameters would achieved better results. It does not appear to be the case for CUB200. Loss history plots, shown on Figure 3, indicate that adding a Triplet loss component does not significantly affect convergence. The negative loss component does not change with addition of the Triplet loss component. Our experiments show that adding a Triplet loss component to Contrastive loss shifts the positive loss margin towards zero, making the positive loss greater in value, while the Triplet loss margin also shifts towards zero. When m triplet is close to zero, Triplet loss only provides penalties when the positive pair is farther away than the negative pair. In that case Triplet loss component pulls positive examples that are too far away closer to the anchor, almost like positive Contrastive loss. For the CUB200 dataset, a combination of Triplet loss and Contrastive loss achieves the same results as Contrastive loss alone. This leads us to believe that either Triplet loss or Contrastive positive loss are redundant in this case. It should be noted that while Contrastive-Triplet loss does not achieve better results on CUB200, the hyperparameter values are much more stable for it than for Contrastive loss, as shown on Figure 4.
However, the approach does achieve slightly better results on Hotels-50k dataset. This might be explained by the fact that Contrastive-Triplet loss has more stable hyperparameters. Fewer Bayesian optimization iterations were done for Hotels-50k, which could lead to better results for the method that takes less steps to optimize hyperparameters. Examples of retrieval are provided on Figure 1. Overall, qualitative analysis indicates that the embedding produced is densely packed, with the distances between random pairs of images being very small, which leads to a lot of false positives. The network is not able to enforce a large margin between hotel chains.

Conclusion
In this paper we approached the task of hotel recognition. Having reviewed the existing approaches we investigated the failure cases of Contrastive and Triplet losses and proposed the Contrastive-Triplet loss. We constructed a robust experiment pipeline to ensure consistent and reproducible results. Our experiments indicate that Contrastive-Triplet loss doesn't outperform the baseline on CUB200, but achieves better retrieval results on Hotels-50k than baseline Contrastive and Triplet losses.