ابدأ

قياس أداء النماذج باستخدام Ultralytics YOLO

Ultralytics YOLO ecosystem and integrations

تصوير نتائج قياس الأداء#

مقدمة#

بعد تدريب النموذج والتحقق من صحته، تتمثل الخطوة التالية في تقييم أدائه عند النشر. يقيس وضع قياس الأداء في Ultralytics YOLO26 سرعة النموذج ودقته عبر مجموعة من تنسيقات التصدير.

اطّلع على معاينة YOLO27 غير المُصدرة للاطلاع على نتائج أولية لقياس السرعة.



شاهد: قياس أداء نماذج Ultralytics YOLO26 | كيفية مقارنة أداء النماذج على أجهزة مختلفة؟

لماذا يُعد قياس الأداء ضروريًا؟#

  • اتخاذ قرارات مستنيرة: اكتساب فهم للمفاضلات بين السرعة والدقة.
  • تخصيص الموارد: فهم أداء تنسيقات التصدير المختلفة على العتاد المختلف.
  • التحسين: معرفة تنسيق التصدير الذي يوفّر أفضل أداء لحالة الاستخدام المحددة.
  • كفاءة التكلفة: استخدام موارد العتاد بكفاءة أكبر استنادًا إلى نتائج قياس الأداء.

المقاييس الأساسية في وضع قياس الأداء#

تنسيقات التصدير المدعومة#

  • ONNX: لتحقيق أفضل أداء على CPU
  • TensorRT: لتحقيق أقصى كفاءة على GPU
  • OpenVINO: لتحسين الأداء على عتاد Intel
  • CoreML وTensorFlow SavedModel وغيرهما: لتلبية احتياجات النشر المتنوعة.
نصيحة
  • صدّر إلى ONNX أو OpenVINO لتحقيق تسريع يصل إلى 3 أضعاف على CPU.
  • صدّر إلى TensorRT لتحقيق تسريع يصل إلى 5 أضعاف على GPU.

أمثلة الاستخدام#

التثبيت الموصى به

ثبّت Ultralytics مع تبعيات التصدير قبل قياس الأداء لتجنّب نقص الحزم.

pip install "ultralytics[export]"

شغّل اختبارات الأداء لـ YOLO26n عبر جميع تنسيقات التصدير المدعومة (ONNX وTensorRT وغيرها).

مثال
from ultralytics.utils.benchmarks import benchmark

# ⁨قياس الأداء على GPU⁩
benchmark(model="yolo26n.pt", data="coco8.yaml", imgsz=640, device=0)

# ⁨قياس أداء تنسيق تصدير محدد⁩
benchmark(model="yolo26n.pt", data="coco8.yaml", imgsz=640, format="onnx")

الوسيطات#

تتيح لك الوسيطات مثل model وdata وimgsz وquantize وdevice وverbose وformat تخصيص اختبارات الأداء لتلبية احتياجاتك ومقارنة أداء تنسيقات التصدير المختلفة.

المفتاحالقيمة الافتراضيةالوصف
modelNoneيحدد مسار ملف النموذج. يقبل تنسيقي .pt و.yaml، مثل "yolo26n.pt" للنماذج المدرّبة مسبقًا أو ملفات الإعداد.
dataNoneمسار ملف YAML لمجموعة البيانات المستخدَمة في قياس الأداء، ويشمل عادةً المسارات والإعدادات الخاصة بـبيانات التحقق. مثال: "coco8.yaml". أما التصنيف فيتطلب دليل مجموعة بيانات أو اسم مجموعة بيانات مضمّنة (مثل imagenet10).
imgsz640حجم صورة الإدخال للنموذج. يجب أن يكون عددًا صحيحًا واحدًا للصور المربعة (مثل 640)؛ ولا يدعم benchmark() سوى أحجام الصور المربعة.
quantizeNoneالدقة المطلوبة: 16 (FP16) أو 8 (INT8؛ تحتاج مخططات PTQ إلى data/fraction للمعايرة، بخلاف مخططات الأوزان فقط)؛ 32/غير المحددة تعني FP32 حيث يدعمها التنسيق — أما التنسيق الذي لا يستطيع التصدير بدقة FP32 فيرفض 32 المحددة صراحةً أو يعود إلى الدقة التي يتطلبها. لا يُصدَّر صف PyTorch الأصلي: يؤثر 16 وحده فيه، إذ يحدد استدلال FP16 خارج CPU؛ وإلا فإنه يعمل بدقة FP32. بعد ذلك، يجري كل تنسيق الاستدلال بدقته الخاصة في وقت التشغيل. يحل هذا محل الرايتين المهجورتين half/int8.
deviceNoneيحدّد جهاز الحوسبة لقياس الأداء، مثل "cpu" أو "cuda:0"؛ وعند تركه دون تعيين، يُستخدم أول GPU متاح، وإلا فـCPU. مع format="openvino" أو "litert"، تحدد "xpu" و"npu" وحدة GPU وNPU من Intel؛ راجع أجهزة الاستدلال.
verboseFalseإذا كانت القيمة True أو عددًا عشريًا، فإن إخفاقات قياس الأداء (باستثناء أخطاء التأكيد) تؤدي إلى رفع استثناء بدلًا من تسجيل خطأ؛ كما أن العدد العشري مثل 0.25 يؤكد تجاوز مقياس كل تنسيق ناجح ذلك الحد الأدنى.
eps0.001قيمة إبسيلون صغيرة (بالمللي ثانية) تُضاف إلى زمن الاستدلال لكل صورة قبل تحويله إلى FPS، منعًا للقسمة على صفر. نادرًا ما تتغير.
format''يقيس أداء تنسيق التصدير المحدد فقط (مثل format=onnx). اتركه فارغًا لاختبار جميع التنسيقات المدعومة تلقائيًا.
القيم الافتراضية لدالة `benchmark()` المستقلة

