Try   HackMD

參考:https://zhuanlan.zhihu.com/p/80809006

Jetson Nano - 開發環境設定

Python 3

Jetson Nano 的內建初始安裝了 Python 2 (預設執行) 與 Python 3. 若希望預設執行時是 Python 3, 那麼建議直接在 .bashrc 將它設一個別名來啟動。

  • 指定 Python 3 為預設的 Python 環境

    $ sudo nano ~/.bashrc
    在文檔的最下面加上
    alias python=python3
    然後存檔離開, 再下指令讓環境變數即刻生效
    $ source ~/.bashrc
    輸入 python 後, 應該就可發現已是 3.x.x 版本了
    $ python 然後輸入 exit() 離開
  • 安裝 pip3

    $ sudo apt-get install python3-pip
    $ pip3 -V

    若需要安裝的是 pip 時
    $ sudo apt-get install python-pip

    後續要安裝其他 Python 套件時, 下 pip3 指令就能將之安裝在 Python 3 環境下
    $ pip3 install 套件名稱

Python 虛擬環境

  • 建議使用 mkvirtualenv 來建構虛擬環境,相較於 virtualenv 易於管理。此外,要進入虛擬環境也可以使用 workon 指令快速進入環境。
    $ sudo pip3 install virtualenv virtualenvwrapper
    $ nano ~/.bashrc
    ​​​​在文檔的最後加入下列文字
    ​​​​# virtualenv and virtualenvwrapper
    ​​​​export WORKON_HOME=$HOME/.virtualenvs
    ​​​​export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
    ​​​​source /usr/local/bin/virtualenvwrapper.sh
    
    $ source ~/.bashrc
  • 建立一個專案的虛擬環境, 例如專案名為 aicar, 解譯器是 python3
    $ mkvirtualenv aicar -p python3
  • 查詢目前已建立虛擬環境的專案名
    $ workon
  • 進入專案 aicar 的虛擬環境
    $ workon aicar
    $ pip3 list
  • 離開
    $ deactivate
    $ pip3 list
    有無發現 list 出來的套件有何差別?

若要刪除虛擬環境
$ rmvirtualenv 專案名

jetson-stats

$ sudo -H pip3 install -U jetson-stats

🚀 That's it! 🚀
記得還要重開機後, jetson-stats 的工具才能被啟動

  • Jtop 監控工具:可用來查看系統運作時 CPU、GPU、記憶體各硬體的情況

    $ jtop

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    按 q 離開監控畫面

    程式中可直接使用 jetson-stats 所提供的函式庫的範例說明

  • jetson_config:

    Check jetson-stats health, enable/disable desktop, enable/disable jetson_clocks, improve the performance of your wifi are available only in one click using
    $ jetson_config

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • jetson_release

    顯示 NVIDIA Jetson 的狀態與所有資訊
    $ jetson_release

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • jetson_swap

    很簡單的就能管理 Jetson Nano 上的 swapfile.
    $ sudo jetson_swap

  • jetson variables

    $ export | grep JETSON

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

CUDA

  • 檢查目前已安裝的 CUDA 版本 (在初始燒錄 Jetson Nano 時就預設已安裝)
    $ nvcc -V

    一開始若直接執行 nvcc -V 是不會成功的,因為在環境變數中尚未設定 CUDA 的路徑。所以, 須直接指名路徑來執行
    $ /usr/local/cuda/bin/nvcc -V

    得知我的 Jetson Nano 預設已裝的是 release 10.2, V10.2.89

  • 將路徑加入環境變數中
    $ ls -al /usr/local
    可以得知目前的 cuda 所鏈結的目錄是 cuda-10.2
    $ sudo nano ~/.bashrc
    在最後面加入以下內容:

    ​​​​export CUDA_HOME=/usr/local/cuda
    ​​​​export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
    ​​​​export PATH=/usr/local/cuda/bin:$PATH
    

    編輯完成後,存檔離開, 下指令使其即刻生效
    $ source ~/.bashrc
    再下指令測試, 可發現已可取得版本資訊, 代表路徑變數設定已成功
    $ nvcc -V

    安装 PyCuda
    $ pip3 install 'pycuda>=2019.1.1'

cuDNN

  • 檢查目前已安裝的 cuDNN 版本 (在初始燒錄 Jetson Nano 時就預設已安裝)
    $ ls -al /usr/src
    可以看到檔案列表中有一個範例程式的目錄 (我的是 cudnn_sample_v8)
    $ cd /usr/src/cudnn_samples_v8/mnistCUDNN
    $ sudo make
    $ sudo chmod a+x mnistCUDNN
    $ ./mnistCUDNN
    執行後, 接著會看到一些運算的過程, 直到最後一句是 "Test passed!" 表示內建的 cuDNN 是運作正常的。

OpenCV

  • 檢查目前已安裝的 OpenCV 版本 (在初始燒錄 Jetson Nano 時就預設已安裝)
    $ python3
    ​​​​>>> import cv2
    ​​​​>>> cv2.__version__
    ​​​​'4.1.1'
    ​​​​>>> exit()
    
    由上可得知, OpenCV 已正常安裝, 且目前版本為 4.1.1
    $ sudo find / -name "cv2*"
    可查得 OpenCV 的安裝載入位置

PyTorch

