[OpenCV] Install OpenCV 4.2+

为什么要安装较高版本?以下是理由。

 

Jetson 使用 OpenCV DNN

Xavier NX - OpenCV 4.5

Xavier NX only works with JetPack 4.4 or later. And JetPack 4.4 is provided with OpenCV 4.1.1 installed. In most cases, there is no problem with using OpenCV 4.1.1.

  1. However, from OpenCV 4.2+, Super Resolution function is provided as C/C++ API.
  2. And from 4.3, it provides Python API.
  3. And finally, in 4.4, CUDA GPU acceleration is available.

Therefore, it is recommended to use version 4.4 or higher to fully use the Super Resolution function provided by OpenCV. Therefore, to use OpenCV's SuperResolution, you need to delete the OpenCV 4.1.1 version of JetPack 4.4 and install 4.4 newly.

And from OpenCV 4.2, the dnn module started supporting Nvidia GPUs. In previous versions, only the CPU was available. The Jetson series has NVidia GPUs, so if you are using OpenCV's dnn, it is recommended to upgrade to version 4.2 or higher. OpenCV version 4.2 or higher uses NVidia GPU to speed up the inference.

If you are not interested in OpenCV's SuperResolution feature or not using OpenCV dnn module, you do not necessarily need to upgrade to OpenCV 4.4 or 4.5.

 

#!/bin/bash
#
# Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
#
# NVIDIA Corporation and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto.  Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA Corporation is strictly prohibited.
#

if [ "$#" -ne 1 ]; then
    echo "Usage: $0 <Install Folder>"
    exit
fi
folder="$1"
user="nvidia"
passwd="nvidia"

echo "** Remove OpenCV4.1 first"
sudo sudo apt-get purge *libopencv*

echo "** Install requirement"
sudo apt-get update
sudo apt-get install -y build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install -y python2.7-dev python3.6-dev python-dev python-numpy python3-numpy
sudo apt-get install -y libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install -y libv4l-dev v4l-utils qv4l2 v4l2ucp
sudo apt-get install -y curl
sudo apt-get update

echo "** Download opencv-4.5.1"
cd $folder
curl -L https://github.com/opencv/opencv/archive/4.5.1.zip -o opencv-4.5.1.zip
curl -L https://github.com/opencv/opencv_contrib/archive/4.5.1.zip -o opencv_contrib-4.5.1.zip
unzip opencv-4.5.1.zip 
unzip opencv_contrib-4.5.1.zip 
cd opencv-4.5.1/

echo "** Building..."
mkdir release
cd release/
cmake -D WITH_CUDA=ON -D ENABLE_PRECOMPILED_HEADERS=OFF  -D CUDA_ARCH_BIN="7.2" -D CUDA_ARCH_PTX="" -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.4.0/modules -D WITH_GSTREAMER=ON -D WITH_LIBV4L=ON -D BUILD_opencv_python2=ON -D BUILD_opencv_python3=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j6
sudo make install

echo "** Install opencv-4.5.1 successfully"
echo "** Bye :)"

 

可见,dnn 有点变化,提供了另外的 dnn_superres。

Python 3.6.9 (default, Jul 17 2020, 12:50:27)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> from cv2 import dnn_superres
>>> cv2.__version__
'4.5.1'

 

那么,性能是否会有大的提升呢?

code: https://github.com/QaisarRajput/mask-rcnn-opencv

Opencv CUDA.

net = cv2.dnn.readNetFromTensorflow(weightsPath, configPath)

net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)

 

Test time.

root@melb-desktop:/jetson-inference/data/tmp/mask-rcnn-opencv# python mask_rcnn.py -i images/example_01.jpg -m mask-rcnn-coco/
[INFO] loading Mask R-CNN from disk...
[INFO] Mask R-CNN took 4.793104 seconds
[INFO] boxes shape: (1, 1, 100, 7)
[INFO] masks shape: (100, 90, 15, 15)
[INFO] Mask R-CNN took 0.348731 seconds
[INFO] Mask R-CNN took 0.769313 seconds
[INFO] Mask R-CNN took 0.876965 seconds
[INFO] Mask R-CNN took 0.866886 seconds
[INFO] Mask R-CNN took 0.741504 seconds
[INFO] Mask R-CNN took 0.879894 seconds

Close CUDA, we can see that it will take long time. So, CUDA here gives 7x faster.

root@melb-desktop:/jetson-inference/data/tmp/mask-rcnn-opencv# python mask_rcnn.py -i images/example_01.jpg -m mask-rcnn-coco/
[INFO] loading Mask R-CNN from disk...
[INFO] Mask R-CNN took 7.261957 seconds
[INFO] boxes shape: (1, 1, 100, 7)
[INFO] masks shape: (100, 90, 15, 15)
[INFO] Mask R-CNN took 6.543088 seconds
[INFO] Mask R-CNN took 5.895172 seconds
[INFO] Mask R-CNN took 6.363254 seconds
[INFO] Mask R-CNN took 6.000929 seconds
[INFO] Mask R-CNN took 5.798403 seconds
[INFO] Mask R-CNN took 5.938069 seconds

 

 

Release versions

Ref: https://opencv.org/releases/

2020年开始的4.4, 2021年开始的4.5。

 

如下是原生Jetson本地优化的性能参考。

Training Instance Segmentation Models Using Mask R-CNN on the NVIDIA Transfer Learning Toolkit

By Yu Wang

 

 

OpenCV 4.4+ 新模型

Ref: https://pjreddie.com/darknet/yolo/

YOLOv3-tiny 30M左右,但输入比较大:416 * 416

Ref: https://stackoverflow.com/questions/49450829/darknet-yolo-image-size

可以修改 input width and height,那牛掰了呢!

Ref: While resizing the images during training( random=1 in .cfg file), does yolo internally changes the grid size? or it uses the initial grid size what we see at the starting of the training. #728

 

Ref: YOLOv3 Input size: 608, 1216, 1920, 2560x2560【可见,调整input后的大小,以及performance的大概评估】

貌似是OpenCV支持YOLOV3-TINY,最后还是以实践为基础。

 

End.

 

posted @ 2021-06-10 07:30  郝壹贰叁  阅读(247)  评论(0)    收藏  举报