تستخدم دالة benchmark() المستقلة (from ultralytics.utils.benchmarks import benchmark) القيم الافتراضية الخاصة بتوقيعها بدلًا من قيم الجدول أعلاه، ولا سيما model="yolo26n.pt" وimgsz=160 وdevice="cpu"؛ مرّر imgsz وdevice صراحةً لمطابقة CLI الخاص بـyolo benchmark.

تنسيقات التصدير#

ستحاول اختبارات قياس الأداء التشغيل تلقائيًا على جميع تنسيقات التصدير الممكنة المدرجة أدناه. ويمكنك بدلًا من ذلك قياس الأداء لتنسيق محدد باستخدام الوسيطة format، التي تقبل أيًا من التنسيقات المذكورة أدناه.

التنسيقوسيط formatالنموذجالبيانات الوصفيةالوسيطات
PyTorch-yolo26n.pt✅-
TorchScripttorchscriptyolo26n.torchscript✅imgsz, quantize, dynamic, nms, batch, device
ONNXonnxyolo26n.onnx✅imgsz, quantize, dynamic, simplify, opset, nms, batch, data, fraction, device
OpenVINOopenvinoyolo26n_openvino_model/✅imgsz, quantize, dynamic, nms, batch, data, fraction, device
TensorRTengineyolo26n.engine✅imgsz, quantize, dynamic, simplify, opset, workspace, nms, batch, data, fraction, device
CoreMLcoremlyolo26n.mlpackage✅imgsz, dynamic, quantize, nms, batch, device
Apple Core AIcoreaiyolo26n.aimodel✅imgsz, batch, quantize
TF SavedModelsaved_modelyolo26n_saved_model/✅imgsz, quantize, opset, nms, batch, data, fraction, device
TF GraphDefpbyolo26n.pb❌imgsz, opset, batch, device
TF Edge TPUedgetpuyolo26n_edgetpu.tflite✅imgsz, quantize, opset, data, fraction, device
LiteRTlitertyolo26n.tflite✅imgsz, quantize, batch, data, fraction, device
PaddlePaddlepaddleyolo26n_paddle_model/✅imgsz, batch, device
MNNmnnyolo26n.mnn✅imgsz, batch, dynamic, quantize, simplify, opset, nms, device
NCNNncnnyolo26n_ncnn_model/✅imgsz, quantize, device
IMX500imxyolo11n_imx_model/✅imgsz, quantize, data, fraction, nms, device
RKNNrknnyolo26n_rknn_model/✅imgsz, batch, name, quantize, simplify, opset, data, fraction, device
ExecuTorchexecutorchyolo26n_executorch_model/✅imgsz, quantize, batch, data, fraction, device
Axeleraaxelerayolo26n_axelera_model/✅imgsz, batch, quantize, data, fraction, device
DEEPXdeepxyolo26n_deepx_model/✅imgsz, quantize, simplify, opset, data, optimize, device
Qualcomm QNNqnnyolo26n_qnn.onnx✅imgsz, batch, name, quantize, simplify, opset, data, fraction, device
Hailohailoyolo26n_hailo_model/✅imgsz, name, quantize, data, fraction, simplify, conf, iou, device
Huawei Ascendascendyolo26n_ascend_model/✅imgsz, batch, name, quantize, opset, simplify, nms, device
AMD Xilinxxilinxyolo26n_xilinx_model/✅imgsz, name, quantize, data, fraction, opset, simplify, device
NXPnxpyolo26n_nxp.tflite✅imgsz, quantize, data, fraction, device
ST Edge AIstedgeaiyolo26n_stedgeai.onnx✅imgsz, quantize, data, fraction, opset, simplify, device

يُخرج nms=None افتراضيًا مخرجات خامًا لاستخدام NMS خارجي (ويحتفظ YOLO26 برأسه الخالي من NMS على Axelera). اضبط nms=False لتحديد رأس خالٍ من NMS متاح؛ أما التنسيقات غير المدعومة فتعود إلى مسار الإخراج الأصلي الخاص بها. تحدد إدخالات nms أعلاه التنسيقات التي يمكنها تضمين NMS باستخدام nms=True.

