GoLang and OpenCV (OpenVino && Cuda)

Good day to all. There are already a lot of articles on working with OpenCV on Go on the hub ( and indeed on the Internet ).

The finished code is certainly interesting, but more detailed information about installing drivers has to be collected in pieces - I will try to combine all the necessary gestures into one article.

image


I have a laptop with Ubuntu 18.04 on board,

  • CPU: Intel
  • GPU: Intel / Nvidia

Nvidia and Intel are trying to beat each other, and I will try to take advantage of OpenVino and Cuda at the same time.
I warn you right away, to use Cuda you need a minimum Compute capability (version) 5.3, you can see for your video card here

Cuda


I chose Cuda 10.0 so I can use Tensorflow as well.

First you need to download a package with developer.nvidia.com (such as the runtime (local) )

image

Set command

sudo sh cuda_10.0.130_410.48_linux.run


cudNN


Link to packages
You need to select the version that matches Cuda, that is
Download cuDNN v7.5.0 (Feb 21, 2019), for CUDA 10.0

image

And download two deb packages
cuDNN Developer Library for Ubuntu18.04 (Deb)
cuDNN Developer Library for Ubuntu18.04 (Deb)

If you do not have an account https://developer.nvidia.com/ , you will be prompted to register

image

Installation:

sudo dpkg -i libcudnn7-dev_7.5.0.56-1+cuda10.0_amd64.deb
sudo dpkg -i libcudnn7_7.5.0.56-1+cuda10.0_amd64.deb

Intel OpenCL Driver


Link to the repository .

Installation:

wget https://github.com/intel/compute-runtime/releases/download/20.04.15428/intel-gmmlib_19.4.1_amd64.deb
wget https://github.com/intel/compute-runtime/releases/download/20.04.15428/intel-igc-core_1.0.3151_amd64.deb
wget https://github.com/intel/compute-runtime/releases/download/20.04.15428/intel-igc-opencl_1.0.3151_amd64.deb
wget https://github.com/intel/compute-runtime/releases/download/20.04.15428/intel-opencl_20.04.15428_amd64.deb
wget https://github.com/intel/compute-runtime/releases/download/20.04.15428/intel-ocloc_20.04.15428_amd64.deb

sudo dpkg -i *.deb

Opencv + OpenVino

Script (naturally with crutches), which will collect everything by itself.

