not able to embed python tensorflow code in c++

Viewed 50

I am building NIST FRVT11 library. I have core code in python, that I want to call from c++, I have followed offical Python C API docs to use python functions in c++.

I am able to load and predict with keras model once during initialization stage. But cannot predict from the same model after that model.predict function takes inifite time. I'm not even able to load the model, this time tf.keras.models.load_model takes inifite time.

I am not getting any error that I can paste here :(

NIST wants everything to be packaged within library, so I have built python locally and attaching that in the submission.

python: 3.9.13 tensorflow: 2.8.0 keras: 2.8.0

python code

import tensorflow as tf
def get_model(model_path):
    embedding_model = tf.keras.models.load_model(model_path)
    return embedding_model 

def predict(model, img):
    out= model.predict(img)
    return out

c++ code

int call_python_code(const std::string &configDir)
{
    this->configDir = configDir;
    // add directory to sys path
    std::string command_sys_path_to_append = "import sys, os\n"
                "sys.path.append(os.path.join(os.getcwd(), '" + this- 
               >configDir + "'))\n";
    PyRun_SimpleString(command_sys_path_to_append.c_str());

    Py_Initialize();
    PyObject *p_module_name = PyUnicode_DecodeFSDefault("fr"); // fr.py file
    this->p_module = PyImport_Import(p_module_name);

    // load the model from c++
    PyObject *outer_tuple_2 = PyTuple_New(1);
    PyTuple_SetItem(outer_tuple_2, 0, PyUnicode_DecodeFSDefault(this->configDir.c_str()));
    PyObject *p_fucntion_model = PyObject_GetAttrString(p_module, "get_model");
this->p_embedding_model_object = PyObject_CallObject(p_fucntion_model, outer_tuple_2);

 

 // get prediction 

    PyObject *p_function_predict= PyObject_GetAttrString(p_module, "predict");


PyObject *full_args = PyTuple_New(2); 
PyTuple_SetItem(full_args, 0, this->p_fucntion_model);
PyTuple_SetItem(full_args, 1, <image array>);
PyObject *outer_tuple = PyTuple_New(1);
PyTuple_SetItem(outer_tuple, 0, full_args);
PyObject *p_return_tuple = PyObject_CallObject(p_function_predict, outer_tuple);

}
0 Answers
Related