Numba 在 np.astype 上使用 BoundFunction 无效

Kri*_*ika 5 jit numpy python-3.x numba

我正在尝试编译一个函数,该函数使用 numba 对图像补丁进行一些计算。这是代码的一部分:

@jit(nopython=True, parallel=True)
def value_at_patch(img, coords, imgsize, patch_radius):
    x_center = coords[0]; y_center = coords[1];
    r = patch_radius
    s = 2*r+1
    xvec = np.arange(x_center-r, x_center+r+1)
    xvec[xvec <= 0] = 0 #prevent negative index
    xvec = xvec.astype(int)
    yvec = np.arange(y_center-r, y_center+r+1)
    yvec[yvec <= 0] = 0
    yvec = yvec.astype(int)
    A = np.zeros((s,s))

    #do some parallel computation on A

    p = np.any(A)
    return p
Run Code Online (Sandbox Code Playgroud)

我能够编译该函数,但是当我运行它时,我收到以下错误消息:

Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of BoundFunction(array.astype for array(float64, 1d, C)) with parameters (Function(<class 'int'>))
 * parameterized
[1] During: resolving callee type: BoundFunction(array.astype for array(float64, 1d, C))
[2] During: typing of call at <ipython-input-17-90e27ac302a8> (42)


File "<ipython-input-17-90e27ac302a8>", line 42:
def value_at_patch(img, coords, imgsize, patch_radius):
    <source elided>
    xvec[xvec <= 0] = 0 #prevent negative index
    xvec = xvec.astype(int)
    ^
Run Code Online (Sandbox Code Playgroud)

我检查了 numba 文档,np.astype 应该只支持一个参数。您知道是什么原因导致了这个问题吗?

kri*_*hna 7

在以下场所np.int64代替使用:int

xvec = xvec.astype(np.int64)

yvec = yvec.astype(np.int64)
Run Code Online (Sandbox Code Playgroud)