網頁

2022年5月11日 星期三

Nvidia TAO(Train, Adapt, and Optimize)

Nvidia TLT(Transfer Learning Toolkit) 改版變成 Nvidia TAO(Train, Adapt, and Optimize)


$ curl https://get.docker.com | sh \
  && sudo systemctl --now enable docker
$ sudo groupadd docker
$ sudo usermod -aG docker $USER
$ docker run hellow-world

$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
      && curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
      && curl -s -L https://nvidia.github.io/libnvidia-container/$distribution/libnvidia-container.list | \
            sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
            sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
$ sudo apt-get update
$ sudo apt-get install -y nvidia-docker2
$ sudo systemctl restart docker
$  sudo docker run --rm --gpus all nvidia/cuda:11.0.3-base-ubuntu20.04 nvidia-smi

$ wget -O ngccli_linux.zip https://ngc.nvidia.com/downloads/ngccli_linux.zip && unzip -o ngccli_linux.zip && chmod u+x ngc
$ md5sum -c ngc.md5
$ echo "export PATH=\"\$PATH:$(pwd)\"" >> ~/.bash_profile && source ~/.bash_profile
$ ngc config set
$ docker login nvcr.io
Username: $oauthtoken
Password: YOUR_NGC_API_KEY
$ ngc registry model list
$ ngc registry image list

$ pip3 install virtualenv virtualenvwrapper
$ mkdir $HOME/.virtualenvs
$ vi ~/.bashrc
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /home/mark/.local/bin/virtualenvwrapper.sh
$ source ~/.bashrc

$ mkvirtualenv tao -p /usr/bin/python3
$ deactivate
$ workon tao

$ pip3 install nvidia-pyindex
$ pip3 install nvidia-tao
$ pip3 install jupyter
$ tao info

$ cd Data/tao/
# 下載 使用 WGET
$ wget --content-disposition https://api.ngc.nvidia.com/v2/resources/nvidia/tao/cv_samples/versions/v1.3.0/zip -O cv_samples_v1.3.0.zip
$ unzip -u cv_samples_v1.3.0.zip  -d ./cv_samples_v1.3.0 && rm -rf cv_samples_v1.3.0.zip && cd ./cv_samples_v1.3.0
# 下載 使用 CLI
$ ngc registry resource download-version "nvidia/tao/cv_samples:v1.3.0"
$ cd ./cv_samples_v1.3.0
# 啟動 jupyter
$ jupyter notebook --ip 0.0.0.0 --port 8888 --allow-root
# 開啟網頁 http://0.0.0.0:8888/tree

以 http://0.0.0.0:8888/notebooks/detectnet_v2/detectnet_v2.ipynb 為例
並參考 TAO 命令及參數文件 Object Detection/DetectNet_v2
修改
os.environ["LOCAL_PROJECT_DIR"] = FIXME
os.environ["LOCAL_PROJECT_DIR"] = "your_host_path_to/cv_samples_vv1.3.0"

會自動產生 ~/.tao_mounts.json

安裝 TAO launcher
!pip3 install nvidia-pyindex
!pip3 install nvidia-tao

下載 kitti object detection 資料集
到 http://www.cvlibs.net/datasets/kitti/eval_object.php?obj_benchmark=2d 網站
申請帳號,並下載
Download left color images of object data set (12 GB) 左眼看到的景
Download training labels of object data set (5 MB)

產生 Tfrecords
下載並安裝 NGC CLI
使用 ngc 下載 pretrained_detectnet_v2:resnet18
train evaluate prune (re)train evaluate inference 
export 產生 resnet18_detector.etlt

Int8 Optimization (效果不好,請改用 QAT)
 calibration_tensorfile 產生 calibration.tensor
 export 產生 calibration.bin resnet18_detector.etlt resnet18_detector.trt.int8(可用於 inference)
 converter 產生 resnet18_detector.trt(可用於 inference)
 inference (效果不好,請改用 QAT)

QAT 需要重新 train
  train evaluate export evaluate inference
 
列出 tao task
$ tao list
刪除 tao task
$ tao stop --container_id 40e28a67c2

