filipradenovic / cnnimageretrieval-pytorch

CNN Image Retrieval in PyTorch: Training and evaluating CNNs for Image Retrieval in PyTorch
http://cmp.felk.cvut.cz/cnnimageretrieval
MIT License
1.43k stars 323 forks source link

You trained the model with contrastive loss, while evaluated with cosine similarity? #40

Open SongZRui opened 5 years ago

SongZRui commented 5 years ago

It seems that you used different criteria during training and testing as the code below shows: IN TEST: scores = np.dot(vecs.T, qvecs) IN TRAIN: dif = x1 - x2 D = torch.pow(dif+eps, 2).sum(dim=0).sqrt()

   y = 0.5*lbl*torch.pow(D,2) + 0.5*(1-lbl)*torch.pow(torch.clamp(margin-D, min=0),2)
   y = torch.sum(y)

I did not get it why you do so?

filipradenovic commented 5 years ago

At training time we use the original formulation of contrastive loss, in which Euclidean distance is used to compare two image representations.

At test time, we use dot product or cosine similarity. Because the vectors are L2-normalized, cosine similarity and Euclidian distance will provide the same ranking/ordering. The dot product is faster and simpler to compute than Euclidian distance, that is why we use it at test time.

For more details, take a look at the equations in my PhD Thesis, Appendix A, paragraph 'Cosine similarity'.

SongZRui commented 5 years ago

Get it. ThX