Setup OpenCV-DNN module with CUDA backend support on Windows

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] and welcome to this video in this video i will be showing you how to set up opencv dnn module with cuda back-end support basically we will be building opencv from source with cuda back-end support enabling the opencv dnn cuda module that makes the inference much faster for object detection models as compared to the opencv without cuda backhand support we install that using the windows installer this will be built from source so let's get started there are six steps for this the first four steps are basically just installing the required software if you watched my previous videos and have been following my tutorials so you will probably already have these software installed but just in case i have mentioned them over here as well so first is install code ncu dnn on your windows system this is for windows i'll be making another tutorial for linux in my next video okay so you can read more about this good and cud installation process over here you have this for both windows and linux so you can follow this tutorial and install cuda ncu dnn download cuda install it set the environment variable paths the same for cu dnn now one thing i have to tell you you have to make sure that you add these path variables to both cu dnn and the path system variable as we will be using cmake in this tutorial i found that cmake sometimes a few versions of cmake cannot locate this cudn variable environment variable so it's a better option to include all these in the path variable as well that ensures that it will find the cu dnn on your system okay i've also mentioned this over here all right moving on to the next step install cmake gui just download it from over here and select your version i have 64-bit windows so i have already installed this on my system now are you installing this make sure to select add cmake to the system path option for current user set the destination folder and that's it now third step install anaconda individual edition for windows you can download from over here now when you run the setup for anaconda i want you to select this just me option this is also the recommended option this is just to avoid any permission errors in future all right so i have mentioned this over here as well i already have anaconda installed on my system so i'll just show you the next step you have to check both these options register anaconda 3 as my default python 3.9 and add anaconda 3 to my path environment variable check both these options and click on install and that is it now next install msvc microsoft visual studio you can download the community edition from over here and in there you can select this module once you run the setup desktop development with c plus this is the one that we require of course you can also add a few other modules later on like python development and a few other modules make sure all these are checked and now we can move on to the next step that is step five create a virtual environment in anaconda now i'm creating a virtual environment as i'll be building the opencv from source for the opencvdn encoder module in a virtual environment it's a better option to do that for a virtual environment than for your base environment i'd recommend you to do that as well so if you want to install it for your base environment you can skip this step all right you can skip this step and move on to step six but i'll be creating the anaconda environment let's create a virtual environment using this command you can change the name to whatever you want i'm naming two opencv dna cuda i'm setting python to 3.9 yes activate the environment using this next we have to install numpy for this environment as we will be needing this in the cmake gui that we will be running later on okay so this is done now we can move on to the next step final step of this let's build opencv from source with coda backend support for your specific nvidia gpu now i'll explain what this means later on first download opencv and opencv contrib these files from over here i have given both these links over here latest opencv version as of now is 4.5.5 now you have to make sure both these versions are same this is important so as you can see at the releases site for opencv the latest one is opencv 4.5.5 and we have to download this sources file zip file normally if you are installing opencv without cuda backend you just use this windows installer for installing opencv but right now we are building from source and we'll be adding a few additional libraries and enabling a few other modules as i've mentioned earlier so okay that's done now next download opencv contrib okay you can download it from over here so this is the latest version 4.5.5 but you can also just change this from over here you can select the version from over here and make sure that it is the exact same version as the opencv source file okay that's done now create a new folder in your c drive named opencv extract and copy the folders inside the master folders to the opencv folder okay let's do that let's create a new folder named opencv you can extract this over here copy the folder inside now you can also rename this opencv and simply paste it over here but i am doing this so it is easy to differentiate if you have multiple open cv the trees on your system so it's just easier all right moving on now i've also given these parts in this tutorial so you will have to change these if you use a different directory all right now create another name folder named build inside opencv okay let's create another folder now i'm doing this since we'll be installing all the necessary libraries and files over here we'll be building everything in this folder and later on we can delete this opencv 4.5.5 folder and also we have to extract the other folder as i mentioned over here extract and copy both the folders these two let's also extract this and copy it to the opencv folder all right now we can open the cmake gui now we have to select the source as opencv opencv 4.5.5 this folder over here give this path or you can simply browse it that's it now for this we'll give the destination as the build folder this one that is it make sure the grouped box is checked and that is it you can click on configure now since i have visual studio 2017 i'll leave it as it is you can choose your version over here next select your optional platform for generator so i'll be using x64 click on finish this will configure settings for you for opencv this is a warning for python 2 i don't have python 2 on my system so you can ignore this as long as there are no errors it's fine okay so it found that the base environment python file and the lib file will change that as well for the virtual environment okay that's done so after the first configuration then this is the output that you will see so the open theme modules to be built we'll have python 3 over here all right now these are the python bindings that it found for the base environment we'll be changing this to the virtual environment as i mentioned earlier i am creating this opencv module in a virtual environment so all right this is the location that it will be installed to openc build install folder now next step we are done with this step now once the first configuration is done select the following modules by tapping them the search box and taking their boxes you can quickly copy them from over here now check this now some of these are already checked but you have to double check these as these are important this is already checked this is for opencv dnn and then opencv python3 this one is also checked by default but just make sure okay now next we have to set the part to the modules folder click over here and then give the path to this modules folder inside opencv contrib and that is it press enter now again you have to do this step only if you are building opencv for the virtual environment which we created in step five if you're not doing it for a virtual environment that is if you're doing it for a base environment you can directly skip to step 6i over here um but since i'm building it for virtual environment i'll be running this step okay so search this again in the search box now we have to set the path for this to point to the virtual environment which is over here the location up till here anaconda 3 is the same and you have to go inside the environments folder over here we have the environment that we created okay and then inside this we'll have the python the python.exe file over here so just copy the path to this in fact since i know this is the only change in the path so okay as you can see it's already showing me the python file click on it okay again i'll just copy this now alright we have the python 3 include directory this one okay just paste it over here as well that's it the same for the python 3 library python 3 numpy include directory and finally python 3 packages path you can also browse the folder from over here all right but since i already know and i've done it previously so all right we're done with this step now we can finally move on to the next step 6i and click on configure now this will detect the code and cue dna on my system since i have selected these packages with cuda i've also selected this package opencv dnn cuda i'll be enabling this module this is very important so all right let's look at the output now this configuration takes a while so just be patient all right so as you can see it has already found cudn on my system it is also found the version of it i have eight point two point two installed for put eleven point four coder detected eleven four okay so that's done let's look at the changes i will see a few changes over here for the anaconda virtual environment that we created all right so we have the cu dnn the cuda over here now as you can see this detects all these architectures over here we have to set this for our particular nvidia gpu now next step you have to select this again search check the box now next as i mentioned just now that we have to set this for our gpu you can find the architecture for your gpu over here now i have a geforce enabled gpu and it is 1070 gtx 1070 this one so it is 6.1 for my gpu this is how you find the compute capability which is also the code architecture bin okay so set this as 6.1 search this you can remove all these and simply set the one for your gpu and that's it now next find c make configuration types remove the debug option and set it for release and that is it we can just click on configure one last time and then we'll generate the project with these settings okay so as you can see now we have a particular architecture compute capability set in this opencv settings all right so just before clicking on generate just double check everything everything seems fine over here the libraries it only takes for the base environment so you don't have to worry about that these are the main ones that we need for the virtual environment the interpreter the numpy directory and the install path all right and also the python that we said python.exe for our virtual environment and that is it now okay it has set the architecture for my gpu and everything seems good now we can click on generate okay this generates the project the settings which you can use to build our opencv now once the generating is done you can either open this project in visual studio and build it over there using build solution or i'll show you an easier option an easier method to do that just open a command prompt and we'll enter this command and it will install the opencv for you okay so generating done now you don't have to open this project in visual studio you can do that but now i'm going to show you an easier option as i said now open the command prompt and just enter this command press enter and that is it this will build the opencv for you now this will take a while this can take up to an hour or so so you can just sit back and relax and come back to it once it's done okay so that's done finally it took around an hour and a half so we're done with this using this command again as i mentioned earlier we could have also done it the other way using visual studio just to open it and you can build a solution over there we can close this and now we have built the opencv in this installed folder over here this is what we require okay and we can now delete these as we already have these modules and other libraries already inside this build folder so moving on to the next step last we have to set the following paths in your environment variables and reboot your system so we have to set these paths let's open the environment variable first we need this one opencv build install x64 basically we need the path to this and this so you can just copy it from here in your system variables you can create a new system variable with this name make sure the spelling is correct all right and you can paste one over here the next was for library type and then for the build folder okay so you can see we have added these three parts i also want you to add this one path bin inside the path variable and that's it click on ok now we have successfully built the opencv dnn cuda module that is open cv with cuda backend after adding the parts we have to reboot the system all right for the parts to take effect let's do that okay i have rebooted the system so now these new paths are set for the environment variables okay next you can run this to check the version of the opencv installed so that will confirm if the opencv was installed or not now to confirm if the opencv installed was set up correctly with cuda back-end support you can use this script you can download it from here i already have this script on my desktop basically it will give you outputs for two different scenarios one is using gpu that is cuda using gpu and the other will be our cpu this will be slower so let's run it okay so as you can see for gpu for cuda using gpu it was done in 0.53 seconds and for cpu it was almost 4 times that around 2 seconds so this has confirmed that our opencv was installed properly the opencv module with coda backend so all right that was it for this video on how to build opencv from source with cuda back-end support to enable the opencv tnn cuda module on windows you can head over to this article and read this i've written everything here in much more detail so if you have any doubts or issues just refer to this and you'll be fine and okay that's it for now if you've enjoyed this video please click on the like button and make sure to subscribe to this channel and all right i'll see you in the next one [Music] you
Info
Channel: techzizou
Views: 8,532
Rating: undefined out of 5
Keywords: opencv, opencv dnn, opencv dnn cuda, opencv dnn windows, build opencv from source on windows, opencv-dnn-cuda module, cuda backend, opencv cuda backend, opencv cuda support, opencv-dnn module, object detection, deep learning, Machine Learning, Machine learning tutorials, yolo, yolo object detection tutorial, yolo object detection, yolo machine learning, machine learning tutorial for beginners, machine learning for beginners
Id: VAoNMlCm_Cg
Channel Id: undefined
Length: 29min 12sec (1752 seconds)
Published: Fri May 13 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.