Setup Nvidia-JetsonNano from scratch

Posted

ติดตั้ง Jetson Nano DevKit

(link)
** พื้นที่ disc ที่ใช้หลังติดตั้งทั้งหมด ~36G ควรใช้ microSD ขนาด 64G

Update python3.8

sudo apt-get update
sudo apt-get search python3 | grep "python3\."
sudo apt-get install python3.8
sudo apt-get install binutils
sudo apt-get install python-pip python3-pip
sudo update-alternatives --list python3
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 2
sudo update-alternatives --list python3
sudo update-alternatives --config python
sudo vi /usr/bin/gnome-terminal #/usr/bin/python3 to /usr/bin/python3.6
sudo reboot
python3

Build OpenCV

Enlarge memory swap

sudo apt-get update
sudo apt-get upgrade
#install dphys-swapfile
sudo apt-get install dphys-swapfile
sudo vi /sbin/dphys-swapfile
#enlarge CONF_MAXSWAP=4096
sudo vi /etc/dphys-swapfile
#give CONF_SWAPSIZE=4096
sudo reboot

Setup dependencies

sudo sh -c "echo '/usr/local/cuda/lib64' >> /etc/ld.so.conf.d/nvidia-tegra.conf"
sudo ldconfig
sudo apt-get install build-essential cmake git unzip pkg-config zlib1g-dev
sudo apt-get install libjpeg-dev libjpeg8-dev libjpeg-turbo8-dev
sudo apt-get install libpng-dev libtiff-dev libglew-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libgtk2.0-dev libgtk-3-dev libcanberra-gtk*
sudo apt-get install python-dev python-numpy python-pip
sudo apt-get install python3-pip python3.8-dev
sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev
sudo apt-get install libtbb2 libtbb-dev libdc1394-22-dev libxine2-dev
sudo apt-get install gstreamer1.0-tools libgstreamer-plugins-base1.0-dev
sudo apt-get install libgstreamer-plugins-good1.0-dev
sudo apt-get install libv4l-dev v4l-utils v4l2ucp qv4l2
sudo apt-get install libtesseract-dev libxine2-dev libpostproc-dev
sudo apt-get install libavresample-dev libvorbis-dev
sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install libopenblas-dev libatlas-base-dev libblas-dev
sudo apt-get install liblapack-dev liblapacke-dev libeigen3-dev gfortran
sudo apt-get install libhdf5-dev libprotobuf-dev protobuf-compiler libprotoc-dev
sudo apt-get install libgoogle-glog-dev libgflags-dev
sudo apt-get install ffmpeg libavutil-dev libavdevice-dev libavfilter-dev
sudo python3 -m pip install ONNX
sudo python3 -m pip install numpy

Download OpenCV

cd ~
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.8.0.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.8.0.zip
# unpack
unzip ./opencv.zip
unzip ./opencv_contrib.zip
# some administration to make live easier later on
mv ./opencv-4.8.0 ./opencv
mv ./opencv_contrib-4.8.0 ./opencv_contrib
# clean up the zip files
rm -f ./opencv.zip
rm -f ./opencv_contrib.zip

Build make

cd ~/opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D EIGEN_INCLUDE_PATH=/usr/include/eigen3 \
-D WITH_OPENCL=OFF \
-D WITH_CUDA=ON \
-D CUDA_ARCH_BIN=5.3 \
-D CUDA_ARCH_PTX="" \
-D WITH_CUDNN=ON \
-D WITH_CUBLAS=ON \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D OPENCV_DNN_CUDA=ON \
-D ENABLE_NEON=ON \
-D WITH_QT=OFF \
-D WITH_OPENMP=ON \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=ON \
-D WITH_V4L=ON \
-D WITH_LIBV4L=ON \
-D WITH_PROTOBUF=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF ..
make -j4
sudo rm -r /usr/include/opencv4/opencv2
sudo make install
sudo ldconfig
# cleaning (frees 300 MB)
make clean
sudo apt-get update
python3 -m pip install opencv-contrib-python
python3 -m pip install imutils
python3 -m pip install screeninfo
python3 -m pip install schedule
python3 -m pip install pyModbusTCP #optional
python3 -m pip install pymodbus #optional

Build & Install PyTorch

