gpt4 book ai didi

python - UMat 很慢(OpenCV、Python)

转载 作者:太空宇宙 更新时间:2023-11-03 21:24:59 27 4
gpt4 key购买 nike

我一直在使用 OpenCV 调整大量 (100k+) 图像 (16-24MP) 的大小。不知何故,使用 CPU 似乎总是快 30-50% 左右。当我运行 ryzen 1700x 和 1080ti 时,我希望它是相反的。如果有人可以提示我做错了什么,那就太好了。我正在运行 OpenCV 4.0.0.pre 和 OpenCL 1.2

#!/usr/bin/python
import numpy as np
import cv2 as cv
import glob
from multiprocessing import Pool
import time
path =''
dic=[]
def resizer(file):
img = cv.imread(file)
height, width = img.shape[:2]

dim = float(width)/float(height)
if dim > 1:
width=4000
height= 4000/dim
start_time = time.time()
res = cv.resize(cv.UMat(img), (int(width), int(height)), interpolation=cv.INTER_CUBIC)
print("--- %s seconds ---" % (time.time() - start_time))
else:
width=4000*dim
height= 4000
start_time = time.time()
res = cv.resize(cv.UMat(img), (int(width), int(height)), interpolation=cv.INTER_CUBIC)
print("--- %s seconds ---" % (time.time() - start_time))
name=file.split('/')[-1]
cv.imwrite('/small/{}'.format(name), res)

for file in glob.glob(path+'*.JPG'):
dic.append(file)

if __name__ == "__main__":
pool=Pool(16)
pool.map(resizer, dic)
pool.terminate()

最佳答案

对于像调整大小这样的计算简单的任务,将数据传送到 GPU 内存并再次返回所需的时间比通过更快的计算保存的时间更长。

特别是因为 openCV 将使用并行 CPU 内核和 CPU 上的长指令字 SIMD 优化汇编器来调整大小。

关于python - UMat 很慢(OpenCV、Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50050744/

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