Skip to content
Snippets Groups Projects
Commit 00f6d036 authored by Tiago de Freitas Pereira's avatar Tiago de Freitas Pereira Committed by Amir MOHAMMADI
Browse files

Fixing vanilla_biometrics. We can run the first pca_atnt example without dask

parent e818caae
No related branches found
No related tags found
1 merge request!180[dask] Preparing bob.bio.base for dask pipelines
...@@ -4,7 +4,6 @@ from . import preprocessor ...@@ -4,7 +4,6 @@ from . import preprocessor
from . import extractor from . import extractor
from . import algorithm from . import algorithm
from . import annotator from . import annotator
from . import mixins
from . import script from . import script
from . import test from . import test
......
from bob.bio.base.pipelines.vanilla_biometrics.legacy import DatabaseConnector from bob.bio.base.pipelines.vanilla_biometrics.legacy import DatabaseConnector
from sklearn.pipeline import make_pipeline from sklearn.pipeline import make_pipeline
from bob.pipelines.transformers import CheckpointSampleLinearize, CheckpointSamplePCA from bob.pipelines.transformers import CheckpointSampleLinearize, CheckpointSamplePCA
from bob.bio.base.pipelines.vanilla_biometrics.biometric_algorithm import ( from bob.bio.base.pipelines.vanilla_biometrics.implemented import (
CheckpointDistance, CheckpointDistance,
) )
from bob.bio.face.database import AtntBioDatabase from bob.bio.face.database import AtntBioDatabase
...@@ -20,9 +20,9 @@ algorithm = CheckpointDistance(features_dir="./example/") ...@@ -20,9 +20,9 @@ algorithm = CheckpointDistance(features_dir="./example/")
# comment out the code below to disable dask # comment out the code below to disable dask
from bob.pipelines.mixins import estimator_dask_it, mix_me_up from bob.pipelines.mixins import estimator_dask_it, mix_me_up
from bob.bio.base.pipelines.vanilla_biometrics.biometric_algorithm import ( from bob.bio.base.pipelines.vanilla_biometrics.mixins import (
BioAlgDaskMixin, BioAlgDaskMixin,
) )
transformer = estimator_dask_it(transformer) #transformer = estimator_dask_it(transformer)
algorithm = mix_me_up([BioAlgDaskMixin], algorithm) #algorithm = mix_me_up(BioAlgDaskMixin, algorithm)
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
from bob.pipelines.sample import Sample, SampleSet, DelayedSample from bob.pipelines.sample import Sample, SampleSet, DelayedSample
import functools
class BioAlgorithm(metaclass=ABCMeta): class BioAlgorithm(metaclass=ABCMeta):
"""Describes a base biometric comparator for the Vanilla Biometrics Pipeline :ref:`_bob.bio.base.struct_bio_rec_sys`_. """Describes a base biometric comparator for the Vanilla Biometrics Pipeline :ref:`_bob.bio.base.struct_bio_rec_sys`_.
...@@ -95,6 +95,7 @@ class BioAlgorithm(metaclass=ABCMeta): ...@@ -95,6 +95,7 @@ class BioAlgorithm(metaclass=ABCMeta):
# a sampleset either after or before scoring. # a sampleset either after or before scoring.
# To be honest, this should be the default behaviour # To be honest, this should be the default behaviour
retval = [] retval = []
for subprobe_id, (s, parent) in enumerate(zip(data, sampleset.samples)): for subprobe_id, (s, parent) in enumerate(zip(data, sampleset.samples)):
# Creating one sample per comparison # Creating one sample per comparison
subprobe_scores = [] subprobe_scores = []
...@@ -201,13 +202,9 @@ def save_scores_four_columns(path, probe): ...@@ -201,13 +202,9 @@ def save_scores_four_columns(path, probe):
line = "{0} {1} {2} {3}\n".format( line = "{0} {1} {2} {3}\n".format(
biometric_reference.subject, biometric_reference.subject,
probe.subject, probe.subject,
probe.key, probe.path,
biometric_reference.data, biometric_reference.data,
) )
f.write(line) f.write(line)
def load(): return DelayedSample(functools.partial(open, path), parent=probe)
with open(path) as f:
return [float(line.split()[-1]) for line in f]
return DelayedSample(load, parent=probe)
...@@ -7,7 +7,7 @@ import os ...@@ -7,7 +7,7 @@ import os
import functools import functools
from collections import defaultdict from collections import defaultdict
from .... import utils from bob.bio.base import utils
from .abstract_classes import BioAlgorithm, Database, save_scores_four_columns from .abstract_classes import BioAlgorithm, Database, save_scores_four_columns
from bob.io.base import HDF5File from bob.io.base import HDF5File
from bob.pipelines.mixins import SampleMixin, CheckpointMixin from bob.pipelines.mixins import SampleMixin, CheckpointMixin
...@@ -30,7 +30,7 @@ def _biofile_to_delayed_sample(biofile, database): ...@@ -30,7 +30,7 @@ def _biofile_to_delayed_sample(biofile, database):
) )
class LegacyDatabaseConnector(Database): class DatabaseConnector(Database):
"""Wraps a bob.bio.base database and generates conforming samples """Wraps a bob.bio.base database and generates conforming samples
This connector allows wrapping generic bob.bio.base datasets and generate This connector allows wrapping generic bob.bio.base datasets and generate
...@@ -50,8 +50,7 @@ class LegacyDatabaseConnector(Database): ...@@ -50,8 +50,7 @@ class LegacyDatabaseConnector(Database):
""" """
def __init__(self, database, **kwargs): def __init__(self, database, **kwargs):
super().__init__(**kwargs)
self.database = database self.database = database
def background_model_samples(self): def background_model_samples(self):
...@@ -96,7 +95,7 @@ class LegacyDatabaseConnector(Database): ...@@ -96,7 +95,7 @@ class LegacyDatabaseConnector(Database):
retval = [] retval = []
for m in self.database.model_ids(groups=group): for m in self.database.model_ids(groups=group):
objects = self.database.enroll_files(groups=group, model_id=m) objects = self.database.enroll_files(group=group, model_id=m)
retval.append( retval.append(
SampleSet( SampleSet(
...@@ -137,7 +136,6 @@ class LegacyDatabaseConnector(Database): ...@@ -137,7 +136,6 @@ class LegacyDatabaseConnector(Database):
# Getting all the probe objects from a particular biometric # Getting all the probe objects from a particular biometric
# reference # reference
objects = self.database.probe_files(group=group, model_id=m) objects = self.database.probe_files(group=group, model_id=m)
# Creating probe samples # Creating probe samples
for o in objects: for o in objects:
if o.id not in probes: if o.id not in probes:
...@@ -194,7 +192,7 @@ def _get_pickable_method(method): ...@@ -194,7 +192,7 @@ def _get_pickable_method(method):
return method return method
class LegacyPreprocessor(CheckpointMixin, SampleMixin, _Preprocessor): class Preprocessor(CheckpointMixin, SampleMixin, _Preprocessor):
def __init__(self, callable, **kwargs): def __init__(self, callable, **kwargs):
instance = callable() instance = callable()
super().__init__( super().__init__(
...@@ -236,7 +234,7 @@ class _Extractor(_NonPickableWrapper, TransformerMixin): ...@@ -236,7 +234,7 @@ class _Extractor(_NonPickableWrapper, TransformerMixin):
return {"requires_fit": self.instance.requires_training} return {"requires_fit": self.instance.requires_training}
class LegacyExtractor(CheckpointMixin, SampleMixin, _Extractor): class Extractor(CheckpointMixin, SampleMixin, _Extractor):
def __init__(self, callable, model_path, **kwargs): def __init__(self, callable, model_path, **kwargs):
instance = callable() instance = callable()
...@@ -288,7 +286,7 @@ class _AlgorithmTransformer(_NonPickableWrapper, TransformerMixin): ...@@ -288,7 +286,7 @@ class _AlgorithmTransformer(_NonPickableWrapper, TransformerMixin):
return {"requires_fit": self.instance.requires_projector_training} return {"requires_fit": self.instance.requires_projector_training}
class LegacyAlgorithmAsTransformer(CheckpointMixin, SampleMixin, _AlgorithmTransformer): class AlgorithmAsTransformer(CheckpointMixin, SampleMixin, _AlgorithmTransformer):
"""Class that wraps :py:class:`bob.bio.base.algorithm.Algoritm` """Class that wraps :py:class:`bob.bio.base.algorithm.Algoritm`
:py:method:`LegacyAlgorithmrMixin.fit` maps to :py:method:`bob.bio.base.algorithm.Algoritm.train_projector` :py:method:`LegacyAlgorithmrMixin.fit` maps to :py:method:`bob.bio.base.algorithm.Algoritm.train_projector`
...@@ -341,7 +339,7 @@ class LegacyAlgorithmAsTransformer(CheckpointMixin, SampleMixin, _AlgorithmTrans ...@@ -341,7 +339,7 @@ class LegacyAlgorithmAsTransformer(CheckpointMixin, SampleMixin, _AlgorithmTrans
return self return self
class LegacyAlgorithmAsBioAlg(BioAlgorithm, _NonPickableWrapper): class AlgorithmAsBioAlg(BioAlgorithm, _NonPickableWrapper):
"""Biometric Algorithm that handles legacy :py:class:`bob.bio.base.algorithm.Algorithm` """Biometric Algorithm that handles legacy :py:class:`bob.bio.base.algorithm.Algorithm`
......
...@@ -26,7 +26,7 @@ class BioAlgCheckpointMixin(CheckpointMixin): ...@@ -26,7 +26,7 @@ class BioAlgCheckpointMixin(CheckpointMixin):
""" """
def __init__(self, features_dir, **kwargs): def __init__(self, features_dir="", **kwargs):
super().__init__(features_dir=features_dir, **kwargs) super().__init__(features_dir=features_dir, **kwargs)
self.biometric_reference_dir = os.path.join( self.biometric_reference_dir = os.path.join(
features_dir, "biometric_references" features_dir, "biometric_references"
...@@ -63,15 +63,16 @@ class BioAlgCheckpointMixin(CheckpointMixin): ...@@ -63,15 +63,16 @@ class BioAlgCheckpointMixin(CheckpointMixin):
# If sample already there, just load # If sample already there, just load
delayed_enrolled_sample = self.load(path) delayed_enrolled_sample = self.load(path)
delayed_enrolled_sample.key = sampleset.key delayed_enrolled_sample.key = sampleset.key
delayed_enrolled_sample.subject = sampleset.subject
return delayed_enrolled_sample return delayed_enrolled_sample
def _score_sample_set(self, sampleset, biometric_references): def _score_sample_set(self, sampleset, biometric_references):
"""Given a sampleset for probing, compute the scores and retures a sample set with the scores """Given a sampleset for probing, compute the scores and retures a sample set with the scores
""" """
# Computing score # Computing score
scored_sample_set = super()._score_sample_set(sampleset, biometric_references) scored_sample_set = super()._score_sample_set(sampleset, biometric_references)
for s in scored_sample_set: for s in scored_sample_set:
# Checkpointing score # Checkpointing score
path = os.path.join(self.score_dir, str(s.path) + ".txt") path = os.path.join(self.score_dir, str(s.path) + ".txt")
......
...@@ -156,7 +156,7 @@ def vanilla_biometrics( ...@@ -156,7 +156,7 @@ def vanilla_biometrics(
import dask.bag import dask.bag
import itertools import itertools
import os import os
from bob.pipelines.sample import Sample from bob.pipelines.sample import Sample, DelayedSample
if not os.path.exists(output): if not os.path.exists(output):
os.makedirs(output, exist_ok=True) os.makedirs(output, exist_ok=True)
...@@ -189,12 +189,10 @@ def vanilla_biometrics( ...@@ -189,12 +189,10 @@ def vanilla_biometrics(
result = itertools.chain(*result) result = itertools.chain(*result)
for probe in result: for probe in result:
for sample in probe.samples: for sample in probe.samples:
if isinstance(sample, Sample): if isinstance(sample, Sample):
line = "{0} {1} {2} {3}\n".format( f.write("{0} {1} {2} {3}\n".format(sample.key, probe.key, probe.path, sample.data))
sample.key, probe.key, probe.path, sample.data elif isinstance(sample, DelayedSample):
) f.writelines(sample.load().readlines())
f.write(line)
else: else:
raise TypeError("The output of the pipeline is not writeble") raise TypeError("The output of the pipeline is not writeble")
......
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