Skip to content
Snippets Groups Projects
Commit 2c96ca74 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI
Browse files

Fixes #8 Algorithm.read_toscore_object should not exist

parent 5d9460a9
No related branches found
No related tags found
1 merge request!58Fixes #8 Algorithm.read_toscore_object should not exist
Pipeline #26925 passed
...@@ -85,7 +85,9 @@ class Algorithm(object): ...@@ -85,7 +85,9 @@ class Algorithm(object):
**Parameters:** **Parameters:**
toscore : object toscore : object
The object to compute the score for. The object to compute the score for. This will be the output of
extractor if performs_projection is False, otherwise this will be the
output of project method of the algorithm.
**Returns:** **Returns:**
...@@ -159,27 +161,6 @@ class Algorithm(object): ...@@ -159,27 +161,6 @@ class Algorithm(object):
""" """
return utils.load(feature_file) return utils.load(feature_file)
def read_toscore_object(self, toscore_object_file):
"""read_toscore_object(toscore_object_file) -> toscore_object
Reads the toscore_object feature from a file.
By default, the toscore_object feature is identical to the projected feature.
Hence, this base class implementation simply calls :py:meth:`read_feature`.
If your algorithm requires different behavior, please overwrite this function.
**Parameters:**
toscore_object_file : str or :py:class:`bob.io.base.HDF5File`
The file open for reading, or the file name to read from.
**Returns:**
toscore_object : object
The toscore_object that was read from file.
"""
return self.read_feature(toscore_object_file)
def train_projector(self, training_features, projector_file): def train_projector(self, training_features, projector_file):
"""This function can be overwritten to train the feature projector. """This function can be overwritten to train the feature projector.
If you do this, please also register the function by calling this base class constructor If you do this, please also register the function by calling this base class constructor
......
...@@ -6,12 +6,12 @@ from bob.bio.base.algorithm import LDA ...@@ -6,12 +6,12 @@ from bob.bio.base.algorithm import LDA
class PadLDA(LDA): class PadLDA(LDA):
"""Wrapper for bob.bio.base.algorithm.LDA, """Wrapper for bob.bio.base.algorithm.LDA,
Here, LDA is used in a PAD context. This means that the feature Here, LDA is used in a PAD context. This means that the feature
will be projected on a single dimension subspace, which acts as a score will be projected on a single dimension subspace, which acts as a score
For more details, you may want to have a look at For more details, you may want to have a look at
`bob.learn.linear Documentation`_ `bob.learn.linear Documentation`_
.. _bob.learn.linear Documentation: .. _bob.learn.linear Documentation:
...@@ -58,26 +58,5 @@ class PadLDA(LDA): ...@@ -58,26 +58,5 @@ class PadLDA(LDA):
**kwargs **kwargs
) )
def read_toscore_object(self, toscore_object_file):
"""Reads the toscore_object feature from a file.
By default, the toscore_object feature is identical to the projected feature.
Hence, this base class implementation simply calls :py:meth:`bob.pad.base.algorithm.Algorithm.read_feature`.
If your algorithm requires different behavior, please overwrite this function.
Parameters
----------
toscore_object_file : str or :py:class:`bob.io.base.HDF5File`
The file open for reading, or the file name to read from.
Returns
-------
object:
The toscore_object that was read from file.
"""
return self.read_feature(toscore_object_file)
def score(self, toscore): def score(self, toscore):
return [toscore[0]] return [toscore[0]]
...@@ -202,6 +202,7 @@ def execute(args): ...@@ -202,6 +202,7 @@ def execute(args):
elif args.sub_task == 'compute-scores': elif args.sub_task == 'compute-scores':
tools.compute_scores( tools.compute_scores(
args.algorithm, args.algorithm,
args.extractor,
groups=[args.group], groups=[args.group],
allow_missing_files=args.allow_missing_files, allow_missing_files=args.allow_missing_files,
force=args.force, force=args.force,
......
...@@ -23,7 +23,7 @@ from .FileSelector import FileSelector ...@@ -23,7 +23,7 @@ from .FileSelector import FileSelector
from bob.bio.base import utils from bob.bio.base import utils
def _compute_scores(algorithm, toscore_objects, allow_missing_files): def _compute_scores(algorithm, extractor, toscore_objects, allow_missing_files):
"""Compute scores for the given list of objects using provided algorithm. """Compute scores for the given list of objects using provided algorithm.
""" """
# the scores to be computed # the scores to be computed
...@@ -37,7 +37,10 @@ def _compute_scores(algorithm, toscore_objects, allow_missing_files): ...@@ -37,7 +37,10 @@ def _compute_scores(algorithm, toscore_objects, allow_missing_files):
scores.insert(i, [numpy.nan]) scores.insert(i, [numpy.nan])
continue continue
# read toscore # read toscore
toscore = algorithm.read_toscore_object(toscore_element) if algorithm.performs_projection:
toscore = algorithm.read_feature(toscore_element)
else:
toscore = extractor.read_feature(toscore_element)
# compute score # compute score
if isinstance(toscore, list) or isinstance(toscore[0], numpy.ndarray): if isinstance(toscore, list) or isinstance(toscore[0], numpy.ndarray):
scores.insert(i, algorithm.score_for_multiple_projections(toscore)) scores.insert(i, algorithm.score_for_multiple_projections(toscore))
...@@ -120,7 +123,7 @@ def _save_scores(score_file, scores, toscore_objects, write_compressed=False): ...@@ -120,7 +123,7 @@ def _save_scores(score_file, scores, toscore_objects, write_compressed=False):
_close_written(score_file, f, write_compressed) _close_written(score_file, f, write_compressed)
def _scores_all(algorithm, group, force, allow_missing_files=False, write_compressed=False): def _scores_all(algorithm, extractor, group, force, allow_missing_files=False, write_compressed=False):
"""Computes scores for all (real, attack) files in a given group using the provided algorithm.""" """Computes scores for all (real, attack) files in a given group using the provided algorithm."""
# the file selector object # the file selector object
fs = FileSelector.instance() fs = FileSelector.instance()
...@@ -148,7 +151,7 @@ def _scores_all(algorithm, group, force, allow_missing_files=False, write_compre ...@@ -148,7 +151,7 @@ def _scores_all(algorithm, group, force, allow_missing_files=False, write_compre
# get the attack files # get the attack files
current_files = fs.get_paths(current_objects, 'projected' if algorithm.performs_projection else 'extracted') current_files = fs.get_paths(current_objects, 'projected' if algorithm.performs_projection else 'extracted')
# compute scores for the list of File objects # compute scores for the list of File objects
cur_scores = _compute_scores(algorithm, current_files, allow_missing_files) cur_scores = _compute_scores(algorithm, extractor, current_files, allow_missing_files)
total_scores += cur_scores total_scores += cur_scores
# Save scores to text file # Save scores to text file
_save_scores(score_file, cur_scores, current_objects, write_compressed) _save_scores(score_file, cur_scores, current_objects, write_compressed)
...@@ -164,7 +167,7 @@ def _scores_all(algorithm, group, force, allow_missing_files=False, write_compre ...@@ -164,7 +167,7 @@ def _scores_all(algorithm, group, force, allow_missing_files=False, write_compre
current_toscore_objects[0]+current_toscore_objects[1], write_compressed) current_toscore_objects[0]+current_toscore_objects[1], write_compressed)
def compute_scores(algorithm, force=False, groups=['dev', 'eval'], allow_missing_files=False, write_compressed=False): def compute_scores(algorithm, extractor, force=False, groups=['dev', 'eval'], allow_missing_files=False, write_compressed=False):
"""Computes the scores for the given groups. """Computes the scores for the given groups.
This function computes all scores for the experiment and writes them to score files. This function computes all scores for the experiment and writes them to score files.
...@@ -175,6 +178,8 @@ def compute_scores(algorithm, force=False, groups=['dev', 'eval'], allow_missing ...@@ -175,6 +178,8 @@ def compute_scores(algorithm, force=False, groups=['dev', 'eval'], allow_missing
algorithm : py:class:`bob.bio.base.algorithm.Algorithm` or derived algorithm : py:class:`bob.bio.base.algorithm.Algorithm` or derived
The algorithm, used for enrolling model and writing them to file. The algorithm, used for enrolling model and writing them to file.
extractor : py:class:`bob.bio.base.extractor.Extractor` or derived
force : bool force : bool
If given, files are regenerated, even if they already exist. If given, files are regenerated, even if they already exist.
...@@ -192,4 +197,4 @@ def compute_scores(algorithm, force=False, groups=['dev', 'eval'], allow_missing ...@@ -192,4 +197,4 @@ def compute_scores(algorithm, force=False, groups=['dev', 'eval'], allow_missing
algorithm.load_projector(fs.projector_file) algorithm.load_projector(fs.projector_file)
for group in groups: for group in groups:
_scores_all(algorithm, group, force, allow_missing_files, write_compressed) _scores_all(algorithm, extractor, group, force, allow_missing_files, write_compressed)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment