我在c ++和python进程之间共享一个ctypes对象。 python进程从该对象获取输入值,通过Tensorflow运行它们,并且我留下了一个numpy数组作为输出。由于这些数组非常大,我想知道是否有更好的方法将数据从tensorflow的输出复制回共享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)
其中data是内存中共享的ctypes对象。 (在this example之后构建)
另一方面,将数据从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, feed_dict={Input: input})
# Now how do I get the data from output into data.pressure?
编辑:作为参考,这是ctypes的外观(python side)
class TransferData(ctypes.Structure):
_fields_ = [
('statusReady', ctypes.c_bool),
# ...
('velocity', ctypes.c_double * (31 * 31 * 31)),
('pressure', ctypes.c_double * (31 * 31 * 31))
]
答案 0 :(得分:1)
这显示了如何将整个数据块从numpy数组复制到ctypes数组:
import numpy as np
import ctypes
# Preparing example
class TransferData(ctypes.Structure):
_fields_ = [
('statusReady', ctypes.c_bool),
('velocity', ctypes.c_double * 4),
('pressure', ctypes.c_double * 4)
]
data = TransferData()
output = np.array([12., 13., 11., 10.])
# Actual code
# Both values should be equal but there could be problems with alignment settings
assert ctypes.sizeof(data.pressure) == output.nbytes
ctypes.memmove(ctypes.byref(data.pressure), output.ctypes.data, output.nbytes)
print(list(data.pressure))