导入 Numba @jit 遇到警告消息,@jit(nopython=True) 将显示错误

improt Numba @jit meet warning message and @jit(nopython=True) will show error

我是 python 新人。

我写了一些代码尝试将图片混合成新图片。

我完成了,但是太浪费时间了。

所以我尝试使用 Numba 让代码在我的 gpu 上运行。 但遇到一些警告和错误

os Ubuntu 16

pyhton version 3.5

for i in piclist:
img = cv2.imread(str(filepath)+'/'+str(i))
print (i)
b,g,r = cv2.split(img)
bcolorlist.append(b)
gcolorlist.append(g)
rcolorlist.append(r)


@jit(nopython = True)
def select(bcolorlist,gcolorlist,rcolorlist):
    for i in range(len(bcolorlist)):
        b = bcolorlist[i].reshape(2073600,1)
        g = gcolorlist[i].reshape(2073600,1)
        r = rcolorlist[i].reshape(2073600,1)
        bcolorlist[i] = b
        gcolorlist[i] = g
        rcolorlist[i] = r
    bbb = np.concatenate(bcolorlist, axis=1)
    ggg = np.concatenate(gcolorlist, axis=1)
    rrr = np.concatenate(rcolorlist, axis=1)
    newb = []
    newg = []
    newr = []
    for line in bbb:
        newb.append(np.argmax(np.bincount(line)))
    newb = np.array(newb)
    for line in ggg:
        newg.append(np.argmax(np.bincount(line)))
    newg = np.array(newg)
    for line in rrr:
        newr.append(np.argmax(np.bincount(line)))
    newr = np.array(newr)
    return newb,newg,newr

没有@jit

也能正常工作

但是 @jit 会显示下面的消息

test.py:61: NumbaWarning: Compilation is falling back to object mode WITH looplifting enabled because Function "select" failed type inference due to: Invalid use of Function() with argument(s) of type(s): (reflected list(array(uint8, 2d, C)), axis=Literalint)

根据建议立即更新代码

for f in files:
    if is_img((os.path.splitext(f)[1])):
         piclist.append(f)
start = time.time()
for i in piclist:
    img = cv2.imread(str(filepath)+'/'+str(i))
    b,g,r = cv2.split(img)
    bcolorlist.append(b)
    gcolorlist.append(g)
    rcolorlist.append(r)
bcolorlist = array(bcolorlist)
gcolorlist = array(gcolorlist)
rcolorlist = array(rcolorlist)

@jit(nopython = True)

def select(bcolorlist,gcolorlist,rcolorlist):
    r_res = np.zeros((len(bcolorlist), 2073600, 1), dtype=np.uint8)
    g_res = np.zeros((len(gcolorlist), 2073600, 1), dtype=np.uint8)
    b_res = np.zeros((len(rcolorlist), 2073600, 1), dtype=np.uint8)
    for i in range(len(bcolorlist)):
        r_res[i] = rcolorlist[i].reshape(2073600,1)
        g_res[i] = gcolorlist[i].reshape(2073600,1)
        b_res[i] = bcolorlist[i].reshape(2073600,1)
    r_res = r_res.reshape(2073600, len(rcolorlist))
    g_res = g_res.reshape(2073600, len(gcolorlist))
    b_res = b_res.reshape(2073600, len(bcolorlist))

    newr = np.zeros((2073600), dtype=np.uint8)
    newg = np.zeros((2073600), dtype=np.uint8)
    newb = np.zeros((2073600), dtype=np.uint8)

    for i in range(b_res[:,0].size):
        newr[i] = np.argmax(np.bincount(r_res[i,:]))
        newg[i] = np.argmax(np.bincount(g_res[i,:]))
        newb[i] = np.argmax(np.bincount(b_res[i,:]))

    return newb, newg, newr

b, g, r = select(bcolorlist,gcolorlist,rcolorlist)
newb = b.reshape(1920,1080)
newg = g.reshape(1920,1080)
newr = r.reshape(1920,1080)
img = cv2.merge([newb, newg, newr])
end = time.time()
print (end - start)

正常工作!!!!

但它不会输出图像.....

麻烦您给点建议好吗?

更新 - 使用 rgb 图像作为输入的工作示例。我以前在 Python 没有做过图像处理,所以这是一次不错的旅行。下面的一些代码。请注意,我向函数添加了一些输入参数以使其更加通用。我还更改了一些变量名,但这只是个人喜好问题,所以不用担心。

import cv2
import numpy as np
from numba import njit

@njit
def select(b, g, r, n, w, h): # added number of input images, width and height
    r_res = r.reshape(n, w*h) # flatten the images' rgb 2d arrays to 1d
    g_res = g.reshape(n, w*h)
    b_res = b.reshape(n, w*h)
    newr = np.zeros((w*h), dtype=np.uint8)
    newg = np.zeros((w*h), dtype=np.uint8)
    newb = np.zeros((w*h), dtype=np.uint8)
    for i in range(w*h): # this is what takes time...
        newr[i] = np.argmax(np.bincount(r_res[:,i]))
        newg[i] = np.argmax(np.bincount(g_res[:,i]))
        newb[i] = np.argmax(np.bincount(b_res[:,i]))
    newb = newb.reshape(w,h) # reshape back from 1d to 2d
    newg = newg.reshape(w,h)
    newr = newr.reshape(w,h)
    return newb, newg, newr

imgs = ['D:/test/DSC02142.jpg', 'D:/test/DSC02606.jpg', 'D:/test/DSC01747.jpg']
bcolorlist, gcolorlist, rcolorlist = [], [], []
for i in imgs:
    b, g, r = cv2.split(cv2.imread(i))
    bcolorlist.append(b)
    gcolorlist.append(g)
    rcolorlist.append(r)

b = np.array(bcolorlist)
g = np.array(gcolorlist)
r = np.array(rcolorlist)

n, w, h = b.shape # assuming all images of equal width x height!
newb, newg, newr = select(b, g, r, n, w, h)

imgmerge = cv2.merge((newb, newg, newr))
cv2.imwrite('D:/test/output.jpg', imgmerge)

...产生这样奇怪的东西: