diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f6ae04..8e16f89 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,5 +9,14 @@ find_package(Torch REQUIRED) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}") +# Temporary patch while find_package(Torch) is not fixed +file( + GLOB + LIBTORCH_PYTHON + "${TORCH_INSTALL_PREFIX}/lib/libtorch_python.so" + "${TORCH_INSTALL_PREFIX}/lib/libtorch_python.dylib" +) +message(STATUS "TORCH Libraries: ${LIBTORCH_PYTHON}") + add_subdirectory(lib/Files) add_subdirectory(src) \ No newline at end of file diff --git a/README.md b/README.md index c1ec2aa..fdda9b6 100644 --- a/README.md +++ b/README.md @@ -2,5 +2,5 @@ Python Wrapper to use Python classifiers from C++ -Explain issues with conda and libstdc++ and how to fix them +Explain issues in Linux with conda and libstdc++ and how to fix them (moving conda libstdc++ to a different folder) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f02df68..c0588f4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,5 +4,5 @@ include_directories(${Python3_INCLUDE_DIRS}) add_executable(main main.cc STree.cc SVC.cc PyClassifier.cc PyWrap.cc) add_executable(example example.cpp) -target_link_libraries(main ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ArffFiles) +target_link_libraries(main ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} ArffFiles) target_link_libraries(example "${TORCH_LIBRARIES}" ArffFiles) diff --git a/src/PyClassifier.cc b/src/PyClassifier.cc index 3012ae8..cef9bdb 100644 --- a/src/PyClassifier.cc +++ b/src/PyClassifier.cc @@ -1,5 +1,7 @@ #include "PyClassifier.h" #include +#include +#include namespace pywrap { @@ -13,7 +15,11 @@ namespace pywrap { { pyWrap->clean(module, className); } - + PyObject* PyClassifier::toPyObject(torch::Tensor& tensor) + { + return torch::utils::tensor_to_numpy(tensor); + //return THPVariable_Wrap(tensor); + } std::string PyClassifier::version() { return pyWrap->version(module, className); @@ -25,10 +31,35 @@ namespace pywrap { } PyClassifier& PyClassifier::fit(torch::Tensor& X, torch::Tensor& y, const std::vector& features, const std::string& className, std::map>& states) { - PyObject* Xp = NULL;//THPVariable_Wrap(X); - PyObject* yp = NULL;//THPVariable_Wrap(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 = toPyObject(X); + std::cout << "Converting y to PyObject" << std::endl; + PyObject* yp = toPyObject(y); + std::cout << "Calling fit" << std::endl; pyWrap->fit(module, className, Xp, yp); + Py_DECREF(Xp); + Py_DECREF(yp); return *this; } + torch::Tensor PyClassifier::predict(torch::Tensor& X) + { + PyObject* Xp = toPyObject(X); + auto PyResult = pyWrap->predict(module, className, Xp); + auto result = THPVariable_Unpack(PyResult); + Py_DECREF(Xp); + Py_DECREF(PyResult); + return result; + } + double PyClassifier::score(torch::Tensor& X, torch::Tensor& y) + { + PyObject* Xp = toPyObject(X); + PyObject* yp = toPyObject(y); + auto result = pyWrap->score(module, className, Xp, yp); + Py_DECREF(Xp); + Py_DECREF(yp); + return result; + } } /* namespace PyWrap */ \ No newline at end of file diff --git a/src/PyClassifier.h b/src/PyClassifier.h index e23a0b2..7e9acab 100644 --- a/src/PyClassifier.h +++ b/src/PyClassifier.h @@ -17,6 +17,7 @@ namespace pywrap { std::string version(); std::string callMethodString(const std::string& method); private: + PyObject* toPyObject(torch::Tensor& tensor); PyWrap* pyWrap; std::string module; std::string className; diff --git a/src/STree.h b/src/STree.h index 5299d6a..b442a61 100644 --- a/src/STree.h +++ b/src/STree.h @@ -7,8 +7,6 @@ namespace pywrap { public: STree() : PyClassifier("stree", "Stree") {}; ~STree() = default; - private: - }; } /* namespace pywrap */ diff --git a/src/SVC.h b/src/SVC.h index c84000a..41cfb52 100644 --- a/src/SVC.h +++ b/src/SVC.h @@ -8,8 +8,6 @@ namespace pywrap { SVC() : PyClassifier("sklearn.svm", "SVC") {}; ~SVC() = default; std::string version(); - private: - }; } /* namespace pywrap */ diff --git a/src/main.cc b/src/main.cc index cc7283a..50e2461 100644 --- a/src/main.cc +++ b/src/main.cc @@ -48,5 +48,10 @@ int main(int argc, char* argv[]) auto svc = pywrap::SVC(); svc.version(); stree.version(); + cout << string(80, '-') << endl; + cout << "X: " << X.sizes() << endl; + cout << "y: " << y.sizes() << endl; + auto result = svc.fit(X, y, features, className, states).score(X, y); + cout << "SVC score " << result << endl; return 0; } \ No newline at end of file