【问题标题】:improt Numba @jit meet warning message and @jit(nopython=True) will show errorimport Numba @jit 遇到警告消息和 @jit(nopython=True) 将显示错误
【发布时间】:2019-08-26 23:27:09
【问题描述】:

我是 python 新手。

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

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

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

操作系统 Ubuntu 16

pyhton 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: 编译正在回退到启用循环提升的对象模式,因为函数“选择”失败了类型推断,原因是:无效使用具有类型参数的 Function(): (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)

正常工作!!!

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

能不能麻烦你给点建议?

【问题讨论】:

  • 你的输入看起来怎么样?
  • 您好,我的输入如下" 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)"
  • 更新我的代码,感谢您的回复。
  • 哈哈好吧,我认为我的回答有点过于复杂,现在我明白了你想要做什么。将在一分钟内发布更新。

标签: python for-loop numba


【解决方案1】:

更新 - 以 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)

...产生如下奇怪的东西:

【讨论】:

  • 您好,感谢您的回复。我已经更新了我的代码。我会尝试使用您的代码并尽快回复。
  • 您好,我更新了代码!它在 CUDA 中工作,但我没有看到输出 iamge...我正在尝试调试。能麻烦您给点建议吗?
  • 进行了更新。希望这对你有用。我的原始代码有点过于复杂,无法按预期方式工作。
  • 嗨对不起.....我可以问一个问题......它工作正常且比以前更快......但我使用“nvidia-smi”来检查......这个python 代码在 GPU 中不起作用……麻烦您给点建议吗?
  • 感谢您的回复!!!!我会自己试试。如果不是我会问一个新问题~~~
猜你喜欢
  • 2020-08-08
  • 1970-01-01
  • 1970-01-01
  • 2019-12-08
  • 1970-01-01
  • 1970-01-01
  • 2019-08-14
  • 2018-02-23
  • 2019-12-29
相关资源
最近更新 更多