2025-01-15 22:31:19 +08:00
|
|
|
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
|
2022-02-12 23:05:43 +08:00
|
|
|
"""
|
2024-01-08 08:29:14 +08:00
|
|
|
Run YOLOv5 benchmarks on all supported export formats.
|
2022-02-12 23:05:43 +08:00
|
|
|
|
|
|
|
Format | `export.py --include` | Model
|
|
|
|
--- | --- | ---
|
|
|
|
PyTorch | - | yolov5s.pt
|
|
|
|
TorchScript | `torchscript` | yolov5s.torchscript
|
|
|
|
ONNX | `onnx` | yolov5s.onnx
|
|
|
|
OpenVINO | `openvino` | yolov5s_openvino_model/
|
|
|
|
TensorRT | `engine` | yolov5s.engine
|
2024-07-29 08:09:57 +08:00
|
|
|
CoreML | `coreml` | yolov5s.mlpackage
|
2022-02-12 23:05:43 +08:00
|
|
|
TensorFlow SavedModel | `saved_model` | yolov5s_saved_model/
|
|
|
|
TensorFlow GraphDef | `pb` | yolov5s.pb
|
|
|
|
TensorFlow Lite | `tflite` | yolov5s.tflite
|
|
|
|
TensorFlow Edge TPU | `edgetpu` | yolov5s_edgetpu.tflite
|
|
|
|
TensorFlow.js | `tfjs` | yolov5s_web_model/
|
|
|
|
|
|
|
|
Requirements:
|
|
|
|
$ pip install -r requirements.txt coremltools onnx onnx-simplifier onnxruntime openvino-dev tensorflow-cpu # CPU
|
|
|
|
$ pip install -r requirements.txt coremltools onnx onnx-simplifier onnxruntime-gpu openvino-dev tensorflow # GPU
|
2022-03-14 22:07:13 +08:00
|
|
|
$ pip install -U nvidia-tensorrt --index-url https://pypi.ngc.nvidia.com # TensorRT
|
2022-02-12 23:05:43 +08:00
|
|
|
|
|
|
|
Usage:
|
2022-11-12 03:43:16 +08:00
|
|
|
$ python benchmarks.py --weights yolov5s.pt --img 640
|
2022-02-12 23:05:43 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
2022-07-08 05:41:34 +08:00
|
|
|
import platform
|
2022-02-12 23:05:43 +08:00
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
|
|
|
|
FILE = Path(__file__).resolve()
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
ROOT = FILE.parents[0] # YOLOv5 root directory
|
2022-02-12 23:05:43 +08:00
|
|
|
if str(ROOT) not in sys.path:
|
|
|
|
sys.path.append(str(ROOT)) # add ROOT to PATH
|
|
|
|
# ROOT = ROOT.relative_to(Path.cwd()) # relative
|
|
|
|
|
|
|
|
import export
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
from models.experimental import attempt_load
|
|
|
|
from models.yolo import SegmentationModel
|
|
|
|
from segment.val import run as val_seg
|
2022-02-12 23:05:43 +08:00
|
|
|
from utils import notebook_init
|
2022-06-27 23:46:49 +08:00
|
|
|
from utils.general import LOGGER, check_yaml, file_size, print_args
|
2022-03-14 22:07:13 +08:00
|
|
|
from utils.torch_utils import select_device
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
from val import run as val_det
|
2022-02-12 23:05:43 +08:00
|
|
|
|
|
|
|
|
2022-03-31 22:52:34 +08:00
|
|
|
def run(
|
2024-01-08 08:29:14 +08:00
|
|
|
weights=ROOT / "yolov5s.pt", # weights path
|
|
|
|
imgsz=640, # inference size (pixels)
|
|
|
|
batch_size=1, # batch size
|
|
|
|
data=ROOT / "data/coco128.yaml", # dataset.yaml path
|
|
|
|
device="", # cuda device, i.e. 0 or 0,1,2,3 or cpu
|
|
|
|
half=False, # use FP16 half-precision inference
|
|
|
|
test=False, # test exports only
|
|
|
|
pt_only=False, # test PyTorch only
|
|
|
|
hard_fail=False, # throw error on benchmark failure
|
2022-03-31 22:52:34 +08:00
|
|
|
):
|
2024-07-09 03:19:04 +08:00
|
|
|
"""
|
|
|
|
Run YOLOv5 benchmarks on multiple export formats and log results for model performance evaluation.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
weights (Path | str): Path to the model weights file (default: ROOT / "yolov5s.pt").
|
|
|
|
imgsz (int): Inference size in pixels (default: 640).
|
|
|
|
batch_size (int): Batch size for inference (default: 1).
|
|
|
|
data (Path | str): Path to the dataset.yaml file (default: ROOT / "data/coco128.yaml").
|
2024-07-09 04:05:56 +08:00
|
|
|
device (str): CUDA device, e.g., '0' or '0,1,2,3' or 'cpu' (default: "").
|
2024-07-09 03:19:04 +08:00
|
|
|
half (bool): Use FP16 half-precision inference (default: False).
|
|
|
|
test (bool): Test export formats only (default: False).
|
|
|
|
pt_only (bool): Test PyTorch format only (default: False).
|
|
|
|
hard_fail (bool): Throw an error on benchmark failure if True (default: False).
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None. Logs information about the benchmark results, including the format, size, mAP50-95, and inference time.
|
|
|
|
|
|
|
|
Notes:
|
2024-07-16 07:20:57 +08:00
|
|
|
Supported export formats and models include PyTorch, TorchScript, ONNX, OpenVINO, TensorRT, CoreML,
|
|
|
|
TensorFlow SavedModel, TensorFlow GraphDef, TensorFlow Lite, and TensorFlow Edge TPU. Edge TPU and TF.js
|
|
|
|
are unsupported.
|
2024-07-09 03:19:04 +08:00
|
|
|
|
2024-07-16 07:20:57 +08:00
|
|
|
Example:
|
2024-07-09 03:19:04 +08:00
|
|
|
```python
|
|
|
|
$ python benchmarks.py --weights yolov5s.pt --img 640
|
|
|
|
```
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
Install required packages:
|
|
|
|
$ pip install -r requirements.txt coremltools onnx onnx-simplifier onnxruntime openvino-dev tensorflow-cpu # CPU support
|
|
|
|
$ pip install -r requirements.txt coremltools onnx onnx-simplifier onnxruntime-gpu openvino-dev tensorflow # GPU support
|
|
|
|
$ pip install -U nvidia-tensorrt --index-url https://pypi.ngc.nvidia.com # TensorRT
|
|
|
|
|
|
|
|
Run benchmarks:
|
|
|
|
$ python benchmarks.py --weights yolov5s.pt --img 640
|
|
|
|
"""
|
2022-02-12 23:05:43 +08:00
|
|
|
y, t = [], time.time()
|
2022-03-14 22:07:13 +08:00
|
|
|
device = select_device(device)
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
model_type = type(attempt_load(weights, fuse=False)) # DetectionModel, SegmentationModel, etc.
|
2022-07-08 05:41:34 +08:00
|
|
|
for i, (name, f, suffix, cpu, gpu) in export.export_formats().iterrows(): # index, (name, file, suffix, CPU, GPU)
|
2022-02-12 23:05:43 +08:00
|
|
|
try:
|
2024-01-08 08:29:14 +08:00
|
|
|
assert i not in (9, 10), "inference not supported" # Edge TPU and TF.js are unsupported
|
|
|
|
assert i != 5 or platform.system() == "Darwin", "inference only supported on macOS>=10.13" # CoreML
|
|
|
|
if "cpu" in device.type:
|
|
|
|
assert cpu, "inference not supported on CPU"
|
|
|
|
if "cuda" in device.type:
|
|
|
|
assert gpu, "inference not supported on GPU"
|
2022-04-09 07:32:16 +08:00
|
|
|
|
|
|
|
# Export
|
2024-01-08 08:29:14 +08:00
|
|
|
if f == "-":
|
2022-03-14 22:07:13 +08:00
|
|
|
w = weights # PyTorch format
|
|
|
|
else:
|
2024-01-08 08:29:14 +08:00
|
|
|
w = export.run(
|
|
|
|
weights=weights, imgsz=[imgsz], include=[f], batch_size=batch_size, device=device, half=half
|
|
|
|
)[-1] # all others
|
|
|
|
assert suffix in str(w), "export failed"
|
2022-04-09 07:32:16 +08:00
|
|
|
|
|
|
|
# Validate
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
if model_type == SegmentationModel:
|
2024-01-08 08:29:14 +08:00
|
|
|
result = val_seg(data, w, batch_size, imgsz, plots=False, device=device, task="speed", half=half)
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
metric = result[0][7] # (box(p, r, map50, map), mask(p, r, map50, map), *loss(box, obj, cls))
|
|
|
|
else: # DetectionModel:
|
2024-01-08 08:29:14 +08:00
|
|
|
result = val_det(data, w, batch_size, imgsz, plots=False, device=device, task="speed", half=half)
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
metric = result[0][3] # (p, r, map50, map, *loss(box, obj, cls))
|
|
|
|
speed = result[2][1] # times (preprocess, inference, postprocess)
|
|
|
|
y.append([name, round(file_size(w), 1), round(metric, 4), round(speed, 2)]) # MB, mAP, t_inference
|
2022-02-12 23:05:43 +08:00
|
|
|
except Exception as e:
|
2022-07-08 05:41:34 +08:00
|
|
|
if hard_fail:
|
2024-01-08 08:29:14 +08:00
|
|
|
assert type(e) is AssertionError, f"Benchmark --hard-fail for {name}: {e}"
|
|
|
|
LOGGER.warning(f"WARNING ⚠️ Benchmark failure for {name}: {e}")
|
2022-06-27 23:46:49 +08:00
|
|
|
y.append([name, None, None, None]) # mAP, t_inference
|
2022-04-25 11:03:23 +08:00
|
|
|
if pt_only and i == 0:
|
|
|
|
break # break after PyTorch
|
2022-02-12 23:05:43 +08:00
|
|
|
|
|
|
|
# Print results
|
2024-01-08 08:29:14 +08:00
|
|
|
LOGGER.info("\n")
|
2022-02-12 23:05:43 +08:00
|
|
|
parse_opt()
|
|
|
|
notebook_init() # print system info
|
2024-01-08 08:29:14 +08:00
|
|
|
c = ["Format", "Size (MB)", "mAP50-95", "Inference time (ms)"] if map else ["Format", "Export", "", ""]
|
2022-06-27 23:46:49 +08:00
|
|
|
py = pd.DataFrame(y, columns=c)
|
2024-01-08 08:29:14 +08:00
|
|
|
LOGGER.info(f"\nBenchmarks complete ({time.time() - t:.2f}s)")
|
2022-04-09 07:32:16 +08:00
|
|
|
LOGGER.info(str(py if map else py.iloc[:, :2]))
|
2022-09-04 22:35:16 +08:00
|
|
|
if hard_fail and isinstance(hard_fail, str):
|
2024-01-08 08:29:14 +08:00
|
|
|
metrics = py["mAP50-95"].array # values to compare to floor
|
2022-09-04 22:35:16 +08:00
|
|
|
floor = eval(hard_fail) # minimum metric floor to pass, i.e. = 0.29 mAP for YOLOv5n
|
2024-01-08 08:29:14 +08:00
|
|
|
assert all(x > floor for x in metrics if pd.notna(x)), f"HARD FAIL: mAP50-95 < floor {floor}"
|
2022-04-09 07:32:16 +08:00
|
|
|
return py
|
|
|
|
|
|
|
|
|
|
|
|
def test(
|
2024-01-08 08:29:14 +08:00
|
|
|
weights=ROOT / "yolov5s.pt", # weights path
|
|
|
|
imgsz=640, # inference size (pixels)
|
|
|
|
batch_size=1, # batch size
|
|
|
|
data=ROOT / "data/coco128.yaml", # dataset.yaml path
|
|
|
|
device="", # cuda device, i.e. 0 or 0,1,2,3 or cpu
|
|
|
|
half=False, # use FP16 half-precision inference
|
|
|
|
test=False, # test exports only
|
|
|
|
pt_only=False, # test PyTorch only
|
|
|
|
hard_fail=False, # throw error on benchmark failure
|
2022-04-09 07:32:16 +08:00
|
|
|
):
|
2024-07-09 03:19:04 +08:00
|
|
|
"""
|
|
|
|
Run YOLOv5 export tests for all supported formats and log the results, including export statuses.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
weights (Path | str): Path to the model weights file (.pt format). Default is 'ROOT / "yolov5s.pt"'.
|
|
|
|
imgsz (int): Inference image size (in pixels). Default is 640.
|
|
|
|
batch_size (int): Batch size for testing. Default is 1.
|
|
|
|
data (Path | str): Path to the dataset configuration file (.yaml format). Default is 'ROOT / "data/coco128.yaml"'.
|
|
|
|
device (str): Device for running the tests, can be 'cpu' or a specific CUDA device ('0', '0,1,2,3', etc.). Default is an empty string.
|
|
|
|
half (bool): Use FP16 half-precision for inference if True. Default is False.
|
|
|
|
test (bool): Test export formats only without running inference. Default is False.
|
|
|
|
pt_only (bool): Test only the PyTorch model if True. Default is False.
|
|
|
|
hard_fail (bool): Raise error on export or test failure if True. Default is False.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
pd.DataFrame: DataFrame containing the results of the export tests, including format names and export statuses.
|
2024-07-09 04:05:56 +08:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
```python
|
|
|
|
$ python benchmarks.py --weights yolov5s.pt --img 640
|
|
|
|
```
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
Supported export formats and models include PyTorch, TorchScript, ONNX, OpenVINO, TensorRT, CoreML, TensorFlow
|
|
|
|
SavedModel, TensorFlow GraphDef, TensorFlow Lite, and TensorFlow Edge TPU. Edge TPU and TF.js are unsupported.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
Install required packages:
|
|
|
|
$ pip install -r requirements.txt coremltools onnx onnx-simplifier onnxruntime openvino-dev tensorflow-cpu # CPU support
|
|
|
|
$ pip install -r requirements.txt coremltools onnx onnx-simplifier onnxruntime-gpu openvino-dev tensorflow # GPU support
|
|
|
|
$ pip install -U nvidia-tensorrt --index-url https://pypi.ngc.nvidia.com # TensorRT
|
|
|
|
Run export tests:
|
|
|
|
$ python benchmarks.py --weights yolov5s.pt --img 640
|
2024-07-09 03:19:04 +08:00
|
|
|
"""
|
2022-04-09 07:32:16 +08:00
|
|
|
y, t = [], time.time()
|
|
|
|
device = select_device(device)
|
2022-05-26 22:07:58 +08:00
|
|
|
for i, (name, f, suffix, gpu) in export.export_formats().iterrows(): # index, (name, file, suffix, gpu-capable)
|
2022-04-09 07:32:16 +08:00
|
|
|
try:
|
2024-01-08 08:29:14 +08:00
|
|
|
w = (
|
|
|
|
weights
|
|
|
|
if f == "-"
|
|
|
|
else export.run(weights=weights, imgsz=[imgsz], include=[f], device=device, half=half)[-1]
|
|
|
|
) # weights
|
|
|
|
assert suffix in str(w), "export failed"
|
2022-04-09 07:32:16 +08:00
|
|
|
y.append([name, True])
|
|
|
|
except Exception:
|
|
|
|
y.append([name, False]) # mAP, t_inference
|
|
|
|
|
|
|
|
# Print results
|
2024-01-08 08:29:14 +08:00
|
|
|
LOGGER.info("\n")
|
2022-04-09 07:32:16 +08:00
|
|
|
parse_opt()
|
|
|
|
notebook_init() # print system info
|
2024-01-08 08:29:14 +08:00
|
|
|
py = pd.DataFrame(y, columns=["Format", "Export"])
|
|
|
|
LOGGER.info(f"\nExports complete ({time.time() - t:.2f}s)")
|
2022-02-12 23:05:43 +08:00
|
|
|
LOGGER.info(str(py))
|
|
|
|
return py
|
|
|
|
|
|
|
|
|
|
|
|
def parse_opt():
|
2024-07-09 03:19:04 +08:00
|
|
|
"""
|
|
|
|
Parses command-line arguments for YOLOv5 model inference configuration.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
weights (str): The path to the weights file. Defaults to 'ROOT / "yolov5s.pt"'.
|
|
|
|
imgsz (int): Inference size in pixels. Defaults to 640.
|
|
|
|
batch_size (int): Batch size. Defaults to 1.
|
|
|
|
data (str): Path to the dataset YAML file. Defaults to 'ROOT / "data/coco128.yaml"'.
|
|
|
|
device (str): CUDA device, e.g., '0' or '0,1,2,3' or 'cpu'. Defaults to an empty string (auto-select).
|
|
|
|
half (bool): Use FP16 half-precision inference. This is a flag and defaults to False.
|
|
|
|
test (bool): Test exports only. This is a flag and defaults to False.
|
|
|
|
pt_only (bool): Test PyTorch only. This is a flag and defaults to False.
|
2024-07-09 04:05:56 +08:00
|
|
|
hard_fail (bool | str): Throw an error on benchmark failure. Can be a boolean or a string representing a minimum
|
|
|
|
metric floor, e.g., '0.29'. Defaults to False.
|
2024-07-09 03:19:04 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
argparse.Namespace: Parsed command-line arguments encapsulated in an argparse Namespace object.
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
The function modifies the 'opt.data' by checking and validating the YAML path using 'check_yaml()'.
|
|
|
|
The parsed arguments are printed for reference using 'print_args()'.
|
|
|
|
"""
|
2022-02-12 23:05:43 +08:00
|
|
|
parser = argparse.ArgumentParser()
|
2024-01-08 08:29:14 +08:00
|
|
|
parser.add_argument("--weights", type=str, default=ROOT / "yolov5s.pt", help="weights path")
|
|
|
|
parser.add_argument("--imgsz", "--img", "--img-size", type=int, default=640, help="inference size (pixels)")
|
|
|
|
parser.add_argument("--batch-size", type=int, default=1, help="batch size")
|
|
|
|
parser.add_argument("--data", type=str, default=ROOT / "data/coco128.yaml", help="dataset.yaml path")
|
|
|
|
parser.add_argument("--device", default="", help="cuda device, i.e. 0 or 0,1,2,3 or cpu")
|
|
|
|
parser.add_argument("--half", action="store_true", help="use FP16 half-precision inference")
|
|
|
|
parser.add_argument("--test", action="store_true", help="test exports only")
|
|
|
|
parser.add_argument("--pt-only", action="store_true", help="test PyTorch only")
|
|
|
|
parser.add_argument("--hard-fail", nargs="?", const=True, default=False, help="Exception on error or < min metric")
|
2022-02-12 23:05:43 +08:00
|
|
|
opt = parser.parse_args()
|
2022-05-22 01:39:19 +08:00
|
|
|
opt.data = check_yaml(opt.data) # check YAML
|
2022-03-31 23:11:43 +08:00
|
|
|
print_args(vars(opt))
|
2022-02-12 23:05:43 +08:00
|
|
|
return opt
|
|
|
|
|
|
|
|
|
|
|
|
def main(opt):
|
2024-07-09 03:19:04 +08:00
|
|
|
"""
|
|
|
|
Executes YOLOv5 benchmark tests or main training/inference routines based on the provided command-line arguments.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
opt (argparse.Namespace): Parsed command-line arguments including options for weights, image size, batch size, data
|
2024-07-16 07:20:57 +08:00
|
|
|
configuration, device, and other flags for inference settings.
|
2024-07-09 03:19:04 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
None: This function does not return any value. It leverages side-effects such as logging and running benchmarks.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
```python
|
|
|
|
if __name__ == "__main__":
|
|
|
|
opt = parse_opt()
|
|
|
|
main(opt)
|
|
|
|
```
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
- For a complete list of supported export formats and their respective requirements, refer to the
|
|
|
|
[Ultralytics YOLOv5 Export Formats](https://github.com/ultralytics/yolov5#export-formats).
|
|
|
|
- Ensure that you have installed all necessary dependencies by following the installation instructions detailed in
|
|
|
|
the [main repository](https://github.com/ultralytics/yolov5#installation).
|
|
|
|
|
|
|
|
```shell
|
|
|
|
# Running benchmarks on default weights and image size
|
|
|
|
$ python benchmarks.py --weights yolov5s.pt --img 640
|
|
|
|
```
|
|
|
|
"""
|
2022-04-09 07:32:16 +08:00
|
|
|
test(**vars(opt)) if opt.test else run(**vars(opt))
|
2022-02-12 23:05:43 +08:00
|
|
|
|
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
if __name__ == "__main__":
|
2022-02-12 23:05:43 +08:00
|
|
|
opt = parse_opt()
|
|
|
|
main(opt)
|