evilblubb
evilblubb

Reputation: 31

UMat is slow (OpenCV, Python)

I've been Tinkering around with OpenCV to resize a huge batch (100k+) of Images (16-24MP). And somehow it seems that using the CPU is always around 30-50% faster. As I am Running a ryzen 1700x and an 1080ti I was expecting it to be the other way round. It would be nice if someone could give me a hint on what I am doing wrong. I am running OpenCV 4.0.0.pre and 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()

Upvotes: 3

Views: 1859

Answers (2)

Martin Beckett
Martin Beckett

Reputation: 96139

For computationally simple tasks like a resize it takes longer to ship the data to the GPU memory and back again than you save by the faster computation.

Especially since openCV will be doping the resize with parallel CPU cores and long instruction word SIMD optomised assembler on the CPU.

Upvotes: 5

thaisacorreia
thaisacorreia

Reputation: 39

The GPU has high processing power, so it means it performs calculations much faster than the CPU, but when you perform a lot of assignment operations in your code, it is not the best option, since the GPU's memory power is lower than CPU. This is why there is more efficiency when you run your code snippet through the CPU, in which the predominance of operations are assignments.

Upvotes: 0

Related Questions