wanji / caffe-sl

caffe for Similarity Learning
Other
83 stars 53 forks source link

batch_triplet_loss_layer.cpp #7

Closed ChloeEHKim closed 7 years ago

ChloeEHKim commented 8 years ago

Hi, I'm looking through the batch_triplet_loss_layer source for triplet embedding vgg-face model. I'd like to filter only hard negative samples so I set the sample parameter "true". But, I found that in the file, line 164 looks like below if (!sample || neg_dist > pos_dist) { smp_rank_loss += cur_rankloss; triplets.push_back(Triplet(i, j, k)); } Is this should be " if (!sample || pos_dist > neg_dist) "? since the distance is euclidean. Thanks for your help in advance.

wanji commented 8 years ago

It is neg_dist > pos_dist. The purpose is to avoid triplets that are too hard. Refer the FaceNet paper for more details:

FaceNet: A Unified Embedding for Face Recognition and Clustering
Florian Schroff, Dmitry Kalenichenko, James Philbin
liuyuyuil commented 8 years ago

Hi, @wanji , in BatchTripletLossLayer:Forward_cpu(), the rank loss is normalized by num_tri (the number of triplets no matter fullfill the constraint). However, in Backwardcpu(), the scale related to ranking loss is set to `Dtype(2) / triplets.size(), wheretriplets_.size()is not equal tonum_triactually.num_tri` should by adopted in both functions, am I right ? Hoping for your reply, and thank you very much.

wanji commented 8 years ago

Hi, @liuyuyuil The code may seem a bit confusing. Actually, rank_loss is averaged over all triplets, but only some the triplets, which are neither too hard nor too easy, are chosen for updating the model.

liuyuyuil commented 8 years ago

Thanks for explanation @wanji