for python 3.6 版本:
$ wget https://nvidia.box.com/shared/static/9eptse6jyly1ggt9axbja2yrmj6pbarc.whl -O torch-1.6.0-cp36-cp36m-linux_aarch64.whl
$ sudo apt-get install libopenblas-base libopenmpi-dev
$ pip3 install Cython
$ pip3 install numpy torch-1.6.0-cp36-cp36m-linux_aarch64.whl

TensorRT

  • 檢查目前已安裝的 TensorRT 版本 (在初始燒錄 Jetson Nano 時就預設已安裝)
    $ dpkg -l | grep TensorRT
    可得知 TensorRT 版本為 7.1.3

TensorFlow GPU

  • 先檢查一下, 目前 JetPack 的版本
    $ jetson_release
    我的是 JetPack 4.4 [L4T 32.4.3]
  • 在安裝 TensorFlow 前, 須先裝裝一些前置套件
    $ sudo apt-get update
    $ sudo apt-get install libhdf5-serial-dev hdf5-tools libhdf5-dev zlib1g-dev zip libjpeg8-dev liblapack-dev libblas-dev gfortran
  • 更新 pip3
    $ sudo apt-get install python3-pip
    $ sudo pip3 install -U pip testresources setuptools
  • 安裝一些相關的套件
    $ sudo pip3 install -U numpy1.16.1 future0.18.2 mock3.0.5 h5py2.10.0 keras_preprocessing1.1.1 keras_applications1.0.8 gast==0.2.2 futures protobuf pybind11
  • 安裝最新版的 TensorFlow, 相容於 JetPack 4.4
    $ sudo pip3 install pre extra-index-url https://developer.download.nvidia.com/compute/redist/jp/v44 tensorflow
  • 檢查目前已安裝的 TensorFlow 版本
    $ python3
    ​​​​>>> import tensorflow
    ​​​​顯示訊息如 .... I tensorflow/stream_executor/.... Successfully opened ...
    ​​​​>>> exit()
    
    若無錯誤訊息出現, 表示已安裝成功

    若要解除安裝 TensorFlow 相當容易, 只須下指令如下:
    $ sudo pip3 uninstall -y tensorflow

Keras

  • 既然裝了 TensorFlow,那就把 Keras 也安装上,它能讓 TensorFlow使用上變得更簡單些
    $ pip3 install keras
  • 檢查目前已安裝的 Keras 版本
    $ python3
    ​​​​>>> import keras
    ​​​​顯示訊息如 .... I tensorflow/stream_executor/.... Successfully opened ...
    ​​​​>>> exit()
    
    若無錯誤訊息出現, 出現剛剛 Tensorflow 一樣的訊息

一個簡單的線性代數測試程式

import tensorflow as tf import numpy as np import matplotlib.pyplot as plt x_data = np.linspace(-0.5, 0.5, 200)[:, np.newaxis] noise = np.random.normal(0, 0.02, x_data.shape) y_data = np.square(x_data) + noise x = tf.placeholder(tf.float32, [None, 1]) y = tf.placeholder(tf.float32, [None, 1]) # 输入层一个神经元,输出层一个神经元,中间10个 # 第一层 Weights_L1 = tf.Variable(tf.random.normal([1, 10])) Biases_L1 = tf.Variable(tf.zeros([1, 10])) Wx_plus_b_L1 = tf.matmul(x, Weights_L1) + Biases_L1 L1 = tf.nn.tanh(Wx_plus_b_L1) # 第二层 Weights_L2 = tf.Variable(tf.random.normal([10, 1])) Biases_L2 = tf.Variable(tf.zeros([1, 1])) Wx_plus_b_L2 = tf.matmul(L1, Weights_L2) + Biases_L2 pred = tf.nn.tanh(Wx_plus_b_L2) # 损失函数 loss = tf.reduce_mean(tf.square(y - pred)) # 训练 train = tf.train.GradientDescentOptimizer(0.1).minimize(loss) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(2000): sess.run(train, feed_dict={x: x_data, y: y_data}) print("第{0}次,loss = {1}".format(i, sess.run(loss,feed_dict={x: x_data, y: y_data}))) pred_vaule = sess.run(pred, feed_dict={x: x_data}) plt.figure() plt.scatter(x_data, y_data) plt.plot(x_data, pred_vaule, 'r-', lw=5)

YOLO3-4 GPU Accelerated Version

$ pip3 install yolo34py-gpu

Jetson.GPIO

參考 https://www.jianshu.com/p/f98a69b94deb
讓我們能透過 Python 操控 Jetson Nano 上的 40組 GPIO 接腳。

  • 安裝 GPIO 函式庫 (預設已安裝)
    $ sudo pip install Jetson.GPIO
    $ sudo pip3 install Jetson.GPIO
  • 給予權限
    $ sudo groupadd -f -r gpio
    $ sudo usermod -a -G gpio 你的帳號
  • 測試是否已安裝可被引用
    $ python3
    ​​​​>>> import Jetson.GPIO as GPIO
    ​​​​若無錯誤訊息出現, 代表 OK
    ​​​​>>> exit()
    

測試 CSI camera 畫面

$ gst-launch-1.0 nvarguscamerasrc ! 'video/x-raw(memory:NVMM), width=1024, height=768, framerate=21/1, format=NV12' ! nvvidconv flip-method=0 ! 'video/x-raw,width=960, height=616' ! nvvidconv ! nvegltransform ! nveglglessink -e