Build CUDA Opencv with Python 3.8 Bindings for GTuner
17 posts
• Page 1 of 2 • 1, 2
Build CUDA Opencv with Python 3.8 Bindings for GTuner
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):
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:
Next, open the Path variable, and check to make sure that it contains the paths:
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).
3. Ok, variables are set and you're ready to rock. Simply copy/paste this into the prompt and watch some magic happen
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:
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
into the prompt, and then type
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:
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
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
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
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
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"
- 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()
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.
-
jaj - Staff Sergeant
- Posts: 13
- Joined: Tue Mar 31, 2020 10:50 am
Re: Build CUDA Opencv with Python 3.8 Bindings for GTuner
Thank you jaj for sharing your findings with us and for taking time to write this comprehensive tutorial.
ConsoleTuner Support Team
-
J2Kbr - 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
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.
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.
-
osori - Corporal
- Posts: 4
- Joined: Sun Dec 06, 2020 4:51 am
Re: Build CUDA Opencv with Python 3.8 Bindings for GTuner
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
-
jaj - Staff Sergeant
- Posts: 13
- Joined: Tue Mar 31, 2020 10:50 am
Re: Build CUDA Opencv with Python 3.8 Bindings for GTuner
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....
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
-
GrandpaMike - Corporal
- Posts: 5
- Joined: Wed Jan 06, 2021 3:29 pm
- Location: Georgia
Re: Build CUDA Opencv with Python 3.8 Bindings for GTuner
i have smiller pc but still I got 0 for gpu.
- Attachments
-
- Untitled.png (10.1 KiB) Viewed 7795 times
-
God of War - Corporal
- Posts: 4
- Joined: Thu May 28, 2020 6:07 pm
Re: Build CUDA Opencv with Python 3.8 Bindings for GTuner
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:
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:
rebuilt again, now it shows
which is what I have compiled
and now I get it working:
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
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
-
ta_trainer - Private First Class
- Posts: 2
- Joined: Tue Dec 14, 2021 11:18 pm
Re: Build CUDA Opencv with Python 3.8 Bindings for GTuner
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.
-
DeinCheffe - Sergeant First Class
- Posts: 20
- Joined: Wed Sep 09, 2020 2:19 pm
Re: Build CUDA Opencv with Python 3.8 Bindings for GTuner
Ok i found a link.It works Visual Studio 2019.Thanx for this great work :-)
-
DeinCheffe - Sergeant First Class
- Posts: 20
- Joined: Wed Sep 09, 2020 2:19 pm
17 posts
• Page 1 of 2 • 1, 2
Return to Gtuner Computer Vision
Who is online
Users browsing this forum: No registered users and 82 guests