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