deepinsight / insightface

State-of-the-art 2D and 3D Face Analysis Project
https://insightface.ai
23.16k stars 5.39k forks source link

why do you use euclidean metric to eval on lfw dataset?Shouldn't the cosine distance be used #243

Closed lzg188 closed 6 years ago

lzg188 commented 6 years ago

we can see the following function。

def calculate_val(thresholds, embeddings1, embeddings2, actual_issame, far_target, nrof_folds=10): assert(embeddings1.shape[0] == embeddings2.shape[0]) assert(embeddings1.shape[1] == embeddings2.shape[1]) nrof_pairs = min(len(actual_issame), embeddings1.shape[0]) nrof_thresholds = len(thresholds) k_fold = LFold(n_splits=nrof_folds, shuffle=False)

val = np.zeros(nrof_folds)
far = np.zeros(nrof_folds)

diff = np.subtract(embeddings1, embeddings2)
dist = np.sum(np.square(diff),1)
indices = np.arange(nrof_pairs)

for fold_idx, (train_set, test_set) in enumerate(k_fold.split(indices)):

    # Find the threshold that gives FAR = far_target
    far_train = np.zeros(nrof_thresholds)
    for threshold_idx, threshold in enumerate(thresholds):
        _, far_train[threshold_idx] = calculate_val_far(threshold, dist[train_set], actual_issame[train_set])
    if np.max(far_train)>=far_target:
        f = interpolate.interp1d(far_train, thresholds, kind='slinear')
        threshold = f(far_target)
    else:
        threshold = 0.0

    val[fold_idx], far[fold_idx] = calculate_val_far(threshold, dist[test_set], actual_issame[test_set])

val_mean = np.mean(val)
far_mean = np.mean(far)
val_std = np.std(val)
return val_mean, val_std, far_mean

waiting for your reply!

nttstar commented 6 years ago

As we just need a threshold

lzg188 commented 6 years ago

i don't understand what's your meaning. but the cosine distance will get the better acc on lfw than euclidean metric ? and because our loss is based the angle,so i think the cosine distance is more appropriate. sphereface 's github use cosine distance. waiting for your reply!

HaoLiuHust commented 6 years ago

the feature has been normalized, so euclidean metric actually plays equal to cos distance

tp-nan commented 6 years ago

||A-B||^2 = ||A||^2+||B||^2-2*dot(A,B) = 2-2 cos(A,B) if ||A||^2 = 1 & ||B||^2 = 1 for A,B belongs to R^n

lzg188 commented 6 years ago

great thanks very much this can be closed