Skip to content
Snippets Groups Projects
Commit 093de20e authored by André Anjos's avatar André Anjos :speech_balloon:
Browse files

All tests passing

parent 8a31c4b5
No related branches found
No related tags found
No related merge requests found
Showing
with 875 additions and 108 deletions
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
This diff is collapsed.
......@@ -13,7 +13,9 @@
void bind_core_tinyvector();
void bind_core_ndarray_numpy();
void bind_core_bz_numpy();
void bind_core_random();
void bind_ip_gabor_wavelet_transform();
void bind_io_hdf5();
/** machine bindings **/
void bind_machine_base();
......@@ -50,6 +52,8 @@ BOOST_PYTHON_MODULE(_library) {
bind_core_ndarray_numpy();
bind_core_bz_numpy();
bind_ip_gabor_wavelet_transform();
bind_io_hdf5();
bind_core_random();
/** machine bindings **/
bind_machine_base();
......
/**
* @author Andre Anjos <andre.anjos@idiap.ch>
* @author Laurent El Shafey <laurent.el-shafey@idiap.ch>
* @date Mon Jul 11 18:31:22 2011 +0200
*
* @brief Bindings for random number generation.
*
* Copyright (C) 2011-2014 Idiap Research Institute, Martigny, Switzerland
*/
#include "ndarray.h"
#include <boost/make_shared.hpp>
#include <boost/random.hpp>
using namespace boost::python;
template <typename T>
static boost::shared_ptr<boost::mt19937> make_with_seed(T s) {
return boost::make_shared<boost::mt19937>(s);
}
template <typename T>
static void set_seed(boost::mt19937& o, T s) {
o.seed(s);
}
void bind_core_random () {
class_<boost::mt19937, boost::shared_ptr<boost::mt19937> >("mt19937",
"A Mersenne-Twister Random Number Generator (RNG)\n" \
"\n" \
"A Random Number Generator (RNG) based on the work 'Mersenne Twister: A 623-dimensionally equidistributed uniform pseudo-random number generator, Makoto Matsumoto and Takuji Nishimura, ACM Transactions on Modeling and Computer Simulation: Special Issue on Uniform Random Number Generation, Vol. 8, No. 1, January 1998, pp. 3-30'", init<>((arg("self")), "Default constructor"))
.def("__init__", make_constructor(&make_with_seed<int64_t>, default_call_policies(), (arg("seed"))), "Builds a new generator with a specific seed")
.def("__init__", make_constructor(&make_with_seed<double>, default_call_policies(), (arg("seed"))), "Builds a new generator with a specific seed")
.def("seed", &set_seed<double>, (arg("self"), arg("seed")), "Sets my internal seed using a floating-point number")
.def("seed", &set_seed<int64_t>, (arg("self"), arg("seed")), "Sets my internal seed using an integer")
.def(self == self)
.def(self != self)
;
}
......@@ -9,81 +9,88 @@
"""
import numpy
import nose.tools
from . import BICMachine, BICTrainer
eps = 1e-5
def equals(x, y, epsilon):
return (abs(x - y) < epsilon).all()
class BICTrainerAndMachineTest(unittest.TestCase):
"""Performs various BIC trainer and machine tests."""
def training_data(self):
data = numpy.array([
(10., 4., 6., 8., 2.),
(8., 2., 4., 6., 0.),
(12., 6., 8., 10., 4.),
(11., 3., 7., 7., 3.),
(9., 5., 5., 9., 1.)], dtype='float64')
return data, -1. * data
def eval_data(self, which):
eval_data = numpy.ndarray((5,), dtype=numpy.float64)
if which == 0:
eval_data.fill(0.)
elif which == 1:
eval_data.fill(10.)
return eval_data
def test_IEC(self):
# Tests the IEC training of the BICTrainer
intra_data, extra_data = self.training_data()
# train BIC machine
machine = BICMachine()
trainer = BICTrainer()
# train machine with intrapersonal data only
trainer.train(machine, intra_data, intra_data)
# => every result should be zero
self.assertAlmostEqual(machine(self.eval_data(0)), 0.)
self.assertAlmostEqual(machine(self.eval_data(1)), 0.)
# re-train the machine with intra- and extrapersonal data
trainer.train(machine, intra_data, extra_data)
# now, only the input vector 0 should give log-likelihood 0
self.assertAlmostEqual(machine(self.eval_data(0)), 0.)
# while a positive vector should give a positive result
self.assertTrue(machine(self.eval_data(1)) > 0.)
def test_BIC(self):
# Tests the BIC training of the BICTrainer
intra_data, extra_data = self.training_data()
# train BIC machine
trainer = BICTrainer(2,2)
# The data are chosen such that the third eigenvalue is zero.
# Hence, calculating rho (i.e., using the Distance From Feature Space) is impossible
machine = BICMachine(True)
def should_raise():
trainer.train(machine, intra_data, intra_data)
self.assertRaises(RuntimeError, should_raise)
# So, now without rho...
machine = BICMachine(False)
# First, train the machine with intrapersonal data only
trainer.train(machine, intra_data, intra_data)
# => every result should be zero
self.assertAlmostEqual(machine(self.eval_data(0)), 0.)
self.assertAlmostEqual(machine(self.eval_data(1)), 0.)
# re-train the machine with intra- and extrapersonal data
trainer.train(machine, intra_data, extra_data)
# now, only the input vector 0 should give log-likelihood 0
self.assertAlmostEqual(machine(self.eval_data(0)), 0.)
# while a positive vector should give a positive result
self.assertTrue(machine(self.eval_data(1)) > 0.)
def training_data():
data = numpy.array([
(10., 4., 6., 8., 2.),
(8., 2., 4., 6., 0.),
(12., 6., 8., 10., 4.),
(11., 3., 7., 7., 3.),
(9., 5., 5., 9., 1.)], dtype='float64')
return data, -1. * data
def eval_data(which):
eval_data = numpy.ndarray((5,), dtype=numpy.float64)
if which == 0:
eval_data.fill(0.)
elif which == 1:
eval_data.fill(10.)
return eval_data
def test_IEC():
# Tests the IEC training of the BICTrainer
intra_data, extra_data = training_data()
# train BIC machine
machine = BICMachine()
trainer = BICTrainer()
# train machine with intrapersonal data only
trainer.train(machine, intra_data, intra_data)
# => every result should be zero
assert abs(machine(eval_data(0))) < eps
assert abs(machine(eval_data(1))) < eps
# re-train the machine with intra- and extrapersonal data
trainer.train(machine, intra_data, extra_data)
# now, only the input vector 0 should give log-likelihood 0
assert abs(machine(eval_data(0))) < eps
# while a positive vector should give a positive result
assert machine(eval_data(1)) > 0.
@nose.tools.raises(RuntimeError)
def test_raises():
# Tests the BIC training of the BICTrainer
intra_data, extra_data = training_data()
# train BIC machine
trainer = BICTrainer(2,2)
# The data are chosen such that the third eigenvalue is zero.
# Hence, calculating rho (i.e., using the Distance From Feature Space) is impossible
machine = BICMachine(True)
trainer.train(machine, intra_data, intra_data)
def test_BIC():
# Tests the BIC training of the BICTrainer
intra_data, extra_data = training_data()
# train BIC machine
trainer = BICTrainer(2,2)
# So, now without rho...
machine = BICMachine(False)
# First, train the machine with intrapersonal data only
trainer.train(machine, intra_data, intra_data)
# => every result should be zero
assert abs(machine(eval_data(0))) < eps
assert abs(machine(eval_data(1))) < eps
# re-train the machine with intra- and extrapersonal data
trainer.train(machine, intra_data, extra_data)
# now, only the input vector 0 should give log-likelihood 0
assert abs(machine(eval_data(0))) < eps
# while a positive vector should give a positive result
assert machine(eval_data(1)) > 0.
......@@ -13,10 +13,13 @@ import numpy
import xbob.io.base
from xbob.io.base.test_utils import datafile
from . import KMeansMachine, GMMMachine
from . import KMeansMachine, GMMMachine, KMeansTrainer, \
ML_GMMTrainer, MAP_GMMTrainer
from . import HDF5File as OldHDF5File
def loadGMM():
gmm = bob.machine.GMMMachine(2, 2)
gmm = GMMMachine(2, 2)
gmm.weights = xbob.io.base.load(datafile('gmm.init_weights.hdf5', __name__))
gmm.means = xbob.io.base.load(datafile('gmm.init_means.hdf5', __name__))
......@@ -28,10 +31,11 @@ def loadGMM():
def equals(x, y, epsilon):
return (abs(x - y) < epsilon).all()
class MyTrainer1(bob.trainer.KMeansTrainer):
class MyTrainer1(KMeansTrainer):
"""Simple example of python trainer: """
def __init__():
bob.trainer.KMeansTrainer.__init__()
def __init__(self):
KMeansTrainer.__init__(self)
def train(self, machine, data):
a = numpy.ndarray((2, 2), 'float64')
......@@ -47,15 +51,15 @@ def test_gmm_ML_1():
gmm = loadGMM()
ml_gmmtrainer = bob.trainer.ML_GMMTrainer(True, True, True)
ml_gmmtrainer = ML_GMMTrainer(True, True, True)
ml_gmmtrainer.train(gmm, ar)
#config = xbob.io.base.HDF5File(datafile('gmm_ML.hdf5", __name__), 'w')
#config = OldHDF5File(datafile('gmm_ML.hdf5", __name__), 'w')
#gmm.save(config)
gmm_ref = bob.machine.GMMMachine(xbob.io.base.HDF5File(datafile('gmm_ML.hdf5', __name__)))
gmm_ref_32bit_debug = bob.machine.GMMMachine(xbob.io.base.HDF5File(datafile('gmm_ML_32bit_debug.hdf5', __name__)))
gmm_ref_32bit_release = bob.machine.GMMMachine(xbob.io.base.HDF5File(datafile('gmm_ML_32bit_release.hdf5', __name__)))
gmm_ref = GMMMachine(OldHDF5File(datafile('gmm_ML.hdf5', __name__)))
gmm_ref_32bit_debug = GMMMachine(OldHDF5File(datafile('gmm_ML_32bit_debug.hdf5', __name__)))
gmm_ref_32bit_release = GMMMachine(OldHDF5File(datafile('gmm_ML_32bit_release.hdf5', __name__)))
assert (gmm == gmm_ref) or (gmm == gmm_ref_32bit_release) or (gmm == gmm_ref_32bit_debug)
......@@ -66,7 +70,7 @@ def test_gmm_ML_2():
ar = xbob.io.base.load(datafile('dataNormalized.hdf5', __name__))
# Initialize GMMMachine
gmm = bob.machine.GMMMachine(5, 45)
gmm = GMMMachine(5, 45)
gmm.means = xbob.io.base.load(datafile('meansAfterKMeans.hdf5', __name__)).astype('float64')
gmm.variances = xbob.io.base.load(datafile('variancesAfterKMeans.hdf5', __name__)).astype('float64')
gmm.weights = numpy.exp(xbob.io.base.load(datafile('weightsAfterKMeans.hdf5', __name__)).astype('float64'))
......@@ -78,7 +82,7 @@ def test_gmm_ML_2():
prior = 0.001
max_iter_gmm = 25
accuracy = 0.00001
ml_gmmtrainer = bob.trainer.ML_GMMTrainer(True, True, True, prior)
ml_gmmtrainer = ML_GMMTrainer(True, True, True, prior)
ml_gmmtrainer.max_iterations = max_iter_gmm
ml_gmmtrainer.convergence_threshold = accuracy
......@@ -102,18 +106,18 @@ def test_gmm_MAP_1():
ar = xbob.io.base.load(datafile('faithful.torch3_f64.hdf5', __name__))
gmm = bob.machine.GMMMachine(xbob.io.base.HDF5File(datafile("gmm_ML.hdf5", __name__)))
gmmprior = bob.machine.GMMMachine(xbob.io.base.HDF5File(datafile("gmm_ML.hdf5", __name__)))
gmm = GMMMachine(OldHDF5File(datafile("gmm_ML.hdf5", __name__)))
gmmprior = GMMMachine(OldHDF5File(datafile("gmm_ML.hdf5", __name__)))
map_gmmtrainer = bob.trainer.MAP_GMMTrainer(16)
map_gmmtrainer = MAP_GMMTrainer(16)
map_gmmtrainer.set_prior_gmm(gmmprior)
map_gmmtrainer.train(gmm, ar)
#config = xbob.io.base.HDF5File(datafile('gmm_MAP.hdf5", 'w', __name__))
#config = OldHDF5File(datafile('gmm_MAP.hdf5", 'w', __name__))
#gmm.save(config)
gmm_ref = bob.machine.GMMMachine(xbob.io.base.HDF5File(datafile('gmm_MAP.hdf5', __name__)))
#gmm_ref_32bit_release = bob.machine.GMMMachine(xbob.io.base.HDF5File(datafile('gmm_MAP_32bit_release.hdf5', __name__)))
gmm_ref = GMMMachine(OldHDF5File(datafile('gmm_MAP.hdf5', __name__)))
#gmm_ref_32bit_release = GMMMachine(OldHDF5File(datafile('gmm_MAP_32bit_release.hdf5', __name__)))
assert (equals(gmm.means,gmm_ref.means,1e-3) and equals(gmm.variances,gmm_ref.variances,1e-3) and equals(gmm.weights,gmm_ref.weights,1e-3))
......@@ -121,21 +125,21 @@ def test_gmm_MAP_2():
# Train a GMMMachine with MAP_GMMTrainer and compare with matlab reference
map_adapt = bob.trainer.MAP_GMMTrainer(4., True, False, False, 0.)
data = xbob.io.base.load(datafile('data.hdf5', 'machine', __name__))
map_adapt = MAP_GMMTrainer(4., True, False, False, 0.)
data = xbob.io.base.load(datafile('data.hdf5', __name__))
data = data.reshape((1, data.shape[0])) # make a 2D array out of it
means = xbob.io.base.load(datafile('means.hdf5', 'machine', __name__))
variances = xbob.io.base.load(datafile('variances.hdf5', 'machine', __name__))
weights = xbob.io.base.load(datafile('weights.hdf5', 'machine', __name__))
means = xbob.io.base.load(datafile('means.hdf5', __name__))
variances = xbob.io.base.load(datafile('variances.hdf5', __name__))
weights = xbob.io.base.load(datafile('weights.hdf5', __name__))
gmm = bob.machine.GMMMachine(2,50)
gmm = GMMMachine(2,50)
gmm.means = means
gmm.variances = variances
gmm.weights = weights
map_adapt.set_prior_gmm(gmm)
gmm_adapted = bob.machine.GMMMachine(2,50)
gmm_adapted = GMMMachine(2,50)
gmm_adapted.means = means
gmm_adapted.variances = variances
gmm_adapted.weights = weights
......@@ -159,7 +163,7 @@ def test_gmm_MAP_3():
# Initialize GMMMachine
n_gaussians = 5
n_inputs = 45
prior_gmm = bob.machine.GMMMachine(n_gaussians, n_inputs)
prior_gmm = GMMMachine(n_gaussians, n_inputs)
prior_gmm.means = xbob.io.base.load(datafile('meansAfterML.hdf5', __name__))
prior_gmm.variances = xbob.io.base.load(datafile('variancesAfterML.hdf5', __name__))
prior_gmm.weights = xbob.io.base.load(datafile('weightsAfterML.hdf5', __name__))
......@@ -173,13 +177,13 @@ def test_gmm_MAP_3():
max_iter_gmm = 1
accuracy = 0.00001
map_factor = 0.5
map_gmmtrainer = bob.trainer.MAP_GMMTrainer(relevance_factor, True, False, False, prior)
map_gmmtrainer = MAP_GMMTrainer(relevance_factor, True, False, False, prior)
map_gmmtrainer.max_iterations = max_iter_gmm
map_gmmtrainer.convergence_threshold = accuracy
map_gmmtrainer.set_prior_gmm(prior_gmm)
map_gmmtrainer.set_t3_map(map_factor);
gmm = bob.machine.GMMMachine(n_gaussians, n_inputs)
gmm = GMMMachine(n_gaussians, n_inputs)
gmm.set_variance_thresholds(threshold)
# Train
......@@ -209,7 +213,7 @@ def test_gmm_test():
# Initialize GMMMachine
n_gaussians = 5
n_inputs = 45
gmm = bob.machine.GMMMachine(n_gaussians, n_inputs)
gmm = GMMMachine(n_gaussians, n_inputs)
gmm.means = xbob.io.base.load(datafile('meansAfterML.hdf5', __name__))
gmm.variances = xbob.io.base.load(datafile('variancesAfterML.hdf5', __name__))
gmm.weights = xbob.io.base.load(datafile('weightsAfterML.hdf5', __name__))
......@@ -234,7 +238,7 @@ def test_custom_trainer():
mytrainer = MyTrainer1()
machine = bob.machine.KMeansMachine(2, 2)
machine = KMeansMachine(2, 2)
mytrainer.train(machine, ar)
for i in range(0, 2):
......
......@@ -16,6 +16,8 @@ import xbob.io.base
from . import Gaussian
from . import HDF5File as OldHDF5File
def equals(x, y, epsilon):
return (abs(x - y) < epsilon)
......@@ -47,8 +49,8 @@ def test_GaussianMachine():
# Save and read from file
filename = str(tempfile.mkstemp(".hdf5")[1])
g.save(xbob.io.base.HDF5File(filename, 'w'))
g_loaded = Gaussian(xbob.io.base.HDF5File(filename))
g.save(OldHDF5File(filename, 'w'))
g_loaded = Gaussian(OldHDF5File(filename))
assert g == g_loaded
assert (g != g_loaded ) is False
assert g.is_similar_to(g_loaded)
......
......@@ -17,6 +17,8 @@ from xbob.io.base.test_utils import datafile
from . import GMMStats, GMMMachine
from . import HDF5File as OldHDF5File
def test_GMMStats():
# Test a GMMStats
......@@ -40,8 +42,8 @@ def test_GMMStats():
# Saves and reads from file
filename = str(tempfile.mkstemp(".hdf5")[1])
gs.save(xbob.io.base.HDF5File(filename, 'w'))
gs_loaded = GMMStats(xbob.io.base.HDF5File(filename))
gs.save(OldHDF5File(filename, 'w'))
gs_loaded = GMMStats(OldHDF5File(filename))
assert gs == gs_loaded
assert (gs != gs_loaded ) is False
assert gs.is_similar_to(gs_loaded)
......@@ -190,7 +192,7 @@ def test_GMMMachine_2():
stats = GMMStats(2, 2)
gmm.acc_statistics(arrayset, stats)
stats_ref = GMMStats(xbob.io.base.HDF5File(datafile("stats.hdf5", __name__)))
stats_ref = GMMStats(OldHDF5File(datafile("stats.hdf5", __name__)))
assert stats.t == stats_ref.t
assert numpy.allclose(stats.n, stats_ref.n, atol=1e-10)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment