Ask Your Question
0

OpenCV GPU Accelerated using Python

asked 2018-11-16 04:45:15 -0600

New_User_Opencv gravatar image

I am trying to run my python code which is basically related to image processing and finding defects. I want to get this code on GPU (it works perfectly fine using CPU but takes time due to many libraries) and was suggested using opencv gpu accelerated library. I have no clue how to start doing this.. I have tried to do this following example but does not have any change in its time taken to complete the task. import cv2 import time

import cv2

import time

t=time.time()

img="Red.jpg"

img_original=cv2.UMat(cv2.imread(img))

imgUmat=cv2.UMat(img_original)

blur= cv2.pyrMeanShiftFiltering(imgUmat,21,49)

gray_image= cv2.cvtColor(blur,cv2.COLOR_BGR2GRAY)

cv2.imshow('original',gray_image)

print('Done in', (time.time()-t))

cv2.waitKey(0)

cv2.destroyAllWindows()

Output:

Done in 9.723002672195435

It would be great, if anyone can suggest where do i start from and how does this even work??

edit retag flag offensive close merge delete

Comments

please use time.clock() or cv2.getTickCount() for measurement.

you also should restrict it to the processing, not imread() imshow(), etc.

berak gravatar imageberak ( 2018-11-16 04:49:59 -0600 )edit

t=time.process_time() i have updated to this as time.clock() is depricated as per this error '' DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead print('Done in', (time.clock()-t))''

New_User_Opencv gravatar imageNew_User_Opencv ( 2018-11-16 05:23:49 -0600 )edit

you also should restrict it to the processing, not imread() imshow(), etc

I dont understand what you meant by this

New_User_Opencv gravatar imageNew_User_Opencv ( 2018-11-16 05:24:33 -0600 )edit

Thanks for responding as well

New_User_Opencv gravatar imageNew_User_Opencv ( 2018-11-16 05:24:58 -0600 )edit

if you want to find out, how long your processing takes, you need to set the timers more "tightly" around that, don't measure, how long imshow() took, or how long you waited for a keypress, even

berak gravatar imageberak ( 2018-11-16 05:53:00 -0600 )edit

ok ok i get it what u say regarding timers

New_User_Opencv gravatar imageNew_User_Opencv ( 2018-11-16 09:02:33 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-11-16 05:50:09 -0600

berak gravatar image

updated 2018-11-16 06:03:18 -0600

a better way to test things might be:

import cv2
import numpy as np

def test(img, N, S):
    t0 = cv2.getTickCount()
    for i in range(N):
        blur= cv2.pyrMeanShiftFiltering(img,21,49)
        gray_image= cv2.cvtColor(blur,cv2.COLOR_BGR2GRAY)
    t1 = cv2.getTickCount()
    print ("%s:\t%d iterations took %f seconds." % (S, N, (t1-t0)/cv2.getTickFrequency()))


img = cv2.imread("Red.jpg")
print("image size: ", img.shape)

N = 100
test(img, N, "Mat")
test(cv2.UMat(img), N, "UMat")

in other words:

  • use more than one iteration
  • exclude up/downloading to/from GPU
  • exclude file-io, printing and gui operations
  • use clock-time, not wall-time to measure
edit flag offensive delete link more

Comments

1

This program does not run completely Output: runfile('*.py', wdir='/') image size: (2764, 3856, 3)

Execution hangs here and I have to restart the console for new session.

New_User_Opencv gravatar imageNew_User_Opencv ( 2018-11-16 08:37:17 -0600 )edit

are you aware, that already a single iteration might take like a second ? you might just have to wait longer ! (the pyrMeanShiftFiltering is VERY expensive !)

if it start to hang or abort in the UMat test, can you try a line like:

cv2.ocl.setUseOpenCL(False)

before running the tests ?

berak gravatar imageberak ( 2018-11-16 08:39:28 -0600 )edit

ya I have much longer than a second and then restarted the console

cv2.ocl.setUseOpenCL(False) I ran the program with this but still the same

how to make sure if this program is even accessing GPU?

New_User_Opencv gravatar imageNew_User_Opencv ( 2018-11-16 09:09:38 -0600 )edit

you misunderstood it

100 iterations will take almost 2 minutes !

and disabling opencl is only meant to be tried, if your program aborts for weird reason, just to check, if it works better without using openCL

berak gravatar imageberak ( 2018-11-16 09:24:27 -0600 )edit
1

This takes more time now without openCL

New_User_Opencv gravatar imageNew_User_Opencv ( 2018-11-20 02:35:41 -0600 )edit

same here, actually. means: our hardware isn't stellar.

berak gravatar imageberak ( 2018-11-20 02:39:26 -0600 )edit

I tried to just do the gray scale without pyr filter and it works fine when trying to apply filter the iterations are not coming to end at all

New_User_Opencv gravatar imageNew_User_Opencv ( 2018-11-20 02:58:49 -0600 )edit

I also wanted to know how to make sure all this is using GPU

New_User_Opencv gravatar imageNew_User_Opencv ( 2018-11-20 02:59:12 -0600 )edit
1

image size: (2764, 3856, 3) Mat: 10 iterations took 95.811139 seconds. UMat: 10 iterations took 92.830256 seconds.

New_User_Opencv gravatar imageNew_User_Opencv ( 2018-11-20 03:01:59 -0600 )edit

Anymore suggestions??

New_User_Opencv gravatar imageNew_User_Opencv ( 2018-11-21 04:19:35 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-11-16 04:45:15 -0600

Seen: 20,011 times

Last updated: Nov 16 '18