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
......@@ -8,11 +8,6 @@ Defining some face recognition baselines
from import Baseline
eigenface = Baseline(name="eigenface",
preprocessors={'default': 'face-crop-eyes', 'atnt': 'base'},
lda = Baseline(name="lda",
preprocessors={'default': 'face-crop-eyes', 'atnt': 'base'},
#!/usr/bin/env python
# compute eigenfaces using the training database
extractor =
subspace_dimension = .95
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Manuel Guenther <>
import numpy
import bob.learn.linear
from import Extractor
import logging
logger = logging.getLogger("")
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:``, here the eigenfces are used as features, i.e., to apply advanced face recognition algorithms on top of them.
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:`` 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.
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])" -> 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
self.subspace_dimension = index" -> Keeping %d eigenvectors" % self.subspace_dimension)
# Machine: get shape, then resize
self.machine.resize(self.machine.shape[0], self.subspace_dimension), "w"))
def load(self, extractor_file):
"""Reads the PCA projection matrix from file.
extractor_file : str
An existing file, from which the PCA projection matrix are read.
# read PCA projector
self.machine = bob.learn.linear.Machine(
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.
image : 2D :py:class:`numpy.ndarray` (floats)
The image to extract the eigenface feature from.
feature : 1D :py:class:`numpy.ndarray` (floats)
The extracted eigenface feature.
# Projects the data
return self.machine(image.flatten())
......@@ -153,44 +153,6 @@ def test_lgbphs():
_compare(feature, reference, lgbphs.write_feature, lgbphs.read_feature)
def test_eigenface():
temp_file =
data = _data()
eigen1 ='eigenface', 'extractor', preferred_package='')
assert isinstance(eigen1,
assert isinstance(eigen1,
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 = = 5)
reference = pkg_resources.resource_filename('', 'data/eigenface_extractor.hdf5')
# 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
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()
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('', 'data/eigenface_feature.hdf5')
_compare(feature, reference, eigen1.write_feature, eigen1.read_feature)
def test05_sift_key_points(self):
# check if VLSIFT is available
......@@ -185,7 +185,6 @@ setup(
'dct-blocks =', # DCT blocks
'grid-graph =', # Grid graph
'lgbphs =', # LGBPHS
'eigenface =', # Eigenface
'': [
......@@ -196,7 +195,6 @@ setup(
'eigenface =',
'lda =',
'plda =',
'gabor_graph =',
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment