Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GSoC 2019 | OpenCV | Adding a CUDA backend to the DNN module

DISCLAIMER

This gist documents the Google Summer of Code project. It is not updated and hence does not indicate current status of the CUDA backend.

For updated details, please see this gist.

Allow the OpenCV's DNN module to work with GPUs

Student: Yashas Samaga B L

Mentor: Davis King

Project Link: https://summerofcode.withgoogle.com/projects/#6021087400296448

Relevant PRs:

Introduction

The OpenCV’s DNN module has a blazing fast inference capability on CPUs. It supports performing inference on GPUs using OpenCL but lacks a CUDA backend. NVIDIA’s GPUs support OpenCL, but their capabilities are limited by OpenCL.

This project adds a new CUDA backend that can perform lightning fast inference on NVIDIA GPUs.

How to use?

Build

The CUDA backend requires CUDA Toolkit and cuDNN (min: 7.5.0) to be installed on the system. The CMake scripts will automatically detect the dependencies when the following options are set:

  • WITH_CUDA
  • WITH_CUDNN

The CUDA backend is enabled by setting the following option:

  • OPENCV_DNN_CUDA

After building, run [build dir]/bin/opencv_test_dnn and [build dir]/bin/opencv_perf_dnn.

Usage

The project adds the following new backends and targets to the existing list.

Backend Target
DNN_BACKEND_CUDA DNN_TARGET_CUDA
DNN_BACKEND_CUDA DNN_TARGET_CUDA_FP16

Support Matrix

The CUDA backend uses OpenCV's CPU backend as a fallback for unsupported layers and partially supported layers with unsupported configurations.

Blip Meaning
✔️ fully supported
🔵 partially supported
unsupported
Layer Status
Activations ✔️
Batch Normalization ✔️
Blank Layer ✔️
Concat Layer ✔️
Const Layer ✔️
Convolution 2d ✔️
Convolution 3d ✔️
Crop and resize
Crop Layer ✔️
Detection Output Layer
Deconvolution 2d 🔵 (most configurations supported)
Deconvolution 3d 🔵 (most configurations supported)
Elementwise Layers ✔️
Eltwise Layer ✔️
Flatten Layer ✔️
Fully Connected Layer ✔️
Input Layer
Interp Layer ✔️
Local Response Normalization ✔️
Max Unpooling 2d ✔️
Max Unpooling 3d ✔️
MVN Layer
Normalize Layer 🔵 (L1 and L2 supported)
Padding Layer ✔️
Permute Layer ✔️
Pooling 2d 🔵 (max and average supported)
Pooling 3d 🔵 (max and average supported)
Prior Box Layer ✔️
Proposal Layer
Region Layer ✔️
Reorg Layer ✔️
Reshape Layer ✔️
Resize Layer ✔️
Scale Layer ✔️
Shift Layer ✔️
Shuffle Channel Layer ✔️
Slice Layer ✔️
Softmax Layer ✔️
Split Layer ✔️
LSTM Layer

OCV CPU vs IE CPU vs CUDA

CPU: i7 7700HQ

GPU: NVIDIA GTX 1050 Mobile

CPU BLAS Library: MKL 2019.0.4

CUDA Version: 10.1

cuDNN: 7.6.2

Warmup Runs: 3 (forward pass is performed three times before benchmarks)

Benchmark Runs: 10 (the average of ten forward passes is reported)

Test Code: https://gist.github.com/YashasSamaga/71157cf0c3768c497e5e70fb95435596

Batch Size = 1

Model CUDA FP32 Inference Engine CPU OpenCV CPU
GoogLeNet 7.2447ms 10.4981ms 17.9176ms
DenseNet121 12.6324ms 19.1823ms 48.0628ms
EAST Text Detection 18.8281ms 49.0508ms 88.9429ms
ENet 11.5014ms Exception 62.5854ms
FastNeuralStyle StaryNight 27.498ms 178.309ms 160.359ms
Inception 5h 7.8546ms 22.2789ms 20.3255ms
Inception v2 FasterRCNN 112.736ms Exception 374.26ms
MobileNet SSD 58.4751ms 9.2896ms 27.3061ms
OpenCV Face Detector 6.9831ms 8.3981ms 17.6683ms
OpenPose Pose MPI 160.561ms 509.446ms 838.161ms
Resnet 50 11.3603ms 28.1529ms 50.2752ms
SqueezeNet 2.4084ms 3.2918ms 5.476ms
VGG16 SSD 70.4117ms 249.725ms 360.207ms
Yolo v3 57.9822ms 214.629ms 296.806ms
Yolo v2 51.5784ms 193.453ms 260.19ms

