plot_fr_pad.py 5.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
import numpy
import bob.measure
from matplotlib import pyplot


def split_scores_genuine_zei_attacks(scores):
  scores_genuine = []
  scores_zei = []
  scores_attack = []
  for s in scores:
    if s['claim_id'] == s['real_id']:
        scores_genuine.append(s['score'])
        continue;
    if s['real_id'] == 'attack':
        scores_attack.append(s['score'])
        continue;
    if s['claim_id'] != s['real_id']:
        scores_zei.append(s['score'])

  return scores_genuine, scores_zei, scores_attack


def plot_scores_distributions_zei_genuine(negatives, positives, title='Score Distribution', n_bins=(50, 20), threshold_height=1):

  if isinstance(n_bins, tuple):
    assert len(n_bins) == 2
  else:
    n_bins = (n_bins, n_bins)

  # compute the threshold on the dev set
  threshold = bob.measure.eer_threshold(negatives, positives)
  
  pyplot.figure(figsize=(7,5))
  pyplot.hist(negatives, density=True, color='r', bins=n_bins[0], alpha=0.4, label='Zero-Effort Impostors')
  pyplot.hist(positives, density=True, color='g', bins=n_bins[1], label='Genuine')
  pyplot.vlines(threshold, 0, threshold_height, colors='r', linestyles='dashed', label='EER Threshold')
  pyplot.title('Development set')
  pyplot.xlabel("Score Value")
  pyplot.ylabel("Probability Density")
  pyplot.legend()




def calc_pass_rate(threshold, attacks):
    return sum(1 for i in attacks if i >= threshold) / float(attacks.size)


def get_iampr_line(fig, attacks):

  ax = fig.get_axes()
  x_limits = ax[0].get_xlim()

  ntick = 100
  step = (x_limits[1] - x_limits[0]) / float(ntick)
  thres = [(k * step) + x_limits[0] for k in range(2, ntick - 1)]
  mix_prob_y = []
  for k in thres:
    mix_prob_y.append(100. * calc_pass_rate(k, numpy.array(attacks)))

  return thres, mix_prob_y


def plot_scores_distributions_zei_attacks_genuine(negatives, positives, attacks, title='Score Distribution', n_bins=(50,50,50), threshold_height=1):

  if isinstance(n_bins, tuple):
    assert len(n_bins) == 3
  else:
    n_bins = (n_bins, n_bins, n_bins)
 
  # compute the threshold on the dev set
  threshold = bob.measure.eer_threshold(negatives, positives)
 

  fig, ax1 = pyplot.subplots(figsize=(7,5))
  ax1.hist(negatives, density=True, color='r', bins=n_bins[0], alpha=0.4, label='Zero-Effort Impostors')
  ax1.hist(positives, density=True, color='g', bins=n_bins[1], alpha=0.7, label='Genuine')
  ax1.hist(attacks, density=True, color='gray', bins=n_bins[2], alpha=0.7, hatch='\\\\', label='Presentation Attacks')
  ax1.vlines(threshold, 0, threshold_height, colors='r', linestyles='dashed', label='EER Threshold')
  ax1.set_xlabel("Score Value")
  ax1.set_ylabel("Probability Density")

  ax2 = ax1.twinx()
  points, iampr = get_iampr_line(fig, attacks)
  ax2.plot(points, iampr, color='b', label ='IAMPR')
  ax2.set_ylabel('IAMPR')
  ax1.legend(loc=6)
  ax2.legend()
  pyplot.show()

  #f = pyplot.figure(figsize=(7,5))
  #pyplot.hist(positives, density=True, color='g', bins=n_bins, alpha=0.7, label='Genuine')
  #pyplot.hist(negatives, density=True, color='r', bins=n_bins, alpha=0.4, label='Zero-Effort Impostors')
  #pyplot.hist(attacks, density=True, color='gray', bins=n_bins, alpha=0.7, hatch='\\\\', label='Presentation Attacks')
  #pyplot.vlines(threshold, 0, threshold_height, colors='r', linestyles='dashed', label='EER Threshold')
 
  #ax = f.get_axes()
  #prob_ax = ax[0].twinx()

  #points, iampr = get_iampr_line(f, attacks)
  #pyplot.plot(points, iampr, color='b')

  #pyplot.plot()
  #pyplot.title('Development set')
  #pyplot.xlabel("Score Value")
  #pyplot.ylabel("Probability Density")
  #pyplot.legend()


def scatter_plot(fr_scores, pad_scores):
  
  fig, ax1 = pyplot.subplots(figsize=(7,5))
 

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
def sort_spoof_scores(scores):
    scores_attack = []
    scores_genuine = []
    for s in scores:
        if s['real_id'] == 'attack':
            scores_attack.append(s['score'])
        else:
            scores_genuine.append(s['score'])
    return scores_attack, scores_genuine
            
def sort_licit_scores(scores):
    scores_zei = []
    scores_genuine = []
    for s in scores:
        if s['claim_id'] == s['real_id']:
            scores_genuine.append(s['score'])
        else:
            scores_zei.append(s['score'])
    return scores_zei, scores_genuine
            

def plot_espc(licit_dev, licit_eval, spoof_dev, spoof_eval):

  licit_dev_neg, licit_dev_pos = sort_licit_scores(licit_dev)
  licit_eval_neg, licit_eval_pos = sort_licit_scores(licit_eval)
  spoof_dev_neg, spoof_dev_pos = sort_spoof_scores(spoof_dev)
  spoof_eval_neg, spoof_eval_pos = sort_spoof_scores(spoof_eval)

  from error_utils import epsc_thresholds
  points = 10
  omega, beta, thrs = epsc_thresholds(
                      licit_dev_neg,
                      licit_dev_pos,
                      spoof_dev_neg,
                      spoof_dev_pos,
                      points=points,
                      criteria='eer',
                      beta=0.5)

  from error_utils import all_error_rates
  errors = all_error_rates(licit_eval_neg, licit_eval_pos, spoof_eval_neg,
                           spoof_eval_pos, thrs, omega, beta)

  # plot
  from matplotlib import pyplot
  fig, ax1 = pyplot.subplots(figsize=(7,5))
  ax1.plot(omega, 100. * errors[4].flatten())
  ax2 = ax1.twinx()
  ax2.plot(omega, 100. * errors[2].flatten())
  pyplot.show()