pad_figure.py 2.33 KB
Newer Older
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
'''Runs error analysis on score sets, outputs metrics and plots'''

import bob.measure.script.figure as measure_figure
import bob.bio.base.script.figure as bio_figure
from .error_utils import calc_threshold

ALL_CRITERIA = ('bpcer20', 'eer', 'min-hter')


class Metrics(measure_figure.Metrics):
    '''Compute metrics from score files'''

    def __init__(self, ctx, scores, evaluation, func_load):
        super(Metrics, self).__init__(
            ctx, scores, evaluation, func_load,
            names=('FtA', 'APCER', 'BPCER', 'FAR', 'FRR', 'ACER'))

    def get_thres(self, criterion, dev_neg, dev_pos, far):
        if self._criterion == 'bpcer20':
            return calc_threshold('bpcer20', dev_neg, dev_pos)
        else:
            return super(Metrics, self).get_thres(
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
                criterion, dev_neg, dev_pos, far)


class MultiMetrics(measure_figure.MultiMetrics):
    '''Compute metrics from score files'''

    def __init__(self, ctx, scores, evaluation, func_load):
        super(MultiMetrics, self).__init__(
            ctx, scores, evaluation, func_load,
            names=('FtA', 'APCER', 'BPCER', 'FAR', 'FRR', 'ACER'))

    def get_thres(self, criterion, dev_neg, dev_pos, far):
        if self._criterion == 'bpcer20':
            return calc_threshold('bpcer20', dev_neg, dev_pos)
        else:
            return super(MultiMetrics, self).get_thres(
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
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
                criterion, dev_neg, dev_pos, far)


class Roc(bio_figure.Roc):
    '''ROC for PAD'''

    def __init__(self, ctx, scores, evaluation, func_load):
        super(Roc, self).__init__(ctx, scores, evaluation, func_load)
        self._x_label = ctx.meta.get('x_label') or 'APCER'
        self._y_label = ctx.meta.get('y_label') or '1 - BPCER'


class Det(bio_figure.Det):
    def __init__(self, ctx, scores, evaluation, func_load):
        super(Det, self).__init__(ctx, scores, evaluation, func_load)
        self._x_label = ctx.meta.get('x_label') or 'APCER (%)'
        self._y_label = ctx.meta.get('y_label') or 'BPCER (%)'


class Hist(measure_figure.Hist):
    ''' Histograms for PAD '''

    def _setup_hist(self, neg, pos):
        self._title_base = 'PAD'
        self._density_hist(
            pos[0], n=0, label='Bona Fide', color='C1'
        )
        self._density_hist(
            neg[0], n=1, label='Presentation attack', alpha=0.4, color='C7',
            hatch='\\\\'
        )