Batch Size = 10

Model CUDA FP32 Inference Engine CPU OpenCV CPU
GoogLeNet 35.7556ms 108.946ms 225.928ms
DenseNet121 74.9241ms 295.105ms 650.924ms
EAST Text Detection 149.58ms 536.946ms 1273.93ms
FastNeuralStyle StaryNight 283.173ms 1966.5ms 2175.3ms
Inception 5h 36.6225ms 180.429ms 233.276ms
MobileNet SSD 277.753ms 111.872ms 316.063ms
OpenCV Face Detector 52.4366ms 95.7866ms 202.657ms
OpenPose Pose MPI 628.617ms 5650.05ms 10683.5ms
Resnet 50 74.283ms 230.817ms 541.308ms
SqueezeNet 15.8144ms 35.4915ms 69.4122ms
VGG16 SSD 594.286ms 2796.23ms 4661.51ms
Yolo v3 488.704ms 2419.8ms 4209.74ms
Yolo v2 491.414ms 2185.47ms 3788.34ms

OpenCV CUDA vs OpenCV CPU

CPU: 2x Intel Xeon E5-2640 v4

GPU: 1x NVIDIA GTX 1080 Ti (11 GB)

CPU BLAS Library: OpenBLAS 0.2.20

CUDA Version: 10.0

cuDNN: 7.6.2

Warmup Runs: 3 (forward pass is performed three times before benchmarks)

Benchmark Runs: 10 (the average of ten forward passes is reported)

Test Code: https://gist.github.com/YashasSamaga/71157cf0c3768c497e5e70fb95435596

Backend Comparision

Batch Size = 1

Model CUDA FP32 OpenCV CPU
GoogLeNet 4.8824ms 14.2981ms
DenseNet121 6.4555ms 57.8244ms
EAST Text Detection 5.901ms 67.4301ms
ENet 4.5979ms 30.2767ms
FastNeuralStyle StaryNight 5.3193ms 51.3313ms
Inception 5h 4.9487ms 16.0048ms
Inception v2 FasterRCNN 82.0298ms 179.245ms
MobileNet SSD 70.9177ms 23.9348ms
OpenCV Face Detector 4.9288ms 15.4205ms
OpenPose Pose MPI 30.5954ms 246.747ms
Resnet 50 4.5968ms 45.1153ms
SqueezeNet 1.0888ms 3.6492ms
VGG16 SSD 23.5926ms 194.976ms
Yolo v3 18.0002ms 141.861ms
Yolo v2 12.1279ms 111.642ms

Batch Size = 10

Model CUDA FP32 OpenCV CPU
GoogLeNet 10.149ms 75.9591ms
DenseNet121 20.269ms 312.426ms
EAST Text Detection 32.1556ms 402.16ms
FastNeuralStyle StaryNight 49.1025ms 461.095ms
Inception 5h 9.9721ms 67.9308ms
MobileNet SSD 96.2898ms 110.783ms
OpenCV Face Detector 22.7501ms 77.8742ms
OpenPose Pose MPI 118.858ms 2321.89ms
Resnet 50 18.4139ms 229.599ms
SqueezeNet 4.4893ms 22.3049ms
VGG16 SSD 194.181ms 1319.67ms
Yolo v3 122.603ms 1044.11ms
Yolo v2 104.072ms 819.177ms

Batch Size = 128

