我正在做一个涉及通过深度学习进行对象检测的项目,底层检测代码是用C编写的。由于项目的要求,这段代码有一个Python包装器,它与所需的C函数通过接口(interface)ctypes。图像从 Python 中读取,然后传输到 C 中以作为批处理进行处理。
在目前的状态下,代码没有经过优化:使用 cv2.imread 读取图像(每张 640x360x3),然后将其堆叠到一个 numpy 数组中。例如,对于 16 的批量大小,此数组的维度为 (16,360,640,3)。完成此操作后,指向此数组的指针将通过 ctypes 传递到 C 中,在 C 中解析数组,对像素值进行归一化并重新排列为二维数组。二维数组的维度为16x691200(16x(640*360*3)),排列如下。
row [0]: Image 0: (B)r0(B)r1(B)r2.... (G)r0(G)r1(G)r2.... (R)r0(R)r1(R)r2....
row [1]: Image 1: (B)r0(B)r1(B)r2.... (G)r0(G)r1(G)r2.... (R)r0(R)r1(R)r2....
.
.
row [15]: Image 15: (B)r0(B)r1(B)r2.... (G)r0(G)r1(G)r2.... (R)r0(R)r1(R)r2....
`
执行此操作的 C 代码目前看起来像这样,其中像素值通过步幅访问并按图像顺序排列。 nb 是批处理中的图像总数(通常为 16); h、w、c分别为360,640和3。
matrix ndarray_to_matrix(unsigned char* src, long* shape, long* strides)
{
int nb = shape[0];
int h = shape[1];
int w = shape[2];
int c = shape[3];
matrix X = make_matrix(nb, h*w*c);
int step_b = strides[0];
int step_h = strides[1];
int step_w = strides[2];
int step_c = strides[3];
int b, i, j, k;
int index1, index2 = 0;
for(b = 0; b < nb ; ++b) {
for(i = 0; i < h; ++i) {
for(k= 0; k < c; ++k) {
for(j = 0; j < w; ++j) {
index1 = k*w*h + i*w + j;
index2 = step_b*b + step_h*i + step_w*j + step_c*k;
X.vals[b][index1] = src[index2]/255.;
}
}
}
}
return X;
}
以及调用这个函数对应的Python代码:(array是原始的numpy数组)
for i in range(start, end):
imgName = imgDir + '/' + allImageName[i]
img = cv2.imread(imgName, 1)
batchImageData[i-start,:,:] = img[:,:]
data = batchImageData.ctypes.data_as(POINTER(c_ubyte))
resmatrix = self.ndarray_to_matrix(data, batchImageData.ctypes.shape, batchImageData.ctypes.strides)
截至目前,此 ctypes 实现对于一批 16 张图像大约需要 35 毫秒。我正在研究 FPS 非常关键的图像处理管道,那么是否有更有效的方法来执行这些操作?具体来说:
- 我能否直接从磁盘中将图像作为 Python 中的“跨步”一维数组读取,从而避免迭代访问和复制?
- 我研究了 numpy 操作,例如:
np.ascontiguousarray(img.transpose(2,0,1).flat, dtype=float)/255.应该实现类似的东西,但这实际上可能需要更多时间,因为它被称为在 Python 中。 - Cython 会在读取操作期间的任何地方提供帮助吗?
请您参考如下方法:
关于 ascontiguousarray 方法,我假设它非常慢,因为 python 必须执行一些内存工作才能返回类似 C 的连续数组。
编辑 1: 我看到了this answer ,显然 openCV 的 imread 函数应该已经返回一个连续的数组。
我对ctypes不是很熟悉,但碰巧用到了PyBind library并且只能推荐使用它。它实现了 Python 的 buffer protocol因此,您可以几乎没有开销地与 Python 数据进行交互。
我已经回答了 question解释如何将 numpy 数组从 Python 传递到 C/C++,如何在 C++ 中对它做一些虚拟操作并将动态创建的数组返回给 Python。
编辑 2:我添加了一个接收 Numpy 数组的简单示例,将其发送到 C 并从 C 打印它。您可以找到它 here .希望对您有所帮助!
编辑 3: 回答你最后的评论,是的,你绝对可以做到。 您可以修改代码以 (1) 在 C++ 中实例化一个 2D numpy 数组,(2) 将其对数据的引用传递给将修改它而不是声明矩阵的 C 函数,以及 (3) 通过引用将该实例返回给 Python .
您的函数将变为:
void ndarray_to_matrix(unsigned char* src, double * x, long* shape, long* strides)
{
int nb = shape[0];
int h = shape[1];
int w = shape[2];
int c = shape[3];
int step_b = strides[0];
int step_h = strides[1];
int step_w = strides[2];
int step_c = strides[3];
int b, i, j, k;
int index1, index2 = 0;
for(b = 0; b < nb ; ++b) {
for(i = 0; i < h; ++i) {
for(k= 0; k < c; ++k) {
for(j = 0; j < w; ++j) {
index1 = k*w*h + i*w + j;
index2 = step_b*b + step_h*i + step_w*j + step_c*k;
X.vals[b][index1] = src[index2]/255.;
}
}
}
}
}
然后你会在你的 C++ 包装器代码中添加
// Instantiate the output array, assuming we know b, h, c,w
py::array_t<double> x = py::array_t<double>(b*h*c*w);
py::buffer_info bufx = x.request();
double*ptrx = (double *) bufx.ptr;
// Call to your C function with ptrx as input
ndarray_to_matrix(src, ptrx, shape, strides);
// now reshape x
x.reshape({b, h*c*w});
不要忘记修改 C++ 包装函数的原型(prototype)以返回一个 numpy 数组,例如:
py::array_t<double> read_matrix(...){}...
这应该可以,但我没有测试它:)
