阵列上的最小值和最大值



我想通过将一些函数转换为cython来加速一个非常简单的Python代码。 但是,在循环体中,我需要找到数组的最小值和最大值,这似乎是关键点。根据.html文件,这些行需要翻译成非常多的c代码。为什么?

这就是整个代码,下面我列出了让我头疼的行:

import numpy as np
cimport numpy as np
cimport cython
from cython cimport boundscheck, wraparound

@boundscheck(False)
@wraparound(False)
cdef box_overlaps_contour(unsigned int[:] boxTopLeftXY, unsigned int boxSize, unsigned int[:, :, :] contourData):
cdef bint isOverlapping = False
cdef unsigned int xmin, xmax, width, boxXmin, boxXmax, ymin, ymax, height, boxYmin, boxYmax
xmin = min(contourData[:, 0, 1])
xmax = max(contourData[:, 0, 1])
width = xmax - xmin
boxXmin = boxTopLeftXY[0]
boxXmax = boxTopLeftXY[0] + boxSize
if xmin > (boxXmin-width/2):
if xmax < (boxXmax+width/2):
ymin = min(contourData[:, 0, 1])
ymax = max(contourData[:, 0, 1])
height = ymax - ymin
boxYmin = boxTopLeftXY[1]
boxYmax = boxTopLeftXY[1] + boxSize
if ymin > (boxYmin-height/2):
if ymax < (boxYmax+width/2):
isOverlapping = True
return isOverlapping

@boundscheck(False)
@wraparound(False)
def def_get_indices_of_overlapping_particles(contours not None, unsigned int[:, :] topLefts, unsigned int boxSize):
cdef Py_ssize_t i, j
cdef unsigned int counter, numParticles, numTopLefts
numParticles = len(contours)
numTopLefts = topLefts.shape[0]
cdef unsigned int[:] overlappingIndices = np.zeros(numParticles, dtype=np.uint32)
cdef unsigned int[:, :, :] currentContour
counter = 0
for i in range(numParticles):
currentContour = contours[i]
for j in range(numTopLefts):
if box_overlaps_contour(topLefts[j, :], boxSize, currentContour):
overlappingIndices[counter] = i
counter += 1
break
return overlappingIndices[:counter]

