Ubuntu nu18 installing tensorrt7 2.3+cuda11. 1+cudnn8. 0+opencv4. four

Posted by mabwi on Wed, 09 Feb 2022 11:25:41 +0100

Download and install TensorRT-7.2.2.3

Download tensorrt-7.2.2.3 on the official website Ubuntu-18.04. x86_ 64-gnu. cuda-11.1. cudnn8. 0.tar. gz

tar xzvf TensorRT-7.2.2.3.Ubuntu-18.04.x86_64-gnu.cuda-11.1.cudnn8.0.tar.gz

Configure environment variables after decompression:

sudo vim ~/.bashrc

After entering, add environment variables at the bottom:

export TRT_PATH=/home/lindsay/TensorRT-7.2.2.3
export PATH=$PATH:$TRT_PATH/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$TRT_PATH/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$TRT_PATH/targets/x86_64-linux-gnu/lib

Enter the python folder under the decompression path and install with pip:

cd TensorRT-7.2.2.3/python
pip3 install tensorrt-7.2.2.3-cp37-none-linux_x86_64.whl

Install uff to support tensorflow

cd TensorRT-7.2.2.3/uff
pip3 install uff-0.6.9-py2.py3-none-any.whl

Install graphsurgeon to support custom structures

cd TensorRT-7.2.2.3/graphsurgeon
pip3 install graphsurgeon-0.4.5-py2.py3-none-any.whl

Install onnx_graphsurgeon to support onnx

cd TensorRT-7.2.2.3/onnx_grahsurgeon
pip3 install onnx_graphsurgeon-0.2.6-py2.py3-none-any.whl

OpenCV download and installation

Download address: OpenCV official website, select the latest version 4.4.0 (if the download speed is too slow, copy the link address and use Xunlei)

https://opencv.org/releases/

Copy the downloaded compressed file to the virtual machine
Compilation and installation

Installing cmake
OpenCV needs to be compiled using cmake

	sudo apt-get install cmake

Installation dependency

sudo apt-get install build-essential pkg-config libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev

The following problems occurred:

The following packages have unmet dependencies:
 libavcodec-dev : Depends: libavutil-dev (= 7:3.4.2-2) but it is not going to be installed
              Depends: libswresample-dev (= 7:3.4.2-2) but it is not going to be installed
 libavformat-dev : Depends: libavformat57 (= 7:3.4.2-2) but it is not going to be installed
               Depends: libavutil-dev (= 7:3.4.2-2) but it is not going to be installed
               Depends: libswresample-dev (= 7:3.4.2-2) but it is not going to be installed
 libgtk2.0-dev : Depends: libglib2.0-dev (>= 2.27.3) but it is not going to be installed
             Depends: libgdk-pixbuf2.0-dev (>= 2.21.0) but it is not going to be installed
             Depends: libpango1.0-dev (>= 1.20) but it is not going to be installed
             Depends: libatk1.0-dev (>= 1.29.2) but it is not going to be installed
             Depends: libcairo2-dev (>= 1.6.4-6.1) but it is not going to be installed
             Depends: libx11-dev (>= 2:1.0.0-6) but it is not going to be installed
             Depends: libxext-dev (>= 1:1.0.1-2) but it is not going to be installed
             Depends: libxinerama-dev (>= 1:1.0.1-4.1) but it is not going to be installed
             Depends: libxi-dev (>= 1:1.0.1-4) but it is not going to be installed
             Depends: libxrandr-dev (>= 2:1.2.99) but it is not going to be installed
             Depends: libxcursor-dev but it is not going to be installed
             Depends: libxfixes-dev (>= 1:3.0.0-3) but it is not going to be installed
             Depends: libxcomposite-dev (>= 1:0.2.0-3) but it is not going to be installed
             Depends: libxdamage-dev (>= 1:1.0.1-3) but it is not going to be installed
             Recommends: python (>= 2.4) but it is not going to be installed
 libjpeg-dev : Depends: libjpeg8-dev but it is not going to be installed
libswscale-dev : Depends: libavutil-dev (= 7:3.4.2-2) but it is not going to be installed
              Depends: libswscale4 (= 7:3.4.2-2) but 7:3.4.8-0ubuntu0.2 is to be installed
 libtiff5-dev : Depends: libtiff5 (= 4.0.9-5) but 4.0.9-5ubuntu0.4 is to be installed
