การใช้งาน pytorch ขั้นต่ำของ YOLOV4
กระดาษ 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 คุณสามารถใช้เครื่องมือ/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)
โหลดไฟล์เครื่องยนต์ Tensorrt ที่แปลงแล้ว (ดูส่วนที่ 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 เป็นเครื่องยนต์ tensorrt
trtexec --onnx= < onnx_file > --explicitBatch --saveEngine= < tensorRT_engine_file > --workspace= < size_in_megabytes > --fp16เรียกใช้คำสั่งต่อไปนี้เพื่อแปลงรุ่น YOLOV4 ONNX เป็นเครื่องยนต์ tensorrt
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 >การสาธิตนี้จะใช้งานได้เฉพาะเมื่อ batchsize เป็นแบบไดนามิก (1 ควรอยู่ในช่วงไดนามิก) หรือ batchsize = 1 แต่คุณสามารถอัปเดตการสาธิตนี้เล็กน้อยสำหรับขนาดแบบไดนามิกหรือแบบคงที่อื่น ๆ
หมายเหตุ 1: input_h และ input_w ควรเห็นด้วยกับขนาดอินพุตในไฟล์ ONNX ดั้งเดิม
หมายเหตุ 2: จำเป็นต้องมีการดำเนินการ NMS พิเศษสำหรับเอาต์พุต Tensorrt การสาธิตนี้ใช้รหัส Python NMS จาก tool/utils.py
ครั้งแรก: การแปลงเป็น onnx
tensorflow> = 2.0
1: ขอบคุณ: gitHub: https: //github.com/onnx/onnx-tensorflow
2: เรียกใช้ git clone https://github.com/onnx/onnx-tensorflow.git && cd onnx-tensorflow run pip ติดตั้ง -e
หมายเหตุ: ข้อผิดพลาดจะเกิดขึ้นเมื่อใช้ "PIP ติดตั้ง 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
หมายเหตุ: maxshapes ไม่สามารถใหญ่กว่ารูปร่างดั้งเดิมแบบจำลอง
อ้างอิง:
@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}
}