-1

I have a CPP project which should use some functions from a python script. when I run the code PyObject_CallObject() works fine for the first time (This is inside a loop), but in the second call it returns null. I know my code may be inefficient or not professional, but my question is that why PyObject_CallObject() is failed when it is called second time inside the loop. I represented related part of my code as follows.

Py_Initialize();
string Python_file_name1 = "Main_Test";
PyObject* myModuleString1 = PyUnicode_DecodeFSDefault(Python_file_name1.c_str());
if (myModuleString1 == NULL) {
    cout << "unable to get python file name ";
    return 1;
}
PyObject* myModule1 = PyImport_Import(myModuleString1);
if (myModule1 == NULL) {
    cout << "unabale to import python module";
    return 1;
}      
PyObject* myFunction1 = PyObject_GetAttrString(myModule1, (char*)"model_forecast");
PyObject* myResult1;
PyObject* args2;
PyObject* pXVec; 
float CurrentStep = 1;
vector<float> v2 = VectorXf::Zero(96);
for (int j =0; j<10; j++){
    pXVec = PyTuple_New(v2.size());
    for (int i = 0; i < v2.size(); ++i) {
        pValue = PyFloat_FromDouble(v2[i]);
        if (!pValue) {
            Py_DECREF(pXVec);
            fprintf(stderr, "Cannot convert array value\n");
            return 1;
        }
        PyTuple_SetItem(pXVec, i, pValue);
    }
    args2 = PyTuple_Pack(2, pXVec, PyFloat_FromDouble((double)CurrentStep));
    myResult1 = PyObject_CallObject(myFunction1, args2);
    vector<float> DL_Load_forecst;
    DL_Load_forecst = listTupleToVector_float(myResult1);
}

Py_DECREF(args2);
Py_DECREF(myResult1);
Py_DECREF(myModuleString1);
Py_DECREF(myFunction1);
Py_DECREF(pXVec);   
Py_Finalize();

the python code is

import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = '-1'
import numpy as np
import keras.backend as K
from keras.models import load_model
model_name = 'forecast_model_final.h5'
model = load_model(model_name)
model.summary()

def model_forecast(x, st_time):
    st_time = int(st_time)
    x = np.reshape(x, (1,-1))
    x_curr = np.zeros((1,96))
    if st_time != 0: x_curr[:,0:st_time] = x[:, -st_time:]
    x = np.roll(x, [0,st_time])
    x = x.reshape((1,96,7,4), order='F')
    y_pred = model.predict([x, x_curr], verbose=0)[:, st_time:st_time+96]
    y_pred_L = y_pred.tolist()
    return y_pred_L
user1538653
  • 121
  • 1
  • 1
  • 10

1 Answers1

0

I found the problem thanks to @molbdnilo,

y_pred_L = y_pred.tolist()

should be changed to

y_pred_L = y_pred.reshape((-1,)).tolist()

which is obviously my mistake given the definition of listTupleToVector_float function. However, I do not know why the function is executed in the first call without error !!!

user1538653
  • 121
  • 1
  • 1
  • 10