PyCUDA无法使用Cuda程序调整图像大小

问题描述

我正在尝试使用PyCuda程序调整图像大小。

 import pycuda.autoinit
 import pycuda.driver as drv
 from pycuda.compiler import SourceModule
 import numpy
 import cv2
 import matplotlib.pyplot as plt
 img=cv2.imread('cat.jpg')
 img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
 w,h,c=img.shape
 img=img.astype(numpy.float32)

 amod=SourceModule("""__global__ void resize_kernel( float *pIn,float *pOut,int widthIn,int heightIn,int widthOut,int heightOut)

{
int i = blockDim.y * blockIdx.y + threadIdx.y;
int j = blockDim.x * blockIdx.x + threadIdx.x;

int channel = 3;

if( i < heightOut && j < widthOut )
{
    int iIn = i * heightIn / heightOut;
    int jIn = j * widthIn / widthOut;
    for(int c = 0; c < channel; c++)
        pOut[(i*widthOut + j)*channel + c] = pIn[ (iIn*widthIn + jIn)*channel + c ];
    }
  }

  """)

  x=numpy.zeros((416,416,3),dtype=float)

  fn_resize=amod.get_function("resize_kernel")

  fn_resize(drv.In(img),drv.Out(x),drv.In(w),drv.In(h),drv.In(416),block=(16,16,1),grid=(1,1)) #getting error here

我遇到以下错误

AttributeError                            Traceback (most recent call last)
D:\Anaconda\envs\mytf1\lib\site-packages\pycuda\driver.py in get_device_alloc(self)
    127             try:
--> 128                 self.dev_alloc = mem_alloc_like(self.array)
    129             except AttributeError:

D:\Anaconda\envs\mytf1\lib\site-packages\pycuda\driver.py in mem_alloc_like(ary)
    719 def mem_alloc_like(ary):
--> 720     return mem_alloc(ary.nbytes)
    721 

AttributeError: 'int' object has no attribute 'nbytes'

During handling of the above exception,another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-37-0a4976a068be> in <module>()
      1 
----> 2 fn_resize(drv.In(img),1))

D:\Anaconda\envs\mytf1\lib\site-packages\pycuda\driver.py in function_call(func,*args,**kwargs)
    435 
    436         func._set_block_shape(*block)
--> 437         handlers,arg_buf = _build_arg_buf(args)
    438 
    439         for handler in handlers:

D:\Anaconda\envs\mytf1\lib\site-packages\pycuda\driver.py in _build_arg_buf(args)
    194             elif isinstance(arg,ArgumentHandler):
    195                 handlers.append(arg)
--> 196                 arg_data.append(int(arg.get_device_alloc()))
    197                 format += "P"
    198             elif isinstance(arg,np.ndarray):

D:\Anaconda\envs\mytf1\lib\site-packages\pycuda\driver.py in get_device_alloc(self)
    128                 self.dev_alloc = mem_alloc_like(self.array)
    129             except AttributeError:
--> 130                 raise TypeError("could not determine array length of '%s': unsupported array type or not an array" % type(self.array))
    131         return self.dev_alloc
    132 

TypeError: could not determine array length of '<class 'int'>': unsupported array type or not an array

我也尝试过以(-1,3)的形式发送图像。我遇到了同样的错误。 在这里,我的img是numpy.ndarray类型的图像。为什么在传递数组时将其视为int? 请帮助我解决错误。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...