naver / deep-image-retrieval

End-to-end learning of deep visual representations for image retrieval
https://europe.naverlabs.com/Research/Computer-Vision/Learning-Visual-Representations/Deep-Image-Retrieval/
BSD 3-Clause "New" or "Revised" License
646 stars 101 forks source link

How to assign weights to AP? #16

Closed fyang93 closed 1 week ago

fyang93 commented 4 years ago

As described in the paper, a weight is introduced into the calculation of mAP in order to counter-balance the dataset imbalance issue. I guess the weight is simply inversely proportional to the number of samples of each class in a single batch, so I wrote code like

uni_labels, inv_ids, counts = torch.unique(img_labels, return_inverse=True, 
                                           return_counts=True)
weights = 1 / counts.float()
weights = weights[inv_ids]  # query weights
...
loss = APLoss(scores, labels, weights)

But training by this code only results in explosion in the loss, while anything works fine without the weight. Could you give an example on how to assign the weights to AP? Thank you very much.