I want to call a python code in language C. In python, a trained tf model will be restored to predict images. But this python code stopped when restore tf model.

I don't know what happened, because in python this code is OK.

C code

PyObject *pModule = NULL;
PyObject *pFunc = NULL;
char py_module_name[] = "prediction";
char py_func_name[] = "inference";

// Initialize python interpreter
Py_Initialize();

// Import python module
pModule = PyImport_ImportModule(py_module_name);

pFunc = PyObject_GetAttrString(pModule, py_func_name);

/* stack images as an array
PyObject *Image_List = PyTuple_New(img_num);

 ...

PyObject *pArgs = PyTuple_New(1);
PyTuple_SetItem(pArgs, 0, Image_List);

*/

PyObject* pResult = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);

int label_size = PyList_Size(pResult);

Python code

print(type(x))
x = np.array(x, dtype=np.float32)
x = x /255.0 - 0.5
x = x.reshape(-1, 28, 28, 1)
print(x.shape)
sess = tf.Session()

imgs = tf.placeholder(tf.float32, shape=[None, 28,28,1])
logits = model.inference(imgs)
probs = tf.nn.softmax(logits, axis=1)
labels = tf.argmax(probs, 1)        

print("load model...")
saver = tf.train.Saver()
saver.restore(sess, "tf2caffe/model-lenet-12000")

print("predict....")
preds = sess.run(labels, feed_dict={imgs:x})
sess.close()

Output info

<class 'tuple'>
(5, 28, 28, 1)
load model...

It doesn't restore model and run out to C program, pResult got a NULL pointer.

0 Answers