微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。