diff --git a/bob/bio/face/baseline/baseline.py b/bob/bio/face/baseline/baseline.py index e8a76e53fddb1c4237de035f85f07dbc5707ec28..cd7aae465d3b6041fc31ce4b4393b042080f2daa 100644 --- a/bob/bio/face/baseline/baseline.py +++ b/bob/bio/face/baseline/baseline.py @@ -8,11 +8,6 @@ Defining some face recognition baselines 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", preprocessors={'default': 'face-crop-eyes', 'atnt': 'base'}, extractor='eigenface', diff --git a/bob/bio/face/config/extractor/eigenface.py b/bob/bio/face/config/extractor/eigenface.py deleted file mode 100644 index f0f38a77964c32092b4a575ff46bd592138126b4..0000000000000000000000000000000000000000 --- a/bob/bio/face/config/extractor/eigenface.py +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env python - -import bob.bio.face - -# compute eigenfaces using the training database -extractor = bob.bio.face.extractor.Eigenface( - subspace_dimension = .95 -) diff --git a/bob/bio/face/extractor/Eigenface.py b/bob/bio/face/extractor/Eigenface.py deleted file mode 100644 index c0dc456c21fb41ac936b9a491d4489267f2b4c10..0000000000000000000000000000000000000000 --- a/bob/bio/face/extractor/Eigenface.py +++ /dev/null @@ -1,112 +0,0 @@ -#!/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()) diff --git a/bob/bio/face/test/test_extractors.py b/bob/bio/face/test/test_extractors.py index 4effa7c6c83ab1cfbf38f42a3aaf9547d4db82e3..65b9dbd23696c0e555f135c83f98d2e52406ba4f 100644 --- a/bob/bio/face/test/test_extractors.py +++ b/bob/bio/face/test/test_extractors.py @@ -153,44 +153,6 @@ def test_lgbphs(): _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): # check if VLSIFT is available diff --git a/setup.py b/setup.py index 942080a27c247e31f14daf3f717e8fa36b3fe0a1..7a32a7a59cb22bea145bc4aa67cf0d8fe12b2745 100644 --- a/setup.py +++ b/setup.py @@ -185,7 +185,6 @@ setup( 'dct-blocks = bob.bio.face.config.extractor.dct_blocks:extractor', # DCT blocks 'grid-graph = bob.bio.face.config.extractor.grid_graph:extractor', # Grid graph 'lgbphs = bob.bio.face.config.extractor.lgbphs:extractor', # LGBPHS - 'eigenface = bob.bio.face.config.extractor.eigenface:extractor', # Eigenface ], 'bob.bio.algorithm': [ @@ -196,7 +195,6 @@ setup( #baselines 'bob.bio.baseline':[ - 'eigenface = bob.bio.face.baseline.baseline:eigenface', 'lda = bob.bio.face.baseline.baseline:lda', 'plda = bob.bio.face.baseline.baseline:plda', 'gabor_graph = bob.bio.face.baseline.baseline:gabor_graph',