اطّلع على التفاصيل الكاملة لـexport في صفحة التصدير.

الأسئلة الشائعة#

  • يوفر Ultralytics YOLO26 وضعًا لقياس الأداء لتقييم أداء نموذجك عبر تنسيقات تصدير مختلفة. ويعرض هذا الوضع مقاييس أساسية مثل متوسط الدقة المتوسطة (mAP50-95) والدقة وزمن الاستدلال بالمللي ثانية. يمكنك تشغيل قياسات الأداء باستخدام أوامر Python أو CLI. على سبيل المثال، لإجراء القياس على GPU:

    مثال
    from ultralytics.utils.benchmarks import benchmark
    
    # ⁨قياس الأداء على GPU⁩
    benchmark(model="yolo26n.pt", data="coco8.yaml", imgsz=640, device=0)

    لمزيد من التفاصيل حول وسيطات قياس الأداء، راجع قسم الوسيطات.

  • يتيح تصدير نماذج YOLO26 إلى تنسيقات مختلفة مثل ONNX وTensorRT وOpenVINO تحسين الأداء وفقًا لبيئة النشر. على سبيل المثال:

    • ONNX: يوفّر تسريعًا يصل إلى 3 أضعاف على CPU.
    • TensorRT: يوفّر تسريعًا يصل إلى 5 أضعاف على GPU.
    • OpenVINO: محسّن خصيصًا لعتاد Intel.

    تحسّن هذه التنسيقات سرعة الاستدلال على العتاد المستهدف، ما يجعل نماذجك أكثر كفاءة في تطبيقات واقعية متنوعة. تفضّل بزيارة صفحة التصدير للاطلاع على التفاصيل الكاملة.

  • يُعد قياس أداء نماذج YOLO26 أمرًا أساسيًا لعدة أسباب:

    • اتخاذ قرارات مستنيرة: فهم المفاضلات بين السرعة والدقة.
    • تخصيص الموارد: تقييم الأداء عبر خيارات العتاد المختلفة.
    • التحسين: تحديد تنسيق التصدير الذي يوفّر أفضل أداء لحالات استخدام محددة.
    • كفاءة التكلفة: تحسين استخدام العتاد استنادًا إلى نتائج قياس الأداء.

    تساعد المقاييس الأساسية، مثل mAP50-95 ودقة Top-1 وزمن الاستدلال، على إجراء هذه التقييمات. راجع قسم المقاييس الأساسية لمزيد من المعلومات.

  • يدعم YOLO26 مجموعة متنوعة من تنسيقات التصدير، صُمم كل منها لعتاد وحالات استخدام محددة:

    • ONNX: الأفضل لأداء CPU.
    • TensorRT: مثالي لكفاءة GPU.
    • OpenVINO: محسّن لعتاد Intel.
    • CoreML وTensorFlow: يُستخدم CoreML لأجهزة Apple، وتُستخدم تنسيقات TensorFlow للنشر على الأجهزة المحمولة والويب وفي تطبيقات تعلم الآلة عمومًا.

    للاطلاع على القائمة الكاملة للتنسيقات المدعومة ومزايا كل منها، راجع قسم تنسيقات التصدير المدعومة.

  • عند تشغيل قياسات الأداء، يمكن تخصيص عدة وسيطات لتلبية احتياجات محددة:

    • model: مسار ملف النموذج (مثل "yolo26n.pt").
    • data: مسار ملف YAML لمجموعة البيانات (مثل "coco8.yaml")؛ أما التصنيف فيتطلب دليل مجموعة بيانات أو اسم مجموعة بيانات مضمّنة (مثل imagenet10).
    • imgsz: حجم صورة الإدخال المربعة كعدد صحيح واحد، مثل 640. يستخدم وضع قياس الأداء حجم الصورة المربعة نفسه في PyTorch والتنسيقات المُصدَّرة لضمان عدالة المقارنة.
    • quantize: الدقة المطلوبة: 16 لـ FP16، و8 لـ INT8 (مفيد للأجهزة الطرفية)؛ 32/غير المحددة تعني FP32 حيث يدعمها التنسيق — أما التنسيق الذي لا يستطيع التصدير بدقة FP32 فيرفض 32 المحددة صراحةً أو يعود إلى الدقة التي يتطلبها. لا يُصدَّر صف PyTorch الأصلي: يؤثر 16 وحده فيه، إذ يحدد استدلال FP16 خارج CPU؛ وإلا فإنه يعمل بدقة FP32. بعد ذلك، يجري كل تنسيق الاستدلال بدقته الخاصة في وقت التشغيل.
    • الجهاز: حدّد جهاز الحوسبة (مثل "cpu" أو "cuda:0")، أو "xpu" و"npu" لوحدة GPU وNPU من Intel مع format="openvino" أو "litert".
    • verbose: ارفع استثناءً عند إخفاق قياس الأداء بدلًا من تسجيله؛ ومرّر عددًا عشريًا لاشتراط تجاوز مقياس كل تنسيق ذلك الحد الأدنى أيضًا.

    للاطلاع على القائمة الكاملة للوسيطات، راجع قسم الوسيطات.

