網頁

2021年6月17日 星期四

LPD LPR on DeepStream

參考
https://developer.nvidia.com/blog/creating-a-real-time-license-plate-detection-and-recognition-app
https://github.com/NVIDIA-AI-IOT/deepstream_lpr_app
https://blogs.nvidia.com.tw/2021/05/12/creating-a-real-time-license-plate-detection-and-recognition-app/

依據 CUDA cuDNN TensorRT 的版本,下載 tlt-converter
https://developer.nvidia.com/tlt-get-started


完整程序
~/Data/DeepStream$ mkdir deepstream_lpr_app
~/Data/DeepStream$ cd deepstream_lpr_app/
~/Data/DeepStream/deepstream_lpr_app$ git clone https://github.com/NVIDIA-AI-IOT/deepstream_lpr_app.git
~/Data/DeepStream/deepstream_lpr_app$ cd deepstream_lpr_app/
~/Data/DeepStream/deepstream_lpr_app/deepstream_lpr_app$ ./download_us.sh
~/Data/DeepStream/deepstream_lpr_app/deepstream_lpr_app$ ../../tlt-converter/cuda11.0_cudnn8.0_trt7.2/tlt-converter -k nvidia_tlt -p image_input,1x3x48x96,4x3x48x96,16x3x48x96 models/LP/LPR/us_lprnet_baseline18_deployable.etlt -t fp16 -e models/LP/LPR/lpr_us_onnx_b16.engine
~/Data/DeepStream/deepstream_lpr_app/deepstream_lpr_app$ make
~/Data/DeepStream/deepstream_lpr_app/deepstream_lpr_app$ cd deepstream-lpr-app/
~/Data/DeepStream/deepstream_lpr_app/deepstream_lpr_app/deepstream-lpr-app$ cp dict_us.txt dict.txt
~/Data/DeepStream/deepstream_lpr_app/deepstream_lpr_app/deepstream-lpr-app$ ./deepstream-lpr-app 1 2 0 /opt/nvidia/deepstream/deepstream-5.1/samples/streams/sample_720p.mp4 output.264
以上可以執行,並在第一次執行時產生 engine
也可以利用下列命令 tlt-converter 產生 engine
~/Data/DeepStream/deepstream_lpr_app/deepstream_lpr_app/deepstream-lpr-app$ cd ../models/tlt_pretrained_models/trafficcamnet/
~/Data/DeepStream/deepstream_lpr_app/deepstream_lpr_app/models/tlt_pretrained_models/trafficcamnet$ mv trafficnet_int8.txt trafficnet_int8.bin
~/Data/DeepStream/deepstream_lpr_app/deepstream_lpr_app/models/tlt_pretrained_models/trafficcamnet$ ../../../../../tlt-converter/cuda11.0_cudnn8.0_trt7.2/tlt-converter -k tlt_encode -o output_bbox/BiasAdd,output_cov/Sigmoid -d 3,544,960 -t int8 -c trafficnet_int8.bin -e resnet18_trafficcamnet_pruned.etlt_b1_gpu0_int8.engine resnet18_trafficcamnet_pruned.etlt
~/Data/DeepStream/deepstream_lpr_app/deepstream_lpr_app/models/tlt_pretrained_models/trafficcamnet$ cd ../../LP/LPD/
~/Data/DeepStream/deepstream_lpr_app/deepstream_lpr_app/models/LP/LPD$ ../../../../../tlt-converter/cuda11.0_cudnn8.0_trt7.2/tlt-converter -k nvidia_tlt -o output_bbox/BiasAdd,output_cov/Sigmoid -d 3,480,640 -t int8 -c usa_lpd_cal.bin -e usa_pruned.etlt_b16_gpu0_int8.engine usa_pruned.etlt
~/Data/DeepStream/deepstream_lpr_app/deepstream_lpr_app/models/LP/LPD$ cd ../../../deepstream-lpr-app/
~/Data/DeepStream/deepstream_lpr_app/deepstream_lpr_app/deepstream-lpr-app$ vi lpd_us_config.txt

