gpt4 book ai didi

python - 数组上的 Cython 最小值和最大值

转载 作者:行者123 更新时间:2023-12-05 06:17:11 25 4
gpt4 key购买 nike

我想通过将一些函数转换为 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):

为什么函数调用已经这么复杂了?数据类型匹配,一切都是无符号整数..

并且已经返回了 bool 值;我扩展了编译器的内容:

+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 可能比 Python minmax 函数更快(可能取决于数组的大小)。 Numpy 函数将使用 C 缓冲区协议(protocol)并对 C 数字类型进行操作,而 Python 函数将使用 Python 迭代器协议(protocol)并将数字视为 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(...) 以返回“ bool 整数”。

关于python - 数组上的 Cython 最小值和最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61751704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com