Posted

Source

An official jetson-inference lib. provided by Dystin Franklin (github)

Revising glDisplay

Add below code to file “/jetson-inference/utils/display/glDisplay.cpp”, lines~1367

LogInfo(LOG_GL "glDisplay -- event KEY_STATE %i %s (%s)\n", a, XKeysymToString(a), b ? "pressed" : "released");
display->mKeyStates[a] = (b == KEY_PRESSED)?1:0;
display->mCurKeyChar = (b == KEY_PRESSED)?(char)a:0;

Change encapsulation of mKeyState[] as public in file “/jetson-inference/utils/display/glDisplay.h”,lines~589 (move from line~683)

bool     mKeyStates[1024];
char     mCurKeyChar = 0;

Add following method to get key state, line~427

inline bool GetKeyI( int key ) const { return mKeyStates[key]; }
inline char GetCurKeyChar() const { return mCurKeyChar; }

Usage

Put this code in main func. of program

if(output->IsType<glDisplay>())
{
	glDisplay* dis = (glDisplay*)output;
	LogInfo(LOG_COLOR_YELLOW "Mouse-pos x,y & key: %u,%u,%u\n",dis->GetMousePosition()[0],dis->GetMousePosition()[1],dis->GetKeyI(XK_a));
}

Result

Author
Categories C++, Nvidia Jetson Nano

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

Posted

หลังจากสร้าง image disc สำหรับ raspberry pi โดยใช้ Raspberry Pi Imager เรียบร้อยแล้ว (link)
ให้ทำการ boot raspi เข้า Desktop และเปิด terminal

ตรวจสอบเวอร์ชันของ raspi

uname -a

สังเกตเวอร์ชันของ pi เป็น “armv7l GNU/Linux”

ติดตั้ง dependencies

sudo apt install libopenblas-dev libblas-dev m4 cmake cython python3-dev python3-yaml python3-setuptools

จากนั้น set env-var สำหรับการ build pytorch ดังนี้

export NO_CUDA=1
export NO_DISTRIBUTED=1
export NO_MKLDNN=1 
export BUILD_TEST=0
export MAX_JOBS=4

Compile pytorch จาก github src

git clone --branch v1.7.0 https://github.com/pytorch/pytorch pytorch
cd pytorch
git submodule update --init --recursive
python3 ./setup.py bdist_wheel  #ใช้เวลา build ~120 นาที

file *.whl จะถูกสร้างไปที่ ./dist/

ติดตั้ง PyTorch

cd dist/
pip3 install Cython numpy torch-1.7.0a0-cp37-cp37m-linux_armv7l.whl

ทดสอบการติดตั้ง PyTorch ว่าสามารถใช้งานได้

python3
import torch
torch.__version__
x=torch.rand(5,4)
print(x)

Author
Categories PyTorch, Raspberry Pi

Posted

หลังจากติดตั้ง Jetson Nano image เรียบร้อยแล้ว (ดูวิธีติดตั้งได้ที่)

ตรวจสอบ Nano version (Jetpack)

sudo su
cd
pip3 --version
pip3 install jetson-stats
jtop
exit #ออกจาก root user
sudo jtop

แถบด้านบนแสดงเวอร์ชัน Jetpack=4.4.1 กดปุ่ม q เพื่อออกจาก jtop

ติดตั้ง PyTorch version 1.9

ดูรายละเอียดเวอร์ชันที่เข้ากันได้กับ Jetpack

wget https://nvidia.box.com/shared/static/h1z9sw4bb1ybi0rm3tu8qdj8hs05ljbm.whl -O  torch-1.9.0-cp36-cp36m-linux_aarch64.whl
sudo apt-get update
sudo apt-get install python3-pip libopenblas-base libopenmpi-dev 
pip3 install Cython
pip3 install numpy torch-1.9.0-cp36-cp36m-linux_aarch64.whl

ติดตั้ง touchvision version 0.10

git clone --branch v0.10.0 https://github.com/pytorch/vision torchvision
cd torchvision
sudo apt-get install libpng-dev libfreetype6-dev
pip3 uninstall pillow
pip3 install --no-cache-dir matplotlib pillow
sudo python3 setup.py install
cd ..

ตรวจสอบว่าติดตั้ง pytorch & torchvision ได้สมบูรณ์

pip3 list | grep torch

Author
Categories Python, PyTorch