0

I am new to tensorflow as well as including python code in c++, therefore I would apprechiate any tips/comments on the following weird behaviour: I have a c++ class pythoninterface with headerfile pythoninterface.h:

#include <string>
#include <iostream>

class pythoninterface{
private:
    const char* file;
    const char* funct;
    const char* filepath;

public:
    pythoninterface();

    ~pythoninterface();

    void CallFunction();
};

The sourcefile pythoninterface.cpp:

#include <Python.h>
#include <string>
#include <sstream>
#include <vector>
#include "pythoninterface.h"

pythoninterface::pythoninterface(){

    file = "TensorflowIncludePy";
    funct = "myTestFunction";
    filepath = "/path/To/TensorflowIncludePy.py";
}

void pythoninterface::CallFunction(){

   PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *presult;

   // Initialize the Python Interpreter
   Py_Initialize();

   //Set in path where to find the custom python module other than the path where Python's system modules/packages are found.
   std::stringstream changepath;
   changepath << "import sys; sys.path.insert(0, '" << filepath << "')";
   const std::string tmp = changepath.str();
   filepath = tmp.c_str();
   PyRun_SimpleString (this->filepath);


   // Build the name object
   pName = PyString_FromString(this->file);

   // Load the module object
   pModule = PyImport_Import(pName);

   if(pModule != NULL) {
   // pDict is a borrowed reference
       pDict = PyModule_GetDict(pModule);


       // pFunc is also a borrowed reference
       pFunc = PyDict_GetItemString(pDict, this->funct);

       if (PyCallable_Check(pFunc))
       {
           pValue=Py_BuildValue("()");
           printf("pValue is empty!\n");
           PyErr_Print();
           presult=PyObject_CallObject(pFunc,pValue);
           PyErr_Print();
       } else
       {
           PyErr_Print();
       }
       printf("Result is %d!\n",PyInt_AsLong(presult));
       Py_DECREF(pValue);

       // Clean up
       Py_DECREF(pModule);
       Py_DECREF(pName);
   }
   else{
       std::cout << "Python retuned null pointer, no file!" << std::endl;
   }

   // Finish the Python Interpreter
   Py_Finalize();
}

And the Python File from which the function should be included (TensorflowIncludePy.py):

def myTestFunction():
    print 'I am a function without an input!'
    gettingStartedTF()
    return 42


def gettingStartedTF():
    import tensorflow as tf #At this point the error occurs!
    hello = tf.constant('Hello, TensorFlow!')
    sess = tf.Session()
    print(sess.run(hello))
    return 42

Finally in my main function I only create a pythoninterface object p and call the function p.CallFunction(). The communication between the c++ and python code works all right, but when (at runtime) the line import tensorflow as tf is reached, I get a *** stack smashing detected *** error message and the program finishes. Can anyone guess what the problem might be or had a similar issue before?

I know there is a c++ tensorflow API, but I feel more comfortable with using tensorflow in python so I thought this might be the perfect solution for me (apparently it is not...:P)

Seastar
  • 386
  • 2
  • 19
  • What are you trying to achieve? I am quite confused by your code. You are executing python code from C++. Why? – iga Aug 23 '17 at 23:32
  • I have a "c++ framework" with multiple classification algorithms implemented. In the end I want to combine and compare those different algorithms in c++. This is why I am looking for a way to call my tensorflow python functions from this "c++ framework" with certain inputs and get the trained model back as output (or save the trained model then), so I can then use it again in my c++ code. I know it is a quite confusing attempt, but until now I don't see a better solution... – Seastar Aug 24 '17 at 06:28
  • I see. I don't know much about cPython libraries to help here. However, If you are not keep on squeezing the last ounce of performance here, you might be better of using some RPC library (say gRPC) to have the python and C++ processes talk to each other. – iga Aug 24 '17 at 18:01
  • That sounds like a good alternative, I will have a look into it. Thanks! – Seastar Aug 25 '17 at 06:30

0 Answers0