Getting Started with ROS for Jetson Nano - Deep Learning Environment Configuration


foreword

insert image description here

Jetson Nano is a small computer launched by NVIDIA. It has excellent performance, low power consumption, and small size. It is very suitable for deep learning applications in embedded systems and edge devices. Jetson Nano is equipped with NVIDIA's Tegra X1 processor, which has 4 ARM Cortex-A57 CPU cores and 128 NVIDIA Maxwell GPU cores, which can provide up to 472GFLOPS of computing power and enable real-time deep learning reasoning.
Jetson Nano supports a variety of deep learning frameworks, such as TensorFlow, PyTorch, Caffe, and MXNet, and can perform deep learning model training and reasoning by installing corresponding software packages. Jetson Nano also provides a wealth of hardware interfaces, such as GPIO, I2C, SPI, UART and CSI, etc., which can easily connect various sensors and actuators to realize intelligent control and data acquisition.


1. Anaconda installation

insert image description here

First of all, Anaconda is installed to solve an environmental conflict problem, because ROS function packages are all developed based on Python2.7, and the current mainstream deep learning frameworks are all developed based on Python3, and different Python versions will lead to compatibility. Sexual problems lead to a bunch of error reports, so the best solution is to create a Python3 virtual environment in Anaconda, which runs the mainstream framework of deep learning, so as not to conflict with ROS

There are many blogs saying that you need to install ROS first, and then install Anaconda. I just installed ROS Melodic first, and I haven’t installed Anaconda3 yet.

Anaconda's official website: https://www.anaconda.com/products/individual
Other versions: https://repo.anaconda.com/archive/

downloadAnaconda3-2022.05-Linux-aarch64.sh, the downloaded directory is /home/nvidia/, open a terminal in this directory, and run the .sh file

bash Anaconda3-2022.05-Linux-aarch64.sh

The next step is to press Enter all the way to confirm, and select yes where yes/no is required. After the installation is complete, edit ~/.bashrc to configure the environment variables of conda, and add the following after the ~/.bashrc file

sudo vim ~/.bashrc

insert image description here
Then source the environment variable

source ~/.bashrc

Open the terminal and check the conda version. If the conda version information is successfully displayed, it means that Anaconda has been installed.

conda --version

But this will conflict with ROS, because Anaconda3 is also a development environment based on Python3, so it is necessary to comment out the previous configuration in the ~/.bashrc file

sudo vim ~/.bashrc

insert image description here

source ~/.bashrc

Re-open the terminal, then start the Anaconda3 environment, and check the Python version. Regardless of the version of Python and Python3 entered in the terminal, only the Python3 version number will appear in the environment. This virtual environment will not be affected by Python2.7

source ~/anaconda3/bin/activate

Run the following command to close the Anaconda virtual environment

conda deactivate

2. Pytorch and TensorFlow environment configuration

insert image description here

Both TensorFlow and PyTorch are currently very popular deep learning frameworks, and they both provide a wealth of tools and APIs to help developers quickly build, train and deploy deep learning models.

TensorFlow is an open source deep learning framework developed by Google, which has a wide range of applications and community support. TensorFlow is mainly based on static calculation graphs. It can perform distributed calculations on multiple devices and support GPU acceleration, making it very suitable for large-scale deep learning tasks. TensorFlow also provides high-level APIs, such as Keras, and the visualization tool TensorBoard, etc., making model building and debugging easier.

PyTorch is an open source deep learning framework developed by Facebook. It is a dynamic computational graph framework that allows for more flexible construction and debugging of deep learning models. PyTorch also supports GPU acceleration and distributed computing, and provides many advanced APIs and tools, such as torchvision and torchaudio, which make model construction and training easier.

In contrast, TensorFlow is more suitable for large-scale deep learning tasks, such as image classification and natural language processing. PyTorch is more suitable for research-oriented deep learning tasks, such as the exploration and experimentation of new models.

Before installing TensorFlow first, create a Python3 virtual environment and open the terminal

conda create -n mydl python=3.6

Activate the virtual environment just created

conda activate mydl

According to the official Nvidia tutorial, the JetPack version of my Jetson Nano is 4.6.3, and the Python version is 3.6. First install some tools and dependencies
https://forums.developer.nvidia.com/t/official-tensorflow-for- jetson-agx-xavier/65523

pip3 install -U pip testresources setuptools