E: Unable to correct problems, you have held broken packages.

Using aptitude

Aptitude, like apt get, is an extremely powerful package management tool in Debian and its derivative systems. Unlike apt get, aptitude is better at dealing with dependency issues. For example, when aptitude deletes a package, it will delete the package it depends on at the same time. In this way, there will be no useless packages left in the system, and the whole system will be cleaner.

sudo aptitude install install build-essential pkg-config libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev

After operation, the non installed scheme is not accepted, and the degraded scheme is accepted.

decompression

unzip opencv-4.4.0

Enter the file directory, create the build directory and enter

cd opencv-4.4.0/
mkdir build
cd build

Generating makefile files using cmake

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_GTK=ON -D OPENCV_GENERATE_PKGCONFIG=YES ..

CMAKE_BUILD_TYPE=RELEASE: indicates the compiled release version
CMAKE_INSTALL_PREFIX: indicates the installation path of generating dynamic library, which can be customized
WITH_GTK=ON: this configuration is to prevent GTK configuration failure: libgtk2 is installed 0-dev dependency, or an error is reported and not installed
OPENCV_GENERATE_PKGCONFIG=YES: indicates that the pkgconfig file of OpenCV is generated automatically, otherwise it needs to be generated manually.
compile

make -j8

-j8 means that multiple system kernels are used for compilation to improve the compilation speed. If you don't know the number of your own system kernels, you can use make -j$(nproc)
If there are errors in the compilation, you can try not to compile with multiple kernels. Although it takes longer to compile, you can avoid some strange errors
install

sudo make install

Note: if you need to cmake again, please empty the files in the build directory first, and then cmake again to avoid errors
Environment configuration

Add OpenCV's library to the system path

Method 1: configure LD so. Conf file

sudo vim /etc/ld.so.conf

Add a line include /usr/loacal/lib in the file. This path is the dynamic library installation path filled in during cmake compilation, plus / lib
Configure LD so. Conf file

Method 2: manually generate OpenCV Conf file

sudo vim /etc/ld.so.conf.d/opencv.conf

It is a new empty file, and the path is added directly. Similarly, this path is the dynamic library installation path filled in during cmake compilation plus / lib

/usr/local/lib

After the above two methods are configured, execute the following commands to make the configured path effective

sudo ldconfig

Configure system bash
Because the pkgconfig file that automatically generates OpenCV is selected during cmake, you can see the file in / usr/local/lib/pkgconfig path
opencv4.pc

Make sure the file exists and execute the following command

sudo vim /etc/bash.bashrc

Add at the end of the text

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

As follows:

bash.bashrc

Save and exit, and then execute the following command to make the configuration effective

source /etc/bash.bashrc

So far, Linux \ Ubuntu 18 The installation and configuration of OpenCV in 04 environment have been completed. You can use the following command to check whether the installation and configuration are successful

pkg-config --modversion opencv4
pkg-config --cflags opencv4
pkg-config --libs opencv4

Using TensorRT

Download yolov5 and tensorrtx

git clone https://github.com/wang-xinyu/tensorrtx.git
git clone https://github.com/ultralytics/yolov5.git

Copy tensorrt/yolov5 to yolov5: sudo cp -r tenorrtx/yolov5 yolov5
Generate WTS corresponding to pt: Python Gen_ wts. py
Put wts into the build peer directory

Modify cmakelists txt:

# tensorrt

#include_directories(/usr/include/x86_64-linux-gnu/)
#link_directories(/usr/lib/x86_64-linux-gnu/)

include_directories(/home/lindsay/TensorRT-7.2.2.3/include)
link_directories(/home/lindsay/TensorRT-7.2.2.3/lib)

Modify yololayer h:

static constexpr int CLASS_NUM = 80;#Modify according to your own category

Start compiling and testing:

mkdir build && cd build
cmake ..
make -j6
sudo ./yolov5 -s ../yolov5s.wts ../yolov5s.engine s# Generation engine
sudo ./yolov5 -d ../yolov5s.engine ../samples#Test c++
python3 yolov5_trt.py  #Test python

Topics: Linux cmake OpenCV Deep Learning torch