inference config file
inferencer_config {
  tensorrt_config {
    etlt_model: "/workspace/tao-experiments/detectnet_v2/experiment_dir_final/resnet18_detector.etlt"
    save_engine: true
    trt_engine: "/workspace/tao-experiments/detectnet_v2/experiment_dir_final/resnet18_detector.trt"
    calibrator_config {
      calibration_cache: "/workspace/tao-experiments/detectnet_v2/experiment_dir_final/calibration.bin"
    }
  }
}
若是 trt_engine 檔案尚未產生,會由 etlt_model 產生 trt_engine
若是已經有 trt_engine,直接使用

2022年4月20日 星期三

Build TensorRT OSS(Open Source Software)

參考 TensorRT Open Source Software


$ git clone -b master https://github.com/nvidia/TensorRT TensorRT_OSS
$ git describe --tags
8.2.0-EA-2-g96e2397
$ cd ..
$ mv TensorRT_OSS TensorRT_OSS-8.2.0 
$ cd TensorRT_OSS-8.2.0/
$ git submodule update --init --recursive
下列三個紅底黑字需一致
$ ./docker/build.sh --file docker/ubuntu-18.04.Dockerfile --tag tensorrt-ubuntu18.04-cuda11.4 --cuda 11.4.2
Building container:
> docker build -f docker/ubuntu-18.04.Dockerfile --build-arg CUDA_VERSION=11.4.2 --build-arg uid=1000 --build-arg gid=1000 --tag=tensorrt-ubuntu18.04-cuda11.4 .
Sending build context to Docker daemon  95.59MB
Step 1/31 : ARG CUDA_VERSION=11.4.2
Step 2/31 : ARG OS_VERSION=18.04
Step 3/31 : FROM nvidia/cuda:${CUDA_VERSION}-cudnn8-devel-ubuntu${OS_VERSION}
11.4.2-cudnn8-devel-ubuntu18.04: Pulling from nvidia/cuda
11323ed2c653: Already exists 
fc717811e279: Pull complete 
ccdba3c8eced: Pull complete 
2e5962ccde0c: Pull complete 
59fd9269dfdb: Pull complete 
ca8ef6b7b26f: Pull complete 
075319d1b973: Pull complete 
a7391b62d0c1: Pull complete 
ef615d2b7b27: Pull complete 
312d7b759a5a: Pull complete 
Digest: sha256:3ccdc2ec6ab45d9e902ac799eb4bfba7a39e2b3057962a95dd2b717493f8e252
Status: Downloaded newer image for nvidia/cuda:11.4.2-cudnn8-devel-ubuntu18.04
 ---> 58c89511016d
Step 4/31 : LABEL maintainer="NVIDIA CORPORATION"
 ---> Running in af3ded1c4476
Removing intermediate container af3ded1c4476
 ---> a72393f203bc
Step 5/31 : ENV TRT_VERSION 8.2.0.6
 ---> Running in 948c6eba13b7
Removing intermediate container 948c6eba13b7
 ---> 67dbe1482cfd

$ ./docker/launch.sh --tag tensorrt-ubuntu18.04-cuda11.4 --gpus all
trtuser@ae7dd87a158f:/workspace$ cd $TRT_OSSPATH
trtuser@ae7dd87a158f:/workspace/TensorRT$ mkdir -p build && cd build
trtuser@ae7dd87a158f:/workspace/TensorRT/build$ cmake .. -DTRT_LIB_DIR=$TRT_LIBPATH -DTRT_OUT_DIR=`pwd`/out
trtuser@ae7dd87a158f:/workspace/TensorRT/build$ make -j$(nproc)
trtuser@ae7dd87a158f:/workspace/TensorRT/build$ exit

$ sudo mv /usr/lib/x86_64-linux-gnu/libnvinfer_plugin.so.8.2.3 backup/
$ sudo cp TensorRT_OSS-8.2.0/build/out/libnvinfer_plugin.so.8.2.0 /usr/lib/x86_64-linux-gnu/libnvinfer_plugin.so.8.2.3
$ sudo ldconfig


