clovaai / voxceleb_trainer

In defence of metric learning for speaker recognition
MIT License
1.02k stars 272 forks source link

How to find the optimal threshold? #118

Closed ukemamaster closed 2 years ago

ukemamaster commented 3 years ago

@joonson I compute the optimal threshold using:

result = tuneThresholdfromScore(sc, lab, [1, 0.1])
fnrs, fprs, thresholds = ComputeErrorRates(sc, lab)    # calculate FRR, FAR and corresponding thresholds
mindcf, threshold = ComputeMinDcf(fnrs, fprs, thresholds, 0.05, 1, 1)

a = [p for p,n in zip(fprs,fnrs) if abs(p-n)<0.0000001]   # find the value where FAR and FRR are almost equal
ind = fprs.index(a)                                                         # find the index of that value
optimal_threshold = thresholds[ind]                              # value in thresholds for that index 

print("\n EER {:2.4f}, MinDCF {:2.5f} at threshold {:2.3f} \n".format(result[1], mindcf, optimal_threshold));
print(f'At this point FAR is: {fprs[ind]*100:2.3f} and FRR is: {fprs[ind]*100:2.3f}')

Output is:

EER 1.3083, MinDCF 0.07430 at threshold 0.539 
At this point FAR is: 1.307 and FRR is: 1.307

But i am not sure if this is correct way to do it. Can somebody confirm this?

zabir-nabil commented 2 years ago

Usually, the threshold will be the value where we get the equal error rate.

image

ukemamaster commented 2 years ago

@zabir-nabil yes, the threshold is the value where we get the equal error rate. See the output in the above question. My question is how to find it?

seacj commented 2 years ago

@zabir-nabil yes, the threshold is the value where we get the equal error rate. See the output in the above question. My question is how to find it?

You can refer to the following code

from sklearn import metrics fpr, tpr, thresholds = metrics.roc_curve(labels, scores, pos_label=1) # fnr = 1 - tpr idxE = np.nanargmin(np.absolute((fnr - fpr))) eer = max(fpr[idxE], fnr[idxE]) * 100 threshold = thresholds[idxE] # get the threshold here

Jungjee commented 2 years ago

Closing this issue as enough explanation has already been explained.