【发布时间】:2017-10-18 20:15:39
【问题描述】:
我正在尝试遍历 PyCUDA 中的二维数组,但最终得到重复的数组值。我最初抛出一个小的随机整数数组,它按预期工作,但是当我向它抛出图像时,我一遍又一遍地看到相同的值。
这是我的代码
img = np.random.randint(20, size = (4,5))
print "Input array"
print img
img_size=img.shape
print img_size
#nbtes determines the number of bytes for the numpy array a
img_gpu = cuda.mem_alloc(img.nbytes)
#Copies the memory from CPU to GPU
cuda.memcpy_htod(img_gpu, img)
mod = SourceModule("""
#include <stdio.h>
__global__ void AHE(int *a, int row, int col)
{
int i = threadIdx.x+ blockIdx.x* blockDim.x;
int j = threadIdx.y+ blockIdx.y* blockDim.y;
if(i==0 && j ==0)
printf("Output array ");
if(i <row && j < col)
{
printf(" %d",a[j + i*col]);
}
}
""")
col = np.int32(img.shape[-1])
row = np.int32(img.shape[0])
func = mod.get_function("AHE")
func(img_gpu, row, col, block=(32,32,1))
img_ahe = np.empty_like(img)
cuda.memcpy_dtoh(img_ahe, img_gpu)
现在,当我用转换为 numpy 数组的图像替换随机整数数组时,我最终得到了这个
img = cv2.imread('Chest.jpg',0)
img_size=img.shape
print img_size
#nbtes determines the number of bytes for the numpy array a
img_gpu = cuda.mem_alloc(img.nbytes)
#Copies the memory from CPU to GPU
cuda.memcpy_htod(img_gpu, img)
mod = SourceModule("""
#include <stdio.h>
__global__ void AHE(int *a, int row, int col)
{
int i = threadIdx.x+ blockIdx.x* blockDim.x;
int j = threadIdx.y+ blockIdx.y* blockDim.y;
if(i==0 && j ==0)
printf("Output array ");
if(i <row && j < col)
{
printf(" %d",a[j + i*col]);
}
}
""")
#Gives you the number of columns
col = np.int32(img.shape[-1])
row = np.int32(img.shape[0])
func = mod.get_function("AHE")
func(img_gpu, row, col, block=(32,32,1))
img_ahe = np.empty_like(img)
cuda.memcpy_dtoh(img_ahe, img_gpu)
【问题讨论】:
-
@talonmies 我尝试过使用浮点数和整数,但我最终还是得到了相同的结果
-
那就发minimal reproducible example吧,不然没办法说是怎么回事
-
@talonmies 我添加了一个示例输入,使我的问题更加清晰易懂
-
代码对我来说看起来工作正常。与您关于一次又一次重复相同值的陈述相反,我在您的输出中看不到这一点,实际上您的输出在输出矩阵打印输出中显示了输入矩阵中出现的每个元素,按列分组。如果有什么我想说的,现在less 清楚了,因为您的输出表明代码工作正常。对于寻求调试帮助的问题,您应该提供minimal reproducible example。 SO期望使用必须 here这个词。
-
好的,现在很明显了。请阅读一些 OpenCV documentation。我的第一条评论是 100% 正确的。您的源图像数据未以每像素 32 位有符号整数存储,并且您的内核正试图以错误的格式读取数据。