Build CUDA Opencv with Python 3.8 Bindings for GTuner

GCV scripting for Gtuner IV and Titan Two. Configuration, examples, questions.

Build CUDA Opencv with Python 3.8 Bindings for GTuner

Postby jaj » Sun Oct 04, 2020 6:27 am

Before we begin: I highly, highly suggest uninstalling all previously installed python environments on your machine (unless you're actually a Python dev and need those things). This includes the Python 3.8 environment that most will be using only for Gtuner. Download Ccleaner, and uninstall the python on your system. You'll also use a fresh instance of Gtuner. This tutorial uses Conda to create a separate python environment that you will from this point forward use for Gtuner. This step could cause or save you many hours of problem solving.

I won't go too much into why things work or need to be done, and there won't be any pictures or video. To ensure this works for you, do everything that I say to do, and don't do anything that I don't say to do. As long as you can follow step by step instructions then this will work for you.


Set Up

1. Download and install Visual Studio 2019 Community. This part is very important. In the installer when the workloads screen pops up, you must check Python Development, and Desktop development with C++

2. Download and install Cmake x64

3. Download and install Anaconda 64bit. In the installer, you must select Register Anaconda as my default Python. DO NOT select the other option adding Anaconda to path. We're also going to set up the separate Conda environment in this step. So once it's done installing, click that start button, and open up the Anaconda Prompt (anaconda3) and type in (or copy/paste):
Code: Select all
conda create -n gpu anaconda python=3.8
This will create the new Python environment called gpu. You can name it whatever you want but I'd recommend sticking with gpu to not get confused during the tutorial. Wait for that to finish, and you can close the prompt for now.

4. Download and install CUDA v11.1

5. Download cuDNN v8.0.4 (for CUDA v11.1). You'll need to sign up for an Nvidia developer account first to gain access to the download. It's quick and free to sign up. Once the zip file is downloaded you want to simply extract the contents into your CUDA installation folder, found at the path: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1 There are 3 folders in the cuDNN download that can be drag+dropped into the CUDA main folder. So the cuDNN files go directly into the matching CUDA folders.

6. Download opencv 4.4.0

7. Download opencv contrib 4.4.0

8. Extract both opencv, and opencv contrib folders into your gpu environment's lib folder. The paths should be: C:\Users\YOUR_USER\anaconda3\envs\gpu\lib\opencv-4.4.0
and
C:\Users\YOUR_USER\anaconda3\envs\gpu\lib\opencv_contrib-4.4.0
*Throughout the tutorial be sure to swap out YOUR_USER with your own user path, especially when entering commands in cmd

Building the Files

So the files and folders are all set up. Now we need to build the Python 3.8 bindings for a CUDA supported opencv. Don't worry, it's much easier than it sounds.

Quick sidebar before we get started on these next steps! We need to be sure that the CUDA system variables have been added properly during installation. So type 'system' into your search bar, and open up 'System' under the 'Settings' area. Click 'Advanced System Settings', and open 'Environment Variables' from the pop up. In the 'System Variables' box on the bottom search for the following system variables:
Code: Select all
Variable                        Value
CUDA_HOME                     C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1
CUDA_PATH                     C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1
CUDA_PATH_V11_1               C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1 
If they are not there you'll have to add them.
Next, open the Path variable, and check to make sure that it contains the paths:
Code: Select all
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\libnvvp

Again, they should be there, but if not, click New and add them in.

1. We need to open an anaconda prompt for these steps. So click the start button, and this time open a prompt for the new gpu environment you created, it'll be called Anaconda Prompt (gpu)

2.You'll have to set some variables for the next steps. Assuming you followed all my steps exactly, your paths will match mine, and you can use the following. Copy/paste these one line at a time and hit enter after each (be sure to swap the user path to your own).
Code: Select all
set "openCvSource=C:\Users\YOUR_USER\anaconda3\envs\gpu\lib\opencv-4.4.0"

Code: Select all
set "openCVExtraModules=C:\Users\YOUR_USER\anaconda3\envs\gpu\lib\opencv_contrib-4.4.0\modules"

Code: Select all
set "openCvBuild=C:\Users\YOUR_USER\anaconda3\envs\gpu\lib\opencv-4.4.0\build"

Code: Select all
set "buildType=Release"

Code: Select all
set "generator=Visual Studio 16 2019"

Code: Select all
set "pathToAnaconda=C:/Users/YOUR_USER/anaconda3/envs/gpu"
*This one must use forward slashes

Code: Select all
set "pyVer=38"


3. Ok, variables are set and you're ready to rock. Simply copy/paste this into the prompt and watch some magic happen

Code: Select all
"C:\Program Files\CMake\bin\cmake.exe" -B"%openCvBuild%/" -H"%openCvSource%/" -G"%generator%" -DCMAKE_BUILD_TYPE=%buildType% -DOPENCV_EXTRA_MODULES_PATH="%openCVExtraModules%/" ^ -DINSTALL_TESTS=ON -DINSTALL_C_EXAMPLES=ON -DBUILD_EXAMPLES=ON ^ -DBUILD_opencv_world=ON ^ -DWITH_CUDA=ON -DCUDA_TOOLKIT_ROOT_DIR="C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1" -DCUDA_FAST_MATH=ON -DWITH_CUBLAS=ON -DCUDA_ARCH_PTX=8.0 -DWITH_NVCUVID=ON ^ -DWITH_OPENGL=ON ^ -DWITH_MFX=ON -DBUILD_opencv_python3=ON -DPYTHON3_INCLUDE_DIR=%pathToAnaconda%/include -DPYTHON3_LIBRARY=%pathToAnaconda%/libs/python%pyVer%.lib -DPYTHON3_EXECUTABLE=%pathToAnaconda%/python.exe -DPYTHON3_NUMPY_INCLUDE_DIRS=%pathToAnaconda%/lib/site-packages/numpy/core/include -DPYTHON3_PACKAGES_PATH=%pathToAnaconda%/Lib/site-packages/ -DOPENCV_SKIP_PYTHON_LOADER=ON


4. Once the cmake configuration is done, it's time to build the files.
Assuming you had no errors while configurating and generating the files, you can now copy/paste in this command:
Code: Select all
"C:\Program Files\CMake\bin\cmake.exe" --build %openCvBuild% --target INSTALL --config Release

This is going to take around 2-3 hours to build the python bindings, depending on your hardware. You will see hundreds of warnings if you watch the command prompt while building, so I'd suggest looking away.

5. Once the build is finished, you're done! Sort of. To check if the cv2 library built successfully, type
Code: Select all
python

into the prompt, and then type
Code: Select all
import cv2

If it comes up with module not found. There are some steps to continue with.

6. Back to the system Path variable. You'll need to make sure that your Path contains the following entries:
Code: Select all
C:\Users\YOUR_USER\anaconda3\envs\gpu\lib
C:\Users\YOUR_USER\anaconda3\envs\gpu\Library
C:\Users\YOUR_USER\anaconda3\envs\gpu\Library\bin
C:\Users\YOUR_USER\anaconda3\envs\gpu\lib\opencv-4.4.0\build\lib\python3\Release
C:\Users\YOUR_USER\anaconda3\envs\gpu\lib\opencv-4.4.0\build\install\x64\vc16\bin

Adding any that it may be missing (be sure to swwap your user path in).

7. This is where I personally add my new instance of Gtuner into the Python gpu environment folder and create a Scripts folder as the working directory. Be sure to link your Gtuner computer vision to your gpu python environment. It should find the rest of the files automatically.

You can check the output of
Code: Select all
cv2.cuda.getCudaEnabledDeviceCount()
from a gcv script. if the output is 1, your gpu is recognized and opencv is using it.

At this point, Gtuner opencv should be using CUDA acceleration on your gpu! To a small extent this is done automatically. You'll notice it taking up gpu resources while running. To a larger extent, scripts will need to be customized to take advantage of the gpu resources.

To come: a script to check the gpu resources. Probably some edits, as this was a very quick and rough draft
Last edited by jaj on Fri Jan 15, 2021 6:16 am, edited 5 times in total.
User avatar
jaj
Staff Sergeant
Staff Sergeant
 
Posts: 13
Joined: Tue Mar 31, 2020 10:50 am

Re: OpenCV w/ CUDA Thread

Postby jaj » Sun Oct 04, 2020 7:58 am

Reserved
User avatar
jaj
Staff Sergeant
Staff Sergeant
 
Posts: 13
Joined: Tue Mar 31, 2020 10:50 am

Re: Build CUDA Opencv with Python 3.8 Bindings for GTuner

Postby J2Kbr » Mon Oct 19, 2020 7:20 pm

Thank you jaj for sharing your findings with us and for taking time to write this comprehensive tutorial. :joia:
ConsoleTuner Support Team
User avatar
J2Kbr
General of the Army
General of the Army
 
Posts: 20323
Joined: Tue Mar 18, 2014 1:39 pm

Re: Build CUDA Opencv with Python 3.8 Bindings for GTuner

Postby osori » Sun Dec 27, 2020 4:25 am

Thank you for the wonderful tutorial.

For those planning to use tensorflow and 1080TI or lower GPU, if you are having trouble I recommend try changing
-DCUDA_ARCH_PTX=8.0
to
-DCUDA_ARCH_PTX=6.1
as suggested by 'ME.' the moderator from discord.
User avatar
osori
Corporal
Corporal
 
Posts: 4
Joined: Sun Dec 06, 2020 4:51 am

Re: Build CUDA Opencv with Python 3.8 Bindings for GTuner

Postby jaj » Tue Jan 05, 2021 2:10 am

osori wrote:Thank you for the wonderful tutorial.

For those planning to use tensorflow and 1080TI or lower GPU, if you are having trouble I recommend try changing
-DCUDA_ARCH_PTX=8.0
to
-DCUDA_ARCH_PTX=6.1
as suggested by 'ME.' the moderator from discord.


That is correct. I'll update the tutorial to clarify the differences shortly
User avatar
jaj
Staff Sergeant
Staff Sergeant
 
Posts: 13
Joined: Tue Mar 31, 2020 10:50 am

Re: Build CUDA Opencv with Python 3.8 Bindings for GTuner

Postby GrandpaMike » Sat Mar 13, 2021 11:16 pm

jaj wrote:This is going to take around 2-3 hours to build the python bindings, depending on your hardware. You will see hundreds of warnings if you watch the command prompt while building, so I'd suggest looking away."

I'm going on 4 and a half hours..
what Hardware is suggested ?

I have :
Processor Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz, 2592 Mhz, 6 Core(s), 12 Logical Processor(s)
Installed Physical Memory (RAM) 16.0 GB

Name NVIDIA GeForce GTX 1660 Ti
Adapter RAM (1,048,576) bytes

1 terabyte HD
500 gig ssd

LOL it finished 5 minutes after i posted.... :smile0202:

after step 5
I did have to manually add the path entries. but I also had to close the anaconda window and reopen it to not get an error after typing import cv2

computer vision.JPG
computer vision.JPG (47.26 KiB) Viewed 7707 times
:smile0203:
User avatar
GrandpaMike
Corporal
Corporal
 
Posts: 5
Joined: Wed Jan 06, 2021 3:29 pm
Location: Georgia

Re: Build CUDA Opencv with Python 3.8 Bindings for GTuner

Postby God of War » Sun Mar 14, 2021 7:42 pm

i have smiller pc but still I got 0 for gpu.
Attachments
Untitled.png
Untitled.png (10.1 KiB) Viewed 7682 times
User avatar
God of War
Corporal
Corporal
 
Posts: 4
Joined: Thu May 28, 2020 6:07 pm

Re: Build CUDA Opencv with Python 3.8 Bindings for GTuner

Postby ta_trainer » Sun Dec 19, 2021 3:53 am

thanks a lot for the tutorial.
I have used it before and it was all good.
but then I had to format my PC and do it again. but it did not work.

here are a few extra steps to verify:
Code: Select all
import cv2
cv2.__version__
 

in my case, the output was 4.5! which should have been 4.4
with some googling, it was a simple fix, just uninstall opencv-python using:
Code: Select all
pip uninstall opencv-python


rebuilt again, now it shows
Code: Select all
>>> cv2.__version__
'4.4.0'


which is what I have compiled

and now I get it working:
Code: Select all
>>> cv2.cuda.getCudaEnabledDeviceCount()
1


I also found this helpful tutorial on youtube that does it in a similar way with some code to test cuda vs cpu.
https://www.youtube.com/watch?v=tjXkW0-4gME
User avatar
ta_trainer
Private First Class
Private First Class
 
Posts: 2
Joined: Tue Dec 14, 2021 11:18 pm

Re: Build CUDA Opencv with Python 3.8 Bindings for GTuner

Postby DeinCheffe » Thu Dec 30, 2021 10:40 pm

Hello i did everything but cmake doesnt find the visual studio.That was the only problem that i hat.The Visual Studio Community is only available as Visual Studio Community 2022.I think that is the Problem. Have somebody a link for VSC 2019? Please helb me.
User avatar
DeinCheffe
Sergeant First Class
Sergeant First Class
 
Posts: 20
Joined: Wed Sep 09, 2020 2:19 pm

Re: Build CUDA Opencv with Python 3.8 Bindings for GTuner

Postby DeinCheffe » Fri Dec 31, 2021 2:48 pm

Ok i found a link.It works Visual Studio 2019.Thanx for this great work :-)
User avatar
DeinCheffe
Sergeant First Class
Sergeant First Class
 
Posts: 20
Joined: Wed Sep 09, 2020 2:19 pm

Next

Return to Gtuner Computer Vision

Who is online

Users browsing this forum: No registered users and 50 guests