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}
}