Commit 47525a93 authored by Tiago de Freitas Pereira's avatar Tiago de Freitas Pereira
Browse files

Binded ZTNorm

parent 99c842d2
......@@ -10,7 +10,27 @@
#endif
#include "main.h"
static PyMethodDef module_methods[] = {};
static PyMethodDef module_methods[] = {
{
zt_norm.name(),
(PyCFunction)PyBobLearnMisc_ztNorm,
METH_VARARGS|METH_KEYWORDS,
zt_norm.doc()
},
{
t_norm.name(),
(PyCFunction)PyBobLearnMisc_tNorm,
METH_VARARGS|METH_KEYWORDS,
t_norm.doc()
},
{
z_norm.name(),
(PyCFunction)PyBobLearnMisc_zNorm,
METH_VARARGS|METH_KEYWORDS,
z_norm.doc()
},
{0}//Sentinel
};
PyDoc_STRVAR(module_docstr, "Bob EM based Machine Learning Routines");
......
......@@ -34,6 +34,9 @@
#include <bob.learn.misc/ISVMachine.h>
#include <bob.learn.misc/IVectorMachine.h>
#include <bob.learn.misc/PLDAMachine.h>
#include <bob.learn.misc/ZTNorm.h>
#include "ztnorm.cpp"
#if PY_VERSION_HEX >= 0x03000000
......
......@@ -14,7 +14,8 @@ import numpy
from bob.io.base.test_utils import datafile
import bob.io.base
from . import znorm, tnorm, ztnorm
#from . import znorm, tnorm, ztnorm
import bob.learn.misc
def sameValue(vect_A, vect_B):
sameMatrix = numpy.zeros((vect_A.shape[0], vect_B.shape[0]), 'bool')
......@@ -59,8 +60,8 @@ def test_ztnorm_simple():
znorm_id = numpy.array([1, 2, 3, 4],'uint32')
# 2x1
tnorm_id = numpy.array([1, 5],'uint32')
scores = ztnorm(my_A, my_B, my_C, my_D,
scores = bob.learn.misc.ztnorm(my_A, my_B, my_C, my_D,
sameValue(tnorm_id, znorm_id))
ref_scores = numpy.array([[-4.45473107e+00, -3.29289322e+00, -1.50519101e+01, -8.42086557e-01, 6.46544511e-03], [-8.27619927e-01, 7.07106781e-01, 1.13757710e+01, 2.01641412e+00, 7.63765080e-01], [ 2.52913570e+00, 2.70710678e+00, 1.24400233e+01, 7.07106781e-01, 6.46544511e-03]], 'float64')
......@@ -75,7 +76,7 @@ def test_ztnorm_big():
# ZT-Norm
ref_scores = bob.io.base.load(datafile("ztnorm_result.hdf5", __name__))
scores = ztnorm(my_A, my_B, my_C, my_D)
scores = bob.learn.misc.ztnorm(my_A, my_B, my_C, my_D)
assert (abs(scores - ref_scores) < 1e-7).all()
# T-Norm
......@@ -101,7 +102,7 @@ def test_tnorm_simple():
assert (abs(zC - zC_py) < 1e-7).all()
empty = numpy.zeros(shape=(0,0), dtype=numpy.float64)
zC = ztnorm(my_A, empty, my_C, empty)
zC = bob.learn.misc.ztnorm(my_A, empty, my_C, empty)
assert (abs(zC - zC_py) < 1e-7).all()
def test_znorm_simple():
......@@ -117,5 +118,5 @@ def test_znorm_simple():
assert (abs(zA - zA_py) < 1e-7).all()
empty = numpy.zeros(shape=(0,0), dtype=numpy.float64)
zA = ztnorm(my_A, my_B, empty, empty)
zA = bob.learn.misc.ztnorm(my_A, my_B, empty, empty)
assert (abs(zA - zA_py) < 1e-7).all()
/**
* @author Tiago de Freitas Pereira <tiago.pereira@idiap.ch>
* @date Sat 31 Jan 02:46:48 2015
*
* @brief Python API for bob::learn::em
*
* Copyright (C) 2011-2014 Idiap Research Institute, Martigny, Switzerland
*/
#include "main.h"
#ifndef BOB_LEARN_MISC_ZTNORM_BIND
#define BOB_LEARN_MISC_ZTNORM_BIND
/*** zt_norm ***/
static auto zt_norm = bob::extension::FunctionDoc(
"ztnorm",
"",
0,
true
)
.add_prototype("rawscores_probes_vs_models,rawscores_zprobes_vs_models,rawscores_probes_vs_tmodels,rawscores_zprobes_vs_tmodels,mask_zprobes_vs_tmodels_istruetrial", "output")
.add_parameter("rawscores_probes_vs_models", "array_like <float, 2D>", "")
.add_parameter("rawscores_zprobes_vs_models", "array_like <float, 2D>", "")
.add_parameter("rawscores_probes_vs_tmodels", "array_like <float, 2D>", "")
.add_parameter("rawscores_zprobes_vs_tmodels", "array_like <float, 2D>", "")
.add_parameter("mask_zprobes_vs_tmodels_istruetrial", "array_like <float, 2D>", "")
.add_return("output","array_like <float, 2D>","");
static PyObject* PyBobLearnMisc_ztNorm(PyObject*, PyObject* args, PyObject* kwargs) {
char** kwlist = zt_norm.kwlist(0);
PyBlitzArrayObject *rawscores_probes_vs_models_o, *rawscores_zprobes_vs_models_o, *rawscores_probes_vs_tmodels_o,
*rawscores_zprobes_vs_tmodels_o, *mask_zprobes_vs_tmodels_istruetrial_o;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&O&O&|O&", kwlist, &PyBlitzArray_Converter, &rawscores_probes_vs_models_o,
&PyBlitzArray_Converter, &rawscores_zprobes_vs_models_o,
&PyBlitzArray_Converter, &rawscores_probes_vs_tmodels_o,
&PyBlitzArray_Converter, &rawscores_zprobes_vs_tmodels_o,
&PyBlitzArray_Converter, &mask_zprobes_vs_tmodels_istruetrial_o)){
zt_norm.print_usage();
Py_RETURN_NONE;
}
// get the number of command line arguments
auto rawscores_probes_vs_models_ = make_safe(rawscores_probes_vs_models_o);
auto rawscores_zprobes_vs_models_ = make_safe(rawscores_zprobes_vs_models_o);
auto rawscores_probes_vs_tmodels_ = make_safe(rawscores_probes_vs_tmodels_o);
auto rawscores_zprobes_vs_tmodels_ = make_safe(rawscores_zprobes_vs_tmodels_o);
//auto mask_zprobes_vs_tmodels_istruetrial_ = make_safe(mask_zprobes_vs_tmodels_istruetrial_o);
blitz::Array<double,2> rawscores_probes_vs_models = *PyBlitzArrayCxx_AsBlitz<double,2>(rawscores_probes_vs_models_o);
blitz::Array<double,2> normalized_scores = blitz::Array<double,2>(rawscores_probes_vs_models.extent(0), rawscores_probes_vs_models.extent(1));
int nargs = (args?PyTuple_Size(args):0) + (kwargs?PyDict_Size(kwargs):0);
if(nargs==4)
bob::learn::misc::ztNorm(*PyBlitzArrayCxx_AsBlitz<double,2>(rawscores_probes_vs_models_o),
*PyBlitzArrayCxx_AsBlitz<double,2>(rawscores_zprobes_vs_models_o),
*PyBlitzArrayCxx_AsBlitz<double,2>(rawscores_probes_vs_tmodels_o),
*PyBlitzArrayCxx_AsBlitz<double,2>(rawscores_zprobes_vs_tmodels_o),
normalized_scores);
else
bob::learn::misc::ztNorm(*PyBlitzArrayCxx_AsBlitz<double,2>(rawscores_probes_vs_models_o),
*PyBlitzArrayCxx_AsBlitz<double,2>(rawscores_zprobes_vs_models_o),
*PyBlitzArrayCxx_AsBlitz<double,2>(rawscores_probes_vs_tmodels_o),
*PyBlitzArrayCxx_AsBlitz<double,2>(rawscores_zprobes_vs_tmodels_o),
*PyBlitzArrayCxx_AsBlitz<bool,2>(mask_zprobes_vs_tmodels_istruetrial_o),
normalized_scores);
return PyBlitzArrayCxx_AsConstNumpy(normalized_scores);
}
/*** t_norm ***/
static auto t_norm = bob::extension::FunctionDoc(
"tnorm",
"",
0,
true
)
.add_prototype("rawscores_probes_vs_models,rawscores_probes_vs_tmodels", "output")
.add_parameter("rawscores_probes_vs_models", "array_like <float, 2D>", "")
.add_parameter("rawscores_probes_vs_tmodels", "array_like <float, 2D>", "")
.add_return("output","array_like <float, 2D>","");
static PyObject* PyBobLearnMisc_tNorm(PyObject*, PyObject* args, PyObject* kwargs) {
char** kwlist = zt_norm.kwlist(0);
PyBlitzArrayObject *rawscores_probes_vs_models_o, *rawscores_probes_vs_tmodels_o;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&", kwlist, &PyBlitzArray_Converter, &rawscores_probes_vs_models_o,
&PyBlitzArray_Converter, &rawscores_probes_vs_tmodels_o)){
zt_norm.print_usage();
Py_RETURN_NONE;
}
auto rawscores_probes_vs_models_ = make_safe(rawscores_probes_vs_models_o);
auto rawscores_probes_vs_tmodels_ = make_safe(rawscores_probes_vs_tmodels_o);
blitz::Array<double,2> rawscores_probes_vs_models = *PyBlitzArrayCxx_AsBlitz<double,2>(rawscores_probes_vs_models_o);
blitz::Array<double,2> normalized_scores = blitz::Array<double,2>(rawscores_probes_vs_models.extent(0), rawscores_probes_vs_models.extent(1));
bob::learn::misc::tNorm(*PyBlitzArrayCxx_AsBlitz<double,2>(rawscores_probes_vs_models_o),
*PyBlitzArrayCxx_AsBlitz<double,2>(rawscores_probes_vs_tmodels_o),
normalized_scores);
return PyBlitzArrayCxx_AsConstNumpy(normalized_scores);
}
/*** z_norm ***/
static auto z_norm = bob::extension::FunctionDoc(
"znorm",
"",
0,
true
)
.add_prototype("rawscores_probes_vs_models,rawscores_zprobes_vs_models", "output")
.add_parameter("rawscores_probes_vs_models", "array_like <float, 2D>", "")
.add_parameter("rawscores_zprobes_vs_models", "array_like <float, 2D>", "")
.add_return("output","array_like <float, 2D>","");
static PyObject* PyBobLearnMisc_zNorm(PyObject*, PyObject* args, PyObject* kwargs) {
char** kwlist = zt_norm.kwlist(0);
PyBlitzArrayObject *rawscores_probes_vs_models_o, *rawscores_zprobes_vs_models_o;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&", kwlist, &PyBlitzArray_Converter, &rawscores_probes_vs_models_o,
&PyBlitzArray_Converter, &rawscores_zprobes_vs_models_o)){
zt_norm.print_usage();
Py_RETURN_NONE;
}
auto rawscores_probes_vs_models_ = make_safe(rawscores_probes_vs_models_o);
auto rawscores_zprobes_vs_models_ = make_safe(rawscores_zprobes_vs_models_o);
blitz::Array<double,2> rawscores_probes_vs_models = *PyBlitzArrayCxx_AsBlitz<double,2>(rawscores_probes_vs_models_o);
blitz::Array<double,2> normalized_scores = blitz::Array<double,2>(rawscores_probes_vs_models.extent(0), rawscores_probes_vs_models.extent(1));
bob::learn::misc::zNorm(*PyBlitzArrayCxx_AsBlitz<double,2>(rawscores_probes_vs_models_o),
*PyBlitzArrayCxx_AsBlitz<double,2>(rawscores_zprobes_vs_models_o),
normalized_scores);
return PyBlitzArrayCxx_AsConstNumpy(normalized_scores);
}
#endif
......@@ -61,7 +61,7 @@ setup(
"bob/learn/misc/cpp/KMeansMachine.cpp",
"bob/learn/misc/cpp/LinearScoring.cpp",
"bob/learn/misc/cpp/PLDAMachine.cpp",
#"bob/learn/misc/cpp/ZTNorm.cpp",
"bob/learn/misc/cpp/ZTNorm.cpp",
"bob/learn/misc/cpp/FABase.cpp",
"bob/learn/misc/cpp/JFABase.cpp",
......@@ -120,7 +120,8 @@ setup(
"bob/learn/misc/ivector_machine.cpp",
"bob/learn/misc/plda_base.cpp",
"bob/learn/misc/plda_machine.cpp",
"bob/learn/misc/plda_machine.cpp",
"bob/learn/misc/ztnorm.cpp",
"bob/learn/misc/main.cpp",
],
......
Supports Markdown
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