Model CUDA FP32 OpenCV CPU
GoogLeNet 90.3755ms 775.769ms
DenseNet121 199.516ms 3536.38ms
EAST Text Detection 376.458ms 7685.72ms
FastNeuralStyle StaryNight 801.778ms 6607.15ms
Inception 5h 93.4188ms 771.575ms
MobileNet SSD 1028.93ms 1110.37ms
OpenCV Face Detector 276.992ms 977.997ms
OpenPose Pose MPI 1279.26ms 32159.3ms
Resnet 50 200.789ms 1719.92ms
SqueezeNet 55.6244ms 255.397ms
VGG16 SSD 2969.05ms 17201ms
Yolo v3 1564.78ms 13699.2ms
Yolo v2 1362.84ms 11254.9ms

Images processed per second (CUDA FP32)

Model batch size = 1 batch size = 10 batch size = 128
GoogLeNet 204 985 1416
DenseNet121 154 493 641
EAST Text Detection 169 311 340
ENet 217 Not Applicable Not Applicable
FastNeuralStyle StaryNight 188 204 160
Inception 5h 202 1002 1370
Inception v2 FasterRCNN 12 Not Aplicable Not Applicable
MobileNet SSD 14 104 124
OpenCV Face Detector 202 440 462
OpenPose Pose MPI 33 84 100
Resnet 50 217 540 637
SqueezeNet 918 2228 2301
VGG16 SSD 42 52 43
Yolo v3 55 82 81
Yolo v2 82 96 93

OpenCV CUDA vs TensorFlow

GPU: NVIDIA GTX 1080 Ti (11 GB)

Batch of 1

Model OpenCV CUDA TensorFlow
ResNet-50 4.5968ms 7.1163ms
EAST Text Detection 5.901ms 8.6890ms

Batch of 10

Model OpenCV CUDA TensorFlow
ResNet-50 18.4139ms 22.3665ms
EAST Text Detection 32.1556ms 39.4857ms

Batch of 128

Model OpenCV CUDA TensorFlow
ResNet-50 200.789ms 216.3923ms
EAST Text Detection 376.458ms 421.8292ms
@Saafke

This comment has been minimized.

Copy link

Saafke commented Aug 25, 2019

Amazing work! 🙌🎉

@Avrohom

This comment has been minimized.

Copy link

Avrohom commented Sep 12, 2019

Hi @YashasSamaga,

Can you compile it in Windows 10?

I have tried to compile your Master branch. And I am getting errors:

image

image

@YashasSamaga

This comment has been minimized.

Copy link
Owner Author

YashasSamaga commented Sep 12, 2019

@Avrohom Please use opencv/opencv#14827 for further discussions.

This is an issue with CMake. You have to update CUDA_ARCH_BIN option. I had pushed a commit to throw a cmake error for unsupported configuration. Updating your clone might help.

@stephenvidler

This comment has been minimized.

Copy link

stephenvidler commented Oct 2, 2019

I've been trying to compile on Windows 10 with VS2015 but get the following errors when compiling activations.cu:

error C2912: explicit specialization 'unsigned int cv::dnn::cuda4dnn::csl::device::detail::getGridDim<0>(void)' is not a specialization of a function template

Any suggestions?

@YashasSamaga

This comment has been minimized.

Copy link
Owner Author

YashasSamaga commented Oct 2, 2019

@stephenvidler Please use opencv/opencv#14827 for further discussions.

At first sight, it appears like an MSVC bug. The identified function is a function template which is contradictory to what the error reports. Somebody watching the PR might know more about this.

@kuronekodaisuki

This comment has been minimized.

Copy link

kuronekodaisuki commented Oct 21, 2019

Great works!

@Hastyrush

This comment has been minimized.

Copy link

Hastyrush commented Oct 25, 2019

Nice work!

Any idea on why the MobileNet SSD pales in comparison drastically on the CUDA backend as compared to the Inference Engine or the CPU? I presume it has something to do with depthwise separable convolution layers not being supported at the moment?

Thanks a lot!

@YashasSamaga

This comment has been minimized.

Copy link
Owner Author

YashasSamaga commented Oct 25, 2019

@Hastyrush cuDNN performs depthwise convolutions poorly.

More information is available here: opencv/opencv#14827 (comment)

@phil-ludewig

This comment has been minimized.

Copy link

phil-ludewig commented Nov 27, 2019

Thanks for your work! How would I implement it with opencv 4.1.1? It's not merged with that branch yet.

@YashasSamaga

This comment has been minimized.

Copy link
Owner Author

