【发布时间】:2017-11-14 11:52:22
【问题描述】:
我试图在我的 GPU 上为图像计算本地二进制模式,同样使用 python 中的 cuda 模块。但是在 CPU 和 GPU 上执行类似算法所产生的结果却产生了不同的结果。你能帮我找出问题吗?
下面是我尝试执行的代码的 sn-p:
from __future__ import division
from skimage.io import imread, imshow
from numba import cuda
import time
import math
import numpy
# CUDA Kernel
@cuda.jit
def pointKernelLBP(imgGPU, histVec, pos) :
''' Computes Point Local Binary Pattern '''
row, col = cuda.grid(2)
if row+1 < imgGPU.shape[0] and col+1 < imgGPU.shape[1] and col-1>=0 and row-1>=0 :
curPos = 0
mask = 0
for i in xrange(-1, 2) :
for j in xrange(-1, 2) :
if i==0 and j==0 :
continue
if imgGPU[row+i][col+j] > imgGPU[row][col] :
mask |= (1<<curPos)
curPos+=1
histVec[mask]+=1
#Host Code for computing LBP
def pointLBP(x, y, img) :
''' Computes Local Binary Pattern around a point (x,y),
considering 8 nearest neighbours '''
pos = [0, 1, 2, 7, 3, 6, 5, 4]
curPos = 0
mask = 0
for i in xrange(-1, 2) :
for j in xrange(-1, 2) :
if i==0 and j==0 :
continue
if img[x+i][y+j] > img[x][y] :
mask |= (1<<curPos)
curPos+=1
return mask
def LBPHistogram(img, n, m) :
''' Computes LBP Histogram for given image '''
HistVec = [0] * 256
for i in xrange(1, n-1) :
for j in xrange(1, m-1) :
HistVec[ pointLBP(i, j, img) ]+=1
return HistVec
if __name__ == '__main__' :
# Reading Image
img = imread('cat.jpg', as_grey=True)
n, m = img.shape
start = time.time()
imgHist = LBPHistogram(img, n, m)
print "Computation time incurred on CPU : %s seconds.\n" % (time.time() - start)
print "LBP Hisogram Vector Using CPU :\n"
print imgHist
print type(img)
pos = numpy.ndarray( [0, 1, 2, 7, 3, 6, 5, 4] )
img_global_mem = cuda.to_device(img)
imgHist_global_mem = cuda.to_device(numpy.full(256, 0, numpy.uint8))
pos_global_mem = cuda.to_device(pos)
threadsperblock = (32, 32)
blockspergrid_x = int(math.ceil(img.shape[0] / threadsperblock[0]))
blockspergrid_y = int(math.ceil(img.shape[1] / threadsperblock[1]))
blockspergrid = (blockspergrid_x, blockspergrid_y)
start = time.time()
pointKernelLBP[blockspergrid, threadsperblock](img_global_mem, imgHist_global_mem, pos_global_mem)
print "Computation time incurred on GPU : %s seconds.\n" % (time.time() - start)
imgHist = imgHist_global_mem.copy_to_host()
print "LBP Histogram as computed on GPU's : \n"
print imgHist, len(imgHist)
【问题讨论】:
-
请正确格式化您的代码
-
我在您的代码中看到至少三个问题,最明显的是内核和主机代码之间的实际直方图代码不同。你怎么能期望它们产生相同的输出?
-
抱歉,我找不到任何区别?你能提一个吗?
-
如果您看不到内核中的内部直方图代码与 pointLBP 中其他相同的代码之间的区别,那么您要么没有尝试,要么无能为力。 Stack Overflow不是小错误发现服务,请不要把它当成一个。
-
很抱歉。现在,我已经包含了所需的编辑,但即使是现在结果也不一致。你现在能帮忙吗?
标签: python-2.7 cuda numba pycuda numba-pro