python - 无法在与 libtensorflow 链接的 C 程序中嵌入导入 tensorflow 的 Python 脚本
问题描述
我正在创建一个链接到 tensorflow C api 的 C 程序。在这个 C 程序中,调用了一个 Python 脚本,在该脚本中它使用 TensorFlow 来训练 ANN 模型。ANN 模型保存在本地目录中,C 程序结束时将加载 ANN 并使用它来计算一些东西。
这是我的 C 程序的片段:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "ontheflysurrogate.h"
#include "tensorflow/c/c_api.h"
typedef struct /*Struct to store neccesary ANN session properties*/
{
TF_Session* Session;
TF_Graph* Graph;
TF_Status* Status;
TF_SessionOptions* SessionOpts;
TF_Buffer* RunOpts;
}Session_Props;
char* trainingANN(char*, char*, int);
void *load_session(char*);
int main()
{
char* filepath_training = "/home/abc/def/training_data.csv";
char* path_ANN_eta_PB = trainingANN(filepathtraining_eta_PB,prefixres,count);
Session_props* sess = load_session(path_ANN_eta_PB);
return 1;
}
char* trainingANN(char* filepathtrainingdata, char* prefixres, int count);
{
/*Call a python script within which it imports tensorflow here!*/
return saved_model_path;
}
void *load_session(char* saved_model_dir)
{
//***************Adjust the logging of tensorflow by setting a variable
//***************in the environment called TF_CPP_MIN_LOG_LEVEL
char *var = "TF_CPP_MIN_LOG_LEVEL=0";
int ret;
ret = putenv(var);
//***************Allocate dynamic memory from the heap for Session_Props data struct
Session_Props *sess = malloc(sizeof(Session_Props));
//***************Instantiate Session Properties
TF_Graph* Graph = TF_NewGraph(); TF_Status* Status = TF_NewStatus();
TF_SessionOptions* SessionOpts = TF_NewSessionOptions(); TF_Buffer* RunOpts = NULL;
const char* tags = "serve"; int ntags = 1;
TF_Session* Session = TF_LoadSessionFromSavedModel
(
SessionOpts,
RunOpts,
saved_model_dir,
&tags,
ntags,
Graph,
NULL,
Status
);
//***************************Populate the struct
sess->Graph = Graph; sess->Session = Session; sess->Status = Status;
sess->SessionOpts = SessionOpts; sess->RunOpts = RunOpts;
return sess;
}
但是,我不断收到这个致命错误说:
Traceback (most recent call last):
File "/home/philgun/Documents/codecodecode/codecodecode/GSL_Project/ANN.py", line 2, in <module>
import tensorflow as tf
File "/home/philgun/.local/lib/python2.7/site-packages/tensorflow/__init__.py", line 101, in <module>
from tensorflow_core import *
File "/home/philgun/.local/lib/python2.7/site-packages/tensorflow_core/__init__.py", line 40, in <module>
from tensorflow.python.tools import module_util as _module_util
ImportError: No module named tools
Failed to load "ANN"
Traceback (most recent call last):
File "/home/philgun/Documents/codecodecode/codecodecode/GSL_Project/ANN.py", line 2, in <module>
import tensorflow as tf
File "/home/philgun/.local/lib/python2.7/site-packages/tensorflow/__init__.py", line 101, in <module>
from tensorflow_core import *
File "/home/philgun/.local/lib/python2.7/site-packages/tensorflow_core/__init__.py", line 42, in <module>
from . _api.v2 import audio
File "/home/philgun/.local/lib/python2.7/site-packages/tensorflow_core/_api/v2/audio/__init__.py", line 10, in <module>
from tensorflow.python.ops.gen_audio_ops import decode_wav
File "/home/philgun/.local/lib/python2.7/site-packages/tensorflow_core/python/ops/gen_audio_ops.py", line 9, in <module>
from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
File "/home/philgun/.local/lib/python2.7/site-packages/tensorflow_core/python/pywrap_tensorflow.py", line 74, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "/home/philgun/.local/lib/python2.7/site-packages/tensorflow_core/python/pywrap_tensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "/home/philgun/.local/lib/python2.7/site-packages/tensorflow_core/python/pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "/home/philgun/.local/lib/python2.7/site-packages/tensorflow_core/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
ImportError: /home/philgun/.local/lib/python2.7/site-packages/tensorflow_core/python/_pywrap_tensorflow_internal.so: undefined symbol: _ZNK6google8protobuf7Message11GetTypeNameEv
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/errors
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
我试图研究https://github.com/tensorflow/tensorflow/issues/7541和https://github.com/tensorflow/tensorflow/issues/20561。我尝试使用 bazel 从源代码构建 TensorFlow pip 包,就像在 TF Github 中所写的一样。我还尝试关注https://github.com/tensorflow/tensorflow/issues/20561讨论并尝试实施每一项建议,但我无法使其发挥作用。
这是我的 tensorflow 链接标志,从 python 2.7 调用
>>> tensorflow.sysconfig.get_link_flags()
['-L/home/philgun/.local/lib/python2.7/site-packages/tensorflow_core', '-l:libtensorflow_framework.so.2']
我不明白的一件事是我的 Python 2 似乎缺少这个文件
_pywrap_tensorflow_internal.so
虽然它存在于我的 python 3 中,但我使用 pip 安装了 TensorFlow:
pip install tensorflow
并且还从 Python 2 的源代码构建。
目前,我对如何解决这个问题没有任何想法,任何想法都将不胜感激!
干杯,PG
解决方案
推荐阅读
- python-3.x - 如何在保存神经网络的同时解决路径问题
- c# - 从 Office Web 加载项访问 C# 库
- python - 给定各种条件,如何在熊猫数据框中更改日期的年份?
- c - 为什么 C 宏调用会占用大括号?
- python - Django 2.0 查询集显示“对象(1)”不显示值
- oracle-apex - 是否有可能使用我们从 Oracle Apex 中的程序 Refcursor 获得的数据
- performance - 如何以非线性方式在 C++ 中保存矩阵
- html - 它如何设置网格模板列:[outer-start] 1fr [center-start] 1fr [center-end] 1fr [outer-end]?
- sql - SQL Server 中字符串的单引号
- php - 从父类方法返回子方法的可能性?