YashasSamaga commented Nov 27, 2019

@phil-ludewig I'm sorry, you can't. You need to use the master branch.

@lorenzolightsgdwarf

This comment has been minimized.

Copy link

lorenzolightsgdwarf commented Dec 3, 2019

@YashasSamaga thank you for your work. Unfortunately I cannot reproduce the benchmark results on mobile net ssd v2 my machine.
The avg inference time is 180ms on a a batch size 1.
I'm using Cuda 10.2, cuDNN 7.6.3, NVIDIA GTX 1050, CPU i7-7700HQ.
Using TensorFlow Cpp library with CUDA the inference time is only 30 ms.
Do you have any suggestion?
This is my code for the benchmark

#include <iostream>
#include <opencv2/dnn.hpp>

using namespace std;

int main() {
    std::vector<cv::Mat> images;
    cv::Mat image(cv::Size(300, 300), CV_8UC3);
    cv::randu(image, cv::Scalar(0, 0, 0), cv::Scalar(255, 255, 255));
    images.push_back(image);
    cv::Mat blob = cv::dnn::blobFromImages(images, 1.0f, cv::Size(300, 300), 0.0f);
    auto net = cv::dnn::readNet("frozen_inference_graph.pb", "ssd_mobilenet_v2_coco_2018_03_29.pbtxt");
    net.setPreferableBackend(cv::dnn::Backend::DNN_BACKEND_CUDA);
    net.setPreferableTarget(cv::dnn::Target::DNN_TARGET_CUDA);
    // Warm up
    for (int i = 0; i < 5; i++) {
        net.setInput(blob);
        net.forward();
    }
    for (int i = 0; i < 10; i++) {
        cv::TickMeter tic;
        tic.start();
        net.setInput(blob);
        net.forward();
        tic.stop();
        std::cout << tic.getTimeMilli() << std::endl;
    }

    return 0;
}

My cmake configuration

