使用numba对numpy加速遇到的坑.pdfVIP

  • 14
  • 0
  • 约3.67千字
  • 约 2页
  • 2023-03-10 发布于湖北
  • 举报
使使⽤⽤numba对对numpy加加速速遇遇到到的的坑坑 问题⼀: numba.errors.UntypedAttributeError: Failed at nopython (nopython frontend) Unknow n attribute fill of type array(float64 2d C) 经过查阅以下⽂档: /numba-doc/latest/reference/numpysupported.html 发现numba并不⽀持 np.fill()。因此将 码改成: background = np.zeros((sourceIm.shape[0] sourceIm.shape[1])) # supported for i j in np.ndindex (background.shape): # np.fill not supported by numba background[i j] = threshold background=background.astype(np.float32) 问题⼆: numba.errors.TypingError: Failed at nopython (nopython frontend) Internal error at numba.typeinfer.CallConstraint object at 0x7f726d5ba8: numba.errors.InternalError: Integer object has no attribute ndim [1] During: resolving callee type: Function(function w here at 0x7f86f67bf8) [2] During: typing of call at test_depth_ im.py (24) 查看错误出现位置,发现在np.w here(),但查询上述⽂档,发现numba是⽀持np.w here()的。于是怀疑是数据类型未指明导致nopython模式 ⽆法启动。 在np.w here()之前添加如下两⾏ 码,以检查numba推断出的类型: @nj it(float32[: : :](float32[: : :] float32) parallel=True fastmath=True) def depthFilter(depthInfo threshold): #print(numba.typeof(depthInfo)) #print(numba.typeof(threshold)) 不料竟然⼜报错: numba.errors.TypingError: Failed at nopython (nopython frontend) Untyped global name numba: cannot determine Numba type of class object numba.errors.UntypedAttributeError: Failed at nopython (nopython frontend) Unknow n attribute typeof of type Module(module numba from /usr/lib/python3/dist-packages/numba/__ init__ .py) 解决⽅法: 将函数上⽅的nj it装饰器注释掉即可 () 数据类型为: array(int32 3d C) float64 于是考虑将数据类型转化⼀致 1. 数组需要⽤np.astype()转换: x=x.astype(np.float32) 2. 标量通过加点: 7300 - 7300. 并在装饰器中指明signature为 float32 否则可能遇到如下错误: Traceback (most recent call last): File test_depth_ im.py line 47 in module depthFilter(float(x) 7300.) TypeError: only size-1 arrays can be converted to Python scalars 或者: numba.errors.TypingError: Failed at nopython (nopyth

文档评论(0)

1亿VIP精品文档

相关文档