Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing tensorflow when embedding python in c++ returns null

I've a question regarding embedding python into a C++ application. The setup is as follows: I have a large C++ application which generates some data (renders images in real-time) and displays them. I have also trained a neural network in python using tensorflow which will accept these images.

My idea was to embed python and send data as a numpy array, predict using the neural network and get back another processed numpy array to display (in C++). I've made some basic tests without tensorflow on python side to get a feel for embedding python in c and such and it seems to work.

However, once i place "import tensorflow" into any python script that I want to import, I'll get a NULL from PyImport_ImportModule in c++ part.

e.g.

import numpy as np
def foo(img):
    return np.clip(img * 2.0, 0, 255).astype(np.uint8)

works fine. But the following doesn't:

import numpy as np
import tensorflow as tf #this causes the fail

def foo(img):
    return np.clip(img * 2.0, 0, 255).astype(np.uint8)

In the second case, I still get the message in stdout from tensorflow that it has found cuda etc, but then the module import fails.

My setup is on Windows 10 x64, Anaconda Python 3.5, tensorflow-0.12 and CUDA 8. Has anyone ran across a similar problem? Other modules that I've tested (numpy, pil, scipy) seem to load fine.

If it looks like it cannot be resolved, I'll resort to IPC of some sort between the c++ part and python.

like image 851
Nemanja Bartolovic Avatar asked Feb 12 '17 01:02

Nemanja Bartolovic


1 Answers

I've resolved the issue. I needed to set argc and argv with PySys_SetArgv. I've uncovered this using PyErr_Occurred() and PyErr_Print() right after the failed import to see the problem.

like image 59
Nemanja Bartolovic Avatar answered Oct 20 '22 18:10

Nemanja Bartolovic