在C ++中嵌入python时导入Tensorflow返回null [英] Importing tensorflow when embedding python in c++ returns null

查看:87
本文介绍了在C ++中嵌入python时导入Tensorflow返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对将python嵌入C ++应用程序有疑问。设置如下:我有一个大型的C ++应用程序,该应用程序生成一些数据(实时渲染图像)并显示它们。我还使用tensorflow在python中训练了一个神经网络,它将接受这些图像。

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.

我的想法是嵌入python并以numpy数组的形式发送数据,使用神经网络进行预测,并获取另一个要处理的numpy数组进行显示(在C ++中)。我进行了一些基本的测试,但没有在python上使用tensorflow来获得将python嵌入c等语言的感觉,并且似乎可以正常工作。

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.

但是,一旦我将 import tensorflow导入到我要导入的任何python脚本中,我将从c ++部分的PyImport_ImportModule中获取NULL。

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.

例如

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)

在第二种情况下,我仍然从tensorflow的stdout中得到消息,它已找到cuda等,但是随后模块导入失败。

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.

我的设置是在Windows 10 x64,Anaconda Python 3.5,tensorflow-0.12和CUDA 8上进行的。有人遇到过类似的问题吗?我测试过的其他模块(numpy,pil,scipy)似乎可以正常加载。

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.

如果看起来无法解决,我将采用IPC

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

推荐答案

我已经解决了这个问题。我需要使用PySys_SetArgv设置argc和argv。我在导入失败后立即使用PyErr_Occurred()和PyErr_Print()发现了这一点。

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.

这篇关于在C ++中嵌入python时导入Tensorflow返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