Update cmake (>=3.13)

sudo apt-get install -y software-properties-common lsb-release
sudo wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | sudo tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null
sudo vi /usr/bin/apt-add-repository #change /usr/bin/python3.6
sudo apt-add-repository "deb https://apt.kitware.com/ubuntu/ $(lsb_release -cs) main"
sudo apt-get update
sudo apt-get install -y cmake

Download Pytorch

git clone -b v1.11.0 --depth 1 --recursive --recurse-submodules --shallow-submodules https://github.com/pytorch/pytorch.git
cd pytorch
python3 -m pip install -r requirements.txt
wget https://raw.githubusercontent.com/otamajakusi/build_jetson_nano_libraries/main/pytorch/pytorch-1.11-jetson.patch
patch -p1 < pytorch-1.11-jetson.patch
export BUILD_CAFFE2_OPS=OFF
export USE_FBGEMM=OFF
export USE_FAKELOWP=OFF
export BUILD_TEST=OFF
export USE_MKLDNN=OFF
export USE_NNPACK=OFF
export USE_XNNPACK=OFF
export USE_QNNPACK=OFF
export USE_PYTORCH_QNNPACK=OFF
export USE_CUDA=ON
export USE_CUDNN=ON
export TORCH_CUDA_ARCH_LIST="5.3;6.2;7.2"
export USE_NCCL=OFF
export USE_SYSTEM_NCCL=OFF
export USE_OPENCV=ON
export MAX_JOBS=2
# set path to ccache
export PATH=/usr/lib/ccache:/usr/local/cuda/bin:$PATH
# set clang compiler
export CC=clang
export CXX=clang++
python3 ./setup.py bdist_wheel  #(~12 ชั่วโมง)
find dist/*.whl -type f | xargs python3 -m pip install

Build & Install TorchVision

cd ~
git clone --depth=1 https://github.com/pytorch/vision torchvision -b v0.12.0
cd torchvision
export TORCH_CUDA_ARCH_LIST="5.3;6.2;7.2"
export FORCE_CUDA=1
export MAX_JOBS=2
python3 ./setup.py bdist_wheel  #(~15 นาที)
find dist/*.whl -type f | xargs python3 -m pip install

Install Ultralytics

cd ~
python3 -m pip install ultralytics

Build & Install Jetson-Inference

cd ~
git clone --recursive --depth=1 https://github.com/dusty-nv/jetson-inference
cd jetson-inference
mkdir build
cd build
cmake ..
make -j4    #(~5 mins.)
sudo make install
sudo ldconfig

Build & Install Open3D

cd
git clone https://github.com/isl-org/Open3D
cd ./Open3D
sudo ./util/install_deps_ubuntu.sh
mkdir build
cd build
cmake -DBUILD_CUDA_MODULE=ON -DBUILD_GUI=ON ..
make -j4    #(~5 hrs.)
make install-pip-package -j4
# Test C++ viewer app
./Open3D/Open3D

Build & Install TorchAudio

cd ~
git clone -b release/0.10 https://github.com/pytorch/audio.git torchaudio
cd torchaudio
export USE_FBGEMM=OFF
export BUILD_CAFFE2_OPS=OFF
export USE_FAKELOWP=OFF
export BUILD_TEST=OFF
export USE_MKLDNN=OFF
export USE_NNPACK=OFF
export USE_XNNPACK=OFF
export USE_QNNPACK=OFF
export USE_PYTORCH_QNNPACK=OFF
export USE_CUDA=ON
export USE_CUDNN=ON
export TORCH_CUDA_ARCH_LIST="5.3;6.2;7.2"
export USE_NCCL=OFF
export USE_SYSTEM_NCCL=OFF
export USE_OPENCV=ON
export MAX_JOBS=4
export PATH=/usr/lib/ccache:$PATH
# set clang compiler
export CC=clang
export CXX=clang++
export CUDACXX=/usr/local/cuda-10.2/bin/nvcc
vi ./CMakeLits.txt #change CMAKE_CXX_STANDARD 14
python3 setup.py clean
python3 setup.py bdist_wheel
find dist/*.whl -type f | xargs python3 -m pip install

Check the installation

python3
import torchaudio
print(torchaudio.__version__)

Author
Categories Nvidia Jetson Nano