Jetson Xavier Setup - OpenCV, torch2trt

Raspberry Pi, Jeston Nano


henrywu
Posts: 180
Joined: Sun Apr 17, 2022 4:57 pm

Jetson Xavier Setup - OpenCV, torch2trt

Post by henrywu »

Add CUDA support for opencv. We can see there is no CUDA spport initially.

Code: Select all

nvidia@nvidia-desktop:~$ jetson_release -v
 - NVIDIA Jetson AGX Xavier [16GB]
   * Jetpack 4.6 [L4T 32.6.1]
   * NV Power Mode: MODE_30W_ALL - Type: 3
   * jetson_stats.service: active
 - Board info:
   * Type: AGX Xavier [16GB]
   * SOC Family: tegra194 - ID:25
   * Module: P2888-0001 - Board: P2822-0000
   * Code Name: galen
   * CUDA GPU architecture (ARCH_BIN): 7.2
   * Serial Number: 1420222030140
 - Libraries:
   * CUDA: 10.2.300
   * cuDNN: 8.2.1.32
   * TensorRT: 8.0.1.6
   * Visionworks: 1.6.0.501
   * OpenCV: 4.1.1 compiled CUDA: NO
   * VPI: ii libnvvpi1 1.1.15 arm64 NVIDIA Vision Programming Interface library
   * Vulkan: 1.2.70
 - jetson-stats:
   * Version 3.1.4
   * Works on Python 3.6.9

So we need to compile it from source.

Code: Select all

#!/bin/bash
#
# Copyright (c) 2020, 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.

# First uninstall the old one
pip uninstall opencv-python

version="4.5.0"
folder="workspace"

echo "** Remove other OpenCV 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 libdc1394-22-dev
sudo apt-get install -y libv4l-dev v4l-utils qv4l2 v4l2ucp
sudo apt-get install -y curl

export PATH=/usr/lib/ccache:$PATH
export CC=clang-8
export CXX=clang++-8

echo "** Download opencv-"${version}
mkdir -p $folder
cd ${folder}
curl -L https://github.com/opencv/opencv/archive/${version}.zip -o opencv-${version}.zip
curl -L https://github.com/opencv/opencv_contrib/archive/${version}.zip -o opencv_contrib-${version}.zip
unzip opencv-${version}.zip
unzip opencv_contrib-${version}.zip
cd opencv-${version}/

echo "** Building..."
mkdir release
cd release/
cmake -D WITH_CUDA=ON -D WITH_CUDNN=ON -D CUDA_ARCH_BIN="5.3,6.2,7.2" -D CUDA_ARCH_PTX="" -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-${version}/modules -D WITH_GSTREAMER=ON -D WITH_LIBV4L=ON -D BUILD_opencv_python2=OFF -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 -j$(nproc)
sudo make install


echo "** Install opencv-"${version}" successfully"
echo "** Bye :)"

After compiling, it becomes:

Code: Select all

nvidia@nvidia-desktop:~/workspace/opencv-4.5.0/modules/python/test$ jetson_release -v
 - NVIDIA Jetson AGX Xavier [16GB]
   * Jetpack 4.6 [L4T 32.6.1]
   * NV Power Mode: MODE_30W_ALL - Type: 3
   * jetson_stats.service: active
 - Board info:
   * Type: AGX Xavier [16GB]
   * SOC Family: tegra194 - ID:25
   * Module: P2888-0001 - Board: P2822-0000
   * Code Name: galen
   * CUDA GPU architecture (ARCH_BIN): 7.2
   * Serial Number: 1420222030140
 - Libraries:
   * CUDA: 10.2.300
   * cuDNN: 8.2.1.32
   * TensorRT: 8.0.1.6
   * Visionworks: 1.6.0.501
   * OpenCV: 4.5.0 compiled CUDA: YES
   * VPI: ii libnvvpi1 1.1.15 arm64 NVIDIA Vision Programming Interface library
   * Vulkan: 1.2.70
 - jetson-stats:
   * Version 3.1.4
   * Works on Python 3.6.9

Need to do some extra work

Code: Select all

cd /usr/local/lib/python3.6/dist-packages/
sudo ln -s /usr/local/lib/python3.6/site-packages/cv2 cv2
henrywu
Posts: 180
Joined: Sun Apr 17, 2022 4:57 pm

Re: Jetson Xavier Setup - OpenCV, torch2trt

Post by henrywu »

Code: Select all

git clone https://github.com/NVIDIA-AI-IOT/torch2trt.git
cd torch2trt/
python setup.py bdist_wheel
cd dist/
pip install torch2trt-0.3.0-py3-none-any.whl
henrywu
Posts: 180
Joined: Sun Apr 17, 2022 4:57 pm

Re: Jetson Xavier Setup - OpenCV, torch2trt

Post by henrywu »

torch with quantization backend

Code: Select all

#!/bin/bash

#sudo apt-get install clang
## the dependencies
#sudo apt-get install ninja-build git cmake
#sudo apt-get install libjpeg-dev libopenmpi-dev libomp-dev ccache
#sudo apt-get install libopenblas-dev libblas-dev libeigen3-dev
#sudo -H pip3 install -U --user wheel mock pillow

export BUILD_CAFFE2_OPS=OFF
export USE_FBGEMM=OFF
export USE_FAKELOWP=OFF
export BUILD_TEST=OFF
export USE_MKLDNN=OFF
export USE_CUDA=ON
export USE_CUDNN=ON
export USE_NNPACK=ON
export USE_XNNPACK=ON
export USE_QNNPACK=ON
export MAX_JOBS=8
export USE_OPENCV=OFF
export USE_NCCL=OFF
export USE_SYSTEM_NCCL=OFF
export USE_DISTRIBUTED=ON
export PATH=/usr/lib/ccache:$PATH
# set clang compiler
export CC=clang-8
export CXX=clang++-8
# clean up the previous build, if necessary
#python3 setup.py clean
# start the build
python3 setup.py bdist_wheel

Attachments
torch-1.10.0a0+git71f889c-cp36-cp36m-linux_aarch64.whl
(104.8 MiB) Downloaded 479 times
henrywu
Posts: 180
Joined: Sun Apr 17, 2022 4:57 pm

Re: Jetson Xavier Setup - OpenCV, torch2trt

Post by henrywu »

check torch version and torchvision version

Code: Select all

import torch

print(torch.__version__)
print('CUDA available: ' + str(torch.cuda.is_available()))
print('cuDNN version: ' + str(torch.backends.cudnn.version()))

a = torch.cuda.FloatTensor(2).zero_()
print('Tensor a = ' + str(a))
b = torch.randn(2).cuda()
print('Tensor b = ' + str(b))

c = a + b
print('Tensor c = ' + str(c))

import torchvision
print(torchvision.__version__)
Post Reply