baseline.py 3.24 KB
Newer Older
1
2
3
4
5
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Tiago de Freitas Pereira <tiago.pereira@idiap.ch>

"""
6
A script to run biometric recognition baselines
7
8
9
"""


10
11
12
13
from .. import load_resource
from .verify import main as verify
from ..baseline import get_available_databases, search_preprocessor
from bob.extension.scripts.click_helper import verbosity_option
14
import click
15
16
import tempfile
import logging
17
import os
18
19

logger = logging.getLogger("bob.bio.base")
20
21


22
23
24
25
26
27
@click.command(context_settings={'ignore_unknown_options': True,
                                 'allow_extra_args': True})
@click.argument('baseline', required=True)
@click.argument('database', required=True)
@verbosity_option()
@click.pass_context
28
def baseline(ctx, baseline, database, **kwargs):
29
    """Run a biometric recognition baseline.
30

31
32
33
    \b
    Example:
        $ bob bio baseline eigenface atnt -vvv
34

35
36
    which will run the eigenface baseline (from bob.bio.face) on the atnt
    database.
37

38
39
40
41
42
43
44
45
46
47
    \b
    Check out all baselines available by running:
    `resource.py --types baseline`
    and all available databases by running:
    `resource.py --types database`

    This script accepts parameters accepted by verify.py as well.
    See `verify.py --help` for the extra options that you can pass.

    Hint: pass `--grid demanding` to run the baseline on the SGE grid.
48

49
    Hint: pass `--temp-directory <dir>` to set the directory for temporary files
50

51
    Hint: pass `--result-directory <dir>` to set the directory for resulting score files
52

53
54
55
56
    """
    # Triggering training for each baseline/database
    loaded_baseline = load_resource(
        baseline, 'baseline', package_prefix="bob.bio.")
57

58
    # find the compatible preprocessor for this database
59
    database_data = get_available_databases()[database]
60
61
62
    db = search_preprocessor(database, loaded_baseline.preprocessors.keys())
    preprocessor = loaded_baseline.preprocessors[db]

63
64
65
66
67
    # this is the default sub-directory that is used
    if "-T" in ctx.args or  "--temp-directory" in ctx.args:
        sub_directory = os.path.join(database, baseline)
    else:
        sub_directory = baseline
68

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    logger.debug('Database groups are %s', database_data["groups"])

    # call verify with newly generated config file. We will create a new config
    # file to allow people to use chain-loading and further modify the
    # baselines. See: https://gitlab.idiap.ch/bob/bob.bio.video/issues/12
    config = '''
preprocessor = '{preprocessor}'
extractor = '{extractor}'
algorithm = '{algorithm}'
database = '{database}'
sub_directory = '{sub_directory}'
groups = ['{groups}']
verbose = {verbose}
'''.format(
        preprocessor=preprocessor,
        extractor=loaded_baseline.extractor,
        algorithm=loaded_baseline.algorithm,
        database=database,
87
        sub_directory=sub_directory,
88
89
90
91
92
93
94
95
96
97
98
        groups="', '".join(database_data["groups"]),
        verbose=ctx.meta['verbosity'],
    )

    with tempfile.NamedTemporaryFile(mode='w+t', prefix='{}_'.format(baseline),
                                     suffix='.py', delete=False, dir='.') as f:
        f.write(config)
        f.flush()
        f.seek(0)
        verify([f.name] + ctx.args)
        click.echo("You may want to delete `{}' after the experiments are "
99
100
                    "finished running.".format(f.name))