gen.py 3.18 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
"""Generate random scores.
"""
import os
import logging
import numpy
import click
from click.types import FLOAT
from bob.extension.scripts.click_helper import verbosity_option
from bob.core import random
from bob.io.base import create_directories_safe

logger = logging.getLogger(__name__)

NUM_GENUINE_ACCESS = 5000
NUM_ZEIMPOSTORS = 5000
NUM_PA = 5000

18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def gen_score_distr(mean_gen, mean_zei, mean_pa, sigma_gen=1, sigma_zei=1,
                    sigma_pa=1):
    mt = random.mt19937()  # initialise the random number generator

    genuine_generator = random.normal(numpy.float32, mean_gen, sigma_gen)
    zei_generator = random.normal(numpy.float32, mean_zei, sigma_zei)
    pa_generator = random.normal(numpy.float32, mean_pa, sigma_pa)

    genuine_scores = [genuine_generator(mt) for i in range(NUM_GENUINE_ACCESS)]
    zei_scores = [zei_generator(mt) for i in range(NUM_ZEIMPOSTORS)]
    pa_scores = [pa_generator(mt) for i in range(NUM_PA)]

    return genuine_scores, zei_scores, pa_scores


34
def write_scores_to_file(neg, pos, filename, attack=False):
35
36
37
38
39
40
41
    """Writes score distributions into 4-column score files. For the format of
      the 4-column score files, please refer to Bob's documentation.

    Parameters
    ----------
    neg : array_like
        Scores for negative samples.
42
43
    pos : array_like
        Scores for positive samples.
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
    filename : str
        The path to write the score to.
    """
    create_directories_safe(os.path.dirname(filename))
    with open(filename, 'wt') as f:
        for i in pos:
            f.write('x x foo %f\n' % i)
        for i in neg:
            if attack:
                f.write('x attack foo %f\n' % i)
            else:
                f.write('x y foo %f\n' % i)


@click.command()
@click.argument('outdir')
@click.option('--mean-gen', default=10, type=FLOAT, show_default=True)
@click.option('--mean-zei', default=0, type=FLOAT, show_default=True)
@click.option('--mean-pa', default=5, type=FLOAT, show_default=True)
@verbosity_option()
def gen(outdir, mean_gen, mean_zei, mean_pa):
    """Generate random scores.
    Generates random scores for three types of verification attempts:
    genuine users, zero-effort impostors and spoofing attacks and writes them
    into 4-column score files for so called licit and spoof scenario. The
    scores are generated using Gaussian distribution whose mean is an input
    parameter. The generated scores can be used as hypothetical datasets.
    """
    # Generate the data
    genuine_dev, zei_dev, pa_dev = gen_score_distr(
        mean_gen, mean_zei, mean_pa)
    genuine_eval, zei_eval, pa_eval = gen_score_distr(
        mean_gen, mean_zei, mean_pa)

    # Write the data into files
    write_scores_to_file(genuine_dev, zei_dev,
                         os.path.join(outdir, 'licit', 'scores-dev'))
    write_scores_to_file(genuine_eval, zei_eval,
                         os.path.join(outdir, 'licit', 'scores-eval'))
    write_scores_to_file(genuine_dev, pa_dev,
                         os.path.join(outdir, 'spoof', 'scores-dev'),
                         attack=True)
    write_scores_to_file(genuine_eval, pa_eval,
                         os.path.join(outdir, 'spoof', 'scores-eval'),
                         attack=True)