How to Compute EER Metrics in Voiceprint and Face Recognition – Machine Leaning Tutorial

By | June 21, 2022

In voiceprint and face recognition, EER is an important metrics. How to compute it? In this tutorial, we will introduce you how to do.

What is EER?

To understand EER, you can read:

Understand TPR, FPR, FAR, FRR and EER Metrics in Voiceprint Recognition – Machine Learning Tutorial

How to compute EER?

In order to compute EER, we should get FPR and TPR.

EER in ROC Curve

As to TPR and FPR, we can use python sklearn based on scores and labels.

For example:

compute eer in voiceprint recognition

As to label = 1, which means two wav files are from the same person.

As to score, we can compute the cosine similarity between two wav files based their voiceprint feature.

As to face recognition, we also can compute cosine similarity from two different face images.

Here is an example code:

We will import some libraries first.

from scipy import interpolate
from sklearn.metrics import (
    accuracy_score,
    precision_score,
    recall_score,
    f1_score,
    roc_curve,
)
import numpy
from scipy.interpolate import interp1d
from scipy.optimize import brentq

Then, we will set some scores and labels.

labels = [0, 1, 1, 0, 0, 1, 0, 1]
scores = [0.2, 0.5, 0.6, 0.1, -0.1, 0.7, 0.5, 0.3]

And we will create a function to compute EER.

def compute_eer(scores, labels):
    """
    Compute the equal error rate score
    """
    fpr, tpr, _ = roc_curve(labels, scores, pos_label = 1)
    eer = brentq(lambda x: 1.0 - x - interp1d(fpr, tpr)(x), 0.0, 1.0)
    return eer

Finally, we can compute EER as follows:

eer = compute_eer(scores, labels)
print(eer)

Run this code, we will see:

0.2499999999999996

We also can use FAR to estimate EER, for example:

def tuneThresholdfromScore(scores, labels, target_fa, target_fr=None):
    fpr, tpr, thresholds = roc_curve(labels, scores, pos_label=1)
    fnr = 1 - tpr  # fpr
    tunedThreshold = [];
    if target_fr:
        for tfr in target_fr:
            idx = numpy.nanargmin(numpy.absolute((tfr - fnr)))
            tunedThreshold.append([thresholds[idx], fpr[idx], fnr[idx]])
    for tfa in target_fa:  # FPR
        idx = numpy.nanargmin(numpy.absolute((tfa - fpr)))  # numpy.where(fpr<=tfa)[0][-1]
        tunedThreshold.append([thresholds[idx], fpr[idx], fnr[idx]])
    idxE = numpy.nanargmin(numpy.absolute((fnr - fpr)))
    eer = max(fpr[idxE], fnr[idxE])

    return tunedThreshold, eer, fpr, fnr

print(tuneThresholdfromScore(scores, labels, target_fa = numpy.arange(0,1, 0.05))[1])

Run this code, we will see:

EER = 0.25

Leave a Reply