diff --git a/bob/learn/em/empca_trainer.cpp b/bob/learn/em/empca_trainer.cpp index 0a2653b8c622297c444ad01e12a80f84bfd25074..f197713852fe39c13691867f2fa5445d82b62755 100644 --- a/bob/learn/em/empca_trainer.cpp +++ b/bob/learn/em/empca_trainer.cpp @@ -155,7 +155,7 @@ PyObject* PyBobLearnEMEMPCATrainer_getRng(PyBobLearnEMEMPCATrainerObject* self, (PyBoostMt19937Object*)PyBoostMt19937_Type.tp_alloc(&PyBoostMt19937_Type, 0); retval->rng = self->cxx->getRng().get(); - return Py_BuildValue("O", retval); + return Py_BuildValue("N", retval); BOB_CATCH_MEMBER("Rng method could not be read", 0) } int PyBobLearnEMEMPCATrainer_setRng(PyBobLearnEMEMPCATrainerObject* self, PyObject* value, void*) { diff --git a/bob/learn/em/isv_base.cpp b/bob/learn/em/isv_base.cpp index 185463bede107b53ba368372b84dce8df00fe3eb..28e86d5df408e4d5dab2bddf787de0bd9f1b3d12 100644 --- a/bob/learn/em/isv_base.cpp +++ b/bob/learn/em/isv_base.cpp @@ -267,7 +267,7 @@ PyObject* PyBobLearnEMISVBase_getUBM(PyBobLearnEMISVBaseObject* self, void*){ (PyBobLearnEMGMMMachineObject*)PyBobLearnEMGMMMachine_Type.tp_alloc(&PyBobLearnEMGMMMachine_Type, 0); retval->cxx = ubm_gmmMachine; - return Py_BuildValue("O",retval); + return Py_BuildValue("N",retval); BOB_CATCH_MEMBER("ubm could not be read", 0) } int PyBobLearnEMISVBase_setUBM(PyBobLearnEMISVBaseObject* self, PyObject* value, void*){ diff --git a/bob/learn/em/isv_machine.cpp b/bob/learn/em/isv_machine.cpp index 89de14588244f32c402141ca7bd8d24e37d80674..00f4fcdc3286040c7939fd627ad410369f91bbc1 100644 --- a/bob/learn/em/isv_machine.cpp +++ b/bob/learn/em/isv_machine.cpp @@ -242,7 +242,7 @@ PyObject* PyBobLearnEMISVMachine_getISVBase(PyBobLearnEMISVMachineObject* self, (PyBobLearnEMISVBaseObject*)PyBobLearnEMISVBase_Type.tp_alloc(&PyBobLearnEMISVBase_Type, 0); retval->cxx = isv_base_o; - return Py_BuildValue("O",retval); + return Py_BuildValue("N",retval); BOB_CATCH_MEMBER("isv_base could not be read", 0) } int PyBobLearnEMISVMachine_setISVBase(PyBobLearnEMISVMachineObject* self, PyObject* value, void*){ diff --git a/bob/learn/em/isv_trainer.cpp b/bob/learn/em/isv_trainer.cpp index 66f99e1a506634dff809fd08db17b0f7b1c9d9d4..dc357b7dac94db7aa6a94462fc274a9e1b4478a2 100644 --- a/bob/learn/em/isv_trainer.cpp +++ b/bob/learn/em/isv_trainer.cpp @@ -340,7 +340,7 @@ PyObject* PyBobLearnEMISVTrainer_getRng(PyBobLearnEMISVTrainerObject* self, void (PyBoostMt19937Object*)PyBoostMt19937_Type.tp_alloc(&PyBoostMt19937_Type, 0); retval->rng = self->cxx->getRng().get(); - return Py_BuildValue("O", retval); + return Py_BuildValue("N", retval); BOB_CATCH_MEMBER("Rng method could not be read", 0) } int PyBobLearnEMISVTrainer_setRng(PyBobLearnEMISVTrainerObject* self, PyObject* value, void*) { diff --git a/bob/learn/em/jfa_base.cpp b/bob/learn/em/jfa_base.cpp index d4fd23b3141ff05b8f846110c1cf87f442da8a91..7637cc6c37516c110efe8a05010f245a53cfb863 100644 --- a/bob/learn/em/jfa_base.cpp +++ b/bob/learn/em/jfa_base.cpp @@ -302,7 +302,7 @@ PyObject* PyBobLearnEMJFABase_getUBM(PyBobLearnEMJFABaseObject* self, void*){ (PyBobLearnEMGMMMachineObject*)PyBobLearnEMGMMMachine_Type.tp_alloc(&PyBobLearnEMGMMMachine_Type, 0); retval->cxx = ubm_gmmMachine; - return Py_BuildValue("O",retval); + return Py_BuildValue("N",retval); BOB_CATCH_MEMBER("ubm could not be read", 0) } int PyBobLearnEMJFABase_setUBM(PyBobLearnEMJFABaseObject* self, PyObject* value, void*){ diff --git a/bob/learn/em/jfa_machine.cpp b/bob/learn/em/jfa_machine.cpp index 2567a956beed58b1699ad6215cec92444d93343f..29f2a34cad34499db09f330907335baf53329383 100644 --- a/bob/learn/em/jfa_machine.cpp +++ b/bob/learn/em/jfa_machine.cpp @@ -271,7 +271,7 @@ PyObject* PyBobLearnEMJFAMachine_getJFABase(PyBobLearnEMJFAMachineObject* self, (PyBobLearnEMJFABaseObject*)PyBobLearnEMJFABase_Type.tp_alloc(&PyBobLearnEMJFABase_Type, 0); retval->cxx = jfa_base_o; - return Py_BuildValue("O",retval); + return Py_BuildValue("N",retval); BOB_CATCH_MEMBER("jfa_base could not be read", 0) } int PyBobLearnEMJFAMachine_setJFABase(PyBobLearnEMJFAMachineObject* self, PyObject* value, void*){ diff --git a/bob/learn/em/jfa_trainer.cpp b/bob/learn/em/jfa_trainer.cpp index 5a1734c100ab4e13379523cb939e2014d7c576f4..f0a7a168811da767ef9f30aa31aeccb0045896f6 100644 --- a/bob/learn/em/jfa_trainer.cpp +++ b/bob/learn/em/jfa_trainer.cpp @@ -446,7 +446,7 @@ PyObject* PyBobLearnEMJFATrainer_getRng(PyBobLearnEMJFATrainerObject* self, void (PyBoostMt19937Object*)PyBoostMt19937_Type.tp_alloc(&PyBoostMt19937_Type, 0); retval->rng = self->cxx->getRng().get(); - return Py_BuildValue("O", retval); + return Py_BuildValue("N", retval); BOB_CATCH_MEMBER("Rng method could not be read", 0) } int PyBobLearnEMJFATrainer_setRng(PyBobLearnEMJFATrainerObject* self, PyObject* value, void*) { diff --git a/bob/learn/em/kmeans_trainer.cpp b/bob/learn/em/kmeans_trainer.cpp index 92da9c1a096bab332d68bb06163ecaab6a24d04b..ffe7629af0208a7aaca5c9ea7ecd7dcb0886e550 100644 --- a/bob/learn/em/kmeans_trainer.cpp +++ b/bob/learn/em/kmeans_trainer.cpp @@ -281,7 +281,7 @@ PyObject* PyBobLearnEMKMeansTrainer_getRng(PyBobLearnEMKMeansTrainerObject* self (PyBoostMt19937Object*)PyBoostMt19937_Type.tp_alloc(&PyBoostMt19937_Type, 0); retval->rng = self->cxx->getRng().get(); - return Py_BuildValue("O", retval); + return Py_BuildValue("N", retval); BOB_CATCH_MEMBER("Rng method could not be read", 0) } int PyBobLearnEMKMeansTrainer_setRng(PyBobLearnEMKMeansTrainerObject* self, PyObject* value, void*) { diff --git a/bob/learn/em/plda_machine.cpp b/bob/learn/em/plda_machine.cpp index 79859c2a7af9caf45fc1796d4c27450b05304756..6f73f4dc4fdef37d73d1fefbd0a4c638738da162 100644 --- a/bob/learn/em/plda_machine.cpp +++ b/bob/learn/em/plda_machine.cpp @@ -248,7 +248,7 @@ PyObject* PyBobLearnEMPLDAMachine_getPLDABase(PyBobLearnEMPLDAMachineObject* sel (PyBobLearnEMPLDABaseObject*)PyBobLearnEMPLDABase_Type.tp_alloc(&PyBobLearnEMPLDABase_Type, 0); retval->cxx = plda_base_o; - return Py_BuildValue("O",retval); + return Py_BuildValue("N",retval); BOB_CATCH_MEMBER("plda_base could not be read", 0) } int PyBobLearnEMPLDAMachine_setPLDABase(PyBobLearnEMPLDAMachineObject* self, PyObject* value, void*){ @@ -642,21 +642,28 @@ static PyObject* PyBobLearnEMPLDAMachine_computeLogLikelihood(PyBobLearnEMPLDAMa BOB_TRY char** kwlist = compute_log_likelihood.kwlist(0); - + PyBlitzArrayObject* samples; + PyArrayObject* numpy_samples; PyObject* with_enrolled_samples = Py_True; + + /*Convert to PyObject first to access the number of dimensions*/ + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|O!", kwlist, &PyArray_Type, &numpy_samples, + &PyBool_Type, &with_enrolled_samples)) return 0; + auto numpy_samples_ = make_safe(numpy_samples); + Py_INCREF(numpy_samples); + int dim = PyArray_NDIM(numpy_samples); - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|O!", kwlist, &PyBlitzArray_Converter, &samples, - &PyBool_Type, &with_enrolled_samples)) return 0; + /*Now converting to PyBlitzArrayObject*/ + samples = reinterpret_cast<PyBlitzArrayObject*>(PyBlitzArray_FromNumpyArray(numpy_samples)); auto samples_ = make_safe(samples); - - blitz::Array<double,2> blitz_test = *PyBlitzArrayCxx_AsBlitz<double,2>(samples); - - //There are 2 methods in C++, one <double,1> and the another <double,2> - if (blitz_test.extent(1)==0) + + /*Using the proper method according to the dimension*/ + if (dim==1) return Py_BuildValue("d",self->cxx->computeLogLikelihood(*PyBlitzArrayCxx_AsBlitz<double,1>(samples), f(with_enrolled_samples))); else return Py_BuildValue("d",self->cxx->computeLogLikelihood(*PyBlitzArrayCxx_AsBlitz<double,2>(samples), f(with_enrolled_samples))); + BOB_CATCH_MEMBER("`compute_log_likelihood` could not be read", 0) } @@ -678,13 +685,20 @@ static PyObject* PyBobLearnEMPLDAMachine_forward(PyBobLearnEMPLDAMachineObject* char** kwlist = forward.kwlist(0); PyBlitzArrayObject* samples; + PyArrayObject* numpy_samples; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&", kwlist, &PyBlitzArray_Converter, &samples)) return 0; + /*Convert to PyObject first to access the number of dimensions*/ + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &PyArray_Type, &numpy_samples)) return 0; + auto numpy_samples_ = make_safe(numpy_samples); + Py_INCREF(numpy_samples); + int dim = PyArray_NDIM(numpy_samples); + + /*Now converting to PyBlitzArrayObject*/ + samples = reinterpret_cast<PyBlitzArrayObject*>(PyBlitzArray_FromNumpyArray(numpy_samples)); auto samples_ = make_safe(samples); - blitz::Array<double,2> blitz_test = *PyBlitzArrayCxx_AsBlitz<double,2>(samples); //There are 2 methods in C++, one <double,1> and the another <double,2> - if (blitz_test.extent(1)==0) + if(dim==1) return Py_BuildValue("d",self->cxx->forward(*PyBlitzArrayCxx_AsBlitz<double,1>(samples))); else return Py_BuildValue("d",self->cxx->forward(*PyBlitzArrayCxx_AsBlitz<double,2>(samples))); diff --git a/bob/learn/em/test/test_plda.py b/bob/learn/em/test/test_plda.py index 062857e6aa74ec9206fa72ac905c59773355ff57..6ea34cc64aff12181ad00dc3d38f2c3ed072727f 100644 --- a/bob/learn/em/test/test_plda.py +++ b/bob/learn/em/test/test_plda.py @@ -480,6 +480,10 @@ def test_plda_machine_log_likelihood_Python(): ar_s = numpy.vstack([ar_e, ar_p]) assert abs(m.compute_log_likelihood(ar_s, False) - compute_log_likelihood(ar_s, mu, C_F, C_G, sigma)) < 1e-10 ar_p2d = numpy.reshape(ar_p, (1,C_dim_d)) + + print ar_p.shape + a = m.compute_log_likelihood(ar_p, False) + assert abs(m.compute_log_likelihood(ar_p, False) - compute_log_likelihood(ar_p2d, mu, C_F, C_G, sigma)) < 1e-10 # Defines (random) samples and check forward method