General configuration for OpenCV 4.1.2-dev =====================================
  Version control:               4.1.2-238-g78c5e41c23

  Extra modules:
    Location (extra):            C:/Users/Lorenzo Lucignano/opencv_contrib/modules
    Version control (extra):     4.1.2-65-g763a4516

  Platform:
    Timestamp:                   2019-12-02T11:02:35Z
    Host:                        Windows 10.0.18362 AMD64
    CMake:                       3.15.3
    CMake generator:             Visual Studio 15 2017
    CMake build tool:            C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/MSBuild/15.0/Bin/MSBuild.exe
    MSVC:                        1916

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (13 files):         + SSSE3 SSE4_1
      SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (0 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (4 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (26 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
      AVX512_SKX (3 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX

  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe  (ver 19.16.27026.1)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP8   /MD /O2 /Ob2 /DNDEBUG 
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP8   /MDd /Zi /Ob0 /Od /RTC1 
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise       /MP8    /MD /O2 /Ob2 /DNDEBUG 
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise       /MP8  /MDd /Zi /Ob0 /Od /RTC1 
    Linker flags (Release):      /machine:x64  /INCREMENTAL:NO 
    Linker flags (Debug):        /machine:x64  /debug /INCREMENTAL 
    ccache:                      NO
    Precompiled headers:         YES
    Extra dependencies:          cudart_static.lib nppc.lib nppial.lib nppicc.lib nppicom.lib nppidei.lib nppif.lib nppig.lib nppim.lib nppist.lib nppisu.lib nppitc.lib npps.lib cublas.lib cudnn.lib cufft.lib -LIBPATH:C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/lib/x64 -LIBPATH:C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.0/lib/x64
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 calib3d core cudev dnn features2d flann highgui imgcodecs imgproc ml objdetect photo stitching ts video videoio
    Disabled:                    aruco bgsegm bioinspired ccalib cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping datasets dnn_objdetect dnn_superres dpm face fuzzy gapi hfs img_hash line_descriptor optflow phase_unwrapping plot python3 quality reg rgbd saliency shape stereo structured_light superres surface_matching text tracking videostab world xfeatures2d ximgproc xobjdetect xphoto
    Disabled by dependency:      -
    Unavailable:                 cnn_3dobj cvv freetype hdf java js matlab ovis python2 sfm viz
    Applications:                perf_tests
    Documentation:               NO
    Non-free algorithms:         NO

  Windows RT support:            NO

  GUI: 
    Win32 UI:                    YES
    VTK support:                 NO

  Media I/O: 
    ZLib:                        build (ver 1.2.11)
    JPEG:                        build-libjpeg-turbo (ver 2.0.2-62)
    WEBP:                        build (ver encoder: 0x020e)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.0.10)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (58.54.100)
      avformat:                  YES (58.29.100)
      avutil:                    YES (56.31.100)
      swscale:                   YES (5.5.100)
      avresample:                YES (4.0.0)
    GStreamer:                   NO
    DirectShow:                  YES
    Media Foundation:            YES
      DXVA:                      YES

  Parallel framework:            TBB (ver 2019.0 interface 11008)

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Intel IPP:                   2019.0.0 Gold [2019.0.0]
           at:                   C:/Users/Lorenzo Lucignano/opencv/build/3rdparty/ippicv/ippicv_win/icv
    Intel IPP IW:                sources (2019.0.0)
              at:                C:/Users/Lorenzo Lucignano/opencv/build/3rdparty/ippicv/ippicv_win/iw
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.5.1)

  NVIDIA CUDA:                   YES (ver 10.2, CUFFT CUBLAS)
    NVIDIA GPU arch:             53 60 61 70 75
    NVIDIA PTX archs:

  cuDNN:                         YES (ver 7.6.3)

  OpenCL:                        YES (NVD3D11)
    Include path:                C:/Users/Lorenzo Lucignano/opencv/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python (for build):            C:/Python27/python.exe

  Java:                          
    ant:                         NO
    JNI:                         C:/Program Files/Java/jdk1.8.0_211/include C:/Program Files/Java/jdk1.8.0_211/include/win32 C:/Program Files/Java/jdk1.8.0_211/include
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    C:/Users/Lorenzo Lucignano/opencv/build/install
-----------------------------------------------------------------
@YashasSamaga

This comment has been minimized.

Copy link
Owner Author

YashasSamaga commented Dec 3, 2019

The benchmarks I posted are for MobileNetSSD_deploy.prototxt/MobileNetSSD_deploy.caffemodel which you can find here.

MobileNet is slow with the CUDA backend because of depthwise convolutions. The CUDA backend fully relies on cuDNN for convolutions and cuDNN is very bad at depthwise convolutions.

@JulienMaille

This comment has been minimized.

Copy link

JulienMaille commented Dec 11, 2019

Hi I spent quite some time trying to build a project hoping to run my network on gpu but noticed only today that "CUDA backend for DNN module requires CC 5.3 or higher". Unfortunately I only have a Geforce 970
What is the main reason for this limitation?
Do I need to integrate Nvidia SDK if I want my model to run on older GPUs?

@YashasSamaga

This comment has been minimized.

Copy link
Owner Author

YashasSamaga commented Dec 12, 2019

@neFAST

The backend uses half-precision intrinsics which isn't available on older GPUs. Currently, there is no way to disable the half-precision target even if you won't be using it. I'll see if it can be made a build option.

@JulienMaille

This comment has been minimized.

Copy link

JulienMaille commented Dec 12, 2019

Thanks for the feedback. Supporting older Gpus would be a nice addition, even if performances are worse because it runs at full precision.

@YashasSamaga

This comment has been minimized.

Copy link
Owner Author

YashasSamaga commented Dec 21, 2019

@neFAST The vast bulk of changes would be required for files in dnn/src/cuda. The explicit template instantiations for the __half type would have to be disabled via a #ifdef. The overloads in dnn/src/cuda/math.hpp would also have to be dealt with. Some checks in dnn.cpp to identify use of DNN_TARGET_CUDA_FP16 when half precision is disabled.

@JulienMaille

This comment has been minimized.

Copy link

JulienMaille commented Dec 21, 2019

@YashasSamaga
I've commited this: JulienMaille/opencv@2622a56
It compiles and I was able to run some inference on my Geforce 960!

I'll clean this up in the next days, any advice from you will be appreciated

Right now I limited support to CC 5.2+ but I suppose we can go lower

@Rasoul20sh

This comment has been minimized.

Copy link

Rasoul20sh commented Dec 23, 2019

I've been trying to compile on Windows 10 with VS2015 but get the following errors when compiling activations.cu:

error C2912: explicit specialization 'unsigned int cv::dnn::cuda4dnn::csl::device::detail::getGridDim<0>(void)' is not a specialization of a function template

Any suggestions?

I have the same error. Did you solve it?

@YashasSamaga

This comment has been minimized.

Copy link
Owner Author

YashasSamaga commented Dec 23, 2019

@Rasoul20sh I think this issue is specific to older MSVC compilers. Can you try upgrading to a newer compiler? It builds without errors on VS17 and VS19.

I have got a reproducer: https://godbolt.org/z/CvttCb

I have tried messing around with the code in godbolt. I am not able to find a working solution to get around the bug.

@stephenvidler

This comment has been minimized.

Copy link

stephenvidler commented Dec 23, 2019

@dsgh2

This comment has been minimized.

Copy link

dsgh2 commented Dec 24, 2019

The template declarations were using C++ features not supported by the 2015 compiler. So I rewrote them to be simpler as they were only used in a couple of places. I will send you the changes later today,, when I can access my computer. Regards, Steve

On Mon, 23 Dec 2019, 5:57 a.m. Yashas Samaga B L, @.***> wrote: @Rasoul20sh https://github.com/Rasoul20sh I think this issue is specific to older MSVC compilers. Can you try upgrading to a newer compiler? It builds without errors on VS17 and VS19. I have got a reproducer: https://godbolt.org/z/CvttCb I have tried messing around with the code in godbolt. I am not able to find a working solution to get around the bug. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://gist.github.com/a84cf2826ab2dc755005321fe17cd15d?email_source=notifications&email_token=AF62OBOH3XF7Y56SLJ27U43Q2BHMXA5CNFSM4IPILTCKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAF6LS6#gistcomment-3118895, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF62OBLJEX375DBGJ7DY6TDQ2BHMXANCNFSM4IPILTCA .

Hi,
I have same error.
What should I change in source code? Please let me know the way.

I guess, in grid_stride_range.hpp
template device auto getGridDim()->decltype(dim3::x);
template <> inline device auto getGridDim<0>()->decltype(dim3::x) { return gridDim.x; }
template <> inline device auto getGridDim<1>()->decltype(dim3::x) { return gridDim.y; }
template <> inline device auto getGridDim<2>()->decltype(dim3::x) { return gridDim.z; }

should be changed. but I do not know how to change.
Thanks in advance

@YashasSamaga

This comment has been minimized.

Copy link
Owner Author

YashasSamaga commented Dec 24, 2019

#include <cstddef>
#include <utility>

struct dim3 {
    unsigned x, y, z;
};

using ret_type = decltype(dim3::x);

template <int>
ret_type getGridDim();

template <> inline ret_type getGridDim<0>() { return 0; }

solves the problem

@YashasSamaga

This comment has been minimized.

Copy link
Owner Author

YashasSamaga commented Dec 24, 2019

@dsgh2 @Rasoul20sh Can you try building this branch with a fix?

@dsgh2

This comment has been minimized.

Copy link

dsgh2 commented Dec 24, 2019

Thanks, I will try it

@stephenvidler

This comment has been minimized.

Copy link

stephenvidler commented Dec 24, 2019

@Rasoul20sh

This comment has been minimized.

Copy link

Rasoul20sh commented Dec 24, 2019

@dsgh2 @Rasoul20sh Can you try building this branch with a fix?

Yes, Of course. I was building it using visual studio 2019. But with that version I faced with this kind of Errors:
#error: -- unsupported Microsoft Visual Studio version! Only the versions between 2013 and 2017 (inclusive) are supported! opencv_cudaarithm C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\include\crt\host_config.h 143

@dsgh2

This comment has been minimized.

Copy link

dsgh2 commented Dec 24, 2019

@dsgh2 @Rasoul20sh Can you try building this branch with a fix?
C2912 'unsigned int cv::dnn::cuda4dnn::csl::device::detail::getThreadIdx<2>(void) opencv_dnn d:__dev_data\opencv420\source\modules\dnn\src\cuda\grid_stride_range.hpp 33

It still shows above error.

@dsgh2

This comment has been minimized.

Copy link

dsgh2 commented Dec 26, 2019

template <> inline device auto

This works, Thank you!

@YashasSamaga

This comment has been minimized.

Copy link
Owner Author

YashasSamaga commented Dec 26, 2019

@dsgh2 Sorry, I forgot to reply back. I don't really know why that change works. Can you confirm if you are able to use the CUDA backend?

@Rasoul20sh The only CUDA module that the DNN module requires is cudev (in fact, it's a false dependency because of the current CMake scripts). If you do not need other CUDA modules, like opencv_cudaarithm which is causing the error, remove them from the build.

@dsgh2

This comment has been minimized.

Copy link

dsgh2 commented Dec 27, 2019

I have applied stephenvidler's solution on your branch.
I have just finished my build and that should be confirmed as you told.

FYI, I have seen error in debug mode whereas release mode was built well.
The problem has been solved by changing to INCREMENTAL BUILDING to NO

@YashasSamaga

This comment has been minimized.

Copy link
Owner Author

YashasSamaga commented Dec 31, 2019

@dsgh2 @Rasoul20sh @stephenvidler FYI: The build issues (related to VS15 and older versions of MSVC) have been fixed on master.

@Rasoul20sh

This comment has been minimized.

Copy link

Rasoul20sh commented Jan 22, 2020

@YashasSamaga Thanks, it is fixed. Do you know that this implementation can support unet? I have an error when I want to set input to an unet structure.

@davisking

This comment has been minimized.

Copy link

davisking commented Jan 25, 2020

@YashasSamaga, out of curiosity, what version of TF was used for these benchmarks and was XLA turned on? Supposedly XLA makes TF run a lot faster (see https://medium.com/tensorflow/pushing-the-limits-of-gpu-performance-with-xla-53559db8e473) but I'm a little skeptical and have seen more mixed results than that medium post would suggest.

@YashasSamaga

This comment has been minimized.

Copy link
Owner Author

YashasSamaga commented Jan 25, 2020

@davisking If I remember correctly, it was Tensorflow 1.14. XLA wasn't turned on for the benchmark. I tried to benchmark with XLA enabled (using XLA_GPU as device) but kept getting CUDNN_STATUS_INTERNAL_ERROR.

@davisking

This comment has been minimized.

Copy link

davisking commented Jan 27, 2020

Not super surprising. XLA has some funky requirements it seems. Thanks :)

@eyebies

This comment has been minimized.

Copy link

eyebies commented Feb 12, 2020

@YashasSamaga Does dnn yolo support custom anchors, classes? I seem to have parsing issues?

@YashasSamaga

This comment has been minimized.

Copy link
Owner Author

YashasSamaga commented Feb 13, 2020

@YashasSamaga Yes, if the OpenCV CPU backend can run your model, it'll most likely work with the CUDA backend as well. If are unable to run/import your model into OpenCV DNN, you might need to make an issue requesting for a feature.

@isra60

This comment has been minimized.

Copy link

isra60 commented Feb 13, 2020

Hi @YashasSamaga , can you take a look to this?? opencv/opencv#16420
There seems to be something wrong with the detections mat

@YashasSamaga

This comment has been minimized.

Copy link
Owner Author

YashasSamaga commented Feb 13, 2020

@isra60

csresnext50-panet-spp-original-optimal.cfg requires components such as grouped convolutions which are not yet supported in the darknet importer. Hence, even though the network is imported successfully, the outputs are not correct.

enet-coco.cfg requires PR16436 which I will mostly finish off this weekend.

lp-recognition.cfg uses some options in region block (bias_match and jitter) which don't seem to be handled by the darknet importer. I'm not sure what these are.

@whittenator

This comment has been minimized.

Copy link

whittenator commented Mar 9, 2020

Good Morning,

I have everything set up and working great! Fantastic work! My system has 2 GPUs, is there a way to specify which one to use?

Thanks

@YashasSamaga

This comment has been minimized.

Copy link
Owner Author

YashasSamaga commented Mar 9, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.