Guest User

Untitled

a guest
Aug 21st, 2019
235
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Copyright 2019. DSPLabs. All Rights Reserved.
  2.  
  3.  
  4. import numpy as np
  5.  
  6.  
  7. def compute_frr_far(tar, imp):
  8. pt = np.concatenate((np.ones(len(tar)), np.zeros(len(imp))), axis=0)
  9. pi = np.concatenate((np.zeros(len(tar)), np.ones(len(imp))), axis=0)
  10.  
  11. tar_imp = np.hstack((tar, imp))
  12. arg_sort = np.argsort(tar_imp)
  13.  
  14. pt = pt[arg_sort]
  15. pi = pi[arg_sort]
  16. tar_imp = tar_imp[arg_sort]
  17.  
  18. fr = np.zeros(pt.shape[0])
  19. fa = np.zeros(pi.shape[0])
  20.  
  21. for i in range(1, len(pt)):
  22. fr[i] = fr[i - 1] + pt[i - 1]
  23.  
  24. for i in range(len(pt) - 2, -1, -1):
  25. fa[i] = fa[i + 1] + pi[i]
  26.  
  27. frr = fr / len(tar)
  28. far = fa / len(imp)
  29.  
  30. return tar_imp, frr, far
  31.  
  32.  
  33. def compute_err(tar, imp):
  34. tar_imp, fr, fa = compute_frr_far(tar, imp)
  35.  
  36. index_min = np.argmin(np.abs(fr - fa))
  37. eer = 100.0 * np.mean((fr[index_min], fa[index_min]))
  38. threshold = tar_imp[index_min]
  39.  
  40. return eer, threshold
  41.  
  42.  
  43. def compute_min_c(pt, tar, imp, c_miss=1, c_fa=1):
  44. tar_imp, fnr, fpr = compute_frr_far(tar, imp)
  45.  
  46. beta = c_fa * (1 - pt) / (c_miss * pt)
  47. log_beta = np.log(beta)
  48. act_c = fnr + beta * fpr
  49. index_min = np.argmin(act_c)
  50. min_c = act_c[index_min]
  51. threshold = tar_imp[index_min]
  52.  
  53. return min_c, threshold, log_beta
  54.  
  55.  
  56. def compute_act_c(pt, tar, imp, c_miss=1, c_fa=1):
  57. beta = c_fa * (1 - pt) / (c_miss * pt)
  58. log_beta = np.log(beta)
  59.  
  60. f_tar = list(filter(lambda t: t < log_beta, tar))
  61. f_imp = list(filter(lambda i: i > log_beta, imp))
  62.  
  63. fnr = len(f_tar) / len(tar)
  64. fpr = len(f_imp) / len(imp)
  65.  
  66. act_c = fnr + beta * fpr
  67.  
  68. return act_c, fpr, fnr
  69.  
  70.  
  71. def compute_llr_c(tar, imp):
  72. sum_tar = np.sum([np.log(1. + 1. / np.exp(score)) for score in tar])
  73. sum_imp = np.sum([np.log(1. + np.exp(score)) for score in imp])
  74.  
  75. c_llr = 1 / (2 * np.log(2)) * (sum_tar / len(tar) + sum_imp / len(imp))
  76.  
  77. return c_llr
  78.  
  79.  
  80. def get_eer(tar, imp):
  81. return compute_err(tar, imp)[0]
  82.  
  83.  
  84. def get_min_c(p_target, tar, imp, c_miss=1, c_fa=1):
  85. if not hasattr(p_target, '__iter__'):
  86. p_target = [p_target]
  87.  
  88. values = list(map(lambda pt: compute_min_c(pt, tar, imp, c_miss, c_fa)[0], p_target))
  89.  
  90. return sum(values) / len(values)
  91.  
  92.  
  93. def get_act_c(p_target, tar, imp, c_miss=1, c_fa=1):
  94. if not hasattr(p_target, '__iter__'):
  95. p_target = [p_target]
  96.  
  97. values = list(map(lambda pt: compute_act_c(pt, tar, imp, c_miss, c_fa)[0], p_target))
  98.  
  99. return sum(values) / len(values)
  100.  
  101.  
  102. def get_llr_c(tar, imp):
  103. return compute_llr_c(tar, imp)
  104.  
  105.  
  106. def get_fr_fa_at_threshold(tar, imp, threshold=0.5):
  107. fr = len(np.where(tar < threshold)[0])
  108. fa = len(np.where(imp > threshold)[0])
  109. fr = fr * 100. / len(tar)
  110. fa = fa * 100. / len(imp)
  111. return fr, fa
  112.  
  113.  
  114. def get_acer_at_threshold(tar, imp, threshold=0.5):
  115. fr, fa = get_fr_fa_at_threshold(tar, imp, threshold=threshold)
  116. return (fr + fa) / 2.0
  117.  
  118.  
  119. def get_bpcer_at_apcer(tar, imp, apcer=0.01):
  120. tar_imp, fr, fa = compute_frr_far(tar, imp)
  121. return 100.0 * fr[np.argmax(fa <= apcer)]
RAW Paste Data