From cadc55d5139133137e10456891d91dacb01721ef Mon Sep 17 00:00:00 2001
From: Manuel Guenther <manuel.guenther@idiap.ch>
Date: Fri, 26 Jun 2015 11:55:55 +0200
Subject: [PATCH] Implemented PCA subspace by percentage of vairance in PLDA

---
 bob/bio/base/algorithm/PLDA.py            | 12 +++++++++++-
 bob/bio/base/config/algorithm/pca_plda.py |  2 +-
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/bob/bio/base/algorithm/PLDA.py b/bob/bio/base/algorithm/PLDA.py
index a561f43d..21f29753 100644
--- a/bob/bio/base/algorithm/PLDA.py
+++ b/bob/bio/base/algorithm/PLDA.py
@@ -77,8 +77,18 @@ class PLDA (Algorithm):
 
     logger.info("  -> Training LinearMachine using PCA ")
     trainer = bob.learn.linear.PCATrainer()
-    machine, _ = trainer.train(data)
+    machine, eigen_values = trainer.train(data)
+
+    if isinstance(self.subspace_dimension_pca, float):
+      cummulated = numpy.cumsum(eigen_values) / numpy.sum(eigen_values)
+      for index in range(len(cummulated)):
+        if cummulated[index] > self.subspace_dimension_pca:
+          self.subspace_dimension_pca = index
+          break
+      self.subspace_dimension_pca = index
+
     # limit number of pcs
+    logger.info("  -> limiting PCA subspace to %d dimensions", self.subspace_dimension_pca)
     machine.resize(machine.shape[0], self.subspace_dimension_pca)
     return machine
 
diff --git a/bob/bio/base/config/algorithm/pca_plda.py b/bob/bio/base/config/algorithm/pca_plda.py
index 2ff3a8e6..9166e073 100644
--- a/bob/bio/base/config/algorithm/pca_plda.py
+++ b/bob/bio/base/config/algorithm/pca_plda.py
@@ -5,5 +5,5 @@ import bob.bio.base
 algorithm = bob.bio.base.algorithm.PLDA(
     subspace_dimension_of_f = 16, # Size of subspace F
     subspace_dimension_of_g = 16, # Size of subspace G
-    subspace_dimension_pca = 150   # Size of the PCA subspace
+    subspace_dimension_pca = 0.9  # Percentage of PCA variance
 )
-- 
GitLab