Build/Compile OpenCV v3.3 on Windows with CUDA 8.0, Intel MKL+TBB and python bindings

Posted on Posted in CUDA, OpenCV

 

 
Because the pre-built Windows libraries available for OpenCV v3.3 do not include the CUDA modules, I have included the build instructions, which are almost identical to those for OpenCV v3.2, below for anyone who is interested. If you just need the Windows libraries then see Download OpenCV 3.3 with Cuda 8.0.

The guide below details instructions on compiling the 64 bit version of OpenCV v3.3 shared libraries with Visual Studio 2013 (will also work with Visual Studio 2015 if selected in CMake), CUDA 8.0, support for both the Intel Math Kernel Libraries (MKL) and Intel Threaded Building Blocks (TBB), and bindings to allow you to call OpenCV functions from within python.

Note: The procedure outlined will not work for Visual Studio 2017 because this is not supported by the CUDA 8.0 Toolkit.

For Visual Studio 2013 and 2015 you first need to:

  • Download the source files, available on GitHub. Either clone the git repo making sure to checkout the 3.3.0 tag or download this archive containing all the source file.
  • Install CMake – Version 3.7.1 is used in the guide.
  • Install The CUDA 8.0 Toolkit (v8.0.61) and Patch2.
  • Optional – Install both the Intel MKL and TBB by registering for community licensing, and downloading for free.
  • Optional – Install the x64 bit version of Anaconda2 and/or Anaconda3 to use OpenCV with Python 2 and/or Python 3, making sure to tick “Register Anaconda as my default Python ..”
     

     

 

Building OpenCV v3.3 with CUDA 8.0
  1. Fire up CMake, making sure that the Grouped checkbox is ticked, select the location of the source files, downloaded from GitHub, and the location where the build will take place, E:/opencv/ and E:/build/opencv/vs2013/x64/cuda_mkl/ in this example.
     

     
  2. Skip if you are not including the Intel performance libraries. The CMake script needs to be amended to enable it to correctly locate the Intel MKL and TBB libraries. To do this open up OPENCV_SOURCE/cmake/OpenCVFindMKL.cmake (where OPENCV_SOURCE is E:/opencv/ in this example), comment out lines 55 and 63 (shown below), and save.
     

    #if(WITH_MKL AND NOT mkl_root_paths)
      if(WIN32)
          set(ProgramFilesx86 "ProgramFiles(x86)")
          list(APPEND mkl_root_paths $ENV{${ProgramFilesx86}}/IntelSWTools/compilers_and_libraries/windows/mkl)
      endif()
      if(UNIX)
        list(APPEND mkl_root_paths "/opt/intel/mkl")
      endif()
    #endif()

     

  3. Click the Configure button and select Visual Studio 2013 Win64 (32 bit CUDA support is limited). This may take a while as CMake will download ffmpeg and the Intel Integrated Performance Primitives for Image processing and Computer Vision (IPP-ICV).
     

     
  4. Skip if you are not including the Intel performance libraries. If MKL and TBB are installed correctly the path to these should have been picked up in CMake (see below), all that is required is to enable TBB by ticking MKL_WITH_TBB.
     

     
    Then press configure again and verify your output resembles the below.
     

     
  5. Expand the BUILD group and tick BUILD_opencv_world (builds to a single dll).
  6. Expand the CUDA tab, the CUDA_TOOLKIT_ROOT_DIR should point to your CUDA 8.0 toolkit installation, if you have more than one version of the toolkit installed and it has picked that one then simply change the path to point to CUDA 8.0.

    The default CUDA_ARCH_BIN option is to build microcode for all architectures from 2.0-6.1 (FermiPascal). This setting results in a large build time (~3.5hours on an i7) but the binaries produced will run on all supported devices. If you only want to execute OpenCV on a specific device then only enter the compute capability of that device here, remember that this the produced libraries are not guaranteed to run on any device’s of a different major compute version to the ones entered, see the CUDA C Programming Guide for details.

    If you are comfortable with the implications, you can also enable CUDA_FAST_MATH which will enable the –use_fast_math compiler option, again see CUDA C Programming Guide for details.
     

     

  7. Expand WITH and enable WITH_CUBLAS to enable the CUDA Basic Linear Algebra Subroutines (cuBLAS).
     

     
  8. Skip if you are not including the Python bindings. If you have installed only one version of Anaconda, then CMake should pick up its location (as long as you ticked “Register Anaconda as my default Python” on installation) and already ticked the correct build option (BUILD_opencv_python2[3]). However, if you are building for both Python 2 and 3, you will have to manually enter in the locations for Anaconda3 as below.
     

     
    Then once you press configure again, both build options will be selected.
     

     
  9. Press Configure again, your CUDA options should resemble the below.
     

     
    There should be no warning messages in red displayed in the configuration window. If there are then the Visual Studio solution may be generated but it it will probably fail to build.

    Note: More recent versions of CMake, than the v3.7.1, may give warnings resembling the below:
     

     
    These can be safely ignored.

  10. Press Generate and wait until the bottom of the window indicates success.
     

     
  11. Press Open Project (not available in older versions of CMake, for those just locate and open the Visual Studio solution file) to open up the solution in Visual Studio.
     

     
  12. Click Solution Explorer, expand CMakeTargets, right click on INSTALL and click Build. This will both build the library and copy the necessary redistributable parts to the install directory, E:/build/opencv/vs2013/x64/cuda_mkl/install in this example. Additionally if you build the python bindings then the cv2.pyd and/or cv2.cp36-win_amd64.pyd shared libs will have been copied to your python Anaconda2[3]\Lib\site-packages\ directory.

    If everything was successful, congratulations, you now have OpenCV v3.3 built with CUDA 8.0.
     

     

  13. NOTE: If you change remove any options after pressing Configure a second time, the build may fail, it is best to remove build directory and start again. This may seem over cautions but it is preferable to waiting for an hour for the build to fail and then starting again.

