99 lines
4.4 KiB
C++
99 lines
4.4 KiB
C++
#include "PyClassifier.h"
|
|
#include <boost/python/numpy.hpp>
|
|
#include <torch/csrc/autograd/python_variable.h>
|
|
#include <torch/csrc/utils/tensor_numpy.h>
|
|
//#include "tensorflow/python/lib/core/py_func.h"
|
|
#include <iostream>
|
|
|
|
namespace pywrap {
|
|
namespace p = boost::python;
|
|
namespace np = boost::python::numpy;
|
|
PyClassifier::PyClassifier(const std::string& module, const std::string& className) : module(module), className(className)
|
|
{
|
|
pyWrap = PyWrap::GetInstance();
|
|
pyWrap->importClass(module, className);
|
|
}
|
|
|
|
PyClassifier::~PyClassifier()
|
|
{
|
|
std::cout << "Cleaning Classifier" << std::endl;
|
|
pyWrap->clean(module, className);
|
|
std::cout << "Classifier cleaned" << std::endl;
|
|
}
|
|
PyObject* PyClassifier::toPyObject(torch::Tensor& data_tensor)
|
|
{
|
|
|
|
// return torch::utils::tensor_to_numpy(data_tensor);
|
|
return THPVariable_Wrap(data_tensor);
|
|
//auto data_numpy = np::from_data(data_tensor.data_ptr(), np::dtype::get_builtin<float>(), p::make_tuple(m, n), p::make_tuple(sizeof(data_tensor.dtype()) * 2 * n, sizeof(data_tensor.dtype()) * 2), p::object());
|
|
// PyObject* numpyObject = data_numpy.ptr();
|
|
|
|
// return numpyObject;
|
|
}
|
|
// PyObject* PyClassifier::toPyObjecty(torch::Tensor& data_tensor)
|
|
// {
|
|
// //return THPVariable_Wrap(tensor);
|
|
// auto y_numpy = np::from_data(data_tensor.data_ptr(), np::dtype::get_builtin<int32_t>(), p::make_tuple(m), p::make_tuple(sizeof(data_tensor.dtype()) * 2), p::object());
|
|
// PyObject* numpyObject = y_numpy.ptr();
|
|
|
|
// }
|
|
std::string PyClassifier::version()
|
|
{
|
|
return pyWrap->version(module, className);
|
|
}
|
|
|
|
std::string PyClassifier::callMethodString(const std::string& method)
|
|
{
|
|
return pyWrap->callMethodString(module, className, method);
|
|
}
|
|
void print_array(np::ndarray& array)
|
|
{
|
|
std::cout << "Array: " << std::endl;
|
|
std::cout << p::extract<char const*>(p::str(array)) << std::endl;
|
|
}
|
|
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 << "Converting X to PyObject" << std::endl;
|
|
std::cout << "X.defined() = " << X.defined() << std::endl;
|
|
//std::cout << "X.pyobj() = " << X.pyobj() << std::endl;
|
|
//PyObject* Xp = torch::utils::tensor_to_numpy(X);
|
|
auto XX = X.transpose(0, 1);
|
|
int m = XX.size(0);
|
|
int n = XX.size(1);
|
|
auto data_numpy = np::from_data(XX.data_ptr(), np::dtype::get_builtin<float>(), p::make_tuple(m, n), p::make_tuple(sizeof(XX.dtype()) * 2 * n, sizeof(XX.dtype()) * 2), p::object());
|
|
print_array(data_numpy);
|
|
CPyObject Xp = data_numpy.ptr();
|
|
std::cout << "Converting y to PyObject" << std::endl;
|
|
auto y_numpy = np::from_data(y.data_ptr(), np::dtype::get_builtin<int32_t>(), p::make_tuple(m), p::make_tuple(sizeof(y.dtype()) * 2), p::object());
|
|
CPyObject yp = y_numpy.ptr();
|
|
std::cout << "Calling fit" << std::endl;
|
|
pyWrap->fit(module, this->className, Xp, yp);
|
|
return *this;
|
|
}
|
|
torch::Tensor PyClassifier::predict(torch::Tensor& X)
|
|
{
|
|
CPyObject Xp = toPyObject(X);
|
|
auto PyResult = pyWrap->predict(module, className, Xp);
|
|
auto result = THPVariable_Unpack(PyResult);
|
|
return result;
|
|
}
|
|
double PyClassifier::score(torch::Tensor& X, torch::Tensor& y)
|
|
{
|
|
std::cout << "Converting X to PyObject" << std::endl;
|
|
std::cout << "X.defined() = " << X.defined() << std::endl;
|
|
//std::cout << "X.pyobj() = " << X.pyobj() << std::endl;
|
|
//PyObject* Xp = torch::utils::tensor_to_numpy(X);
|
|
auto XX = X.transpose(0, 1);
|
|
int m = XX.size(0);
|
|
int n = XX.size(1);
|
|
auto data_numpy = np::from_data(XX.data_ptr(), np::dtype::get_builtin<float>(), p::make_tuple(m, n), p::make_tuple(sizeof(XX.dtype()) * 2 * n, sizeof(XX.dtype()) * 2), p::object());
|
|
print_array(data_numpy);
|
|
CPyObject Xp = data_numpy.ptr();
|
|
std::cout << "Converting y to PyObject" << std::endl;
|
|
auto y_numpy = np::from_data(y.data_ptr(), np::dtype::get_builtin<int32_t>(), p::make_tuple(m), p::make_tuple(sizeof(y.dtype()) * 2), p::object());
|
|
CPyObject yp = y_numpy.ptr();
|
|
auto result = pyWrap->score(module, className, Xp, yp);
|
|
return result;
|
|
}
|
|
|
|
} /* namespace PyWrap */ |