OpenCV  4.10.0
Open Source Computer Vision
Loading...
Searching...
No Matches
How to enable Halide backend for improve efficiency

Prev Tutorial: Load Caffe framework models
Next Tutorial: How to schedule your network for Halide backend

Original author Dmitry Kurtaev
Compatibility OpenCV >= 3.3

Introduction

This tutorial guidelines how to run your models in OpenCV deep learning module using Halide language backend. Halide is an open-source project that let us write image processing algorithms in well-readable format, schedule computations according to specific device and evaluate it with a quite good efficiency.

An official website of the Halide project: http://halide-lang.org/.

An up to date efficiency comparison: https://github.com/opencv/opencv/wiki/DNN-Efficiency

Requirements

LLVM compiler

Note
LLVM compilation might take a long time.
  • Download LLVM source code from http://releases.llvm.org/4.0.0/llvm-4.0.0.src.tar.xz. Unpack it. Let llvm_root is a root directory of source code.
  • Create directory llvm_root/tools/clang
  • Download Clang with the same version as LLVM. In our case it will be from http://releases.llvm.org/4.0.0/cfe-4.0.0.src.tar.xz. Unpack it into llvm_root/tools/clang. Note that it should be a root for Clang source code.
  • Build LLVM on Linux
    cd llvm_root
    mkdir build && cd build
    cmake -DLLVM_ENABLE_TERMINFO=OFF -DLLVM_TARGETS_TO_BUILD="X86" -DLLVM_ENABLE_ASSERTIONS=ON -DCMAKE_BUILD_TYPE=Release ..
    make -j4
  • Build LLVM on Windows (Developer Command Prompt)
    mkdir \\path-to-llvm-build\\ && cd \\path-to-llvm-build\\
    cmake.exe -DLLVM_ENABLE_TERMINFO=OFF -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_ENABLE_ASSERTIONS=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=\\path-to-llvm-install\\ -G "Visual Studio 14 Win64" \\path-to-llvm-src\\
    MSBuild.exe /m:4 /t:Build /p:Configuration=Release .\\INSTALL.vcxproj
Note
\\path-to-llvm-build\\ and \\path-to-llvm-install\\ are different directories.

Halide language.

  • Download source code from GitHub repository, https://github.com/halide/Halide or using git. The root directory will be a halide_root.
    git clone https://github.com/halide/Halide.git
  • Build Halide on Linux
    cd halide_root
    mkdir build && cd build
    cmake -DLLVM_DIR=llvm_root/build/lib/cmake/llvm -DCMAKE_BUILD_TYPE=Release -DLLVM_VERSION=40 -DWITH_TESTS=OFF -DWITH_APPS=OFF -DWITH_TUTORIALS=OFF ..
    make -j4
  • Build Halide on Windows (Developer Command Prompt)
    cd halide_root
    mkdir build && cd build
    cmake.exe -DLLVM_DIR=\\path-to-llvm-install\\lib\\cmake\\llvm -DLLVM_VERSION=40 -DWITH_TESTS=OFF -DWITH_APPS=OFF -DWITH_TUTORIALS=OFF -DCMAKE_BUILD_TYPE=Release -G "Visual Studio 14 Win64" ..
    MSBuild.exe /m:4 /t:Build /p:Configuration=Release .\\ALL_BUILD.vcxproj

Build OpenCV with Halide backend

When you build OpenCV add the following configuration flags:

  • WITH_HALIDE - enable Halide linkage
  • HALIDE_ROOT_DIR - path to Halide build directory

Set Halide as a preferable backend

net.setPreferableBackend(DNN_BACKEND_HALIDE);