將自有數據集下yolov訓練結果(*.weights) 在進行部署



原始的 *.weigths是由darknet訓練的(目前也存在tensorflow和其它庫的改寫版本,這里不做介紹),AlexeyAB版的darknet( https://github.com/AlexeyAB/darknet)的配進行了較多改進,是我推薦的版本。在他的git中 非常詳細地講解了配置方法,特別關鍵的是在訓練過程中能夠直觀回顯loss和mAP圖。


它的中文翻譯版本(有所簡化)
關於自有數據下yolo訓練,請參考《YOLOv3自有數據集訓練(AlexeyAB等)》
最后我們希望能夠部署結果,我認為主要有兩種方法:
一是基於darkhelp直接部署。由於darknent網絡是C語言類庫,所以就有 darkhelp ( DarkHelp is  not  Darknet! DarkHelp is a C++ API layer used to call Darknet's original C API.)  這樣純C的部署,目前這個庫運行在Linux上,初步發現在部署的過程中存在一些需要修改的部分,目前還需要進一步研究。
二是 在OpenVINO上面,由於OpenVINO只識別自家的IR模型,所以就需要首先將.weights轉換為pb,再轉換為IR。這個方面的資料以OpenVINO自家的最為全面( https://docs.openvinotoolkit.org/latest/_docs_MO_DG_prepare_model_convert_model_tf_specific_Convert_YOLO_From_Tensorflow.html ),簡單進行翻譯。

一、模型轉換主要分為兩個步驟。
1、由*.weights轉換為*.pb

To dump TensorFlow model out of https://github.com/mystic123/tensorflow-yolo-v3 GitHub repository (commit ed60b90), follow the instructions below:

  1. Clone the repository:
    git clone https : / /github.com /mystic123 /tensorflow -yolo -v3.git
    cd tensorflow -yolo -v3
  2. (Optional) Checkout to the commit that the conversion was tested on:
    git checkout ed60b90
  3. Download coco.names file from the DarkNet website OR use labels that fit your task.
  4. Download the yolov3.weights (for the YOLOv3 model) or yolov3-tiny.weights (for the YOLOv3-tiny model) file OR use your pretrained weights with the same structure
  5. Run a converter:
  • for YOLO-v3:
    python3 convert_weights_pb.py - -class_names coco.names - -data_format NHWC - -weights_file yolov3.weights
  • for YOLOv3-tiny:
    python3 convert_weights_pb.py - -class_names coco.names - -data_format NHWC - -weights_file yolov3 -tiny.weights - -tiny

If you have YOLOv3 weights trained for an input image with the size different from 416 (320, 608 or your own), please provide the --size key with the size of your image specified while running the converter. For example, run the following command for an image with size 608:

python3 convert_weights_pb.py - -class_names coco.names - -data_format NHWC - -weights_file yolov3_608.weights - -size 608

這部分的操作比較簡單,只需要注意根據你數據集的情況修改.names等文件就可以。

 2、由*.pb轉換為IR(xml+bin)

To solve the problems explained in the YOLOv3 architecture overview section, use the yolo_v3.json or yolo_v3_tiny.json (depending on a model) configuration file with custom operations located in the <OPENVINO_INSTALL_DIR>/deployment_tools/model_optimizer/extensions/front/tf repository.

It consists of several attributes:

[
{
"id" : "TFYOLOV3",
"match_kind" : "general",
"custom_attributes" : {
"classes" : 80,
"anchors" : [ 10, 13, 16, 30, 33, 23, 30, 61, 62, 45, 59, 119, 116, 90, 156, 198, 373, 326],
"coords" : 4,
"num" : 9,
"masks" :[[ 6, 7, 8], [ 3, 4, 5], [ 0, 1, 2]],
"entry_points" : [ "detector/yolo-v3/Reshape", "detector/yolo-v3/Reshape_4", "detector/yolo-v3/Reshape_8"]
}
}
]

where:

  • id and match_kind are parameters that you cannot change.
  • custom_attributes is a parameter that stores all the YOLOv3 specific attributes:
    • classes, coords, num, and masks are attributes that you should copy from the configuration file file that was used for model training. If you used DarkNet officially shared weights, you can use yolov3.cfg or yolov3-tiny.cfg configuration file from https://github.com/pjreddie/darknet/tree/master/cfg. Replace the default values in custom_attributes with the parameters that follow the [yolo] titles in the configuration file.
    • anchors is an optional parameter that is not used while inference of the model, but it used in a demo to parse Region layer output
    • entry_points is a node name list to cut off the model and append the Region layer with custom attributes specified above.

To generate the IR of the YOLOv3 TensorFlow model, run:

python3 mo_tf.py
--input_model /path /to /yolo_v3.pb
--tensorflow_use_custom_operations_config $MO_ROOT /extensions /front /tf /yolo_v3.json
--batch 1

To generate the IR of the YOLOv3-tiny TensorFlow model, run:

python3 mo_tf.py
--input_model /path /to /yolo_v3_tiny.pb
--tensorflow_use_custom_operations_config $MO_ROOT /extensions /front /tf /yolo_v3_tiny.json
--batch 1

where:

--batch defines shape of model input. In the example, --batch is equal to 1, but you can also specify other integers larger than 1.

--tensorflow_use_custom_operations_config adds missing Region layers to the model. In the IR, the Region layer has name RegionYolo.

這部分可能出現的問題比較多,注意事項:
1、出現:'Graph' object has no attribute 'node '
解決方法: pip install networkx==2.3  
2、*.json文件作為訓練參數文件,需要做較多修改。
以上部分,我都是直接在ubuntu中進行的修改。

二、模型部署主要參考官方文檔

對於OpenVINO來說,只需要有最后的IR模型就可以。我發現對於自己訓練的IR來說,由於類別比較少,能夠以較快的速度運行;但是對於原始的80類數據,運轉起來速度就會比較慢—甚至無法運行;此外目前只有tiny下是可以運行的,yolo是無法跑的,這里出現一些新的錯誤,都是由於采用了復雜模型帶來的異常,只有能夠進一步懂原理,才能夠最終靈活運用。





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM