PyCUDA unable to resize images with a cuda program

171 Views Asked by At

I am trying to resize images using a PyCuda program.

 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),drv.In(416),block=(16,16,1), grid=(1,1)) #getting error here

I am getting the following error

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),drv.Out(x),drv.In(w),drv.In(h),drv.In(416),drv.In(416),block=(16,16,1), grid=(1,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

I also tried sending the image in the shape of (-1,3). I got the same error. Here my img is the image of type numpy.ndarray. Why is it treating the array as an int when it is passed? Please help me with resolving the error.

0

There are 0 best solutions below