jetson-tx2 installation, pytorch, yolov5, opencv-python, onnxruntime-gpu
The environment is as follows
- System: ubuntu18.04
- jetpack:4.5.1
- cuda:10.2
- hidden:8.0.0
- pytorch:1.7
- torchvison:0.8.1
- opencv:4.5.4
- archiconda3
System image installation and backup
System Image Restoration
Enter the burning mode:
Connect the machine to the virtual machine ubunutu18.04 through the USB data cable, then press and hold the repower button for a long time, and then press the reset button to enter the burning mode.
Note: Generally use another machine virtual machine or linux link
mirror to install
# 替换镜像文件,本例是将刚克隆的镜像替换为系统镜像的操作
root@mm-desktop:~/tx2-NX/tx2-BIOS-4.5/tx2-nx4.5.1sdk# cd bootloader/
root@mm-desktop:~/tx2-NX/tx2-BIOS-4.5/tx2-nx4.5.1sdk/bootloader# mv system.img system.img.bak
root@mm-desktop:~/tx2-NX/tx2-BIOS-4.5/tx2-nx4.5.1sdk/bootloader# cp ../clone.img system.img
root@mm-desktop:~/tx2-NX/tx2-BIOS-4.5/tx2-nx4.5.1sdk/bootloader# cd ../../
# 烧录镜像
root@mm-desktop:~/tx2-NX/tx2-BIOS-4.5# ./run_tx2_bios_4.5.1.sh
mirror backup
root@mm-desktop:~/tx2/tx2-BIOS-4.5# ls
run_tx2_bios_4.5.1.sh tx2-nx4.5.1sdk tx2-nx4.5.1sdk.tar.gz
root@mm-desktop:~/tx2/tx2-BIOS-4.5# cd tx2-nx4.5.1sdk/
root@mm-desktop:~/tx2/tx2-BIOS-4.5/tx2-nx4.5.1sdk# sudo ./flash.sh -r -k APP -G clone.img jetson-xavier-nx-devkit-tx2-nx mmcblk0p1
...
[ 13.2921 ] Reading partition
[ 13.2964 ] tegradevflash_v2 --read APP /home/xm/tx2-NX/tx2-BIOS-4.5/tx2-nx4.5.1sdk/clone.img
[ 13.3010 ] Bootloader version 01.00.0000
[ 13.4607 ] [................................................] 100%
[ 1919.0289 ]
*** The [APP] has been read successfully. ***
Converting RAW image to Sparse image...
# 成功后,会在该目录下生成clone.img和clone.img.raw 两个文件
Crop system desktop
Later, I will explain in detail why the system should be cut
apt-get purge xorg* -y
apt-get purge x11* -y
apt-get purge gnome* -y
apt-get purge printer-driver-* -y
apt-get purge libreoffice* -y
apt autoremove -y
cuda, cudnn installation
cd install_cuda
bash install_package.sh
#选择1安装cuda,之后选择2安装cudnn
Move cuda to another hard disk. Here, you must ensure that the system has a hard disk that can store files in addition to the 16GB storage that comes with it. The reason will be explained later.
# files为其他硬盘挂载目录
mv /usr/local/cuda-10.2 /files
# 修改cuda环境变量
vim ~/.bashrc
#修改如下:
export CUDA_HOME=/files/cuda-10.2
export LD_LIBRARY_PATH=/files/cuda-10.2/lib64:$LD_LIBRARY_PATH
export PATH=/files/cuda-10.2/bin:$PATH
archiconda3
Why use archiconda3? One is to facilitate the management of the environment. To compare this test machine, you need to install many different environments to test different models; Installed under the mounted hard disk, this solves the problem of insufficient storage
bash archiconda3.sh
Install pytorch, torchvison, opencv-python, onnxruntime-gpu
Install pytorch
The reason why pytorch1.7 was used in the end is because the source code compilation of the latest version of pytorch1.11 requires a lot of memory. If the memory is not enough, we can only make it up by swap. Pytorch can only be compiled when the swap is expanded to 8GB. The system needs 5- 6 GB, cudnn already occupies hundreds of MB in the root directory, and I only have 9.1GB left after following the above steps. This is why the system desktop needs to be trimmed and cuda must be removed. Note: I don't know how to do small systems and I don't have the conditions to provide them
installpytorch1.11
There will be a lot of missing files in the third_party directory. I don’t know if it’s because of my network or other reasons. Just go to github to find them one by one. Let’s start with the
dependencies
apt install libatlas-dev liblapack-dev
apt install liblapacke-dev checkinstall
apt install libffi-dev
apt install ninja-build
apt install cmake (cmake version > 3.17)
Next, the packages required by the environment, because the whole cycle is a bit long, it is not sure which packages are necessary, but it is necessary to install the above several large packages
pip install cython
pip install numpy
pip install pyyaml
pip install scikit-build
pip install cffi
pip install typing-extensions
pip install dataclass
Finally, install pytroch1.11
cd torch
mkdir build
cd buld
cmake ..
make -j6
cd ..
python setup.py install
# 由于我直接最后一步一直报错所以尝试了这种办法,可行
This is very troublesome, and it is easy to break. It takes 5-6 hours to compile without any problems. I didn’t experience it in the 2 hours written by other blogs. It may be due to version problems. The newer the more support Need to compile more files; so I found a simpler way after continuous exploration, continue to read below
installpytorch1.7
Why is it 1.7, because I didn’t download other versions of pytorch, this is a whl file, that is, Nvidia has already compiled it
Paste the installation package link provided by Nvidia: https://elinux.org/Jetson_Zoo#PyTorch_.28 The latest version of Caffe2.29 is only 1.10
apt-get install libopenblas-base libopenmpi-dev python3-pip
pip install torch-1.7.0...
This is simple and convenient, but here comes the problem. I didn’t find the corresponding torchvision whl installation package for Nvidia. After searching around, I can only use the source code to compile; the version of this place must be correct, otherwise it will only report endless errors. , the error is reported to doubt life, the version can only be checked on the official website: https://pytorch.org/get-started/previous-versions/
Install torchvision
Still 8g of swap
git clone -b v0.8.1 https://github.com/pytorch/vision vision-0.8.1
cd vision-0.8.1
sudo python3 setup.py install
Install opencv-python
There will be a missing package, not sure if it is caused by my network, but the download is very slow during installation, and it is easy to report errors such as boostdesc_bgm.i, vgg_generated_48.i and other download addresses https://download.csdn.net/download/m0_37661841/44322678
git clone https://github.com/opencv/opencv.git
git checkout <你所要用的 OpenCV 版本,建议是用最新版>
git clone git://github.com/Itseez/opencv_contrib
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D BUILD_opencv_python3=YES \
-D CMAKE_INSTALL_PREFIX=./install \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
# Contrib path
-D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules \
# Your virtual environment's Python executable
# You need to specify the result of echo $(which python)
-D PYTHON_EXECUTABLE=~/env/bin/python \
-D BUILD_EXAMPLES=ON ..
make install
Install onnxruntime-gpu
pip install onnxruntime-gpu==1.7
yolov5 test
My test has some additional logic processing, maybe the performance will be a bit worse than the original version, but not too much
Model | input size | speed |
---|---|---|
yolov5s | 640*384 | 100ms |
yolov5s6 | 1280*768 | 300ms |
After the installation is complete, the swap can be released, delete the file, and there is still 4GB of storage. When deploying, you can move archiconda3 to the root directory, so that it can be used without mounting the hard disk; written at the end: In fact, they are all
small Problems, insufficient memory, insufficient storage, missing files, which files can be moved and which cannot, etc., but the bloggers are copying and pasting, no one records these small problems, from swap2GB to 4GB to 6GB to 8GB, constantly Try, try to doubt life, crawl in the pit for half a month, and finally land; follow-up to update the performance test of other models