Begin predict
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#include "PyClassifier.h"
|
||||
|
||||
#include "numpy/arrayobject.h"
|
||||
#include <iostream>
|
||||
|
||||
namespace pywrap {
|
||||
@@ -16,11 +16,6 @@ namespace pywrap {
|
||||
pyWrap->clean(module, className);
|
||||
std::cout << "Classifier cleaned" << std::endl;
|
||||
}
|
||||
void print_array(np::ndarray& array)
|
||||
{
|
||||
std::cout << "Array: " << std::endl;
|
||||
std::cout << p::extract<char const*>(p::str(array)) << std::endl;
|
||||
}
|
||||
np::ndarray tensor2numpy(torch::Tensor& X)
|
||||
{
|
||||
int m = X.size(0);
|
||||
@@ -33,51 +28,63 @@ namespace pywrap {
|
||||
{
|
||||
int n = X.size(1);
|
||||
auto yn = np::from_data(y.data_ptr(), np::dtype::get_builtin<int32_t>(), p::make_tuple(n), p::make_tuple(sizeof(y.dtype()) * 2), p::object());
|
||||
//std::cout << "Printing from within tensors2numpy" << std::endl;
|
||||
// print_array(yn);
|
||||
return { tensor2numpy(X), yn };
|
||||
}
|
||||
std::string PyClassifier::version()
|
||||
{
|
||||
return pyWrap->version(module, className);
|
||||
}
|
||||
std::string PyClassifier::graph()
|
||||
{
|
||||
return pyWrap->graph(module, className);
|
||||
}
|
||||
std::string PyClassifier::callMethodString(const std::string& method)
|
||||
{
|
||||
return pyWrap->callMethodString(module, className, method);
|
||||
}
|
||||
|
||||
PyClassifier& PyClassifier::fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states)
|
||||
{
|
||||
std::cout << "PyClassifier:fit:Converting X to PyObject" << std::endl;
|
||||
auto [Xn, yn] = tensors2numpy(X, y);
|
||||
CPyObject Xp = boost::python::incref(boost::python::object(Xn).ptr());
|
||||
std::cout << "PyClassifier:fit:Converting y to PyObject" << std::endl;
|
||||
print_array(yn);
|
||||
CPyObject yp = boost::python::incref(boost::python::object(yn).ptr());
|
||||
std::cout << "PyClassifier:fit:Calling fit" << std::endl;
|
||||
CPyObject Xp = p::incref(p::object(Xn).ptr());
|
||||
CPyObject yp = p::incref(p::object(yn).ptr());
|
||||
pyWrap->fit(module, this->className, Xp, yp);
|
||||
return *this;
|
||||
}
|
||||
void print_array(np::ndarray& array)
|
||||
{
|
||||
std::cout << "Array: " << std::endl;
|
||||
std::cout << p::extract<char const*>(p::str(array)) << std::endl;
|
||||
}
|
||||
torch::Tensor PyClassifier::predict(torch::Tensor& X)
|
||||
{
|
||||
int dimension = X.size(1);
|
||||
auto Xn = tensor2numpy(X);
|
||||
CPyObject Xp = boost::python::incref(boost::python::object(Xn).ptr());
|
||||
auto PyResult = pyWrap->predict(module, className, Xp);
|
||||
auto result = torch::tensor({ 1,2,3 });
|
||||
CPyObject Xp = p::incref(p::object(Xn).ptr());
|
||||
PyObject* incoming = pyWrap->predict(module, className, Xp);
|
||||
std::cout << "Return from predict" << std::endl;
|
||||
p::handle<> handle(incoming);
|
||||
p::object object(handle);
|
||||
np::ndarray prediction = np::from_object(object);
|
||||
print_array(prediction);
|
||||
// import_array();
|
||||
// if (!PyArray_Check(incoming)) {
|
||||
// throw std::logic_error("Returned value is not array");
|
||||
// }
|
||||
// std::cout << "Returned value is array" << std::endl;
|
||||
// PyArrayObject* np_ret = (PyArrayObject*)incoming;
|
||||
// if (PyArray_NDIM(np_ret) != dimension - 1) {
|
||||
// throw std::logic_error("Returned array has wrong dimension" + std::to_string(PyArray_NDIM(np_ret)) + "!=" + std::to_string(dimension - 1));
|
||||
// }
|
||||
// std::cout << "Returned array has correct dimension" << PyArray_NDIM(np_ret) << std::endl;
|
||||
// int len{ PyArray_SHAPE(np_ret)[0] };
|
||||
// int* data = reinterpret_cast<int*>(PyArray_DATA(np_ret));
|
||||
|
||||
return result;
|
||||
// int* data = reinterpret_cast<int*>(prediction.get_data());
|
||||
// auto resultTensor = torch::tensor({ data }, torch::kInt32);
|
||||
auto resultTensor = torch::zeros({ prediction.shape(0) }, torch::kInt32);
|
||||
return resultTensor;
|
||||
}
|
||||
double PyClassifier::score(torch::Tensor& X, torch::Tensor& y)
|
||||
{
|
||||
std::cout << "PyClassifier::Score:Converting X to PyObject" << std::endl;
|
||||
auto [Xn, yn] = tensors2numpy(X, y);
|
||||
CPyObject Xp = boost::python::incref(boost::python::object(Xn).ptr());
|
||||
CPyObject yp = boost::python::incref(boost::python::object(yn).ptr());
|
||||
print_array(yn);
|
||||
CPyObject Xp = p::incref(p::object(Xn).ptr());
|
||||
CPyObject yp = p::incref(p::object(yn).ptr());
|
||||
auto result = pyWrap->score(module, className, Xp, yp);
|
||||
return result;
|
||||
}
|
||||
|
@@ -10,7 +10,6 @@
|
||||
#include "PyWrap.h"
|
||||
|
||||
namespace pywrap {
|
||||
|
||||
class PyClassifier {
|
||||
public:
|
||||
PyClassifier(const std::string& module, const std::string& className);
|
||||
@@ -19,7 +18,6 @@ namespace pywrap {
|
||||
torch::Tensor predict(torch::Tensor& X);
|
||||
double score(torch::Tensor& X, torch::Tensor& y);
|
||||
std::string version();
|
||||
std::string graph();
|
||||
std::string callMethodString(const std::string& method);
|
||||
private:
|
||||
PyWrap* pyWrap;
|
||||
|
@@ -47,12 +47,10 @@ namespace pywrap {
|
||||
}
|
||||
void PyWrap::importClass(const std::string& moduleName, const std::string& className)
|
||||
{
|
||||
std::cout << "Importando clase" << std::endl;
|
||||
auto result = moduleClassMap.find({ moduleName, className });
|
||||
if (result != moduleClassMap.end()) {
|
||||
return;
|
||||
}
|
||||
std::cout << "No estaba en el mapa" << std::endl;
|
||||
CPyObject module = PyImport_ImportModule(moduleName.c_str());
|
||||
if (PyErr_Occurred()) {
|
||||
errorAbort("Could't import module " + moduleName);
|
||||
@@ -75,12 +73,12 @@ namespace pywrap {
|
||||
void PyWrap::clean(const std::string& moduleName, const std::string& className)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
std::cout << "Limpiando" << std::endl;
|
||||
std::cout << "Start cleaning " << moduleName << "." << className << std::endl;
|
||||
auto result = moduleClassMap.find({ moduleName, className });
|
||||
if (result == moduleClassMap.end()) {
|
||||
return;
|
||||
}
|
||||
std::cout << "--> Limpiando" << std::endl;
|
||||
std::cout << "--> Cleaning PyObject" << std::endl;
|
||||
Py_DECREF(std::get<0>(result->second));
|
||||
Py_DECREF(std::get<1>(result->second));
|
||||
Py_DECREF(std::get<2>(result->second));
|
||||
@@ -92,7 +90,7 @@ namespace pywrap {
|
||||
if (moduleClassMap.empty()) {
|
||||
RemoveInstance();
|
||||
}
|
||||
std::cout << "Limpieza terminada" << std::endl;
|
||||
std::cout << "End Cleaning " << moduleName << "." << className << std::endl;
|
||||
}
|
||||
void PyWrap::errorAbort(const std::string& message)
|
||||
{
|
||||
@@ -107,12 +105,10 @@ namespace pywrap {
|
||||
if (item == moduleClassMap.end()) {
|
||||
errorAbort("Module " + moduleName + " and class " + className + " not found");
|
||||
}
|
||||
std::cout << "Clase encontrada" << std::endl;
|
||||
return std::get<2>(item->second);
|
||||
}
|
||||
std::string PyWrap::callMethodString(const std::string& moduleName, const std::string& className, const std::string& method)
|
||||
{
|
||||
std::cout << "Llamando método " << method << std::endl;
|
||||
PyObject* instance = getClass(moduleName, className);
|
||||
PyObject* result;
|
||||
try {
|
||||
@@ -125,21 +121,15 @@ namespace pywrap {
|
||||
exit(1);
|
||||
}
|
||||
std::string value = PyUnicode_AsUTF8(result);
|
||||
std::cout << "Result: " << value << std::endl;
|
||||
Py_DECREF(result);
|
||||
Py_XDECREF(result);
|
||||
return value;
|
||||
}
|
||||
std::string PyWrap::version(const std::string& moduleName, const std::string& className)
|
||||
{
|
||||
return callMethodString(moduleName, className, "version");
|
||||
}
|
||||
std::string PyWrap::graph(const std::string& moduleName, const std::string& className)
|
||||
{
|
||||
return callMethodString(moduleName, className, "graph");
|
||||
}
|
||||
void PyWrap::fit(const std::string& moduleName, const std::string& className, CPyObject& X, CPyObject& y)
|
||||
{
|
||||
std::cout << "Llamando método fit" << std::endl;
|
||||
PyObject* instance = getClass(moduleName, className);
|
||||
CPyObject result;
|
||||
std::string method = "fit";
|
||||
@@ -153,11 +143,12 @@ namespace pywrap {
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
CPyObject PyWrap::predict(const std::string& moduleName, const std::string& className, CPyObject& X)
|
||||
|
||||
PyObject* PyWrap::predict(const std::string& moduleName, const std::string& className, CPyObject& X)
|
||||
{
|
||||
std::cout << "Llamando método predict" << std::endl;
|
||||
CPyObject instance = getClass(moduleName, className);
|
||||
CPyObject result;
|
||||
PyObject* instance = getClass(moduleName, className);
|
||||
PyObject* result;
|
||||
std::string method = "predict";
|
||||
try {
|
||||
if (!(result = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString(method.c_str()), X.getObject(), NULL)))
|
||||
@@ -168,11 +159,10 @@ namespace pywrap {
|
||||
RemoveInstance();
|
||||
exit(1);
|
||||
}
|
||||
return result;
|
||||
return result; // Caller must free this object
|
||||
}
|
||||
double PyWrap::score(const std::string& moduleName, const std::string& className, CPyObject& X, CPyObject& y)
|
||||
{
|
||||
std::cout << "Llamando método score" << std::endl;
|
||||
PyObject* instance = getClass(moduleName, className);
|
||||
CPyObject result;
|
||||
std::string method = "score";
|
||||
|
@@ -22,9 +22,8 @@ namespace pywrap {
|
||||
~PyWrap() = default;
|
||||
std::string callMethodString(const std::string& moduleName, const std::string& className, const std::string& method);
|
||||
std::string version(const std::string& moduleName, const std::string& className);
|
||||
std::string graph(const std::string& moduleName, const std::string& className);
|
||||
void fit(const std::string& moduleName, const std::string& className, CPyObject& X, CPyObject& y);
|
||||
CPyObject predict(const std::string& moduleName, const std::string& className, CPyObject& X);
|
||||
PyObject* predict(const std::string& moduleName, const std::string& className, CPyObject& X);
|
||||
double score(const std::string& moduleName, const std::string& className, CPyObject& X, CPyObject& y);
|
||||
void clean(const std::string& moduleName, const std::string& className);
|
||||
void importClass(const std::string& moduleName, const std::string& className);
|
||||
|
@@ -1,10 +1,8 @@
|
||||
#include "STree.h"
|
||||
|
||||
namespace pywrap {
|
||||
|
||||
std::string STree::graph()
|
||||
{
|
||||
// return callMethodString("graph");
|
||||
return PyClassifier::graph();
|
||||
return callMethodString("graph");
|
||||
}
|
||||
} /* namespace pywrap */
|
@@ -9,6 +9,5 @@ namespace pywrap {
|
||||
~STree() = default;
|
||||
std::string graph();
|
||||
};
|
||||
|
||||
} /* namespace pywrap */
|
||||
#endif /* STREE_H */
|
@@ -1,10 +1,8 @@
|
||||
#include "SVC.h"
|
||||
|
||||
namespace pywrap {
|
||||
|
||||
std::string SVC::version()
|
||||
{
|
||||
return callMethodString("_repr_html_");
|
||||
}
|
||||
|
||||
} /* namespace pywrap */
|
23
src/main.cc
23
src/main.cc
@@ -47,21 +47,18 @@ int main(int argc, char* argv[])
|
||||
auto [X, y, features, className, states] = loadDataset("iris", true);
|
||||
cout << "X: " << X.sizes() << endl;
|
||||
cout << "y: " << y.sizes() << endl;
|
||||
auto clf = pywrap::PyClassifier("stree", "Stree");
|
||||
auto clf = pywrap::STree();
|
||||
cout << "STree Version: " << clf.version() << endl;
|
||||
if (true) {
|
||||
auto svc = pywrap::PyClassifier("sklearn.svm", "SVC");
|
||||
cout << "SVC Version: " << svc.callMethodString("_repr_html_") << endl;
|
||||
cout << "Calling fit" << endl;
|
||||
svc.fit(X, y, features, className, states);
|
||||
cout << "Calling score" << endl;
|
||||
cout << "SVC Score: " << svc.score(X, y) << endl;
|
||||
}
|
||||
cout << "Graph: " << clf.graph() << endl;
|
||||
cout << "Calling fit" << endl;
|
||||
// if (true) {
|
||||
// auto svc = pywrap::SVC();
|
||||
// svc.fit(X, y, features, className, states);
|
||||
// cout << "SVC Score: " << svc.score(X, y) << endl;
|
||||
// }
|
||||
// cout << "Graph: " << endl << clf.graph() << endl;
|
||||
clf.fit(X, y, features, className, states);
|
||||
cout << "Calling score" << endl;
|
||||
cout << "STree Score: " << clf.score(X, y) << endl;
|
||||
// cout << "STree Score: " << clf.score(X, y) << endl;
|
||||
auto prediction = clf.predict(X);
|
||||
cout << "Prediction: " << prediction << endl;
|
||||
}
|
||||
cout << "* End." << endl;
|
||||
}
|
Reference in New Issue
Block a user