python - Umat 很慢(OpenCV、Python)
问题描述
我一直在修补 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 优化汇编程序来调整大小。
推荐阅读
- firebase - 哪个firestore规则可以显示所有带有我的uid的文档?
- javascript - CRUD - 如果页面没有刷新,添加和删除一个接一个地不起作用
- android - 如何使用约束布局设置recyclerview最小高度wrap_content但最大百分比?
- angular - NGRX 路由器成对单元测试
- symfony - Symfony 5.1 如何从其他位置获取控制器
- php - 如何使用 webhook 安全地触发 git pull?
- node.js - wss 通过 nginx 连接到节点 ws 服务器,ERR_CERT_AUTHORITY_INVALID
- c++ - 解析 RapidJSON 时出现错误的地方
- python - AKS,无法部署模型,读取 csv 文件时出错
- debian - 在 debian 10 启动时以 root 身份自动启动程序