画EER曲线,声纹确认

画EER曲线首先需要知道每个阈值点对应的错误接受率和错误拒绝率(跟计算准确率召回率,画准确率召回率曲线差不多):
scores文件的格式: score target/nontarget

#!coding=utf-8

def read_file(filename):
    with open(filename, 'r') as f:
        lines = f.readlines()
    return lines

def write_file(filename, lines):
    with open(filename, 'w') as f:
        f.writelines(lines)

def compute_FA(nontarget_is_target, target_is_target):
    #False_accept = nontarget_is_target / (nontarget_is_target + target_is_target)
    FA = float(nontarget_is_target) / (nontarget_is_target + target_is_target)
    return FA

def compute_FR(target_is_nontarget, nontarget_is_nontarget):
    #False_accept = nontarget_is_target / (nontarget_is_target + target_is_target)
    FA = float(target_is_nontarget) / (nontarget_is_nontarget + target_is_nontarget)
    return FA

score_path = 'scores'
score_lines = read_file(score_path)
all_scores = []
score2label ={}
for line in score_lines:
    splits = line.strip().split(' ')
    score, label = splits
    all_scores.append(eval(score))
    score2label[str(score)] = label
print('--->',len(all_scores))
sorted_scores = all_scores
sorted_scores.sort()

max_score = max(sorted_scores)
min_score = min(sorted_scores)
print("max score", max_score, "min_score", min_score)
step = (max_score - min_score) / 10000

#对于每一个门限点计算一个 准确率 和 召回率
threshold_list = []
infos_list = []

for ind in range(1,10000):
    target_is_target = 0
    nontarget_is_nontarget = 0
    nontarget_is_target = 0
    target_is_nontarget = 0
    threshold = ind * step + min_score # 从min 到 max 20 个 门限
    threshold_list.append(threshold)
    small_threshold = []
    big_threshold = []
    for score in sorted_scores:
        if score > threshold:
            big_threshold.append(score)
        else:
            small_threshold.append(score)
    print "big", len(big_threshold), 'small', len(small_threshold)
    #在big_threshold 的数据里确定 target_is_target 和 nontarget_is_target
    for s in big_threshold:
        label = score2label[str(s)]
        if label == 'target':
            target_is_target += 1
        else:
            nontarget_is_target += 1
    # 在small_threshold 的数据里确定 child_is_other
    for t in small_threshold:
        label = score2label[str(t)]
        if label == 'nontarget':
            nontarget_is_nontarget += 1
        else:
            target_is_nontarget += 1
    FA = compute_FA(nontarget_is_target, target_is_target)
    FR = compute_FR(target_is_nontarget, nontarget_is_nontarget)
    #print("threshold", threshold, "Acc, Recall", child_acc, child_recall)
    infos_list += [[FA, FR]]

result_lines = []
result_path = 'result_infos_eer'
for (threshold, info) in zip(threshold_list, infos_list):
    s = str(threshold) + ' ' + str(info[0]) + ' ' + str(info[1]) + '\n'
    result_lines.append(s)
write_file(result_path, result_lines)

根据计算得到的每个阈值点准确率和召回率来画曲线:

#!coding=utf-8
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator

#显示刻度: http://blog.csdn.net/fortware/article/details/51934814

def read_file(filepath):
    with open(filepath, 'r') as f:
        lines = f.readlines()
    return lines
result_infos_path = 'result_infos_eer'
acc_arr = []
recall_arr = []
threshholds = []
results_info_lines = read_file(result_infos_path)
for line in results_info_lines:
    splits = line.strip().split(' ')
    threshhold, acc, recall = splits
    threshholds.append(eval(threshhold))
    acc_arr.append(eval(acc))
    recall_arr.append(eval(recall))

max_x = max(threshholds)
min_x = min(threshholds)
print("minx, maxx", min_x, max_x)
# with legend
fig, ax = plt.subplots()
plt.title("EER curve")

plt.xlabel('threshhold')
plt.ylabel('FA/FR')
yminorLocator = MultipleLocator(0.02) #设置y轴的精度
ax.yaxis.set_minor_locator(yminorLocator) #设置次刻度线

"""set min and max value for axes"""
ax.set_ylim([0, 0.5])
ax.set_xlim([min_x, max_x])
ax.yaxis.grid(yminorLocator)

ax.xaxis.grid(True, which='major')
ax.yaxis.grid(True, which='minor')
plt.plot(threshholds, acc_arr,  '.' , label="FA")
plt.plot(threshholds, recall_arr, '.', label="FR")
plt.legend(loc='upper right')

plt.show()# show the plot on the screen

画EER曲线,声纹确认_第1张图片

----OK

你可能感兴趣的:(sre,deep,learning)