الحد الأدنى من تنفيذ Pytorch من YOLOV4.
Paper 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 ، يمكنك استخدام Tool/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 Script لإنشاء نموذج ONNX وتشغيل العرض التوضيحي
python demo_darknet2onnx.py < cfgFile > < namesFile > < weightFile > < imageFile > < batchSize > يمكنك تحويل طراز Pytorch المدربين إلى OnNx باستخدام هذا البرنامج النصي
أوصت نسخة Pytorch:
تثبيت onnxruntime
pip install onnxruntimeقم بتشغيل برنامج Python Script لإنشاء نموذج 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 install -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}
}