IT之家 1 月 2 日消息,OpenAtom 社區(qū)已于 12 月 31 日發(fā)布了 OpenHarmony-v3.1-Beta 版本。
當(dāng)前版本在 OpenHarmony 3.0 LTS 的基礎(chǔ)上,更新支持了以下能力:
標(biāo)準(zhǔn)系統(tǒng) OS 基礎(chǔ)能力增強(qiáng):內(nèi)核提升 CMA 利用率特性、圖形新增支持 RenderService 渲染后端引擎、短距離通信支持 STA(Station)和 SoftAP 基礎(chǔ)特性、支持地磁場(chǎng)的算法接口、傳感器驅(qū)動(dòng)模型能力增強(qiáng)、支持應(yīng)用帳號(hào)信息查詢和訂閱等、全球化特性支持、編譯構(gòu)建支持統(tǒng)一的構(gòu)建模板、編譯運(yùn)行時(shí)提供 Windows / MacOS / Linux 的前端編譯工具鏈、JS 運(yùn)行時(shí)支持預(yù)覽器、新增支持 JSON 處理、Eventbus、Vcard、Protobuf、RxJS、LibphoneNumber 等 6 個(gè) JS 三方庫(kù)、新增時(shí)間時(shí)區(qū)管理、DFX 新增支持 HiSysEvent 部件提供查詢和訂閱接口。
標(biāo)準(zhǔn)系統(tǒng)分布式能力增強(qiáng):包括新增支持分布式 DeviceProfile 特性、分布式數(shù)據(jù)管理支持跨設(shè)備同步和訂閱、分布式軟總線支持網(wǎng)絡(luò)切換組網(wǎng)、分布式文件系統(tǒng)支持 Statfs API 能力等。
標(biāo)準(zhǔn)系統(tǒng)應(yīng)用程序框架能力增強(qiáng):新增 ArkUI 自定義繪制能力和 Lottie 動(dòng)畫能力、新增包管理探秘隱式查詢和多 hap 包安裝、事件通知支持權(quán)限管理、設(shè)置通知振動(dòng)、通知聲音設(shè)置和查詢、通知免打擾、會(huì)話類通知等。
標(biāo)準(zhǔn)系統(tǒng)應(yīng)用能力增強(qiáng):輸入法應(yīng)用支持文本輸入和橫屏下布局顯示、短信應(yīng)用信息管理、聯(lián)系人應(yīng)用通話記錄和撥號(hào)盤顯示、設(shè)置應(yīng)用更多設(shè)置項(xiàng)。
輕量系統(tǒng)能力增強(qiáng):HiStreamer 輕量級(jí)支持可定制的媒體管線框架、Linux 版本 init 支持熱插拔、OS 輕內(nèi)核 & 驅(qū)動(dòng)啟動(dòng)優(yōu)化、快速啟動(dòng)能力支持。
IT之家了解到,開發(fā)者現(xiàn)可通過 repo + ssh 下載(需注冊(cè)公鑰)或者通過 repo + https 下載源碼,芯片及開發(fā)板適配狀態(tài)請(qǐng)參考官方 SIG-Devboard 信息。
方式一(推薦)
通過 repo + ssh 下載(需注冊(cè)公鑰,請(qǐng)參考碼云幫助中心)。
repo init -u git@gitee.com:openharmony/manifest.git -b refs/tags/OpenHarmony-v3.1-Beta --no-repo-verifyrepo sync -crepo forall -c 'git lfs pull'
方式二
通過 repo + https 下載。
repo init -u https://gitee.com/openharmony/manifest.git -b refs/tags/OpenHarmony-v3.1-Beta --no-repo-verifyrepo sync -crepo forall -c 'git lfs pull'
據(jù)公開資料,OpenHarmony 是由開放原子開源基金會(huì)(OpenAtom Foundation)孵化及運(yùn)營(yíng)的開源項(xiàng)目,目標(biāo)是面向全場(chǎng)景、全連接、全智能時(shí)代,搭建一個(gè)智能終端設(shè)備操作系統(tǒng)的框架和平臺(tái),促進(jìn)萬(wàn)物互聯(lián)產(chǎn)業(yè)的繁榮發(fā)展。
OpenHarmony 支持如下幾種系統(tǒng)類型:
輕量系統(tǒng)(mini system)
面向 MCU 類處理器例如 Arm Cortex-M、RISC-V 32 位的設(shè)備,硬件資源極其有限,支持的設(shè)備最小內(nèi)存為 128KiB,可以提供多種輕量級(jí)網(wǎng)絡(luò)協(xié)議,輕量級(jí)的圖形框架,以及豐富的 IOT 總線讀寫部件等。可支撐的產(chǎn)品如智能家居領(lǐng)域的連接類模組、傳感器設(shè)備、穿戴類設(shè)備等。
小型系統(tǒng)(small system)
面向應(yīng)用處理器例如 Arm Cortex-A 的設(shè)備,支持的設(shè)備最小內(nèi)存為 1MiB,可以提供更高的安全能力、標(biāo)準(zhǔn)的圖形框架、視頻編解碼的多媒體能力。可支撐的產(chǎn)品如智能家居領(lǐng)域的 IP Camera、電子貓眼、路由器以及智慧出行域的行車記錄儀等。
標(biāo)準(zhǔn)系統(tǒng)(standard system)
面向應(yīng)用處理器例如 Arm Cortex-A 的設(shè)備,支持的設(shè)備最小內(nèi)存為 128MiB,可以提供增強(qiáng)的交互能力、3D GPU 以及硬件合成能力、更多控件以及動(dòng)效更豐富的圖形能力、完整的應(yīng)用框架。可支撐的產(chǎn)品如高端的冰箱顯示屏。
OpenHarmony 主庫(kù)組織地址:https://gitee.com/openharmony
OpenHarmonySIG 組織地址:https://gitee.com/openharmony-sig
OpenHarmony 三方庫(kù)組織地址:https://gitee.com/openharmony-tpc
OpenHarmony 歸檔組織地址:https://gitee.com/openharmony-retired
更多內(nèi)容請(qǐng)查閱官方站點(diǎn)。
從在自定義數(shù)據(jù)集中訓(xùn)練檢測(cè)器到使用TensorFlow 1.15在Jetson納米板或云上進(jìn)行推理的詳細(xì)步驟
完整代碼可在GitHub上獲得
這個(gè)清單永無止境……
為克服上述一些問題,我們?cè)贛onk Object Detection Toolkit中的Tensorflow 對(duì)象檢測(cè) API的兩個(gè)版本上添加了一個(gè)低代碼的pythonic包裝器
使用它,開發(fā)人員和研究人員可以輕松地
下面提到的是使用TF訓(xùn)練和部署定制探測(cè)器的過程。在描述過程流程的同時(shí),還強(qiáng)調(diào)了一個(gè)人在使一切正常工作時(shí)所面臨的問題;還提到了tf1.0和2.0版本的對(duì)象檢測(cè)庫(kù)的區(qū)別
讓我們從版本1.0開始,每次使用一個(gè)對(duì)象檢測(cè)API模塊。
將要安裝的庫(kù)
(確保CUDA 10.0和CUDNN 7隨系統(tǒng)一起安裝了NVidia驅(qū)動(dòng)程序)
當(dāng)模型要部署在Jetson Nano板上時(shí),請(qǐng)按照以下說明配置你的開發(fā)(訓(xùn)練)機(jī)器
安裝必備的Python庫(kù)
$ git clone https://github.com/Tessellate-Imaging/Monk_Object_Detection.git
$ cd Monk_Object_Detection/12_tf_obj_1/installation
$ chmod +x install_cuda10_tensorrt6_part1.sh && ./install_cuda10_tensorrt6_part1.sh
安裝TensorRT 6.0.1
# Go to https://developer.nvidia.com/tensorrt
# Download
# - nv-tensorrt-repo-ubuntu1804-cuda10.0-trt6.0.1.5-ga-20190913_1-1_amd64.deb (For Ubuntu18.04)
# - nv-tensorrt-repo-ubuntu1604-cuda10.0-trt6.0.1.5-ga-20190913_1-1_amd64.deb (For Ubuntu16.04)
# Run the following commands to install trt (in a terminal)
$ sudo dpkg -i nv-tensorrt-repo-ubuntu1804-cuda10.0-trt6.0.1.5-ga-20190913_1-1_amd64.deb
$ sudo apt-key add <key value will be mentioned as the output of previous command>
$ sudo apt-get update
$ sudo apt-get install tensorrt
$ sudo apt-get install uff-converter-tf
$ sudo apt-get install python3-libnvinfer-dev
安裝Bazel 0.26.1并從GitHub克隆TensorFlow
# Install bazel version 0.26.1
# Download bazel deb package from https://github.com/bazelbuild/bazel/releases/tag/0.26.1
$ sudo dpkg -i bazel_0.26.1-linux-x86_64.deb
# Clone Tensorflow and switch to tensorflow 1.15.2
$ git clone https://github.com/tensorflow/tensorflow.git
$ cd tensorflow
$ git checkout v1.15.2
配置TensorFlow
# Configure tensorflow
$ ./configure
- Do you wish to build TensorFlow with XLA JIT support? [Y/n]: Y
- Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: N
- Do you wish to build TensorFlow with ROCm support? [y/N]: N
- Do you wish to build TensorFlow with CUDA support? [y/N]: Y
- Do you wish to build TensorFlow with TensorRT support? [y/N]: Y
- And press enter (set default) for all other config questions asked by the setup
構(gòu)建并安裝TensorFlow(在AWS P3.2x實(shí)例上大約需要5個(gè)小時(shí))
# Build tensorflow using bazel
$ bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
# Once built create a wheel file for python installation and run pip installer
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package tensorflow_pkg
$ cd tensorflow_pkg && pip install tensorflow*.whl
最后構(gòu)建對(duì)象檢測(cè)API 1.0
# Compile Object Detection API v1
$ cd Monk_Object_Detection/12_tf_obj_1/installation
$ chmod +x install_cuda10_tensorrt6_part2.sh && ./install_cuda10_tensorrt6_part2.sh
當(dāng)不打算在Jetson Nano Board上部署模型時(shí),請(qǐng)按照以下說明配置你的開發(fā)(訓(xùn)練)機(jī)器
安裝所有必需的庫(kù)并編譯對(duì)象檢測(cè)API 1.0
$ git clone https://github.com/Tessellate-Imaging/Monk_Object_Detection.git
$ cd Monk_Object_Detection/12_tf_obj_1/installation
$ chmod +x install_cuda10.sh && ./install_cuda10.sh
安裝TensorRT 5.1.5作為預(yù)構(gòu)建的TensorFlow 1.15.0支持
# Go to https://developer.nvidia.com/tensorrt
# Download
# - nv-tensorrt-repo-ubuntu1804-cuda10.0-trt5.1.5.0-ga-20190427_1-1_amd64.deb (For Ubuntu18.04)
# - nv-tensorrt-repo-ubuntu1604-cuda10.0-trt5.1.5.0-ga-20190427_1-1_amd64.deb(For Ubuntu16.04)
# Run the following commands to install trt (in a terminal)
$ sudo dpkg -i nv-tensorrt-repo-ubuntu1804-cuda10.0-trt5.1.5.0-ga-20190427_1-1_amd64.deb
$ sudo apt-key add <key value will be mentioned as the output of previous command>
$ sudo apt-get update
$ sudo apt-get install tensorrt
$ sudo apt-get install uff-converter-tf
$ sudo apt-get install python3-libnvinfer-dev
使用google colab時(shí),請(qǐng)遵循以下說明(TensorRT在colab上可能無法正常運(yùn)行)
# Switch to TF 1.0 version (Run the following line)
$ %tensorflow_version 1.x
# Now reset the runetime if prompted by colab
# Run the following commands
$ git clone https://github.com/Tessellate-Imaging/Monk_Object_Detection.git
$ cd Monk_Object_Detection/12_tf_obj_1/installation
$ chmod +x install_colab.sh && ./install_colab.sh
Monk對(duì)象檢測(cè)解析器要求數(shù)據(jù)集采用COCO或Pascal VOC格式。對(duì)于本教程,讓我們堅(jiān)持使用Pascal VOC格式
要將數(shù)據(jù)集從任何格式轉(zhuǎn)換為Pascal VOC,請(qǐng)查看以下詳細(xì)教程
在這個(gè)例子中,船檢測(cè)數(shù)據(jù)集取自一個(gè)關(guān)于使用retinanet進(jìn)行對(duì)象檢測(cè)的舊博客
在這個(gè)jupyter notebook中提到了使用這些數(shù)據(jù)的步驟
加載訓(xùn)練引擎
from train_detector import Detector
gtf=Detector();
在TF 1.15模型庫(kù)中加載所有可用模型
目前,它支持24種不同型號(hào)的SSD和Faster RCNN
加載訓(xùn)練驗(yàn)證數(shù)據(jù)集
將注釋轉(zhuǎn)換為VOC格式后加載數(shù)據(jù)集
根據(jù)可用的GPU設(shè)置批次大小。在本教程中,使用了帶v100gpu(16gbvram)的AWS ec2p3.2x計(jì)算機(jī),批次大小為24非常適合。
train_img_dir="ship/images/Train";
train_anno_dir="ship/voc/";
class_list_file="ship/classes.txt";
gtf.set_train_dataset(train_img_dir, train_anno_dir, class_list_file, batch_size=24)
運(yùn)行解析器將數(shù)據(jù)集轉(zhuǎn)換為tfrecords
Tf Record文件將存儲(chǔ)在data_tfrecord文件夾中
gtf.create_tfrecord(data_output_dir="data_tfrecord")
選擇并加載模型
下載模型后,Monk會(huì)根據(jù)所選參數(shù)自動(dòng)更新配置文件
在本教程中,我們使用了SSD MobileNet V1,它可以接收形狀為320x320x3 RGB圖像的輸入圖像
gtf.set_model_params(model_name="ssd_mobilenet_v1")
設(shè)置其他訓(xùn)練和優(yōu)化器參數(shù)
set_hyper_params(num_train_steps=10000,
lr=0.004,
lr_decay_rate=0.945,
output_dir="output_dir/",
sample_1_of_n_eval_examples=1,
sample_1_of_n_eval_on_train_examples=5,
checkpoint_dir=False,
run_once=False,
max_eval_retries=0,
num_workers=4,
checkpoint_after_every=500)
設(shè)置存儲(chǔ)導(dǎo)出參數(shù)的目錄
gtf.export_params(output_directory="export_dir");
設(shè)置tensorRT優(yōu)化參數(shù)
TensorRT優(yōu)化器創(chuàng)建一個(gè)計(jì)劃,然后構(gòu)建它。構(gòu)建計(jì)劃是為了優(yōu)化它正在構(gòu)建的GPU的模型。
如前所述,在具有不同cuda計(jì)算能力的GPU上優(yōu)化的模型無法在jetson nano上運(yùn)行,因此Monk庫(kù)確保該計(jì)劃在開發(fā)機(jī)(云或colab)上編譯,而該計(jì)劃則在運(yùn)行時(shí)在部署機(jī)(jetson nano)上構(gòu)建
使用INT8優(yōu)化時(shí),無法執(zhí)行此功能,計(jì)劃的編制和構(gòu)建都必須在同一臺(tái)機(jī)器上,并且Jetson納米板與8位整數(shù)運(yùn)算不太兼容
gtf.TensorRT_Optimization_Params(conversion_type="FP16", trt_dir="trt_fp16_dir")
訓(xùn)練探測(cè)器
檢測(cè)器訓(xùn)練運(yùn)行一個(gè)執(zhí)行sys.exit()函數(shù)的會(huì)話,因此在其上運(yùn)行的包裝程序?qū)㈥P(guān)閉python系統(tǒng)。
為了解決此問題,提供了一個(gè)名為train.py的腳本,該腳本可以在jupyter notebook或終端命令上運(yùn)行
根據(jù)參數(shù)設(shè)置,訓(xùn)練好的模型將保存在名為“ output_dir”的文件夾中。
# Run in a terminal
$ python Monk_Object_Detection/12_tf_obj_1/lib/train.py
# or run this command on a jupyter notebook
%run Monk_Object_Detection/12_tf_obj_1/lib/train.py
導(dǎo)出訓(xùn)練有素的檢查點(diǎn)模型
export函數(shù)運(yùn)行一個(gè)執(zhí)行sys.exit()函數(shù)的會(huì)話,因此在其上運(yùn)行的包裝器將關(guān)閉python系統(tǒng)。
為了解決此問題,提供了一個(gè)名為export.py的腳本,該腳本可以在jupyter notebook或終端命令上運(yùn)行
根據(jù)參數(shù)設(shè)置,導(dǎo)出的模型將保存在名為“ export_dir”的文件夾中。
# Run in a terminal
$ python Monk_Object_Detection/12_tf_obj_1/lib/export.py
# or run this command on a jupyter notebook
%run Monk_Object_Detection/12_tf_obj_1/lib/export.py
優(yōu)化導(dǎo)出模型
優(yōu)化函數(shù)運(yùn)行一個(gè)執(zhí)行sys.exit()函數(shù)的會(huì)話,因此在其上運(yùn)行的包裝程序?qū)㈥P(guān)閉python系統(tǒng)。
為了解決此問題,提供了一個(gè)名為optimize.py的腳本,該腳本可以在jupyter notebook電腦或終端命令上運(yùn)行
根據(jù)參數(shù)設(shè)置,優(yōu)化的模型將保存在名為“ trt_fp16_dir”的文件夾中。
# Run in a terminal
$ python Monk_Object_Detection/12_tf_obj_1/lib/optimize.py
# or run this command on a jupyter notebook
%run Monk_Object_Detection/12_tf_obj_1/lib/optimize.py
加載推理機(jī)
from infer_detector import Infer
gtf=Infer();
載入模型
首先加載導(dǎo)出的模型并運(yùn)行步驟,然后通過加載優(yōu)化的模型重復(fù)相同的步驟(步驟保持不變)
# To load exported model
gtf.set_model_params('export_dir/frozen_inference_graph.pb', "ship/classes.txt")
# To load optimized model
gtf.set_model_params('trt_fp16_dir/trt_graph.pb', "ship/classes.txt")
對(duì)單個(gè)圖像進(jìn)行推斷
scores, bboxes, labels=gtf.infer_on_image('ship/test/img1.jpg', thresh=0.1);
使用兩個(gè)模型運(yùn)行速度測(cè)試分析
gtf.benchmark_for_speed('ship/test/img1.jpg')
在AWS P3.2x V100 GPU上使用導(dǎo)出的模型(未優(yōu)化)進(jìn)行分析
Average Image loading time : 0.0091 sec
Average Inference time : 0.0103 sec
Result extraction time : 0.0801 sec
total_repetitions : 100
total_time : 1.0321 sec
images_per_sec : 96
latency_mean : 10.3218 ms
latency_median : 10.3234 ms
latency_min : 9.4773 ms
在AWS P3.2x V100 GPU上使用優(yōu)化模型進(jìn)行分析
處理后優(yōu)化使速度提高約2.5倍
Average Image loading time : 0.0092 sec
Average Inference time : 0.0042 sec
Result extraction time : 0.0807 sec
total_repetitions : 100
total_time : 0.4241 sec
images_per_sec : 235
latency_mean : 4.2412 ms
latency_median : 4.2438 ms
latency_min : 4.0156 ms
步驟1:更新Apt
$ sudo apt-get update
$ sudo apt-get upgrade
步驟2:安裝系統(tǒng)庫(kù)
$ sudo apt-get install nano git cmake libatlas-base-dev gfortran libhdf5-serial-dev hdf5-tools nano locate libfreetype6-dev python3-setuptools protobuf-compiler libprotobuf-dev openssl libssl-dev libcurl4-openssl-dev cython3 libxml2-dev libxslt1-dev python3-pip
$ sudo apt-get install libopenblas-dev libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler libgflags-dev libgoogle-glog-dev liblmdb-dev
$ sudo pip3 install virtualenv virtualenvwrapper
步驟3:更新bashrc文件
將這些行添加到?/ .bashrc文件
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh
export PATH=/usr/local/cuda-10.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64\
${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
運(yùn)行以下命令
$ source ~/.bashrc
步驟4:創(chuàng)建虛擬環(huán)境并安裝所有必需的python庫(kù),安裝numpy大約需要15分鐘
$ mkvirtualenv -p /usr/bin/python3.6 tf2
$ pip install numpy==1.19.1
安裝scipy大約需要40分鐘
$ pip install scipy==1.5.1
安裝Jetson Nano TensorFlow-1.15。再花15分鐘
$ pip install scikit-build protobuf cython -vvvv
$ pip install grpcio absl-py py-cpuinfo psutil portpicker six mock requests gast h5py astor termcolor protobuf keras-applications keras-preprocessing wrapt google-pasta -vvvv
$ pip install https://developer.download.nvidia.com/compute/redist/jp/v43/tensorflow-gpu/tensorflow_gpu-1.15.0+nv19.12-cp36-cp36m-linux_aarch64.whl -vvvv
安裝OpenCV需要1.5個(gè)小時(shí)
$ mkdir opencv && cd opencv
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.1.2.zip
$ unzip opencv.zip
$ mv opencv-4.1.2 opencv
$ cd opencv && mkdir build && cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_CUDA=OFF -D WITH_CUBLAS=OFF -D WITH_LIBV4L=ON -D BUILD_opencv_python3=ON -D BUILD_opencv_python2=OFF -D BUILD_opencv_java=OFF -D WITH_GSTREAMER=ON -D WITH_GTK=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF -D OPENCV_ENABLE_NONFREE=OFF ..
$ make -j3
$ sudo make install
$ cd ~/.virtualenvs/tf2/lib/python3.6/site-packages
$ ln -s /usr/local/lib/python3.6/site-packages/cv2/python-3.6/cv2.cpython-36m-aarch64-linux-gnu.so cv2.so
最后克隆Monk對(duì)象檢測(cè)庫(kù)并安裝TF對(duì)象檢測(cè)API
$ git clone https://github.com/Tessellate-Imaging/Monk_Object_Detection.git
$ cd Monk_Object_Detection/12_tf_obj_1/installation/
$ chmod +x install_nano.sh && ./install_nano.sh
將優(yōu)化的權(quán)重文件夾復(fù)制/下載到j(luò)etson nano工作目錄(克隆Monk庫(kù))
從Monk_Object_Detection庫(kù)復(fù)制示例圖像
$ cp -r Monk_Object_Detection/example_notebooks/sample_dataset/ship .
加載推理引擎和模型(此步驟大約需要4到5分鐘)
from infer_detector import Infer
gtf=Infer();
gtf.set_model_params('trt_fp16_dir/trt_graph.pb', "ship/classes.txt")
現(xiàn)在,如前所述,TensorRT負(fù)責(zé)計(jì)劃并在運(yùn)行時(shí)構(gòu)建(優(yōu)化)計(jì)劃,因此第一次運(yùn)行大約需要3-4分鐘
scores, bboxes, labels=gtf.infer_on_image('ship/test/img5.jpg', thresh=0.5, img_size=300);
突出顯示的區(qū)域顯示了Jetson Nano的TesnorRT建立(優(yōu)化)計(jì)劃(模型)(作者擁有的圖像)
再次運(yùn)行它不會(huì)花費(fèi)太多時(shí)間。
Benchmark板基準(zhǔn)分析
gtf.benchmark_for_speed('ship/test/img1.jpg')
# With Jetson Nano power mode - 5W ModeAverage Image loading time : 0.0275 sec
Average Inference time : 0.0621 sec
total_repetitions : 100
total_time : 6.2172sec
images_per_sec : 16
latency_mean : 67.1722 ms
latency_median : 60.7875 ms
latency_min : 57.4391 ms
# With Jetson Nano power mode - MAXN ModeAverage Image loading time : 0.0173 sec
Average Inference time : 0.0426 sec
total_repetitions : 100
total_time : 4.2624 sec
images_per_sec : 23
latency_mean : 42.6243 ms
latency_median : 41.9758 ms
latency_min : 40.9001 ms
jupyter notebook提供TensorFlow對(duì)象檢測(cè)API 1.0的完整代碼
從谷歌驅(qū)動(dòng)器下載所有預(yù)先訓(xùn)練的權(quán)重
從在自定義數(shù)據(jù)集上訓(xùn)練檢測(cè)器到在Jetson納米板或云上使用TensorFlow 2.3進(jìn)行推理的詳細(xì)步驟
要安裝的庫(kù)
前提條件:numpy,scipy,pandas,pandas,pillow,OpenCV-python
帶TensorRT 6.0.1的TensorFlow-GPU V2.3.0
使用Monk Object Detection Toolkit的TF Object Detection API 2.0
將進(jìn)行TensorRT安裝
后續(xù)部分(確保CUDA 10.0和CUDNN 7隨系統(tǒng)一起安裝了NVidia驅(qū)動(dòng)程序)
在開發(fā)(訓(xùn)練)機(jī)器中運(yùn)行以下步驟
$ git clone https://github.com/Tessellate-Imaging/Monk_Object_Detection.git
#For Cuda 10 systems
$ cd Monk_Object_Detection/13_tf_obj_1/installation && chmod +x install_cuda10.sh && ./install_cuda10.sh
#For Google colab
$ cd Monk_Object_Detection/13_tf_obj_1/installation && chmod +x install_colab.sh && ./install_colab.sh
這與第1部分中的相同。Monk對(duì)象檢測(cè)解析器要求數(shù)據(jù)集采用COCO或Pascal VOC格式。對(duì)于本教程,讓我們堅(jiān)持使用Pascal VOC格式
要將你的數(shù)據(jù)集從任何格式轉(zhuǎn)換為Pascal VOC,請(qǐng)查看以下詳細(xì)教程
在此示例中,船檢測(cè)數(shù)據(jù)集是從一篇對(duì)象檢測(cè)的舊博客中獲取的
此jupyter notebook中提到了使用數(shù)據(jù)的步驟
加載訓(xùn)練引擎
from train_detector import Detector
gtf=Detector();
在TF 2.0 Model Zoo中加載所有可用的模型
目前,它支持26種SSD,F(xiàn)aster RCNN和EfficientDet不同的型號(hào)
即將添加對(duì)Centernet模型的支持,原始管道在訓(xùn)練中有錯(cuò)誤
加載訓(xùn)練和驗(yàn)證數(shù)據(jù)集
將注釋轉(zhuǎn)換為VOC格式后加載數(shù)據(jù)集
根據(jù)可用的GPU設(shè)置批處理大小。在本教程中,使用了具有V100 GPU(16 GB VRAM)的AWS EC2 P3.2x計(jì)算機(jī),批次大小為24非常適合。
train_img_dir="ship/images/Train";
train_anno_dir="ship/voc/";
class_list_file="ship/classes.txt";
gtf.set_train_dataset(train_img_dir, train_anno_dir, class_list_file, batch_size=24)
運(yùn)行解析器將數(shù)據(jù)集轉(zhuǎn)換為tfrecords
Tf Record文件將存儲(chǔ)在data_tfrecord文件夾中
gtf.create_tfrecord(data_output_dir="data_tfrecord")
選擇并加載模型
下載模型后,Monk會(huì)根據(jù)所選參數(shù)自動(dòng)更新配置文件
在本教程中,我們使用了SSD MobileNet V2,它可以接收形狀為320x320x3 RGB圖像的輸入圖像
gtf.set_model_params(model_name="ssd_mobilenet_v2_320")
設(shè)置其他訓(xùn)練和優(yōu)化器參數(shù)
set_hyper_params(num_train_steps=10000,
lr=0.004,
lr_decay_rate=0.945,
output_dir="output_dir/",
sample_1_of_n_eval_examples=1,
sample_1_of_n_eval_on_train_examples=5,
checkpoint_dir=False,
run_once=False,
max_eval_retries=0,
num_workers=4,
checkpoint_after_every=500)
設(shè)置目錄,將存儲(chǔ)導(dǎo)出的參數(shù)
gtf.export_params(output_directory="export_dir");
設(shè)置tensorRT優(yōu)化參數(shù)
TensorRT優(yōu)化器創(chuàng)建一個(gè)計(jì)劃,然后構(gòu)建它。構(gòu)建計(jì)劃是為了優(yōu)化它正在構(gòu)建的GPU的模型。
如前所述,在具有不同cuda計(jì)算能力的GPU上優(yōu)化的模型無法在jetson nano上運(yùn)行,因此Monk庫(kù)確保該計(jì)劃在開發(fā)機(jī)(云或colab)上編譯,而該計(jì)劃則在運(yùn)行時(shí)在部署機(jī)(jetson nano)上構(gòu)建
使用INT8優(yōu)化時(shí),無法執(zhí)行此功能,計(jì)劃的編制和構(gòu)建都必須在同一臺(tái)機(jī)器上,并且Jetson納米板與8位整數(shù)運(yùn)算不太兼容
gtf.TensorRT_Optimization_Params(conversion_type="FP16", trt_dir="trt_fp16_dir")
訓(xùn)練探測(cè)器
檢測(cè)器訓(xùn)練運(yùn)行一個(gè)執(zhí)行sys.exit()函數(shù)的會(huì)話,因此在其上運(yùn)行的包裝程序?qū)㈥P(guān)閉python系統(tǒng)。
為了解決此問題,提供了一個(gè)名為train.py的腳本,該腳本可以在jupyter notebook或終端命令上運(yùn)行
根據(jù)參數(shù)設(shè)置,訓(xùn)練好的模型將保存在名為“ output_dir”的文件夾中。
# For terminal users
$ python Monk_Object_Detection/13_tf_obj_2/lib/train.py
# For jupyter notebook or colab users
%run Monk_Object_Detection/13_tf_obj_2/lib/train.py
導(dǎo)出訓(xùn)練有素的檢查點(diǎn)模型
export函數(shù)運(yùn)行一個(gè)執(zhí)行sys.exit()函數(shù)的會(huì)話,因此在其上運(yùn)行的包裝器將關(guān)閉python系統(tǒng)。
為了解決此問題,提供了一個(gè)名為export.py的腳本,該腳本可以在jupyter notebook或終端命令上運(yùn)行
根據(jù)參數(shù)設(shè)置,導(dǎo)出的模型將保存在名為“ export_dir”的文件夾中。
# For terminal users
$ python Monk_Object_Detection/13_tf_obj_2/lib/export.py
# For jupyter notebook and colab users
%run Monk_Object_Detection/13_tf_obj_2/lib/export.py
安裝TensorRT版本6.0.1
轉(zhuǎn)到Nvidia TensorRT頁(yè)面并下載基于OS和CUDA的TRT6軟件包。
下面提到的是適用于Ubuntu OS和Cuda 10.1的步驟
# Optimizing For TensorRT - Feature Not tested on colab
# This requires TensorRT 6.0.1 to be installed
# Go to https://developer.nvidia.com/tensorrt
# Download
# - nv-tensorrt-repo-ubuntu1804-cuda10.1-trt6.0.1.5-ga-20190913_1-1_amd64.deb (For Ubuntu18.04)
# - nv-tensorrt-repo-ubuntu1604-cuda10.1-trt6.0.1.5-ga-20190913_1-1_amd64.deb (For Ubuntu16.04)
# Run the following commands to install trt (in a terminal)
$ sudo dpkg -i nv-tensorrt-repo-ubuntu1804-cuda10.1-trt6.0.1.5-ga-20190913_1-1_amd64.deb
$ sudo apt-key add /var/nv-tensorrt-repo-cuda10.1-trt6.0.1.5-ga-20190913/7fa2af80.pub
$ sudo apt-get update
$ sudo apt-get install tensorrt
$ sudo apt-get install uff-converter-tf
$ sudo apt-get install python3-libnvinfer-dev
優(yōu)化導(dǎo)出模型
優(yōu)化函數(shù)運(yùn)行一個(gè)執(zhí)行sys.exit()函數(shù)的會(huì)話,因此在其上運(yùn)行的包裝程序?qū)㈥P(guān)閉python系統(tǒng)。
為了解決此問題,提供了一個(gè)名為optimize.py的腳本,該腳本可以在jupyter notebook電腦或終端命令上運(yùn)行
根據(jù)參數(shù)設(shè)置,優(yōu)化的模型將保存在名為“ trt_fp16_dir”的文件夾中。
# For terminal users
$ python Monk_Object_Detection/13_tf_obj_2/lib/optimize.py
# For jupyter notebook and colab users
%run Monk_Object_Detection/13_tf_obj_2/lib/optimize.py
加載推理機(jī)
from infer_detector import Infer
gtf=Infer();
載入模型
首先加載導(dǎo)出的模型并運(yùn)行步驟;稍后通過加載優(yōu)化的模型重復(fù)相同的步驟(步驟保持不變)
# To load exported model
gtf.set_model_params(exported_model_dir='export_dir')
# To load optimized model
gtf.set_model_params(exported_model_dir='trt_fp16_dir')
對(duì)單個(gè)圖像進(jìn)行推斷
scores, bboxes, labels=gtf.infer_on_image('ship/test/img1.jpg', thresh=0.1);
樣本推斷結(jié)果
使用兩個(gè)模型運(yùn)行速度測(cè)試分析
gtf.benchmark_for_speed('ship/test/img1.jpg')
在AWS P3.2x V100 GPU上使用導(dǎo)出的模型(未優(yōu)化)進(jìn)行分析
Average Image loading time : 0.0110 sec
Average Inference time : 0.0097 sec
Result extraction time : 0.0352 sec
total_repetitions : 100
total_time : 0.9794 sec
images_per_sec : 102
latency_mean : 9.7949 ms
latency_median : 9.7095 ms
latency_min : 9.1238 ms
在AWS P3.2x V100 GPU上使用優(yōu)化模型進(jìn)行分析
約1.5倍的速度加快處理后期優(yōu)化
Average Image loading time : 0.0108 sec
Average Inference time : 0.0062 sec
Result extraction time : 0.0350 sec
total_repetitions : 100
total_time : 0.6241 sec
images_per_sec : 160
latency_mean : 6.2422 ms
latency_median : 6.2302 ms
latency_min : 5.9401 ms
步驟1:下載Jetpack 4.3 SD卡映像 https://developer.nvidia.com/jetpack-43-archive
步驟2:將此圖片寫入SD卡。你可以使用 https://www.balena.io/etcher/
步驟3:將你的SD卡插入Nano板并啟動(dòng)系統(tǒng),然后完成安裝步驟
獲取有關(guān)Nvidia的“ Jetson Nano入門”頁(yè)面的更多詳細(xì)信息
步驟1:更新Apt
$ sudo apt-get update
$ sudo apt-get upgrade
步驟2:安裝系統(tǒng)庫(kù)
$ sudo apt-get install nano git cmake libatlas-base-dev gfortran libhdf5-serial-dev hdf5-tools nano locate libfreetype6-dev python3-setuptools protobuf-compiler libprotobuf-dev openssl libssl-dev libcurl4-openssl-dev cython3 libxml2-dev libxslt1-dev python3-pip
$ sudo apt-get install libopenblas-dev libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler libgflags-dev libgoogle-glog-dev liblmdb-dev
$ sudo pip3 install virtualenv virtualenvwrapper
步驟3:更新bashrc文件
將這些行添加到?/ .bashrc文件
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh
export PATH=/usr/local/cuda-10.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64\
${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
運(yùn)行以下命令
$ source ~/.bashrc
步驟4:創(chuàng)建虛擬環(huán)境并安裝所有必需的python庫(kù)
安裝numpy大約需要15分鐘
$ mkvirtualenv -p /usr/bin/python3.6 tf2
$ pip install numpy==1.19.1
安裝scipy大約需要40分鐘
$ pip install scipy==1.5.1
安裝Jetson Nano TensorFlow-2.0.0需再花費(fèi)15分鐘
$ pip install scikit-build protobuf cython -vvvv
$ pip install grpcio absl-py py-cpuinfo psutil portpicker six mock requests gast h5py astor termcolor protobuf keras-applications keras-preprocessing wrapt google-pasta -vvvv
$ pip install https://developer.download.nvidia.com/compute/redist/jp/v43/tensorflow-gpu/tensorflow_gpu-2.0.0+nv19.12-cp36-cp36m-linux_aarch64.whl -vvvv
安裝OpenCV需要1.5個(gè)小時(shí)
$ mkdir opencv && cd opencv
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.1.2.zip
$ unzip opencv.zip
$ mv opencv-4.1.2 opencv
$ cd opencv && mkdir build && cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_CUDA=OFF -D WITH_CUBLAS=OFF -D WITH_LIBV4L=ON -D BUILD_opencv_python3=ON -D BUILD_opencv_python2=OFF -D BUILD_opencv_java=OFF -D WITH_GSTREAMER=ON -D WITH_GTK=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF -D OPENCV_ENABLE_NONFREE=OFF ..
$ make -j3
$ sudo make install
$ cd ~/.virtualenvs/tf2/lib/python3.6/site-packages
$ ln -s /usr/local/lib/python3.6/site-packages/cv2/python-3.6/cv2.cpython-36m-aarch64-linux-gnu.so cv2.so
最后克隆Monk Object Detection庫(kù)
注意:不要像在開發(fā)機(jī)器中那樣運(yùn)行13_tf_obj_2的安裝。用tf2.0安裝tf對(duì)象檢測(cè)有一些問題。推理代碼不需要對(duì)象檢測(cè)API工具。
$ git clone https://github.com/Tessellate-Imaging/Monk_Object_Detection.git
將優(yōu)化的權(quán)重文件夾復(fù)制/下載到j(luò)etson nano工作目錄(Monk庫(kù)為克隆目錄)
從Monk_Object_Detection庫(kù)復(fù)制示例圖像
$ cp -r Monk_Object_Detection/example_notebooks/sample_dataset/ship .
加載推理引擎和模型(此步驟大約需要4到5分鐘)
from infer_detector_nano import Infer
gtf=Infer();
gtf.set_model_params(exported_model_dir='trt_fp16_dir')
現(xiàn)在,如前所述,TensorRT采用計(jì)劃并在運(yùn)行時(shí)構(gòu)建(優(yōu)化)它,因此第一次運(yùn)行大約需要3-4分鐘
scores, bboxes, labels=gtf.infer_on_image('ship/test/img1.jpg', thresh=0.1);
# Oputput will be saved as output.jpg
gtf.draw_on_image(self, bbox_thickness=3, text_size=1, text_thickness=2)
突出顯示的區(qū)域顯示了Jetson Nano的TesnorRT建立(優(yōu)化)計(jì)劃(模型)(作者擁有的圖像)
再次運(yùn)行它不會(huì)花費(fèi)太多時(shí)間。
Benchmark板基準(zhǔn)分析
gtf.benchmark_for_speed('ship/test/img1.jpg')
# With Jetson Nano power mode - 5W ModeAverage Image loading time : 0.0486 sec
Average Inference time : 0.1182 sec
total_repetitions : 100
total_time : 11.8244 sec
images_per_sec : 8
latency_mean : 118.2443 ms
latency_median : 117.8019 ms
latency_min : 111.0002 ms
# With Jetson Nano power mode - MAXN ModeAverage Image loading time : 0.0319 sec
Average Inference time : 0.0785 sec
total_repetitions : 100
total_time : 7.853 sec
images_per_sec : 12
latency_mean : 78.5399 ms
latency_median : 78.1973 ms
latency_min : 76.2658 ms
jupyter notebook提供TensorFlow對(duì)象檢測(cè)API 2.0的完整代碼
從谷歌驅(qū)動(dòng)器下載所有預(yù)先訓(xùn)練的權(quán)重
TensorFlow對(duì)象檢測(cè)API V 2.0的所有工作到此結(jié)束