Install commonly used data analysis and machine learning libraries such as numpy and keras

pip3 install -U numpy==1.19.4 future mock keras_preprocessing keras_applications gast==0.2.1 protobuf pybind11 cython pkgconfig packaging

Download the optimized TensorFlow framework from NVIDIA official website

pip3 install --extra-index-url https://developer.download.nvidia.com/compute/redist/jp/v461 tensorflow

Then create the Pytorch environment (version 1.10)

conda install pytorch torchvision cudatoolkit=10.2 -c pytorch

After installation, test whether the installation is successful, and open the python3 input interface in the terminal

python3

Import the tensorflow package. If there is no error, it means that tensorflow is installed successfully.

import tensorflow as tf

Import the pytorch package, if there is no error, it means that the pytorch installation is successful

import torch
import torchvision

# 该指令显示pytorch版本
print(torch.__version__)

# 若cuda已安装,将显示true
torch.cuda.is_available()

If cuda is installed and configured, true will be displayed

3. TensorRT reasoning engine configuration

insert image description here
NVIDIA TensorRT is a deep learning inference engine that optimizes and accelerates deep learning inference applications for production environments. It optimizes and accelerates inference using deep learning models, thereby improving the inference performance and efficiency of the model.

TensorRT can automatically analyze deep learning models and optimize them to improve inference performance. These optimizations include techniques such as network pruning, layer fusion, memory optimization, and precision mixing. In addition, TensorRT also supports the conversion of deep learning models into a highly optimized TensorRT engine format for deployment in production environments.

TensorRT also provides a set of APIs for programming and performance tuning, as well as integrations with popular deep learning frameworks such as TensorFlow, Caffe, and ONNX. The optimized model of TensorRT can achieve faster reasoning speed, reduce latency, and improve throughput; TensorRT automatically optimizes and accelerates the process, reducing the workload and time of manual optimization; TensorRT has high reliability and stability, and has been Widely used in various computer vision and natural language processing applications; TensorRT provides easy-to-use APIs and integrations so that developers can easily apply it to existing deep learning applications.

The official documentation of Jetson Nano recommends two examples to us, one of which uses Tensor RT for item recognition. For details, please refer to the NVIDIA jetson-inference example. The server storing these models is blocked, so the previously downloaded packages can only be remotely transferred to the corresponding download directory.
First if you don't have git and cmake installed, install them first

sudo apt-get install git cmake

Then clone the jetson-inference library from git

git clone https://github.com/dusty-nv/jetson-inference

Go to the folder jetson-inference

cd jetson-inference

Here I didn't download the model by scientific Internet, I directly uploaded the model to Jetson Nano remotely. The operation is as follows:
1) Edit jetson-inference/CMakePrebuild.sh. Comment out ./download-models.sh, (add a # comment in front)

insert image description here
2) Remotely transfer the model to the data/networks directory

Create a new build folder to store compiled files

mkdir build    #创建build文件夹

enter the folder

cd build       #进入build

run cmake

cmake ../      #运行cmake,它会自动执行上一级目录下面的 CMakePrebuild.sh

Then perform decompression in this directory:

for tar in *.tar.gz; do tar xvf $tar; done

After cmake succeeds, you need to compile and enter the build folder

cd jetson-inference/build	

start compiling

make (或者make -j4)    //注意:(在build目录下)
// 这里的 make 不用 sudo
// 后面 -j4 使用 4 个 CPU 核同时编译,缩短时间

If the compilation is successful, the following folder structure will be generated
insert image description here
Start the test and test the image recognition results

cd jetson-inference/build/aarch64/bin

Import pictures for recognition

./imagenet-console ./images/bird_0.jpg output_wyk.jpg

Summarize

That's all for this note. Deep learning is one of the hottest technologies today and has been widely used in computer vision, natural language processing, speech recognition and other fields. In deep learning applications, a large number of computing resources and algorithm support are usually required, which requires an appropriate deep learning environment to be configured.

The configuration of the deep learning environment includes installing various deep learning frameworks, GPU drivers, libraries and tools such as CUDA and cuDNN. Properly configuring the deep learning environment can make the development and debugging of deep learning algorithms more convenient and efficient. At the same time, when using GPU to accelerate deep learning, a suitable deep learning environment can also improve computing speed and resource utilization, and speed up algorithm training and reasoning.

Guess you like

Origin blog.csdn.net/m0_55202222/article/details/131205287