Skip to content
Snippets Groups Projects
Commit 27c75ad2 authored by Tiago de Freitas Pereira's avatar Tiago de Freitas Pereira
Browse files

Porting some baselines

parent 0a00da69
No related branches found
No related tags found
2 merge requests!66Adding some baselines as transformers,!64Dask pipelines
from bob.bio.base.pipelines.vanilla_biometrics import Distance, VanillaBiometricsPipeline, BioAlgorithmLegacy from bob.bio.base.pipelines.vanilla_biometrics import (
from bob.bio.face.helpers import face_crop_solver Distance,
VanillaBiometricsPipeline,
BioAlgorithmLegacy,
)
from bob.bio.face.config.baseline.helpers import crop_80x64
import math import math
import numpy as np import numpy as np
import bob.bio.face import bob.bio.face
...@@ -19,57 +23,22 @@ else: ...@@ -19,57 +23,22 @@ else:
####### SOLVING THE FACE CROPPER TO BE USED ########## ####### SOLVING THE FACE CROPPER TO BE USED ##########
# Cropping # Cropping
CROPPED_IMAGE_HEIGHT = 80 face_cropper, transform_extra_arguments = crop_80x64(
CROPPED_IMAGE_WIDTH = CROPPED_IMAGE_HEIGHT * 4 // 5 annotation_type, fixed_positions, color_channel="gray"
# eye positions for frontal images
RIGHT_EYE_POS = (CROPPED_IMAGE_HEIGHT // 5, CROPPED_IMAGE_WIDTH // 4 - 1)
LEFT_EYE_POS = (CROPPED_IMAGE_HEIGHT // 5, CROPPED_IMAGE_WIDTH // 4 * 3)
cropped_image_size = (CROPPED_IMAGE_HEIGHT, CROPPED_IMAGE_WIDTH)
color_channel = "gray"
if annotation_type == "bounding-box":
transform_extra_arguments = (("annotations", "annotations"),)
TOP_LEFT_POS = (0, 0)
BOTTOM_RIGHT_POS = (CROPPED_IMAGE_HEIGHT, CROPPED_IMAGE_WIDTH)
# Detects the face and crops it without eye detection
face_cropper = face_crop_solver(
cropped_image_size,
color_channel=color_channel,
cropped_positions={"topleft": TOP_LEFT_POS, "bottomright": BOTTOM_RIGHT_POS},
fixed_positions=fixed_positions,
)
elif annotation_type == "eyes-center":
transform_extra_arguments = (("annotations", "annotations"),)
# eye positions for frontal images
# Detects the face and crops it without eye detection
face_cropper = face_crop_solver(
cropped_image_size,
color_channel=color_channel,
cropped_positions={"leye": LEFT_EYE_POS, "reye": RIGHT_EYE_POS},
fixed_positions=fixed_positions,
) )
else:
transform_extra_arguments = None
# DEFAULT TO FACE SIMPLE RESIZE
face_cropper = face_crop_solver(cropped_image_size)
preprocessor = bob.bio.face.preprocessor.INormLBP( preprocessor = bob.bio.face.preprocessor.INormLBP(
face_cropper = face_cropper, face_cropper=face_cropper, dtype=np.float64
dtype = np.float64
) )
#### FEATURE EXTRACTOR ###### #### FEATURE EXTRACTOR ######
gabor_graph = bob.bio.face.extractor.GridGraph( # legacy objects needs to be wrapped with legacy transformers
from bob.bio.base.transformers import ExtractorTransformer
gabor_graph = ExtractorTransformer(
bob.bio.face.extractor.GridGraph(
# Gabor parameters # Gabor parameters
gabor_sigma=math.sqrt(2.0) * math.pi, gabor_sigma=math.sqrt(2.0) * math.pi,
# what kind of information to extract # what kind of information to extract
...@@ -77,18 +46,16 @@ gabor_graph = bob.bio.face.extractor.GridGraph( ...@@ -77,18 +46,16 @@ gabor_graph = bob.bio.face.extractor.GridGraph(
# setup of the fixed grid # setup of the fixed grid
node_distance=(8, 8), node_distance=(8, 8),
) )
)
transformer = make_pipeline( transformer = make_pipeline(
wrap( wrap(
["sample"], ["sample"], preprocessor, transform_extra_arguments=transform_extra_arguments,
preprocessor,
transform_extra_arguments=transform_extra_arguments,
), ),
wrap(["sample"], gabor_graph), wrap(["sample"], gabor_graph),
) )
gabor_jet = bob.bio.face.algorithm.GaborJet( gabor_jet = bob.bio.face.algorithm.GaborJet(
gabor_jet_similarity_type="PhaseDiffPlusCanberra", gabor_jet_similarity_type="PhaseDiffPlusCanberra",
multiple_feature_scoring="max_jet", multiple_feature_scoring="max_jet",
...@@ -98,7 +65,4 @@ gabor_jet = bob.bio.face.algorithm.GaborJet( ...@@ -98,7 +65,4 @@ gabor_jet = bob.bio.face.algorithm.GaborJet(
tempdir = tempfile.TemporaryDirectory() tempdir = tempfile.TemporaryDirectory()
algorithm = BioAlgorithmLegacy(gabor_jet, base_dir=tempdir.name) algorithm = BioAlgorithmLegacy(gabor_jet, base_dir=tempdir.name)
pipeline = VanillaBiometricsPipeline( pipeline = VanillaBiometricsPipeline(transformer, algorithm)
transformer,
algorithm
)
...@@ -11,7 +11,7 @@ def embedding_transformer_160x160(embedding, annotation_type, fixed_positions): ...@@ -11,7 +11,7 @@ def embedding_transformer_160x160(embedding, annotation_type, fixed_positions):
This transformer is suited for Facenet based architectures This transformer is suited for Facenet based architectures
.. warning:: .. warning::
This will redirect images to :math:`160 \times 160` This will resize images to :math:`160 \times 160`
""" """
...@@ -72,7 +72,7 @@ def embedding_transformer_112x112(embedding, annotation_type, fixed_positions): ...@@ -72,7 +72,7 @@ def embedding_transformer_112x112(embedding, annotation_type, fixed_positions):
This transformer is suited for Facenet based architectures This transformer is suited for Facenet based architectures
.. warning:: .. warning::
This will redirect images to :math:`160 \times 160` This will resize images to :math:`112 \times 112`
""" """
...@@ -125,3 +125,78 @@ def embedding_transformer_112x112(embedding, annotation_type, fixed_positions): ...@@ -125,3 +125,78 @@ def embedding_transformer_112x112(embedding, annotation_type, fixed_positions):
) )
return transformer return transformer
def crop_80x64(annotation_type, fixed_positions=None, color_channel="gray"):
"""
Crops a face to :math:`80 \times 64`
Parameters
----------
annotation_type: str
Type of annotations. Possible values are: `bounding-box`, `eyes-center` and None
fixed_positions: tuple
A tuple containing the annotations. This is used in case your input is already registered
with fixed positions (eyes or bounding box)
color_channel: str
Returns
-------
face_cropper:
A face cropper to be used
transform_extra_arguments:
The parameters to the transformer
"""
# Cropping
CROPPED_IMAGE_HEIGHT = 80
CROPPED_IMAGE_WIDTH = CROPPED_IMAGE_HEIGHT * 4 // 5
# eye positions for frontal images
RIGHT_EYE_POS = (CROPPED_IMAGE_HEIGHT // 5, CROPPED_IMAGE_WIDTH // 4 - 1)
LEFT_EYE_POS = (CROPPED_IMAGE_HEIGHT // 5, CROPPED_IMAGE_WIDTH // 4 * 3)
cropped_image_size = (CROPPED_IMAGE_HEIGHT, CROPPED_IMAGE_WIDTH)
color_channel = color_channel
if annotation_type == "bounding-box":
transform_extra_arguments = (("annotations", "annotations"),)
TOP_LEFT_POS = (0, 0)
BOTTOM_RIGHT_POS = (CROPPED_IMAGE_HEIGHT, CROPPED_IMAGE_WIDTH)
# Detects the face and crops it without eye detection
face_cropper = face_crop_solver(
cropped_image_size,
color_channel=color_channel,
cropped_positions={"topleft": TOP_LEFT_POS, "bottomright": BOTTOM_RIGHT_POS},
fixed_positions=fixed_positions,
)
elif annotation_type == "eyes-center":
transform_extra_arguments = (("annotations", "annotations"),)
# eye positions for frontal images
# Detects the face and crops it without eye detection
face_cropper = face_crop_solver(
cropped_image_size,
color_channel=color_channel,
cropped_positions={"leye": LEFT_EYE_POS, "reye": RIGHT_EYE_POS},
fixed_positions=fixed_positions,
)
else:
transform_extra_arguments = None
# DEFAULT TO FACE SIMPLE RESIZE
face_cropper = face_crop_solver(cropped_image_size)
return face_cropper, transform_extra_arguments
from bob.bio.base.pipelines.vanilla_biometrics import (
Distance,
VanillaBiometricsPipeline,
BioAlgorithmLegacy,
)
from bob.bio.face.config.baseline.helpers import crop_80x64
import numpy as np
import bob.bio.face
from sklearn.pipeline import make_pipeline
from bob.pipelines import wrap
import tempfile
from bob.bio.base.transformers import AlgorithmTransformer
from bob.pipelines.transformers import SampleLinearize
import os
#### SOLVING IF THERE'S ANY DATABASE INFORMATION
if "database" in locals():
annotation_type = database.annotation_type
fixed_positions = database.fixed_positions
else:
annotation_type = None
fixed_positions = None
####### SOLVING THE FACE CROPPER TO BE USED ##########
# Cropping
face_cropper, transform_extra_arguments = crop_80x64(
annotation_type, fixed_positions, color_channel="gray"
)
preprocessor = bob.bio.face.preprocessor.TanTriggs(
face_cropper=face_cropper, dtype=np.float64
)
#### FEATURE EXTRACTOR ######
tempdir = tempfile.TemporaryDirectory()
lda = bob.bio.base.algorithm.LDA(use_pinv=True, pca_subspace_dimension=0.90)
lda_transformer = AlgorithmTransformer(
lda, projector_file=os.path.join(tempdir.name, "Projector.hdf5")
)
transformer = make_pipeline(
wrap(
["sample"], preprocessor, transform_extra_arguments=transform_extra_arguments,
),
SampleLinearize(),
wrap(["sample"], lda_transformer),
)
### BIOMETRIC ALGORITHM
algorithm = BioAlgorithmLegacy(
lda,
base_dir=tempdir.name,
projector_file=os.path.join(tempdir.name, "Projector.hdf5"),
)
pipeline = VanillaBiometricsPipeline(transformer, algorithm)
from bob.bio.base.pipelines.vanilla_biometrics import (
Distance,
VanillaBiometricsPipeline,
BioAlgorithmLegacy,
)
from bob.bio.face.config.baseline.helpers import crop_80x64
import math
import numpy as np
import bob.bio.face
from sklearn.pipeline import make_pipeline
from bob.pipelines import wrap
import tempfile
import bob.math
#### SOLVING IF THERE'S ANY DATABASE INFORMATION
if "database" in locals():
annotation_type = database.annotation_type
fixed_positions = database.fixed_positions
else:
annotation_type = None
fixed_positions = None
####### SOLVING THE FACE CROPPER TO BE USED ##########
# Cropping
face_cropper, transform_extra_arguments = crop_80x64(
annotation_type, fixed_positions, color_channel="gray"
)
preprocessor = bob.bio.face.preprocessor.TanTriggs(
face_cropper=face_cropper, dtype=np.float64
)
#### FEATURE EXTRACTOR ######
lgbphs = bob.bio.face.extractor.LGBPHS(
# block setup
block_size = 8,
block_overlap = 0,
# Gabor parameters
gabor_sigma = math.sqrt(2.) * math.pi,
# LBP setup (we use the defaults)
# histogram setup
sparse_histogram = True
)
transformer = make_pipeline(
wrap(
["sample"], preprocessor, transform_extra_arguments=transform_extra_arguments,
),
wrap(["sample"], lgbphs),
)
### BIOMETRIC ALGORITHM
histogram = bob.bio.face.algorithm.Histogram(
distance_function = bob.math.histogram_intersection,
is_distance_function = False
)
tempdir = tempfile.TemporaryDirectory()
algorithm = BioAlgorithmLegacy(histogram, base_dir=tempdir.name)
pipeline = VanillaBiometricsPipeline(transformer, algorithm)
...@@ -3,7 +3,10 @@ import pkg_resources ...@@ -3,7 +3,10 @@ import pkg_resources
import numpy as np import numpy as np
from bob.pipelines import Sample, SampleSet, DelayedSample from bob.pipelines import Sample, SampleSet, DelayedSample
from bob.bio.base import load_resource from bob.bio.base import load_resource
from bob.bio.base.pipelines.vanilla_biometrics import checkpoint_vanilla_biometrics, dask_vanilla_biometrics from bob.bio.base.pipelines.vanilla_biometrics import (
checkpoint_vanilla_biometrics,
dask_vanilla_biometrics,
)
import tempfile import tempfile
import os import os
import bob.io.base import bob.io.base
...@@ -44,23 +47,32 @@ def get_fake_sample_set(face_size=(160, 160), purpose="bioref"): ...@@ -44,23 +47,32 @@ def get_fake_sample_set(face_size=(160, 160), purpose="bioref"):
] ]
def run_baseline(baseline): def get_fake_samples_for_training():
data = np.random.rand(10, 3, 400, 400)
annotations = {"reye": (131, 176), "leye": (222, 170)}
return [
Sample(x, key=str(i), subject=str(i), annotations=annotations) for i,x in enumerate(data)
]
def run_baseline(baseline, samples_for_training=[]):
biometric_references = get_fake_sample_set(purpose="bioref") biometric_references = get_fake_sample_set(purpose="bioref")
probes = get_fake_sample_set(purpose="probe") probes = get_fake_sample_set(purpose="probe")
# Regular pipeline # Regular pipeline
pipeline = load_resource(baseline, "baseline") pipeline = load_resource(baseline, "baseline")
scores = pipeline([], biometric_references, probes) scores = pipeline(samples_for_training, biometric_references, probes)
assert len(scores) == 1 assert len(scores) == 1
assert len(scores[0]) == 1 assert len(scores[0]) == 1
# CHECKPOINTING # CHECKPOINTING
import ipdb; ipdb.set_trace()
with tempfile.TemporaryDirectory() as d: with tempfile.TemporaryDirectory() as d:
checkpoint_pipeline = checkpoint_vanilla_biometrics(copy.deepcopy(pipeline), base_dir=d) checkpoint_pipeline = checkpoint_vanilla_biometrics(
copy.deepcopy(pipeline), base_dir=d
)
checkpoint_scores = checkpoint_pipeline([], biometric_references, probes) checkpoint_scores = checkpoint_pipeline([], biometric_references, probes)
assert len(checkpoint_scores) == 1 assert len(checkpoint_scores) == 1
assert len(checkpoint_scores[0]) == 1 assert len(checkpoint_scores[0]) == 1
...@@ -72,11 +84,12 @@ def run_baseline(baseline): ...@@ -72,11 +84,12 @@ def run_baseline(baseline):
assert "samplewrapper-2" in dirs assert "samplewrapper-2" in dirs
assert "scores" in dirs assert "scores" in dirs
# DASK # DASK
with tempfile.TemporaryDirectory() as d: with tempfile.TemporaryDirectory() as d:
dask_pipeline = dask_vanilla_biometrics(checkpoint_vanilla_biometrics(copy.deepcopy(pipeline), base_dir=d)) dask_pipeline = dask_vanilla_biometrics(
checkpoint_vanilla_biometrics(copy.deepcopy(pipeline), base_dir=d)
)
dask_scores = dask_pipeline([], biometric_references, probes) dask_scores = dask_pipeline([], biometric_references, probes)
dask_scores = dask_scores.compute(scheduler="single-threaded") dask_scores = dask_scores.compute(scheduler="single-threaded")
assert len(dask_scores) == 1 assert len(dask_scores) == 1
...@@ -109,8 +122,10 @@ def test_inception_resnetv1_msceleb(): ...@@ -109,8 +122,10 @@ def test_inception_resnetv1_msceleb():
def test_inception_resnetv1_casiawebface(): def test_inception_resnetv1_casiawebface():
run_baseline("inception_resnetv1_casiawebface") run_baseline("inception_resnetv1_casiawebface")
def test_arcface_insight_tf(): def test_arcface_insight_tf():
import tensorflow as tf import tensorflow as tf
tf.compat.v1.reset_default_graph() tf.compat.v1.reset_default_graph()
run_baseline("arcface_insight_tf") run_baseline("arcface_insight_tf")
...@@ -118,3 +133,7 @@ def test_arcface_insight_tf(): ...@@ -118,3 +133,7 @@ def test_arcface_insight_tf():
def test_gabor_graph(): def test_gabor_graph():
run_baseline("gabor_graph") run_baseline("gabor_graph")
#def test_lda():
# run_baseline("lda", get_fake_samples_for_training())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment