简体   繁体   English

如何转换我的python代码以在GPU而不是CPU上运行?

[英]How to convert my python code to run on GPU instead of CPU?

I was given a python code that takes in two images as input and uses the Gabor Filter to find the correlation of RGB of the two images and saves it in a csv file. 给了我一个python代码,该代码接受两个图像作为输入,并使用Gabor滤镜查找两个图像RGB的相关性并将其保存在一个csv文件中。 So I need to execute the program using GPU as it takes much time and CPU utilization. 因此,我需要使用GPU执行该程序,因为这需要大量时间和CPU利用率。 I have a GeForce GTX 1050 Ti and am a complete beginner in programming. 我有GeForce GTX 1050 Ti,并且是编程的完整入门者。

I did some research and learned about CUDA and Tensorflow, but I am really unsure on how to go on about implementing it, and what is the best way to do it without changing much of the code. 我进行了一些研究,并了解了CUDA和Tensorflow,但我真的不确定如何继续实施它,以及在不更改大量代码的情况下实现它的最佳方法是什么。

#Gabor Filter
def build_filters():
    filters = []
    #tesing phrase filter - reduce
    for ksize in range(9, 19, 5):
        for theta in np.arange(45, 225, 45):
            for sigma in range(2,6,2):
                kern = cv2.getGaborKernel((ksize, ksize), sigma, theta, 5.0, 0.5, 0, ktype=cv2.CV_32F)
                kern /= 1.5*kern.sum()
                filters.append(kern)
    return filters

#Apply filter into the image
def process(images, f):
    accum = np.zeros_like(images)
    for kern in f:
        fimg = cv2.filter2D(images, cv2.CV_8UC3, kern)
        np.maximum(accum, fimg, accum)
    return accum

The full code: https://gitlab.com/t.tansuwan/image_diff_kce/blob/master/allPixelNoCrop.py 完整代码: https : //gitlab.com/t.tansuwan/image_diff_kce/blob/master/allPixelNoCrop.py

Thank you! 谢谢!

Numba can convert a small sub-set of Python to . Numba可以将一小部分Python转换为。

You'll want to install numba and cudatoolkit with the conda package manager: conda install numba cudatoolkit . 您将要使用conda软件包管理器安装numba和cudatoolkit: conda install numba cudatoolkit Then you can add @jit(nopython=True, parallel=True) 然后可以添加@jit(nopython=True, parallel=True)

I'm not sure Numba can be used with OpenCV, but you could certainly try. 我不确定Numba是否可以与OpenCV一起使用,但是您可以尝试。 Python is not really suited for high-performance computation, you're better off learning FORTRAN, a shader language, or C and implementing your computation in that. Python并不真正适合高性能计算,您最好学习FORTRAN,着色器语言或C并在其中实现计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM