Skip to content
Snippets Groups Projects
Commit 4e95fc5f authored by Guillaume HEUSCH's avatar Guillaume HEUSCH
Browse files

[algorithm] checkout files from previous branch

parent c5a8fa50
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
import numpy
from bob.pad.base.algorithm import Algorithm
import bob.learn.em
import bob.io.base
class OCGMM(Algorithm):
"""
This class interfaces a GMM-based "classifier" to perform PAD experiments
A GMM is used to model the bonafide features
"""
def __init__(self, n_gaussians=2, max_iter=1000, conv_threshold=1e-5, **kwargs):
Algorithm.__init__(self,
performs_projection=True,
requires_projector_training=True,
**kwargs)
self.n_gaussians = n_gaussians
self.max_iter = max_iter
self.conv_threshold = conv_threshold
self.machine = None
self.trainer = bob.learn.em.ML_GMMTrainer(update_means=True, update_variances=True, update_weights=True)
def train_projector(self, training_features, projector_file):
"""
Trains the GMM using Expectation-Maximimazation with Maximum Likelihood criterion
**Parameters**
training_features:
"""
# training_features[0] - training features for the REAL class.
# training_features[1] - training features for the ATTACK class.
# The data - "positive class only"
pos = numpy.array(training_features[0])
features_dim = pos.shape[1]
# The machine
self.machine = bob.learn.em.GMMMachine(self.n_gaussians, features_dim)
# train
bob.learn.em.train(self.trainer, self.machine, pos, max_iterations=self.max_iter, convergence_threshold=self.conv_threshold)
f = bob.io.base.HDF5File(projector_file, 'w')
self.machine.save(f)
def project(self, feature):
"""
Compute the log-likelihood of the feature
"""
return self.machine(feature)
def score(self, toscore):
return [toscore[0]]
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
import numpy
from bob.pad.base.algorithm import Algorithm
import bob.learn.libsvm
import bob.io.base
class OCSVM(Algorithm):
"""
This class interfaces a One Class SVM classifier used for PAD
"""
def __init__(self, rescale=True, nu=0.01, gamma=0.1, **kwargs):
Algorithm.__init__(self,
performs_projection=True,
requires_projector_training=True,
**kwargs)
self.rescale = rescale
self.nu = nu
self.gamma = gamma
self.machine = None
self.trainer = bob.learn.libsvm.Trainer(machine_type='ONE_CLASS', kernel_type='RBF', probability=True)
setattr(self.trainer, 'nu', self.nu)
setattr(self.trainer, 'gamma', self.gamma)
def train_projector(self, training_features, projector_file):
"""
Trains the One Class SVM
**Parameters**
training_features:
"""
# training_features[0] - training features for the REAL class.
# training_features[1] - training features for the ATTACK class.
# The data - "positive class only"
pos = numpy.array(training_features[0])
if self.rescale:
for i in range(pos.shape[0]):
min_value = numpy.min(pos[i])
max_value = numpy.max(pos[i])
pos[i] = ((2 * (pos[i] - min_value))/ (max_value - min_value)) - 1
pos = [pos]
# train
self.machine = self.trainer.train(pos)
f = bob.io.base.HDF5File(projector_file, 'w')
self.machine.save(f)
def project(self, feature):
"""
Project the given feature
"""
if self.rescale:
min_value = numpy.min(feature)
max_value = numpy.max(feature)
feature = ((2 * (feature - min_value))/ (max_value - min_value)) - 1
return self.machine(feature)
def score(self, toscore):
return [toscore[0]]
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
import numpy
from bob.pad.base.algorithm import Algorithm
import bob.io.base
from sklearn import mixture
class SKLGMM(Algorithm):
"""
This class interfaces a GMM-based "classifier" to perform PAD experiments
A GMM is used to model the bonafide features
"""
def __init__(self, n_gaussians=2, max_iter=1000, conv_threshold=1e-5, **kwargs):
Algorithm.__init__(self,
performs_projection=True,
requires_projector_training=True,
**kwargs)
self.n_gaussians = n_gaussians
self.max_iter = max_iter
self.conv_threshold = conv_threshold
self.machine = mixture.GaussianMixture(n_components=n_gaussians, tol=conv_threshold, max_iter=max_iter)
self.parameters_keys = [ "covariance_type", "covariances_", "lower_bound_", "means_",
"n_components", "weights_", "converged_", "precisions_", "precisions_cholesky_"]
def train_projector(self, training_features, projector_file):
"""
Trains the GMM using Expectation-Maximimazation with Maximum Likelihood criterion
**Parameters**
training_features:
"""
# training_features[0] - training features for the REAL class.
# training_features[1] - training features for the ATTACK class.
# The data - "positive class only"
pos = numpy.array(training_features[0])
features_dim = pos.shape[1]
# train
self.machine.fit(pos)
# save
f = bob.io.base.HDF5File(projector_file, 'w')
for key in self.parameters_keys:
data = getattr(self.machine, key)
f.set(key, data)
def load_projector(self, projector_file):
f = bob.io.base.HDF5File(projector_file, 'r') # file to read the machine from
self.machine = mixture.GaussianMixture()
for key in self.parameters_keys:
data = f.read(key)
setattr(self.machine, key, data)
def project(self, feature):
"""
Compute the log-likelihood of the feature
"""
# load
return self.machine.score_samples(feature)
def score(self, toscore):
return [toscore[0]]
......@@ -3,12 +3,13 @@ from .SVM import SVM
from .OneClassGMM import OneClassGMM
from .LogRegr import LogRegr
from .SVMCascadePCA import SVMCascadePCA
from .Predictions import Predictions
from .MLP import MLP
from .PadLDA import PadLDA
from .OCSVM import OCSVM
from .OCGMM import OCGMM
from .SKLGMM import SKLGMM
from .mySVM import mySVM
# to fix sphinx warnings of not able to find classes, when path is shortened
def __appropriate__(*args):
"""Says object was actually declared here, and not in the import module.
Fixing sphinx warnings of not being able to find classes, when path is
......@@ -33,10 +34,5 @@ __appropriate__(
OneClassGMM,
LogRegr,
SVMCascadePCA,
Predictions,
MLP,
PadLDA
)
# gets sphinx autodoc done right - don't remove it
__all__ = [_ for _ in dir() if not _.startswith('_')]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment