davidsandberg / facenet

Face recognition using Tensorflow
MIT License
13.84k stars 4.81k forks source link

Center loss modification #684

Open Shahnawazgrewal opened 6 years ago

Shahnawazgrewal commented 6 years ago

center loss calculate distance between class features and centers as shown below in tensorflow implementation:

def center_loss(features, label, alfa, nrof_classes):
    """Center loss based on the paper "A Discriminative Feature Learning Approach for Deep Face Recognition"
       (http://ydwen.github.io/papers/WenECCV16.pdf)
    """
    nrof_features = features.get_shape()[1]
    centers = tf.get_variable('centers', [nrof_classes, nrof_features], dtype=tf.float32,
        initializer=tf.constant_initializer(0), trainable=False)
    label = tf.reshape(label, [-1])
    centers_batch = tf.gather(centers, label)
    diff = (1 - alfa) * (centers_batch - features)
    centers = tf.scatter_sub(centers, label, diff)
    with tf.control_dependencies([centers]):
        loss = tf.reduce_mean(tf.square(features - centers_batch))
    return loss, centers

The statement

(features - centers_batch)

computes the difference between features and its corresponding class. In other words, it computes difference when i == j. However, I am interested to compute difference (features - centers_batch) with features and class centers from different classes. In other words, I am interested to calculate the difference when i != j

Please let me know how to compute this difference.

Thanks in advance.

Shahnawazgrewal commented 6 years ago

@davidsandberg please comment