该函数采用轮廓列表(np.ndarray,从 cv2 检索(和一个数组,表示一定数量的 xy 坐标,其中矩形以指示的框大小放置。该函数应该遍历轮廓并返回与其中一个框重叠的轮廓索引。 这里的这些行似乎使整个过程变得非常慢(实际上,它比纯粹的Python版本慢..(:

+13:     xmin = min(contourData[:, 0, 1])
+14:     xmax = max(contourData[:, 0, 1])

并且,同样:

+21:             ymin = min(contourData[:, 0, 1])
+22:             ymax = max(contourData[:, 0, 1])

其他有问题的行(但少一点(,我不明白为什么:

+48:             if box_overlaps_contour(topLefts[j, :], boxSize, currentContour):

为什么函数调用已经如此复杂?数据类型匹配,所有内容都是无符号整数。

并且已经返回布尔值;我扩展了编译器从中得出的内容:

+31:     return isOverlapping
__Pyx_XDECREF(__pyx_r);
__pyx_t_2 = __Pyx_PyBool_FromLong(__pyx_v_isOverlapping); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 31, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_2);
__pyx_r = __pyx_t_2;
__pyx_t_2 = 0;
goto __pyx_L0;

任何帮助将不胜感激!我仍然不太了解cython是如何工作的,因为它似乎是:/如果需要,我可以提供更多信息!

非常感谢!! :)

编辑:这是Cython从np.min((行中得出的内容...:有什么想法吗?

+21:             ymin = np.min(contourData[:, 0, 1])
__Pyx_GetModuleGlobalName(__pyx_t_2, __pyx_n_s_np); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 21, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_2);
__pyx_t_3 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_min); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 21, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_3);
__Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
__pyx_t_4.data = __pyx_v_contourData.data;
__pyx_t_4.memview = __pyx_v_contourData.memview;
__PYX_INC_MEMVIEW(&__pyx_t_4, 0);
__pyx_t_4.shape[0] = __pyx_v_contourData.shape[0];
__pyx_t_4.strides[0] = __pyx_v_contourData.strides[0];
__pyx_t_4.suboffsets[0] = -1;
{
Py_ssize_t __pyx_tmp_idx = 0;
Py_ssize_t __pyx_tmp_stride = __pyx_v_contourData.strides[1];
if ((0)) __PYX_ERR(0, 21, __pyx_L1_error)
__pyx_t_4.data += __pyx_tmp_idx * __pyx_tmp_stride;
}
{
Py_ssize_t __pyx_tmp_idx = 1;
Py_ssize_t __pyx_tmp_stride = __pyx_v_contourData.strides[2];
if ((0)) __PYX_ERR(0, 21, __pyx_L1_error)
__pyx_t_4.data += __pyx_tmp_idx * __pyx_tmp_stride;
}
__pyx_t_2 = __pyx_memoryview_fromslice(__pyx_t_4, 1, (PyObject *(*)(char *)) __pyx_memview_get_unsigned_int, (int (*)(char *, PyObject *)) __pyx_memview_set_unsigned_int, 0);; if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 21, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_2);
__PYX_XDEC_MEMVIEW(&__pyx_t_4, 1);
__pyx_t_4.memview = NULL;
__pyx_t_4.data = NULL;
__pyx_t_5 = NULL;
if (CYTHON_UNPACK_METHODS && unlikely(PyMethod_Check(__pyx_t_3))) {
__pyx_t_5 = PyMethod_GET_SELF(__pyx_t_3);
if (likely(__pyx_t_5)) {
PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_3);
__Pyx_INCREF(__pyx_t_5);
__Pyx_INCREF(function);
__Pyx_DECREF_SET(__pyx_t_3, function);
}
}
__pyx_t_1 = (__pyx_t_5) ? __Pyx_PyObject_Call2Args(__pyx_t_3, __pyx_t_5, __pyx_t_2) : __Pyx_PyObject_CallOneArg(__pyx_t_3, __pyx_t_2);
__Pyx_XDECREF(__pyx_t_5); __pyx_t_5 = 0;
__Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 21, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
__pyx_t_6 = __Pyx_PyInt_As_unsigned_int(__pyx_t_1); if (unlikely((__pyx_t_6 == (unsigned int)-1) && PyErr_Occurred())) __PYX_ERR(0, 21, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
__pyx_v_ymin = __pyx_t_6;

使用np.minnp.max可能比 Pythonminmax函数更快(可能取决于数组的大小(。Numpy函数将使用C缓冲区协议并对C数字类型进行操作,而Python函数将使用Python迭代器协议,数字将被视为Python对象。尽管如此,它们在Cython中看起来就像黄色一样。

编辑:如果这没有帮助,你可能想编写自己的cdef函数来做minmax(以避免Python调用(。类似的东西(未经测试的代码如下...

# return type is a C struct of 2 values - this should be quick...
cdef (double, double) minmax(double arr[:]):
cdef double min = np.inf
cdef double max = -np.inf
cdef int i
for i in range(arr.shape[0]):
if arr[i] < min:
min = arr[i]
if arr[i] > max
max = arr[i]
return min, max

这样做的好处是可以在一个循环中同时执行这两项操作,并且不需要 Python 函数调用。它显然有一个缺点,你需要自己写。

你看到的很多生成的 C 代码都与 memoryview 切片有关,实际上并不太慢(尽管它占用了大量空间(。


cdef box_overlaps_contour(unsigned int[:] boxTopLeftXY, unsigned int boxSize, unsigned int[:, :, :] contourData):

未指定返回类型,因此它作为 Python 对象返回。您可以cdef bint box_overlaps_contour(...)返回"布尔整数"。

最新更新