gpt4 book ai didi

python - import Numba @jit meet warning message and @jit(nopython=True) 将显示错误

转载 作者:行者123 更新时间:2023-12-01 07:22:41 26 4
gpt4 key购买 nike

我是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)

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

关于python - import Numba @jit meet warning message and @jit(nopython=True) 将显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57619756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com