2022年3月2日 星期三

TensorFlow to TensorRT

$ pip install onnxruntime
$ pip install -U tf2onnx
python -m tf2onnx.convert \
--saved-model tensorflow-model-path \
--output output.onnx

2022年2月16日 星期三

yolo v4 to TensorRt

參考: tensorrt_demos

$ cd TensorRT
$ git clone https://github.com/jkjung-avt/tensorrt_demos.git
$ cd tensorrt_demos

安裝環境
$ cd yolo
$ ./install_pycuda.sh
$ pip3 install onnx==1.4.1
$ cd ../plugins
$ make

下載 yolo 的 weights 和 cfg
$ cd ../yolo
$ ./download_yolo.sh
轉換 weights 到 trt
$ python3 yolo_to_onnx.py -m yolov4-tiny-416
$ python3 onnx_to_tensorrt.py -m yolov4-tiny-416
測試
$ cd ..
$ python trt_yolo.py --image doc/dog_trt_yolov4_416.jpg -m yolov4-tiny-416

使用 int8
$ cd yolo
$ ln -s yolov4-tiny-416.cfg yolov4-tiny-int8-416.cfg
$ ln -s yolov4-tiny-416.onnx yolov4-tiny-int8-416.onnx
$ mkdir calib_images
# and copy our image to calib_images
$ python3 onnx_to_tensorrt.py -v --int8 -m yolov4-tiny-int8-416
會產生如下錯誤
[03/02/2022-15:22:07] [TRT] [V] 001_convolutional + 001_convolutional_bn Set Tactic Name: sm70_xmma_fprop_implicit_gemm_f16f16_f16f16_f16_nhwckrsc_nhwc_tilesize128x256x32_stage1_warpsize2x4x1_g1_tensor8x8x4_t1r3s3 Tactic: 46202665595848747
[03/02/2022-15:22:07] [TRT] [V] Deleting timing cache: 2020 entries, 504 hits
[03/02/2022-15:22:07] [TRT] [E] 1: Unexpected exception
ERROR: failed to build the TensorRT engine!
$ vi onnx_to_tensorrt.py
將 from calibrator import YOLOEntropyCalibrator 移到程式開頭

2022年2月9日 星期三

Yolo tiny v4 to tensorflow and tflite

參考 tensorflow-yolov4-tflite

只能使用 tensorflow==2.3.0rc0
不要使用別的版本,也不要用 GPU

視情況修改 core/config.py
__C.YOLO.CLASSES
__C.YOLO.ANCHORS_TINY

for tensorflow format load by tf.saved_model.load()
$ python save_model.py --weights /your_path_to/weights/yolov4-tiny-vehicle-r_final.weights \
--output ./checkpoints/yolov4-tiny-416 \
--input_size 416 --model yolov4 --tiny
$ python convert_tflite.py --weights ./checkpoints/yolov4-tiny-416-tflite \
--output ./checkpoints/yolov4-tiny-416.tflite

for tensorflow tflite load  by tf.lite.Interpreter()
$ python save_model.py --weights /your_path_to/weights/yolov4-tiny-vehicle-r_final.weights \
--output ./checkpoints/yolov4-tiny-416-tflite \
--input_size 416 --model yolov4 --tiny --framework tflite
$ python convert_tflite.py --weights ./checkpoints/yolov4-tiny-416-tflite \
--output ./checkpoints/yolov4-tiny-416-fp16.tflite \
--quantize_mode float16

2022年1月22日 星期六

2022年1月14日 星期五

deepstream with python

參考 DeepStream Python Apps
$ git clone https://github.com/NVIDIA-AI-IOT/deepstream_python_apps.git
$ cd deepstream_python_apps/bindings/
$ git submodule update --init
$ cd ../3rdparty/gst-python/
$ ./autogen.sh
$ make
$ make  install
$ cd  ../../bindings/
$ mkdir build
$ cd build
$ cmake ..
$ make
$ cd ..
$ mkdir export_pyds
$ cp build/pyds*.whl export_pyds
$ pip3 install export_pyds/pyds-1.1.0-py3-none-linux_x86_64.whl