我有一个在 c++ 和 python 进程之间共享的 ctypes 对象。python 进程从该对象获取输入值,通过 Tensorflow 运行它们,然后留下一个 numpy 数组作为输出。由于这些数组非常大,我想知道是否有更好的方法将数据从张量流的输出复制回共享 ctypes 对象,以便 c++ 进程可以对它们进行操作。(速度是问题,是的。)
现在我正在一一复制每个值:
output = np.array([12, 13, 11, 10]) # in reality this is fairly large (the Tensorflow result)
for i, value in enumerate(output):
data.pressure[i] = ctypes.c_double(value)
Run Code Online (Sandbox Code Playgroud)
其中 data 是内存中共享的 ctypes 对象。(在此示例之后构建)
另一方面,将数据从 ctypes 对象复制到 numpy 很容易,我想知道是否有相反的东西(从 numpy 到 ctypes 数组)这是简单的代码:
# Creating a numpy array from the ctypes array
input = np.reshape(data.velocity, (1, 29791))
# Tensorflow returns a numpy array
output = sess.run(final, …Run Code Online (Sandbox Code Playgroud)