4
$\begingroup$

I am doing some image pre-processing using python3, as I know the code is execute on CPU, for that the process is a little bit slow, for the faster process its a good idea to make it run on GPU, because GPU can done graph operation much faster than CPU. Is there a way to make it run on GPU without using library?

Clarification: Make it like GPU version of keras or tensorflow.

$\endgroup$
  • 1
    $\begingroup$ pyimagesearch.com/2016/07/11/compiling-opencv-with-cuda-support $\endgroup$ – Illuminati0x5B Jun 6 '19 at 13:18
  • $\begingroup$ It is pretty rare that image processing be expensive for a single image, it is either for a lot of images or for a very big image. GPUs are memory bound, therefore processing a lot of images or very big images will not improve by using GPUs, the memory copying between RAM and GPU will eat away any improvement. Is it really the processing expensive or it is amount of images? (i.e. you're processing more than a single image) $\endgroup$ – grochmal Jun 9 '19 at 1:26
  • $\begingroup$ If time permits, watch this GPU vs CPU video $\endgroup$ – Continue2Learn Jul 12 '19 at 0:57
1
$\begingroup$

You may apply Wolfram Language to your project. There is a free Wolfram Engine for developers and with the Wolfram Client Library for Python you can use these functions in Python.

Either the CUDALink Overview and CUDALink Guide or the OpenCLLink Overview and OpenCLLink Guide will enable you to run code on your GPU. However, the CUDALink guide image process functions built-in so perhaps best to start there for your project.

from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wl, wlexpr

Start the Wolfram session.

wolfSession = WolframLanguageSession()

The CUDA package is included with the Wolfram Engine but has to be explicitly loaded.

wolfSession.evaluate(wl.Needs('CUDALink`'))

Then you can check if the engine has located one or more compatible GPUs with CUDAQ.

print(wolfSession.evaluate(wl.CUDALink.CUDAQ()))
True

There is a one-time download of CUDA binaries when CUDAQ is evaluated. You can make your first call directly in the kernel to track the one-time download instead of first calling through Python. Run woframscript in the terminal and evaluate the Needs and CUDAQ functions at the input prompt.

enter image description here

Detailed information can be returned with CUDAInformation.

print(wolfSession.evaluate(wl.CUDALink.CUDAInformation(1,'Core Count')))
160

Grab an image for some CUDA image function examples.

imgWolfram=wolfSession.evaluate(
    wl.Rasterize(
        wl.Import('https://i2.wp.com/oemscorp.com/wp-content/uploads/2015/07/wolfram-language-logo.png'),
        'Image'
    )
);

The image can be Exported in one of the supported Raster Image Formats or Vector Graphics Formats.

wolfSession.evaluate(
    wl.Export(
        '<path with image filename>', 
        imgWolfram
        )
    )
)

Wolfram Language logo

A few example CUDA image functions include

  • CUDAImageConvolve

    wolfSession.evaluate(
        wl.Export(
            '<path with image filename>', 
            wl.CUDALink.CUDAImageConvolve(imgWolfram,  [[-1,0,1],[-2,0,2],[-1,0,1]])
        )
    )
    

Mathematica graphics

  • CUDAErosion

    wolfSession.evaluate(
        wl.Export(
            '<path with image filename>', 
            wl.CUDALink.CUDAErosion(imgWolfram,6)
        )
    )
    

Mathematica graphics

  • CUDAOpening

    wolfSession.evaluate(
        wl.Export(
            '<path with image filename>', 
            wl.CUDALink.CUDAOpening(imgWolfram,6)
        )
    )
    

Mathematica graphics

Terminate the Wolfram session.

wolfSession.terminate()

Since you have processing many image files you would get slightly better performance by processing directly in wolfram script with the Wolfram Engine rather than by calling the engine from Python.

Hope this helps.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.