التعليقات39

اطرح سؤالًا، وشارك نتائجك، أو ساعد شخصًا آخر.

  1. bayda369GitHub

    Hi, I want to customize the yolov8 to detect and track only the objects determined by the user, like selecting 3 persons in different positions out of 10 persons in the video appears in the first frame of the video and ignore any other persons, what are your suggestions?

    1
    1. Paula DerrengerUltralytics

      @bayda369 hello,

      For customizing YOLOv8 to detect and track specific objects (like selecting certain individuals from a video), you can approach this by integrating a pre-processing step to identify and tag the objects of interest in the first frame. Here’s a brief outline on how you might proceed:

      1. Object Detection: Use YOLOv8 to detect all persons in the first frame.
      2. Object Selection: Implement a method to select the desired objects. This could be manual selection via bounding box coordinates or an automated method based on certain criteria.
      3. Tracking: Once the objects are tagged, you can use the tracking functionality to follow these objects across frames. You might need to modify the tracking logic to focus only on the tagged objects.

      For the tracking part, you can utilize the existing tracking capabilities of YOLOv8, which supports multiple tracking algorithms. You may need to customize the tracking code to ensure it only tracks the initially selected objects.

      This approach requires some custom coding, especially for the selection and modified tracking logic. If you need further guidance on the specific code modifications, feel free to ask!

    2. Paula DerrengerUltralytics(تم التعديل)

      Hello @bayda369,

      Absolutely, I'd be happy to guide you through the code needed to track only selected objects in a video. The simplest way is to use the built-in tracker, which gives every person a persistent ID, and then keep only the IDs you selected in the first frame.

      Step-by-Step Guide
      1. Track persons: run tracking with classes=[0] (person).
      2. Select IDs: choose the IDs of the persons you want from the first frame (manually or by criteria).
      3. Filter: ignore every other ID in later frames.
      Code Example
      from ultralytics import YOLO
      
      model = YOLO("yolo26n.pt")
      
      selected_ids = None
      for r in model.track("path/to/your/video/file.mp4", classes=[0], persist=True, stream=True):
          if r.boxes.id is None:
              continue
          ids = r.boxes.id.int().tolist()
      
          if selected_ids is None:
              # For simplicity, select the first 3 persons; replace with your own selection method
              selected_ids = set(ids[:3])
      
          keep = [i for i, t in enumerate(ids) if t in selected_ids]
          boxes = r.boxes.xyxy[keep]  # boxes of the selected persons only
          track_ids = [ids[i] for i in keep]
          print(track_ids, boxes)

      Feel free to customize the selection method as per your requirements. If you need further assistance, don't hesitate to ask! 😊

    1. Glenn JocherUltralytics(تم التعديل)

      It runs on Windows too—only some export formats are OS‑specific (e.g., CoreML, EdgeTPU and RKNN aren’t supported on Windows); try a single supported format to verify your setup:

      yolo benchmark model=yolo26n.pt data=coco8.yaml format=onnx device=cpu

      If it still fails, please paste the yolo checks output and the full traceback here; format/OS notes are summarized in the benchmarks reference at ultralytics.utils.benchmarks.

  2. andyg2GitHub

    Which GPUs are used in these benchmarks? I imagine the result would be different between a GTX 1650 and an H100?

    1. Glenn JocherUltralytics(تم التعديل)

      @andyg2 you're absolutely right that benchmark results vary significantly between different GPU models. The documentation doesn't specify which exact GPUs were used for the benchmark results shown. For accurate performance metrics on your specific hardware, I'd recommend running the benchmarks directly on your system using:

      from ultralytics.utils.benchmarks import benchmark
      benchmark(model="yolo26n.pt", data="coco8.yaml", imgsz=640, device=0)

      This will give you hardware-specific results for your GTX 1650 or any other GPU you're using, which will be much more relevant for your deployment needs than generic benchmark numbers.

    2. Glenn JocherUltralytics

      @andyg2 that's a very valid point! Benchmark results without hardware specifications are indeed not very meaningful for comparison purposes. You're absolutely right that performance metrics should always include the testing hardware context. For meaningful benchmarks, it's best to run tests on your specific hardware setup to get accurate performance expectations for your deployment environment.

    3. andyg2GitHub(تم التعديل)

      @glenn-jocher Thanks for the reply. My problem is I need to purchase over 120 small GPUs for a computer vision project.

      I guess I need to buy 10 GPUs and see how each of them do, because nobody at Ultralytics can remember how they tested their own code.

      Sorry if I'm coming across a little sarcastic here, I'm just so flabbergasted, not that this information is not included - everyone makes mistakes in documentation, but that nobody there thinks it should be included.

    4. Glenn JocherUltralytics

      @andyg2 you're absolutely right, and I completely understand your frustration. This is a valid oversight on our part - benchmark results without hardware specifications are essentially meaningless for making informed purchasing decisions, especially at your scale.

      For your project requiring 120 GPUs, I'd suggest focusing on a few key considerations: look for GPUs with sufficient VRAM for your model size (typically 8GB+ for larger YOLO models), prioritize newer architectures that support TensorRT optimization well (RTX 30/40 series, A-series), and consider power efficiency for large-scale deployments.

      Your approach of testing with 10 GPUs first is actually the most reliable path forward given the current documentation gap. This will give you real-world performance data specific to your models, datasets, and deployment constraints.

      Thank you for the feedback - this highlights an important area where our documentation needs improvement to better serve users making hardware decisions.

      1
  3. josiahkim29GitHub

    How do i proceed to calculate the mean Average Precision of the predictions? I have noted the predictions = model(10 sample images from COCO val2017 dataset)

    is a bit different especially when you want to get the classes and scores of the predictions

    for pred in predictions[0]:
    x, y, w, h, class_id, score = pred[:6]

    1. Glenn JocherUltralytics

      To calculate mean Average Precision (mAP) for your predictions, you can use the val method in Ultralytics YOLO, which evaluates the model on a dataset and provides mAP metrics. For more details, refer to the benchmarking guide.

    2. josiahkim29GitHub

      @glenn-jocher thank you for the response. Based on the benchmarking guide I see that I need a custom.yaml file for this specific images I am using. Is this the case? If so how do you proceed to create a custom.yaml file.

    3. Glenn JocherUltralytics

      To create a custom yaml file for your specific images, you'll need to define the dataset paths and class names. You can start by copying an existing dataset file like coco128.yaml and modifying it to suit your dataset structure. For detailed guidance, please refer to the dataset preparation section in the documentation.

  4. xpeedent289GitHub

    Hi sir! when I use benchmark for "yolov8n.pt", it auto exports TorchScript, ONNX,... and auto benchmark for these too, but I just wanna benchmark for yolov8n.pt, so how can I adjust it ?

    1. Glenn JocherUltralytics(تم التعديل)

      To benchmark only the yolov8n.pt model without exporting to other formats, you can use the format argument to limit the benchmark to the PyTorch model. Here's how you can do it:

      from ultralytics.utils.benchmarks import benchmark
      
      # Benchmark only the PyTorch model ("-" is the PyTorch format)
      benchmark(model="yolov8n.pt", data="coco8.yaml", imgsz=640, device=0, format="-")

      Or using the CLI:

      yolo benchmark model=yolov8n.pt data=coco8.yaml imgsz=640 device=0 format=-

      This will ensure that only the PyTorch model is benchmarked. For more details, you can visit the benchmark documentation.

    2. Glenn JocherUltralytics(تم التعديل)

      The benchmark function supports a format argument that limits the run to one export format, e.g. format=onnx. To benchmark only the PyTorch model, use format="-", for example yolo benchmark model=yolov8n.pt data=coco8.yaml format=-. Leave format unset to benchmark all formats. See the benchmark documentation for details.

  5. themeowsketeerGitHub

    Greetings, I'm facing the following error when trying to benchmark using trained weight from RT-DeTR-L model, which is known to be fully supported by Ultralytics:

    KeyError Traceback (most recent call last) File /opt/conda/lib/python3.10/site-packages/ultralytics/utils/benchmarks.py:133, in benchmark(model, data, imgsz, half, int8, device, verbose) 132 assert i != 5 or platform.system() == "Darwin", "inference only supported on macOS>=10.13" # CoreML --> 133 exported_model.predict(ASSETS / "bus.jpg", imgsz=imgsz, device=device, half=half) 135 # Validate

    File /opt/conda/lib/python3.10/site-packages/ultralytics/engine/model.py:563, in Model.predict(self, source, stream, predictor, **kwargs) 562 self.predictor.set_prompts(prompts) --> 563 return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream)

    File /opt/conda/lib/python3.10/site-packages/ultralytics/engine/predictor.py:168, in BasePredictor.call(self, source, model, stream, *args, **kwargs) 167 else: --> 168 return list(self.stream_inference(source, model, *args, **kwargs))

    File /opt/conda/lib/python3.10/site-packages/torch/utils/_contextlib.py:36, in _wrap_generator..generator_context(*args, **kwargs) 35 with ctx_factory(): ---> 36 response = gen.send(None) 38 while True:

    File /opt/conda/lib/python3.10/site-packages/ultralytics/engine/predictor.py:274, in BasePredictor.stream_inference(self, source, model, *args, **kwargs) 273 if self.args.verbose or self.args.save or self.args.save_txt or self.args.show: --> 274 s[i] += self.write_results(i, Path(paths[i]), im, s) 276 # Print batch results

    File /opt/conda/lib/python3.10/site-packages/ultralytics/engine/predictor.py:334, in BasePredictor.write_results(self, i, p, im, s) 333 result.save_dir = self.save_dir.str() # used in other locations --> 334 string += f"{result.verbose()}{result.speed['inference']:.1f}ms" 336 # Add predictions to image

    File /opt/conda/lib/python3.10/site-packages/ultralytics/engine/results.py:633, in Results.verbose(self) 632 n = (boxes.cls == c).sum() # detections per class --> 633 log_string += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, " 634 return log_string

    KeyError: 25

    During handling of the above exception, another exception occurred:

    AssertionError Traceback (most recent call last) Cell In[11], line 3 1 from ultralytics.utils.benchmarks import benchmark ----> 3 benchmark( 4 model = "/kaggle/working/weight_rtdetr/last_rtdetr.pt", 5 data = "Yolo_path_fixed.yaml", 6 imgsz = 640, 7 device = 0, 8 half = False, 9 verbose = True 10 )

    File /opt/conda/lib/python3.10/site-packages/ultralytics/utils/benchmarks.py:146, in benchmark(model, data, imgsz, half, int8, device, verbose) 144 except Exception as e: 145 if verbose: --> 146 assert type(e) is AssertionError, f"Benchmark failure for {name}: {e}" 147 LOGGER.warning(f"ERROR ❌️ Benchmark failure for {name}: {e}") 148 y.append([name, emoji, round(file_size(filename), 1), None, None, None]) # mAP, t_inference

    AssertionError: Benchmark failure for PyTorch: 25

    Any idea how this exception is caused? My code snippet is as follows: from ultralytics.utils.benchmarks import benchmark

    benchmark( model = "/kaggle/working/weight_rtdetr/last_rtdetr.pt", data = "Yolo_path_fixed.yaml", imgsz = 640, device = 0, half = False, verbose = True )

    1. Glenn JocherUltralytics

      @themeowsketeer thank you for reporting this issue. It seems like the error is related to a missing class name in the model's names attribute. Please ensure that your model and dataset are correctly configured and that the latest versions of the Ultralytics package are installed. If the issue persists, try running the benchmark with a different model or dataset to isolate the problem. For more details on benchmarking, you can refer to our documentation.

    2. themeowsketeerGitHub

      YOLO model works flawlessly with the command, while RT-DeTR models just do not work at all. AssertionError returns different error code everytime: 8, 13 or 25. Any other method I could try?

    3. Glenn JocherUltralytics

      It seems like the issue might be related to the RT-DeTR model's class names. Please ensure your dataset and model are correctly configured, and try running the benchmark with the latest version of the Ultralytics package. If the problem persists, consider testing with a different model or dataset to isolate the issue. For more details, refer to our benchmarking documentation.

  6. WooFy11GitHub

    @title Speed Tracker

    import cv2 import numpy as np from ultralytics import YOLO

    from ultralytics.utils.checks import check_imshow from ultralytics.utils.plotting import Annotator, colors

    from collections import defaultdict

    track_history = defaultdict(lambda: []) model = YOLO("/content/drive/MyDrive/DATASET_TUGASAKHIR/PlayerResults/weights/best.pt") names = model.model.names video_path = "/content/drive/MyDrive/DATASET_TUGASAKHIR/testvid/BDEvid1.mp4" cap = cv2.VideoCapture(video_path) assert cap.isOpened(), "Error reading video file"

    w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

    result = cv2.VideoWriter("SpeedTracker.avi", cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))

    time_per_frame = 1.0 / fps real_world_scale = 0.05 # Example: 0.05 meters per pixel

    while cap.isOpened(): success, frame = cap.read() if success: results = model.track(frame, persist=True, verbose=False, tracker="/content/drive/MyDrive/DATASET_TUGASAKHIR/Tracker/bytetrack.yaml") boxes = results[0].boxes.xyxy.cpu()

        if results[0].boxes.id is not None:
    
            # Extract prediction results
            clss = results[0].boxes.cls.cpu().tolist()
            track_ids = results[0].boxes.id.int().cpu().tolist()
            confs = results[0].boxes.conf.float().cpu().tolist()
    
            # Annotator Init
            annotator = Annotator(frame, line_width=2)
    
            for box, cls, track_id in zip(boxes, clss, track_ids):
                # Store tracking history
                track = track_history[track_id]
                track.append((int((box[0] + box[2]) / 2), int((box[1] + box[3]) / 2)))
                if len(track) > 30:
                    track.pop(0)
    
                if len(track) >= 2:
                    # Calculate pixel distance between the last and second to last points
                    dx = track[-1][0] - track[-2][0]
                    dy = track[-1][1] - track[-2][1]
                    pixel_distance = (dx ** 2 + dy ** 2) ** 0.5
                    real_distance = pixel_distance * real_world_scale
                    speed = real_distance / time_per_frame  # speed in meters per second
                    label = f"{names[int(cls)]} {track_id} {speed:.2f} m/s"
                    annotator.box_label(box, color=colors(int(cls), True), label=label)
    
                    # Plot tracks
                    points = np.array(track, dtype=np.int32).reshape((-1, 1, 2))
                    cv2.circle(frame, (track[-1]), 7, colors(int(cls), True), -1)
                    cv2.polylines(frame, [points], isClosed=False, color=colors(int(cls), True), thickness=2)
                else:
                    label = f"{names[int(cls)]} {track_id}"
                    annotator.box_label(box, color=colors(int(cls), True), label=label)
    
        result.write(frame)
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    else:
        break

    result.release() cap.release() cv2.destroyAllWindows()

    how to collect the ground truth ID's from this code

  7. WooFy11GitHub

    @title Speed Tracker

    import cv2 import numpy as np from ultralytics import YOLO

    from ultralytics.utils.checks import check_imshow from ultralytics.utils.plotting import Annotator, colors

    from collections import defaultdict

    track_history = defaultdict(lambda: []) model = YOLO("/content/drive/MyDrive/DATASET_TUGASAKHIR/PlayerResults/weights/best.pt") names = model.model.names video_path = "/content/drive/MyDrive/DATASET_TUGASAKHIR/testvid/BDEvid1.mp4" cap = cv2.VideoCapture(video_path) assert cap.isOpened(), "Error reading video file"

    w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

    result = cv2.VideoWriter("SpeedTracker.avi", cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))

    time_per_frame = 1.0 / fps real_world_scale = 0.05 # Example: 0.05 meters per pixel

    while cap.isOpened(): success, frame = cap.read() if success: results = model.track(frame, persist=True, verbose=False, tracker="/content/drive/MyDrive/DATASET_TUGASAKHIR/Tracker/bytetrack.yaml") boxes = results[0].boxes.xyxy.cpu()

        if results[0].boxes.id is not None:
    
            # Extract prediction results
            clss = results[0].boxes.cls.cpu().tolist()
            track_ids = results[0].boxes.id.int().cpu().tolist()
            confs = results[0].boxes.conf.float().cpu().tolist()
    
            # Annotator Init
            annotator = Annotator(frame, line_width=2)
    
            for box, cls, track_id in zip(boxes, clss, track_ids):
                # Store tracking history
                track = track_history[track_id]
                track.append((int((box[0] + box[2]) / 2), int((box[1] + box[3]) / 2)))
                if len(track) > 30:
                    track.pop(0)
    
                if len(track) >= 2:
                    # Calculate pixel distance between the last and second to last points
                    dx = track[-1][0] - track[-2][0]
                    dy = track[-1][1] - track[-2][1]
                    pixel_distance = (dx ** 2 + dy ** 2) ** 0.5
                    real_distance = pixel_distance * real_world_scale
                    speed = real_distance / time_per_frame  # speed in meters per second
                    label = f"{names[int(cls)]} {track_id} {speed:.2f} m/s"
                    annotator.box_label(box, color=colors(int(cls), True), label=label)
    
                    # Plot tracks
                    points = np.array(track, dtype=np.int32).reshape((-1, 1, 2))
                    cv2.circle(frame, (track[-1]), 7, colors(int(cls), True), -1)
                    cv2.polylines(frame, [points], isClosed=False, color=colors(int(cls), True), thickness=2)
                else:
                    label = f"{names[int(cls)]} {track_id}"
                    annotator.box_label(box, color=colors(int(cls), True), label=label)
    
        result.write(frame)
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    else:
        break

    result.release() cap.release() cv2.destroyAllWindows()

    how do we benchmark the bytetrack perfomance per player ID?

    1. Paula DerrengerUltralytics(تم التعديل)

      Hey there! 😊 To benchmark only PyTorch, ONNX, OpenVINO, and TensorRT, use the format argument of benchmark, which limits the run to one export format. Call it once per format ("-" is the PyTorch model):

      from ultralytics.utils.benchmarks import benchmark
      
      # "-" = PyTorch, then ONNX, OpenVINO (CPU only) and TensorRT (GPU only)
      for fmt, device in [("-", 0), ("onnx", 0), ("openvino", "cpu"), ("engine", 0)]:
          benchmark(model="yolo26n.pt", data="coco8.yaml", imgsz=640, device=device, format=fmt)

      And for CLI:

      yolo benchmark model=yolo26n.pt data=coco8.yaml imgsz=640 device=0 format=onnx
      yolo benchmark model=yolo26n.pt data=coco8.yaml imgsz=640 device=cpu format=openvino
      yolo benchmark model=yolo26n.pt data=coco8.yaml imgsz=640 device=0 format=engine

      Benchmark exports the model for you, so you don't need to export it first. Hope this helps! If there are more questions, feel free to ask.

      1
    2. Zency-SunGitHub

      when I use the under code to deploy: ' model = YOLO("yolov8s.engine") metrics = model.val(data="ultralytics/cfg/datasets/coco128.yaml",device='cuda:0') ' but I meet the error,What should I do?,Thanks your reply!!!

      Traceback (most recent call last): File "/media/zency/SSD-Data/Codes/yolov8_v8.1.0/ultralytics/nn/autobackend.py", line 220, in init import tensorrt as trt # noqa https://developer.nvidia.com/nvidia-tensorrt-download File "/home/zency/anaconda3/envs/yolov8plus/lib/python3.9/site-packages/tensorrt/init.py", line 68, in from .tensorrt import * ImportError: libnvinfer.so.8: cannot open shared object file: No such file or directory

      During handling of the above exception, another exception occurred:

      Traceback (most recent call last): File "/media/zency/SSD-Data/Codes/yolov8_v8.1.0/run_test.py", line 190, in yolo_mytest() File "/media/zency/SSD-Data/Codes/yolov8_v8.1.0/run_test.py", line 116, in yolo_mytest metrics = model.val(data="ultralytics/cfg/datasets/coco128.yaml",device='cuda:0') # cpu cuda:0 File "/media/zency/SSD-Data/Codes/yolov8_v8.1.0/ultralytics/engine/model.py", line 513, in val validator(model=self.model) File "/home/zency/anaconda3/envs/yolov8plus/lib/python3.9/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context return func(*args, **kwargs) File "/media/zency/SSD-Data/Codes/yolov8_v8.1.0/ultralytics/engine/validator.py", line 124, in call model = AutoBackend( File "/home/zency/anaconda3/envs/yolov8plus/lib/python3.9/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context return func(*args, **kwargs) File "/media/zency/SSD-Data/Codes/yolov8_v8.1.0/ultralytics/nn/autobackend.py", line 224, in init import tensorrt as trt # noqa File "/home/zency/anaconda3/envs/yolov8plus/lib/python3.9/site-packages/tensorrt/init.py", line 68, in from .tensorrt import * ImportError: libnvinfer.so.8: cannot open shared object file: No such file or directory Ultralytics YOLOv8.1.24 🚀 Python-3.9.18 torch-1.12.1 CUDA:0 (NVIDIA GeForce RTX 3090, 24268MiB) Loading yolov8s.engine for TensorRT inference...

    3. Zency-SunGitHub

      I solved the problem above, but there is a new problem:

      [TRT] [W] CUDA initialization failure with error: 35

    4. Paula DerrengerUltralytics

      Hey there! 🌟 It looks like you've encountered the CUDA initialization failure error with code 35, which usually indicates a CUDA driver/runtime version mismatch or an issue with accessing your GPU.

      Here are a couple of steps you can try to resolve the issue:

      1. Check CUDA Compatibility: Ensure that the installed CUDA version is compatible with the version required by TensorRT.
      2. Update Drivers: Sometimes, simply updating your NVIDIA drivers can resolve such issues.
      3. Environment Check: Make sure your environment variables (like PATH and LD_LIBRARY_PATH for Linux) are correctly set to include paths to CUDA toolkit and libraries.

      If these steps don't help, could you provide a bit more context about your setup? Specifically:

      • The versions of CUDA, TensorRT, and PyTorch you're using.
      • The output of nvidia-smi to check if the GPU is being recognized properly.

      This information can help pinpoint the issue more accurately. Thanks for reaching out, and looking forward to getting you past this hurdle! 🚀

    5. Paula DerrengerUltralytics(تم التعديل)

      @Zency-Sun i'm glad to hear you've resolved your previous issue! Regarding your question about model benchmarking, it's a great way to understand your model's performance across different formats. Here's a quick overview of what you might find useful:

      • Why Benchmark?: Helps you make informed decisions by understanding the trade-off between speed and accuracy across different formats and on varied hardware.
      • Key Metrics: Look at mAP50-95 for detection accuracy, and inference time for speed. This gives you a holistic view of how well your model performs.

      For benchmarking specific formats like PyTorch, ONNX, OpenVINO, and TensorRT, use the benchmark utility with the format argument. It exports the model for you:

      from ultralytics.utils.benchmarks import benchmark
      
      # "-" = PyTorch; OpenVINO runs on CPU, TensorRT on GPU
      for fmt, device in [("-", "cuda:0"), ("onnx", "cuda:0"), ("openvino", "cpu"), ("engine", "cuda:0")]:
          benchmark(model="yolo26n.pt", data="coco8.yaml", imgsz=640, device=device, format=fmt)

      And for CLI:

      yolo benchmark model=yolo26n.pt data=coco8.yaml imgsz=640 device=0 format=onnx
      # Repeat with format=openvino (device=cpu) and format=engine

      Remember, exporting to ONNX or OpenVINO can speed up CPU inference, and TensorRT is excellent for maximizing GPU efficiency. 🚀

      Let me know if there's anything else you need help with!