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

Removed EigenFace Baseline. This is covered in other base packages

parent 7e74b2f0
No related branches found
No related tags found
1 merge request!64Dask pipelines
...@@ -8,11 +8,6 @@ Defining some face recognition baselines ...@@ -8,11 +8,6 @@ Defining some face recognition baselines
from bob.bio.base.baseline import Baseline from bob.bio.base.baseline import Baseline
eigenface = Baseline(name="eigenface",
preprocessors={'default': 'face-crop-eyes', 'atnt': 'base'},
extractor='linearize',
algorithm='pca')
lda = Baseline(name="lda", lda = Baseline(name="lda",
preprocessors={'default': 'face-crop-eyes', 'atnt': 'base'}, preprocessors={'default': 'face-crop-eyes', 'atnt': 'base'},
extractor='eigenface', extractor='eigenface',
......
#!/usr/bin/env python
import bob.bio.face
# compute eigenfaces using the training database
extractor = bob.bio.face.extractor.Eigenface(
subspace_dimension = .95
)
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Manuel Guenther <Manuel.Guenther@idiap.ch>
import numpy
import bob.learn.linear
import bob.io.base
from bob.bio.base.extractor import Extractor
import logging
logger = logging.getLogger("bob.bio.face")
class Eigenface (Extractor):
"""Performs a principal component analysis (PCA) on the given data.
This algorithm computes a PCA projection (:py:class:`bob.learn.linear.PCATrainer`) on the given training images, and projects the images into face space.
In opposition to :py:class:`bob.bio.base.algorithm.PCA`, here the eigenfces are used as features, i.e., to apply advanced face recognition algorithms on top of them.
**Parameters:**
subspace_dimension : int or float
If specified as ``int``, defines the number of eigenvectors used in the PCA projection matrix.
If specified as ``float`` (between 0 and 1), the number of eigenvectors is calculated such that the given percentage of variance is kept.
kwargs : ``key=value`` pairs
A list of keyword arguments directly passed to the :py:class:`bob.bio.base.extractor.Extractor` base class constructor.
"""
def __init__(self, subspace_dimension):
# We have to register that this function will need a training step
Extractor.__init__(self, requires_training = True, subspace_dimension = subspace_dimension)
self.subspace_dimension = subspace_dimension
def _check_data(self, data):
"""Checks that the given data are appropriate."""
assert isinstance(data, numpy.ndarray)
assert data.ndim == 2
assert data.dtype == numpy.float64
def train(self, training_images, extractor_file):
"""Generates the PCA covariance matrix and writes it into the given extractor_file.
Beforehand, all images are turned into a 1D pixel vector.
**Parameters:**
training_images : [2D :py:class:`numpy.ndarray`]
A list of 2D training images to train the PCA projection matrix with.
extractor_file : str
A writable file, into which the PCA projection matrix (as a :py:class:`bob.learn.linear.Machine`) will be written.
"""
[self._check_data(image) for image in training_images]
# Initializes an array for the data
data = numpy.vstack([image.flatten() for image in training_images])
logger.info(" -> Training LinearMachine using PCA (SVD)")
t = bob.learn.linear.PCATrainer()
self.machine, variances = t.train(data)
# compute variance percentage, if desired
if isinstance(self.subspace_dimension, float):
cummulated = numpy.cumsum(variances) / numpy.sum(variances)
for index in range(len(cummulated)):
if cummulated[index] > self.subspace_dimension:
self.subspace_dimension = index
break
self.subspace_dimension = index
logger.info(" -> Keeping %d eigenvectors" % self.subspace_dimension)
# Machine: get shape, then resize
self.machine.resize(self.machine.shape[0], self.subspace_dimension)
self.machine.save(bob.io.base.HDF5File(extractor_file, "w"))
def load(self, extractor_file):
"""Reads the PCA projection matrix from file.
**Parameters:**
extractor_file : str
An existing file, from which the PCA projection matrix are read.
"""
# read PCA projector
self.machine = bob.learn.linear.Machine(bob.io.base.HDF5File(extractor_file))
def __call__(self, image):
"""__call__(image) -> feature
Projects the given image using the stored covariance matrix.
Beforehand, the image is turned into a 1D pixel vector.
**Parameters:**
image : 2D :py:class:`numpy.ndarray` (floats)
The image to extract the eigenface feature from.
**Returns:**
feature : 1D :py:class:`numpy.ndarray` (floats)
The extracted eigenface feature.
"""
self._check_data(image)
# Projects the data
return self.machine(image.flatten())
...@@ -153,44 +153,6 @@ def test_lgbphs(): ...@@ -153,44 +153,6 @@ def test_lgbphs():
_compare(feature, reference, lgbphs.write_feature, lgbphs.read_feature) _compare(feature, reference, lgbphs.write_feature, lgbphs.read_feature)
def test_eigenface():
temp_file = bob.io.base.test_utils.temporary_filename()
data = _data()
eigen1 = bob.bio.base.load_resource('eigenface', 'extractor', preferred_package='bob.bio.face')
assert isinstance(eigen1, bob.bio.face.extractor.Eigenface)
assert isinstance(eigen1, bob.bio.base.extractor.Extractor)
assert eigen1.requires_training
# create extractor with a smaller number of kept eigenfaces
train_data = utils.random_training_set(data.shape, 400, 0., 255.)
eigen2 = bob.bio.face.extractor.Eigenface(subspace_dimension = 5)
reference = pkg_resources.resource_filename('bob.bio.face.test', 'data/eigenface_extractor.hdf5')
try:
# train the projector
eigen2.train(train_data, temp_file)
assert os.path.exists(temp_file)
if regenerate_refs: shutil.copy(temp_file, reference_file)
# check projection matrix
eigen1.load(reference)
eigen2.load(temp_file)
assert eigen1.machine.shape == eigen2.machine.shape
for i in range(5):
assert numpy.abs(eigen1.machine.weights[:,i] - eigen2.machine.weights[:,i] < 1e-5).all() or numpy.abs(eigen1.machine.weights[:,i] + eigen2.machine.weights[:,i] < 1e-5).all()
finally:
if os.path.exists(temp_file): os.remove(temp_file)
# now, we can execute the extractor and check that the feature is still identical
feature = eigen1(data)
assert feature.ndim == 1
reference = pkg_resources.resource_filename('bob.bio.face.test', 'data/eigenface_feature.hdf5')
_compare(feature, reference, eigen1.write_feature, eigen1.read_feature)
""" """
def test05_sift_key_points(self): def test05_sift_key_points(self):
# check if VLSIFT is available # check if VLSIFT is available
......
...@@ -185,7 +185,6 @@ setup( ...@@ -185,7 +185,6 @@ setup(
'dct-blocks = bob.bio.face.config.extractor.dct_blocks:extractor', # DCT blocks 'dct-blocks = bob.bio.face.config.extractor.dct_blocks:extractor', # DCT blocks
'grid-graph = bob.bio.face.config.extractor.grid_graph:extractor', # Grid graph 'grid-graph = bob.bio.face.config.extractor.grid_graph:extractor', # Grid graph
'lgbphs = bob.bio.face.config.extractor.lgbphs:extractor', # LGBPHS 'lgbphs = bob.bio.face.config.extractor.lgbphs:extractor', # LGBPHS
'eigenface = bob.bio.face.config.extractor.eigenface:extractor', # Eigenface
], ],
'bob.bio.algorithm': [ 'bob.bio.algorithm': [
...@@ -196,7 +195,6 @@ setup( ...@@ -196,7 +195,6 @@ setup(
#baselines #baselines
'bob.bio.baseline':[ 'bob.bio.baseline':[
'eigenface = bob.bio.face.baseline.baseline:eigenface',
'lda = bob.bio.face.baseline.baseline:lda', 'lda = bob.bio.face.baseline.baseline:lda',
'plda = bob.bio.face.baseline.baseline:plda', 'plda = bob.bio.face.baseline.baseline:plda',
'gabor_graph = bob.bio.face.baseline.baseline:gabor_graph', 'gabor_graph = bob.bio.face.baseline.baseline:gabor_graph',
......
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