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

Fixed the examples

parent 80b35bcb
No related branches found
No related tags found
1 merge request!180[dask] Preparing bob.bio.base for dask pipelines
Pipeline #38398 failed
...@@ -2,20 +2,17 @@ from bob.bio.face.database import AtntBioDatabase ...@@ -2,20 +2,17 @@ from bob.bio.face.database import AtntBioDatabase
from bob.bio.base.algorithm import LDA from bob.bio.base.algorithm import LDA
from bob.bio.face.preprocessor import FaceCrop from bob.bio.face.preprocessor import FaceCrop
from sklearn.pipeline import make_pipeline from sklearn.pipeline import make_pipeline
from bob.bio.base.mixins.legacy import (
LegacyPreprocessor,
LegacyAlgorithmAsTransformer,
)
from bob.pipelines.transformers import CheckpointSampleLinearize from bob.pipelines.transformers import CheckpointSampleLinearize
from bob.bio.base.pipelines.vanilla_biometrics.legacy import LegacyDatabaseConnector from bob.bio.base.pipelines.vanilla_biometrics.legacy import DatabaseConnector, Preprocessor, AlgorithmAsTransformer
import functools import functools
from bob.bio.base.pipelines.vanilla_biometrics.biometric_algorithm import ( from bob.bio.base.pipelines.vanilla_biometrics.implemented import (
Distance,
CheckpointDistance, CheckpointDistance,
) )
# DATABASE # DATABASE
database = LegacyDatabaseConnector( database = DatabaseConnector(
AtntBioDatabase(original_directory="./atnt", protocol="Default"), AtntBioDatabase(original_directory="./atnt", protocol="Default"),
) )
...@@ -49,20 +46,20 @@ lda = functools.partial(LDA, use_pinv=True, pca_subspace_dimension=0.90) ...@@ -49,20 +46,20 @@ lda = functools.partial(LDA, use_pinv=True, pca_subspace_dimension=0.90)
transformer = make_pipeline( transformer = make_pipeline(
LegacyPreprocessor(callable=face_cropper, features_dir="./example/transformer0"), Preprocessor(callable=face_cropper, features_dir="./example/transformer0"),
CheckpointSampleLinearize(features_dir="./example/transformer1"), CheckpointSampleLinearize(features_dir="./example/transformer1"),
LegacyAlgorithmAsTransformer( AlgorithmAsTransformer(
callable=lda, features_dir="./example/transformer2", model_path="./example/" callable=lda, features_dir="./example/transformer2", model_path="./example/lda_projector.hdf5"
), ),
) )
algorithm = CheckpointDistance(features_dir="./example/") algorithm = CheckpointDistance(features_dir="./example/")
# algorithm = Distance()
# 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,
) )
......
...@@ -5,10 +5,11 @@ from bob.bio.base.pipelines.vanilla_biometrics.implemented import ( ...@@ -5,10 +5,11 @@ 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
import os
database = DatabaseConnector( database = DatabaseConnector(
AtntBioDatabase(original_directory="./atnt"), protocol="Default" AtntBioDatabase(original_directory=os.environ.get("ATNT_DATABASE_DIRECTORY")), protocol="Default"
) )
transformer = make_pipeline( transformer = make_pipeline(
CheckpointSampleLinearize(features_dir="./example/extractor0"), CheckpointSampleLinearize(features_dir="./example/extractor0"),
...@@ -18,11 +19,11 @@ transformer = make_pipeline( ...@@ -18,11 +19,11 @@ transformer = make_pipeline(
) )
algorithm = CheckpointDistance(features_dir="./example/") 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.mixins 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 bob.bio.base.pipelines.vanilla_biometrics.biometric_algorithm import ( from bob.bio.base.pipelines.vanilla_biometrics.implemented import (
CheckpointDistance, CheckpointDistance,
) )
from bob.bio.base.pipelines.vanilla_biometrics.legacy import ( from bob.bio.base.pipelines.vanilla_biometrics.legacy import (
LegacyDatabaseConnector, DatabaseConnector,
LegacyPreprocessor, Preprocessor,
) )
from bob.bio.face.database.mobio import MobioBioDatabase from bob.bio.face.database.mobio import MobioBioDatabase
from bob.bio.face.preprocessor import FaceCrop from bob.bio.face.preprocessor import FaceCrop
...@@ -13,7 +13,7 @@ from sklearn.pipeline import make_pipeline ...@@ -13,7 +13,7 @@ from sklearn.pipeline import make_pipeline
import functools import functools
database = LegacyDatabaseConnector( database = DatabaseConnector(
MobioBioDatabase( MobioBioDatabase(
original_directory=rc["bob.db.mobio.directory"], original_directory=rc["bob.db.mobio.directory"],
annotation_directory=rc["bob.db.mobio.annotation_directory"], annotation_directory=rc["bob.db.mobio.annotation_directory"],
...@@ -36,17 +36,17 @@ preprocessor = functools.partial( ...@@ -36,17 +36,17 @@ preprocessor = functools.partial(
) )
transformer = make_pipeline( transformer = make_pipeline(
LegacyPreprocessor(preprocessor), Preprocessor(preprocessor, features_dir="./example/extractor0"),
CheckpointSampleLinearize(features_dir="./example/extractor0"), CheckpointSampleLinearize(features_dir="./example/extractor1"),
CheckpointSamplePCA( CheckpointSamplePCA(
features_dir="./example/extractor1", model_path="./example/pca.pkl" features_dir="./example/extractor2", model_path="./example/pca.pkl"
), ),
) )
algorithm = CheckpointDistance(features_dir="./example/") 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,
) )
......
...@@ -2,6 +2,7 @@ from abc import ABCMeta, abstractmethod ...@@ -2,6 +2,7 @@ from abc import ABCMeta, abstractmethod
from bob.pipelines.sample import Sample, SampleSet, DelayedSample from bob.pipelines.sample import Sample, SampleSet, DelayedSample
import functools 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`_.
...@@ -102,7 +103,9 @@ class BioAlgorithm(metaclass=ABCMeta): ...@@ -102,7 +103,9 @@ class BioAlgorithm(metaclass=ABCMeta):
for ref in [ for ref in [
r for r in biometric_references if r.key in sampleset.references r for r in biometric_references if r.key in sampleset.references
]: ]:
subprobe_scores.append(Sample(self.score(ref.data, s), parent=ref)) score = self.score(ref.data, s)
data = make_score_line(ref.subject, sampleset.subject, sampleset.path, score)
subprobe_scores.append(Sample(data, parent=ref))
# Creating one sampleset per probe # Creating one sampleset per probe
subprobe = SampleSet(subprobe_scores, parent=sampleset) subprobe = SampleSet(subprobe_scores, parent=sampleset)
...@@ -192,6 +195,18 @@ class Database(metaclass=ABCMeta): ...@@ -192,6 +195,18 @@ class Database(metaclass=ABCMeta):
pass pass
def make_score_line(
biometric_reference_subject, probe_subject, probe_path, score,
):
data = "{0} {1} {2} {3}\n".format(
biometric_reference_subject,
probe_subject,
probe_path,
score,
)
return data
def save_scores_four_columns(path, probe): def save_scores_four_columns(path, probe):
""" """
Write scores in the four columns format Write scores in the four columns format
...@@ -199,7 +214,7 @@ def save_scores_four_columns(path, probe): ...@@ -199,7 +214,7 @@ def save_scores_four_columns(path, probe):
with open(path, "w") as f: with open(path, "w") as f:
for biometric_reference in probe.samples: for biometric_reference in probe.samples:
line = "{0} {1} {2} {3}\n".format( line = make_score_line(
biometric_reference.subject, biometric_reference.subject,
probe.subject, probe.subject,
probe.path, probe.path,
...@@ -207,4 +222,8 @@ def save_scores_four_columns(path, probe): ...@@ -207,4 +222,8 @@ def save_scores_four_columns(path, probe):
) )
f.write(line) f.write(line)
return DelayedSample(functools.partial(open, path), parent=probe) def load():
with open(path) as f:
return f.read()
return DelayedSample(load, parent=probe)
...@@ -13,7 +13,7 @@ from bob.io.base import HDF5File ...@@ -13,7 +13,7 @@ from bob.io.base import HDF5File
from bob.pipelines.mixins import SampleMixin, CheckpointMixin from bob.pipelines.mixins import SampleMixin, CheckpointMixin
from bob.pipelines.sample import DelayedSample, SampleSet, Sample from bob.pipelines.sample import DelayedSample, SampleSet, Sample
from bob.pipelines.utils import is_picklable from bob.pipelines.utils import is_picklable
from sklearn.base import TransformerMixin from sklearn.base import TransformerMixin, BaseEstimator
import logging import logging
logger = logging.getLogger("bob.bio.base") logger = logging.getLogger("bob.bio.base")
...@@ -24,6 +24,7 @@ def _biofile_to_delayed_sample(biofile, database): ...@@ -24,6 +24,7 @@ def _biofile_to_delayed_sample(biofile, database):
load=functools.partial( load=functools.partial(
biofile.load, database.original_directory, database.original_extension, biofile.load, database.original_directory, database.original_extension,
), ),
subject=str(biofile.client_id),
key=biofile.path, key=biofile.path,
path=biofile.path, path=biofile.path,
annotations=database.annotations(biofile), annotations=database.annotations(biofile),
...@@ -50,7 +51,7 @@ class DatabaseConnector(Database): ...@@ -50,7 +51,7 @@ class DatabaseConnector(Database):
""" """
def __init__(self, database, **kwargs): def __init__(self, database, **kwargs):
self.database = database self.database = database
def background_model_samples(self): def background_model_samples(self):
...@@ -175,7 +176,7 @@ class _NonPickableWrapper: ...@@ -175,7 +176,7 @@ class _NonPickableWrapper:
return super().__getstate__() return super().__getstate__()
class _Preprocessor(_NonPickableWrapper, TransformerMixin): class _Preprocessor(_NonPickableWrapper, TransformerMixin, BaseEstimator):
def transform(self, X, annotations): def transform(self, X, annotations):
return [self.instance(data, annot) for data, annot in zip(X, annotations)] return [self.instance(data, annot) for data, annot in zip(X, annotations)]
...@@ -212,7 +213,7 @@ def _split_X_by_y(X, y): ...@@ -212,7 +213,7 @@ def _split_X_by_y(X, y):
return training_data return training_data
class _Extractor(_NonPickableWrapper, TransformerMixin): class _Extractor(_NonPickableWrapper, TransformerMixin, BaseEstimator):
def transform(self, X, metadata=None): def transform(self, X, metadata=None):
if self.requires_metadata: if self.requires_metadata:
return [self.instance(data, metadata=m) for data, m in zip(X, metadata)] return [self.instance(data, metadata=m) for data, m in zip(X, metadata)]
...@@ -267,7 +268,7 @@ class Extractor(CheckpointMixin, SampleMixin, _Extractor): ...@@ -267,7 +268,7 @@ class Extractor(CheckpointMixin, SampleMixin, _Extractor):
return self return self
class _AlgorithmTransformer(_NonPickableWrapper, TransformerMixin): class _AlgorithmTransformer(_NonPickableWrapper, TransformerMixin, BaseEstimator):
def transform(self, X): def transform(self, X):
return [self.instance.project(feature) for feature in X] return [self.instance.project(feature) for feature in X]
...@@ -279,7 +280,7 @@ class _AlgorithmTransformer(_NonPickableWrapper, TransformerMixin): ...@@ -279,7 +280,7 @@ class _AlgorithmTransformer(_NonPickableWrapper, TransformerMixin):
if self.instance.split_training_features_by_client: if self.instance.split_training_features_by_client:
training_data = _split_X_by_y(X, y) training_data = _split_X_by_y(X, y)
self.instance.train_projector(self, training_data, self.model_path) self.instance.train_projector(training_data, self.model_path)
return self return self
def _more_tags(self): def _more_tags(self):
......
...@@ -189,12 +189,7 @@ def vanilla_biometrics( ...@@ -189,12 +189,7 @@ 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): f.write(sample.data)
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")
if dask_client is not None: if dask_client is not None:
dask_client.shutdown() dask_client.shutdown()
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