|
测试代码如下:- import cv2
- import timeit
- print('OpenCL available:', cv2.ocl.haveOpenCL())
- # A simple image pipeline that runs on both Mat and Umat
- def img_cal(img, mode='none'):
- if mode=='UMat':
- img = cv2.UMat(img)
- img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- img = cv2.GaussianBlur(img, (7, 7), 1.5)
- img = cv2.Canny(img, 0, 50)
- if type(img) == cv2.UMat:
- img = cv2.UMat.get(img)
- return img
- # Timing function
- def run(processor, function, n_threads, N):
- cv2.setNumThreads(n_threads)
- t = timeit.timeit(function, globals=globals(), number=N)/N*1000
- print('%s avg. with %d threads: %0.2f ms' % (processor, n, t))
- return t
- img = cv2.imread('a.tif')
- N = 100
- threads = [1, 6]
- processor = {'GPU': "img_cal(img,'UMat')",
- 'CPU': "img_cal(img)"}
- results = {}
- for n in threads:
- for pro in processor.keys():
- results[pro,n] = run(processor=pro,
- function= processor[pro],
- n_threads=n, N=N)
- print('\nGPU speed increase over 1 CPU thread [%%]: %0.2f' % \
- (results[('CPU', 1)]/results[('GPU', 1)]*100))
- print('CPU speed increase on 6 threads versus 1 thread [%%]: %0.2f' % \
- (results[('CPU', 1)]/results[('CPU', 6)]*100))
- print('GPU speed increase versus 6 threads [%%]: %0.2f' % \
- (results[('GPU', 1)]/results[('GPU', 6)]*100))
测试结果为:
- toybrick@debian10:~/temp/pytest$ /usr/bin/python3 /home/toybrick/temp/pytest/main.py
- OpenCL available: True
- GPU avg. with 1 threads: 25.12 ms
- CPU avg. with 1 threads: 3.34 ms
- GPU avg. with 6 threads: 16.39 ms
- CPU avg. with 6 threads: 3.14 ms
- GPU speed increase over 1 CPU thread [%]: 13.31
- CPU speed increase on 6 threads versus 1 thread [%]: 106.54
- GPU speed increase versus 6 threads [%]: 153.28
使用了OpenCL/GPU后,性能反而下降了,好奇怪?!
|
|