9 thoughts on “Build/Compile OpenCV v3.3 on Windows with CUDA 8.0, Intel MKL+TBB and python bindings

  1. I did all the steps and it got correctly installed with MKL and CUDA.
    Thank you for that.
    Now I want to import it into a python program.
    What do I import?

    1. I have updated the guide to include building the python bindings.

      If OpenCV has been built with the python bindings then on the your build machine the cv2.pyd and/or cv2.cp36-win_amd64.pyd shared libs should have been copied to the Anaconda2[3]\Lib\site-packages\ directory. If not you need to copy them to that directory on the machine you are using. They should be located in the build\lib directory, e.g. E:/build/opencv/vs2013/x64/cuda_mkl/lib/.

      Therefore to use OpenCV with python just fire up Anaconda Prompt, navigate to the directory containing opencv_world330.dll, e.g. E:/build/opencv/vs2013/x64/cuda_mkl/install/x64/vc12/bin. Start the python interpreter (type: python), then in the interpreter type import cv2. If this is successful then you can use python’s OpenCV bindings. If that works then, add the location of opencv_world330.dll to your system path.

      That said, I am pretty sure that there are no python bindings to the CUDA functions (https://stackoverflow.com/questions/42125084/accessing-opencv-cuda-functions-from-python-no-pycuda).

      Depending on what algorithms you want to accelerate in python, you may be able to use pytorch (if you have conda it can easily be installed with: conda install -c peterjc123 pytorch=0.1.12).

  2. Thanks for the detailed reply. I will try it out.
    But I am having some problem with the build phase in Visual Studio. It has been going on for hours and its stuck at 22%. It’s working with all the CUDA libraries (matmul.h, add.h). And I also get a lot of warnings about deprecated architectures (sm-20). Any chance I can speed up the build. For now, I deleted the directory and am starting again from the Cmake step.
    P.S. I am new to OpenCV and CUDA .

  3. It takes approximately 3.5 hours on a modern intel i7, the CUDA compiler performs a significant amount of optimization while compiling, hence the wait. Warnings regarding sm-20 are fine, as long as you are not getting any errors I would keep waiting.

  4. Hi James

    I am getting the following errors on building OpenCV.
    Severity Code Description Project File Line Suppression State
    Error C2535 ‘std::tuple &std::tuple::operator =(const std::tuple &)’: member function already defined or declared (compiling source file C:\Users\Anuvrat Tiku\Desktop\opencv\sources\modules\cudawarping\perf\perf_warping.cpp) opencv_perf_cudawarping C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\tuple 756

    Severity Code Description Project File Line Suppression State
    Error C2382 ‘std::tuple::operator =’: redefinition; different exception specifications (compiling source file C:\Users\Anuvrat Tiku\Desktop\opencv\sources\modules\cudawarping\perf\perf_warping.cpp) opencv_perf_cudawarping C:\Users\Anuvrat Tiku\Desktop\opencv\sources\modules\ts\include\opencv2\ts\cuda_perf.hpp 73

    Severity Code Description Project File Line Suppression State
    Error C2610 ‘std::tuple::tuple(const std::tuple &)’: is not a special member function which can be defaulted (compiling source file C:\Users\Anuvrat Tiku\Desktop\opencv\sources\modules\cudawarping\perf\perf_warping.cpp) opencv_perf_cudawarping C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\tuple 607

    and 5 more like this, 8 in total. There is no solution online to stop these errors.
    Can you help

    Thanks
    Anuvrat

    1. Hi, this looks like a historic bug with OpenCV, are you compiling version 3.3? Which version of VS2015 are you using, update 3?

  5. Hey James,

    Yes, it is the update 3 for VS community 2015.
    Now the build has 28 errors. Will this affect OpenCV in any way ?

    1. If the errors are just in the performance tests, then the OpenCv libs should have already compiled correctly. Check the bin folder, if opencv_world330.dll is present then you should be able to ignore the warnings.
      Are you certain that you have checked out the 3.3.0 tag and you are not building and earlier version of OpenCv?

  6. I downloaded the 3.3.0 executable from Github. The project is still building, cant find opencv_world330.dll in the path. If I build again without the performance tests, would the errors go away. Is there any catch if I build without performance tests.

Leave a Reply

Your email address will not be published. Required fields are marked *