run.sh
#!/bin/bash
git clone https://github.com/opencv/dldt -b 2019 && \
            (cd dldt/inference-engine && \
            git submodule init && \
            git submodule update --recursive && \
            ./install_dependencies.sh && \
            mv -f thirdparty/clDNN/common/intel_ocl_icd/6.3/linux/Release thirdparty/clDNN/common/intel_ocl_icd/6.3/linux/RELEASE && \
            mkdir -p build && \
            cd build && \
            cmake -D CMAKE_BUILD_TYPE=RELEASE \
                  -D CMAKE_INSTALL_PREFIX=/usr/local \
                  -D BUILD_SHARED_LIBS=ON \
                  -D ENABLE_TESTS=OFF \
                  -D ENABLE_VPU=ON \
                  -D ENABLE_MKL_DNN=ON \
                  -D ENABLE_CLDNN=ON .. && \
                  make -j $(nproc --all) && \
                  touch VERSION && \
                  mkdir -p src/ngraph && \
                  sudo cp -r ../bin/intel64/RELEASE/lib/* /usr/local/lib && \
                  cp thirdparty/ngraph/src/ngraph/version.hpp src/ngraph && \
                  sudo make install)

OPENCV_VERSION="4.2.0"

# install opencv
curl -Lo opencv.zip https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip && \
            unzip -q opencv.zip && \
            curl -Lo opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip && \
            unzip -q opencv_contrib.zip && \
            rm opencv.zip opencv_contrib.zip && \
            (cd opencv-${OPENCV_VERSION} && \
            mkdir build && cd build && \
            cmake -D CMAKE_BUILD_TYPE=RELEASE \
                  -D CMAKE_INSTALL_PREFIX=/usr/local \
                  -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-${OPENCV_VERSION}/modules \
                  -D InferenceEngine_DIR=../../../dldt/inference-engine/build \
                  -D WITH_JASPER=OFF \
                  -D BUILD_DOCS=OFF \
                  -D BUILD_EXAMPLES=OFF \
                  -D ENABLE_CXX11=ON \
                  -D WITH_INF_ENGINE=ON \
                  -D WITH_QT=OFF \
                  -D WITH_GTK=ON \
                  -D WITH_FFMPEG=OFF \
                  -D CUDA_ARCH_BIN=5.3,6.0,6.1,7.0,7.5 \
                  -D CUDA_ARCH_PTX=5.3 \
                  -D WITH_CUDA=ON \
                  -D WITH_CUDNN=ON \
                  -D OPENCV_DNN_CUDA=ON \
                  -D ENABLE_FAST_MATH=1 \
                  -D WITH_CUBLAS=1 \
                  -D WITH_TIFF=OFF \
                  -D WITH_WEBP=OFF \
                  -D WITH_QT=OFF \
                  -D WITH_PNG=OFF \
                  -D WITH_1394=OFF \
                  -D HAVE_OPENEXR=OFF \
                  -D BUILD_TESTS=OFF \
                  -D BUILD_PERF_TESTS=OFF \
                  -D BUILD_opencv_java=NO \
                  -D BUILD_opencv_python=NO \
                  -D BUILD_opencv_python2=NO \
                  -D BUILD_opencv_python3=NO \
                  -D BUILD_SHARED_LIBS=ON \
                  -D OPENCV_GENERATE_PKGCONFIG=ON .. && \
            make -j $(nproc --all) && \
            sudo make preinstall && sudo make install && sudo ldconfig )


Now let's check how this will work!

We clone ourselves the source code:

git clone https://github.com/Danile71/go_realtime_object_recognition
cd go_realtime_object_recognition
go get -d
go build
./go_realtime_object_recognition

Video recognition will work on CPU, VulkanAPI and / or Cuda.

Now I have made several pull requests in
github.com/hybridgroup/gocv

to support OpenVino + Cuda, but it is not known how long they will go to the master branch, so let's do a little trick:

#!/bin/bash
cd $GOPATH/src/gocv.io/x/gocv
wget https://patch-diff.githubusercontent.com/raw/hybridgroup/gocv/pull/607.patch
wget https://patch-diff.githubusercontent.com/raw/hybridgroup/gocv/pull/609.patch
wget https://patch-diff.githubusercontent.com/raw/hybridgroup/gocv/pull/610.patch
wget https://patch-diff.githubusercontent.com/raw/hybridgroup/gocv/pull/612.patch

patch -p1 < 607.patch
patch -p1 < 609.patch
patch -p1 < 610.patch
patch -p1 < 612.patch

So that you can use OpenVino / CPU / VulkanAPI / Cuda at the same time

go build -tags openvino
./go_realtime_object_recognition

And to select a device, change

  • CPU

    modelNet.SetPreferableBackend(gocv.NetBackendDefault)
    modelNet.SetPreferableTarget(gocv.NetTargetCPU)
    

  • VulkanAPI

    modelNet.SetPreferableBackend(gocv.NetBackendVKCOM)
    modelNet.SetPreferableTarget(gocv.NetTargetVulkan)
    

  • Cuda
    modelNet.SetPreferableBackend(gocv.NetBackendCUDA)
    modelNet.SetPreferableTarget(gocv.NetTargetCUDA)
    

  • Intel GPU

    modelNet.SetPreferableBackend(gocv.NetBackendOpenVINO)
    modelNet.SetPreferableTarget(gocv.NetTargetFP16)
    

  • Intel Neural Compute Stick 2
    modelNet.SetPreferableBackend(gocv.NetBackendOpenVINO)
    modelNet.SetPreferableTarget(gocv.NetTargetVPU)
    


Here's another small example with gender / age / emotion recognition.

And here is recognition of the β€œhuman” object using Cuda.

If anyone is interested, and something did not work out, write,

I’ll be happy to help :-) Chukchi is not a writer, but I tried.

upd.
There are times when you need to build a static binary; for this, replace dldt and opencv in the assembly
-D BUILD_SHARED_LIBS=ON

on the
-D BUILD_SHARED_LIBS=OFF

Source: https://habr.com/ru/post/undefined/


All Articles