tbmoon / facenet

FaceNet for face recognition using pytorch
MIT License
245 stars 67 forks source link

Why use " labels.append(np.ones(dists.size(0)))" in training? and #1

Open liuzhongling opened 5 years ago

liuzhongling commented 5 years ago
    dists = l2_dist.forward(embed_anchor, embed_pos)
    distances.append(dists.data.cpu().numpy())
    labels.append(np.ones(dists.size(0)))

    dists = l2_dist.forward(embed_anchor, embed_neg)
    distances.append(dists.data.cpu().numpy())
    labels.append(np.zeros(dists.size(0)))

can you exlpain the code ? what's for?

khrlimam commented 5 years ago

That basically means that all predictions made by comparing anc and neg embeddings should by default labeled as 0 meaning not same. This is intuitively confusing since we get used to with the fact that distances between different images should be far away which 0 is not that "far". But, you can think that the label used here is boolean logic instead of distances.

I have done the training on VGGv2 and tested on LFW datasets. You can found the pretrained weight here https://github.com/khrlimam/res-facenet

Meteor-Stars commented 2 years ago

Hi, thank you for you nice job! I feel puzzled why there is only the 'triplet_loss' but no 'CrossEntropyLoss' during training. It seems that 'model.module.forward_classifier' here not be used to compute the 'CrossEntropyLoss' and I can't understand the meaning of it. Can you help explain the question for me? Thank you! """ model.module.forward_classifier(anc_hard_img) model.module.forward_classifier(pos_hard_img) model.module.forward_classifier(neg_hard_img)

triplet_loss = triploss.forward(anc_hard_embed, pos_hard_embed, neg_hard_embed)

if phase == 'train': optimizer.zero_grad() triplet_loss.backward() optimizer.step() """

khrlimam commented 1 year ago

Hi, thank you for you nice job! I feel puzzled why there is only the 'triplet_loss' but no 'CrossEntropyLoss' during training. It seems that 'model.module.forward_classifier' here not be used to compute the 'CrossEntropyLoss' and I can't understand the meaning of it. Can you help explain the question for me? Thank you! """ model.module.forward_classifier(anc_hard_img) model.module.forward_classifier(pos_hard_img) model.module.forward_classifier(neg_hard_img)

triplet_loss = triploss.forward(anc_hard_embed, pos_hard_embed, neg_hard_embed)

if phase == 'train': optimizer.zero_grad() triplet_loss.backward() optimizer.step() """

Hi @Meteor-Stars, regarding your question why there's only triplet_loss and no cross entropy loss. So, the loss function is the function that will be used to train our network, the network parameters will be optomised (back propagated) based on the value of this function. So this function is really depends to our objective hence loss function also called objective function. We use triplet loss since we want to solve the distance problem between two images (compare their similarity). There're many objective function to use for this purpose there's also contrastive loss function. The one you mentioned is for classification. The detailed explanation of this triplet function you can read on their original paper by google teams here