CANN (Compute Architecture of Neural Networks), developped by Huawei, is a heterogeneous computing architecture for AI. With CANN backend in OpenCV DNN, you can run your AI models on the Ascend NPU. Learn more about Ascend NPU and the CANN library from en_doc, cn_doc. Please note that OpenCV DNN supports CANN backend on Ascend 310 for now.
To use OpenCV DNN with CANN backend, read the following sections:
- Install dependencies,
- Install CANN,
- Compile OpenCV with CANN,
- Python and C++ samples
- OpenCV Zoo benchmark
Before installing CANN, make sure the following packges are installed:
- Python (3.7.x, or 3.8.x, or 3.9.x)
- CMake >= 3.5.1
- make
- gcc & g++ >= 7.3.0
You could also visit this page for a detailed list of dependencies.
You will need to specify the Python you just installed in case there are multiple versions of Python in your computer:
# suppose Python 3.7.5 is installed in default path
export LD_LIBRARY_PATH=/usr/local/python3.7.5/lib:$LD_LIBRARY_PATH
export PATH=/usr/local/python3.7.5/bin:$PATH
NOTE: You could also append these lines in ~/.bashrc
so that you have the same environtment next time you open the terminal.
Download Ascend-cann-toolkit_{version}_{platform}.run
from https://www.hiascend.com/software/cann/community-history.
version >= 5.1.RC1.alpha005
is recommanded and tested by us.- Choose your platform.
linux-x86
andlinux-aarch64
are supported.
Follow instructions from this page (CN, EN) to install the CANN library. The links to the instruction page is for version 5.1.RC1.alpha005. You could switch to your specific version by clicking the top-left drop-down menu.
After installing CANN, you could find set_env.sh
under ${cann_install_prefix}/ascend-toolkit
. In default CANN installation, ${cann_install_prefix}
is set to /usr/local/Ascend
. Run the following command to set up CANN environment for compilation:
# replace ${cann_install_prefix} with your path
source ${cann_install_prefix}/ascend-toolkit/set_env.sh
NOTE: You could also append this line to ~/.bashrc
to have the same environment next time you open the terminal.
Compile OpenCV with CANN using the following commands:
git clone https://github.com/fengyuentau/opencv.git
cd opencv
git checkout cann_backend_221010
mkdir build
cd build
cmake -D WITH_CANN=ON \
-D BUILD_opencv_gapi=OFF \
-D CMAKE_INSTALL_PREFIX=install ..
# ensure you see 'CANN: YES' in the end of the log
# Note: you could append "-j 8" in the following command for multi-job speedup.
# More jobs are used, more memory is needed.
cmake --build . --target install
If OpenCV with Python interface is needed, use this CMake command instead:
# replace the value of PYTHON3_EXECUTABLE to your path to python binary
# replace the value of PYTHON3_LIBRARY to your path to python library (where you can find libpython3.x.so)
# replace the value of PYTHON3_INCLUDE_DIR to your path to the python include directory (where you can find Python.h)
cmake -D WITH_CANN=ON\
-D CMAKE_INSTALL_PREFIX=install \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_gapi=OFF \
-D PYTHON3_EXECUTABLE=/usr/local/python3.7.5/bin/python3.7m \
-D PYTHON3_LIBRARY=/usr/local/python3.7.5/lib/libpython3.7m.so \
-D PYTHON3_INCLUDE_DIR=/usr/local/python3.7.5/include/python3.7m \
..
NOTE: If your build is failed at downloading third-party resources, such as ADE, IPP and so on, you may get some help from the third Q&A in https://github.com/opencv/opencv/wiki/FAQ#build--install.
In this section, we provide C++ and Python samples for PP-ResNet50, MobileNetV1 & YOLOX from opencv_zoo.
You could download the ONNX format of PP-ResNet50, MobileNetV1 and YOLOX from:
- PP-ResNet50: https://github.com/opencv/opencv_zoo/tree/master/models/image_classification_ppresnet
- MobileNetV1: https://github.com/opencv/opencv_zoo/tree/master/models/image_classification_mobilenet
- YOLOX: https://github.com/opencv/opencv_zoo/tree/master/models/object_detection_yolox
Tips: Visit this page to learn how to download models in the zoo.
Copy and save the attached Python scripts. Instructions to run samples:
- modify the paths to image and model,
- enable the OpenCV Python interface
# Replace '/path/to' with your prefix export PYTHONPATH=/path/to/opencv/build/python_loader:$PYTHONPATH
- run samples:
python3 ppresnet50.py python3 mobilenetv1.py python3 yolox.py
Copy and save the attached .cpp
files and CMakeLists.txt
. You will need to
- modify the paths to image and model in
.cpp
files, - use the following commands to build and run the sample:
# Replace `/path/to` with your prefix mkdir build && cd build CMAKE_PREFIX_PATH=/path/to/opencv/build/install cmake .. cmake --build . -j 8
- run samples:
# assume current working directory is in build ./ppresnet50 ./mobilenetv1 ./yolox
TBD