不顯示
~/Data/DeepStream/deepstream_lpr_app/deepstream_lpr_app/deepstream-lpr-app$ ./deepstream-lpr-app 1 2 0 /opt/nvidia/deepstream/deepstream-5.1/samples/streams/sample_720p.mp4 output.264
有顯示
~/Data/DeepStream/deepstream_lpr_app/deepstream_lpr_app/deepstream-lpr-app$ ./deepstream-lpr-app 1 3 0 /opt/nvidia/deepstream/deepstream-5.1/samples/streams/sample_720p.mp4 output.264


重新訓練模型
使用 tlt-launcher
python3 -m venv --system-site-packages tlt
source ~/envs/tlt/bin/activate
(tlt) $ pip3 install nvidia-pyindex
(tlt) $ pip3 install nvidia-tlt
(tlt) $ cat ~/.tlt_mounts.json
{
    "Mounts": [
        {
            "source": "/home/username/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/tlt-experiments",
            "destination": "/workspace/tlt-experiments"
        },
        {
            "source": "/home/username/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/openalpr",
            "destination": "/workspace/openalpr"
        }

    ]
}
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr$ git clone https://github.com/openalpr/benchmarks benchmarks
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr$ mkdir tlt-experiments
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr$ mkdir openalpr
重新訓練 LPD 模型
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr$ cd openalpr/
下載 https://github.com/NVIDIA-AI-IOT/deepstream_tlt_apps/blob/release/tlt3.0/misc/dev_blog/LPDR/lpd/lpd_prepare_data.py
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/openalpr$ python3 lpd_prepare_data.py --input_dir ../benchmarks/endtoend/us --output_dir lpd --target_width 640 --target_height 480
下載 https://github.com/NVIDIA-AI-IOT/deepstream_tlt_apps/blob/release/tlt3.0/misc/dev_blog/LPDR/lpd/SPECS_tfrecord.txt
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/openalpr$ tlt detectnet_v2 dataset_convert -d /workspace/openalpr/SPECS_tfrecord.txt -o /workspace/openalpr/lpd_tfrecord/lpd
下載 https://github.com/NVIDIA-AI-IOT/deepstream_tlt_apps/blob/release/tlt3.0/misc/dev_blog/LPDR/lpd/SPECS_train.txt
下載預先訓練模型 (tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/openalpr$ ngc registry model download-version "nvidia/tlt_lpdnet:unpruned_v1.0"
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/openalpr$ mv tlt_lpdnet_vunpruned_v1.0/usa_unpruned.tlt .
訓練
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/openalpr$ tlt detectnet_v2 train -e /workspace/openalpr/SPECS_train.txt -r /workspace/openalpr/exp_unpruned -k nvidia_tlt
修剪
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/openalpr$ tlt detectnet_v2 prune -m /workspace/openalpr/exp_unpruned/weights/model.tlt -o /workspace/openalpr/exp_pruned/model_pruned_06.tlt -pth 0.6 -k nvidia_tlt
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/openalpr$ cp SPECS_train.txt SPECS_retrain.txt
修改 pretrained_model_file: "/workspace/openalpr/exp_pruned/model_pruned_06.tlt"
再次訓練
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/openalpr$ tlt detectnet_v2 train -e /workspace/openalpr/SPECS_retrain.txt -r /workspace/openalpr/exp_pruned -k nvidia_tlt
匯出
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/openalpr$ tlt detectnet_v2 export -m /workspace/openalpr/exp_unpruned/weights/model.tlt -o /workspace/openalpr/export/unpruned_model.etlt --cal_cache_file /workspace/openalpr/export/unpruned_calibration.bin -e /workspace/openalpr/SPECS_train.txt -k nvidia_tlt --cal_image_dir /workspace/openalpr/lpd/data/image --data_type int8 --batch_size 4 --batches 10 --engine_file /workspace/openalpr/export/unpruned_int8.trt
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/openalpr$ tlt detectnet_v2 export -m /workspace/openalpr/exp_pruned/weights/model.tlt -o /workspace/openalpr/export/pruned_model.etlt --cal_cache_file /workspace/openalpr/export/pruned_calibration.bin -e /workspace/openalpr/SPECS_train.txt -k nvidia_tlt --cal_image_dir /workspace/openalpr/lpd/data/image --data_type int8 --batch_size 4 --batches 10 --engine_file /workspace/openalpr/export/pruned_int8.trt
評估
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/openalpr$ tlt detectnet_v2 evaluate -m /workspace/openalpr/exp_unpruned/weights/model.tlt -k nvidia_tlt -e /workspace/openalpr/SPECS_train.txt
(tlt) mark@z390:~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/openalpr$ tlt detectnet_v2 evaluate -m /workspace/openalpr/exp_pruned/weights/model.tlt -k nvidia_tlt -e /workspace/openalpr/SPECS_train.txt
重新訓練 LPR 模型
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/tlt-experiments$ mkdir lprnet
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/tlt-experiments$ cd lprnet/
下載 https://github.com/NVIDIA-AI-IOT/deepstream_tlt_apps/blob/release/tlt3.0/misc/dev_blog/LPDR/lpr/preprocess_openalpr_benchmark.py
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/tlt-experiments/lprnet$ python3 preprocess_openalpr_benchmark.py --input_dir=../../benchmarks/endtoend/us --output_dir=../data/openalpr
下載 https://github.com/NVIDIA-AI-IOT/deepstream_tlt_apps/blob/release/tlt3.0/misc/dev_blog/LPDR/lpr/tutorial_spec.txt
修改 tutorial_spec.txt
augmentation_config {
    output_width: 96
    output_height: 48
    output_channel: 3
    keep_original_prob: 0.3
    transform_prob: 0.5
    rotate_degree: 5
}
成為
augmentation_config {
    output_width: 96
    output_height: 48
    output_channel: 3
    keep_original_prob: 0.3
    max_rotate_degree: 5
    rotate_prob: 0.5
    blur_prob: 0.5
}

下載預先訓練模型
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/tlt-experiments/lprnet$ ngc registry model download-version "nvidia/tlt_lprnet:trainable_v1.0"
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/tlt-experiments/lprnet$ cp tlt_lprnet_vtrainable_v1.0/us_lprnet_baseline18_trainable.tlt .
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/tlt-experiments/lprnet$ mkdir specs
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/tlt-experiments/lprnet$ cp tlt_lprnet_vtrainable_v1.0/us_lp_characters.txt specs/
訓練模型
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/tlt-experiments/lprnet$ tlt lprnet train -e /workspace/tlt-experiments/lprnet/tutorial_spec.txt -r /workspace/tlt-experiments/lprnet/ -k nvidia_tlt -m /workspace/tlt-experiments/lprnet/us_lprnet_baseline18_trainable.tlt --use_amp
匯出模型
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/tlt-experiments/lprnet$ tlt lprnet export -m /workspace/tlt-experiments/lprnet/weights/lprnet_epoch-24.tlt -k nvidia_tlt -e /workspace/tlt-experiments/lprnet/tutorial_spec.txt
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/tlt-experiments/lprnet$ ls weights/lprnet_epoch-24.etlt
評估
(tlt) ~/Data/DeepStream/deepstream_lpr_app/tlt-lpdr/tlt-experiments/lprnet$ tlt lprnet evaluate -m /workspace/tlt-experiments/lprnet/weights/lprnet_epoch-24.tlt -k nvidia_tlt -e /workspace/tlt-experiments/lprnet/tutorial_spec.txt

沒有留言:

張貼留言