Yolov4的最小Pytorch實施。
紙Yolo V4:https://arxiv.org/abs/2004.10934
源代碼:https://github.com/alexeyab/darknet
更多詳細信息:http://pjreddie.com/darknet/yolo/
推理
火車
├── README.md
├── dataset.py dataset
├── demo.py demo to run pytorch --> tool/darknet2pytorch
├── demo_darknet2onnx.py tool to convert into onnx --> tool/darknet2pytorch
├── demo_pytorch2onnx.py tool to convert into onnx
├── models.py model for pytorch
├── train.py train models.py
├── cfg.py cfg.py for train
├── cfg cfg --> darknet2pytorch
├── data
├── weight --> darknet2pytorch
├── tool
│ ├── camera.py a demo camera
│ ├── coco_annotation.py coco dataset generator
│ ├── config.py
│ ├── darknet2pytorch.py
│ ├── region_loss.py
│ ├── utils.py
│ └── yolo_layer.py
您可以使用darknet2pytorch自己轉換,或下載我的轉換後的型號。
使用Yolov4訓練自己的數據
下載重量
轉換數據
對於可可數據集,您可以使用工具/coco_annotation.py。
# train.txt
image_path1 x1,y1,x2,y2,id x1,y1,x2,y2,id x1,y1,x2,y2,id ...
image_path2 x1,y1,x2,y2,id x1,y1,x2,y2,id x1,y1,x2,y2,id ...
...
...
火車
您可以在cfg.py中設置參數。
python train.py -g [GPU_ID] -dir [Dataset direction] ...
ONNX和Tensorrt模型從Pytorch(Tianxiaomo)轉換:Pytorch-> onnx-> tensorrt。有關轉化的更多詳細信息,請參見以下各節。
| 型號類型 | AP | AP50 | AP75 | APS | APM | APL |
|---|---|---|---|---|---|---|
| Darknet(Yolov4紙) | 0.471 | 0.710 | 0.510 | 0.278 | 0.525 | 0.636 |
| pytorch(tianxiaomo) | 0.466 | 0.704 | 0.505 | 0.267 | 0.524 | 0.629 |
| Tensorrt fp32 + batchednmsplugin | 0.472 | 0.708 | 0.511 | 0.273 | 0.530 | 0.637 |
| Tensorrt FP16 + batchednmsplugin | 0.472 | 0.708 | 0.511 | 0.273 | 0.530 | 0.636 |
| 型號類型 | AP | AP50 | AP75 | APS | APM | APL |
|---|---|---|---|---|---|---|
| Darknet(Yolov4紙) | 0.412 | 0.628 | 0.443 | 0.204 | 0.444 | 0.560 |
| pytorch(tianxiaomo) | 0.404 | 0.615 | 0.436 | 0.196 | 0.438 | 0.552 |
| Tensorrt fp32 + batchednmsplugin | 0.412 | 0.625 | 0.445 | 0.200 | 0.446 | 0.564 |
| Tensorrt FP16 + batchednmsplugin | 0.412 | 0.625 | 0.445 | 0.200 | 0.446 | 0.563 |
圖像輸入大小在320 * 320 416 * 416 512 * 512和608 * 608中不受限制。您可以以不同的輸入比率調整輸入尺寸,例如: 320 * 608 。較大的輸入大小可以幫助檢測較小的目標,但可能會較慢,而GPU內存耗盡。
height = 320 + 96 * n , n in { 0 , 1 , 2 , 3 , ...}
width = 320 + 96 * m , m in { 0 , 1 , 2 , 3 , ...}加載預處理的DarkNet模型和DarkNet重量進行推斷(圖像大小已經在CFG文件中配置)
python demo.py -cfgfile < cfgFile > -weightfile < weightFile > -imgfile < imgFile >加載Pytorch權重(PTH文件)進行推理
python models.py < num_classes > < weightfile > < imgfile > < IN_IMAGE_H > < IN_IMAGE_W > < namefile(optional) >加載轉換後的ONNX文件進行推理(請參閱第3和4節)
加載轉換後的張力引擎文件進行推理(請參閱第5節)
有2個推斷輸出。
[batch, num_boxes, 1, 4] ,代表每個邊界框的x1,y1,x2,y2。[batch, num_boxes, num_classes]指示每個邊界框的所有類的得分。到目前為止,仍然需要一小部分包括NMS在內的後處理。我們正在嘗試最大程度地減少後處理的時間和復雜性。
該腳本是將官方經過認證的Darknet模型轉換為ONNX
建議使用Pytorch版本:
安裝OnnxRuntime
pip install onnxruntime運行Python腳本以生成ONNX模型並運行演示
python demo_darknet2onnx.py < cfgFile > < namesFile > < weightFile > < imageFile > < batchSize > 您可以使用此腳本將經過訓練的Pytorch模型轉換為ONNX
建議使用Pytorch版本:
安裝OnnxRuntime
pip install onnxruntime運行Python腳本以生成ONNX模型並運行演示
python demo_pytorch2onnx.py < weight_file > < image_path > < batch_size > < n_classes > < IN_IMAGE_H > < IN_IMAGE_W >例如:
python demo_pytorch2onnx.py yolov4.pth dog.jpg 8 80 416 416運行以下命令將yolov4 onnx型號轉換為張力引擎
trtexec --onnx= < onnx_file > --explicitBatch --saveEngine= < tensorRT_engine_file > --workspace= < size_in_megabytes > --fp16運行以下命令將yolov4 onnx型號轉換為張力引擎
trtexec --onnx= < onnx_file >
--minShapes=input: < shape_of_min_batch > --optShapes=input: < shape_of_opt_batch > --maxShapes=input: < shape_of_max_batch >
--workspace= < size_in_megabytes > --saveEngine= < engine_file > --fp16例如:
trtexec --onnx=yolov4_-1_3_320_512_dynamic.onnx
--minShapes=input:1x3x320x512 --optShapes=input:4x3x320x512 --maxShapes=input:8x3x320x512
--workspace=2048 --saveEngine=yolov4_-1_3_320_512_dynamic.engine --fp16python demo_trt.py < tensorRT_engine_file > < input_image > < input_H > < input_W >此演示僅在批處理是動態的(1應該在動態範圍內)或批處理= 1時起作用,但是您可以為其他動態或靜態批次大小更新此演示。
Note1:Input_H和Input_w應同意原始ONNX文件中的輸入大小。
Note2:張力輸出需要額外的NMS操作。該演示使用tool/utils.py中的Python NMS代碼。
首先:轉換為ONNX
TensorFlow> = 2.0
1:謝謝:github:https://github.com/onnx/onnx-tensorflow
2:運行git克隆https://github.com/onnx/onnx-tensorflow.git && cd onnx-tensorflow run pip install-e。
注意:使用“ pip install onnx-tf”時,將發生錯誤,至少對我而言,建議使用源代碼安裝
cd DeepStream
make
對於單批
trtexec --onnx=<onnx_file> --explicitBatch --saveEngine=<tensorRT_engine_file> --workspace=<size_in_megabytes> --fp16
對於多批次,
trtexec --onnx=<onnx_file> --explicitBatch --shapes=input:Xx3xHxW --optShapes=input:Xx3xHxW --maxShapes=input:Xx3xHxW --minShape=input:1x3xHxW --saveEngine=<tensorRT_engine_file> --fp16
注意:最大形狀不能大於模型原始形狀。
參考:
@article{yolov4,
title={YOLOv4: YOLOv4: Optimal Speed and Accuracy of Object Detection},
author={Alexey Bochkovskiy, Chien-Yao Wang, Hong-Yuan Mark Liao},
journal = {arXiv},
year={2020}
}