Skip to content

Commit

Permalink
Ultralytics Code Refactor https://ultralytics.com/actions (#2251)
Browse files Browse the repository at this point in the history
* Refactor code for speed and clarity

* Auto-format by https://ultralytics.com/actions

---------

Co-authored-by: UltralyticsAssistant <[email protected]>
  • Loading branch information
glenn-jocher and UltralyticsAssistant authored Jul 15, 2024
1 parent fa45009 commit af56149
Show file tree
Hide file tree
Showing 6 changed files with 445 additions and 306 deletions.
16 changes: 8 additions & 8 deletions benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ def run(
None
Notes:
This function iterates over multiple export formats, performs the export, and then validates the model's
performance using appropriate validation functions for detection and segmentation models. The results are logged,
and optionally, benchmarks can be configured to raise errors on failures using the `hard_fail` argument.
This function iterates over multiple export formats, performs the export, and then validates the model's performance
using appropriate validation functions for detection and segmentation models. The results are logged, and optionally,
benchmarks can be configured to raise errors on failures using the `hard_fail` argument.
Examples:
```python
Expand Down Expand Up @@ -173,7 +173,7 @@ def test(
hard_fail (bool): Raise an error on benchmark failure. Defaults to False.
Returns:
pd.DataFrame: A DataFrame containing the export formats and their success status. (pd.DataFrame)
pd.DataFrame: A DataFrame containing the export formats and their success status.
Examples:
```python
Expand Down Expand Up @@ -223,7 +223,7 @@ def test(

def parse_opt():
"""
Parses command line arguments for YOLOv3 inference and export configuration.
Parses command line arguments for YOLOv3 inference and export configurations.
Args:
--weights (str): Path to the weights file. Default is 'ROOT / "yolov3-tiny.pt"'.
Expand All @@ -233,10 +233,10 @@ def parse_opt():
--device (str): CUDA device identifier, e.g., '0' for single GPU, '0,1,2,3' for multiple GPUs, or 'cpu' for CPU
inference. Default is "".
--half (bool): If set, use FP16 half-precision inference. Default is False.
--test (bool): If set, only test exports without running inference. Default is False.
--test (bool): If set, test only exports without running inference. Default is False.
--pt-only (bool): If set, test only the PyTorch model without exporting to other formats. Default is False.
--hard-fail (str | bool): If set, raise an exception on benchmark failure. Can also be a string representing
the minimum metric floor for success. Default is False.
--hard-fail (str | bool): If set, raise an exception on benchmark failure. Can also be a string representing the
minimum metric floor for success. Default is False.
Returns:
argparse.Namespace: The parsed arguments as a namespace object.
Expand Down
73 changes: 40 additions & 33 deletions detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def run(
vid_stride=1, # video frame-rate stride
):
"""
Performs YOLOv3 detection on various input sources including images, videos, streams, and YouTube URLs.
Run YOLOv3 detection inference on various input sources such as images, videos, streams, and YouTube URLs.
Args:
weights (str | Path): Path to the model weights file or a Triton URL (default: 'yolov5s.pt').
Expand Down Expand Up @@ -287,39 +287,45 @@ def run(

def parse_opt():
"""
Parses and returns command-line options for model inference configurations.
Parses and returns command-line options for running YOLOv3 model detection.
Args:
--weights (list[str]): Model path or triton URL. Default: `ROOT / "yolov3-tiny.pt"`.
--source (str): File/directory/URL/glob/screen/0(webcam) for input data. Default: `ROOT / "data/images"`.
--data (str): (Optional) Path to dataset.yaml. Default: `ROOT / "data/coco128.yaml"`.
--imgsz (list[int]): Inference size as height, width. Accepts multiple values. Default: `[640]`.
--conf-thres (float): Confidence threshold for predictions. Default: `0.25`.
--iou-thres (float): IoU threshold for Non-Maximum Suppression (NMS). Default: `0.45`.
--max-det (int): Maximum number of detections per image. Default: `1000`.
--device (str): CUDA device to run the model on, e.g., `0` or `0,1,2,3` or `cpu`. Default: `""`.
--view-img (bool): Display results on the screen. Default: `False`.
--save-txt (bool): Save results to text files. Default: `False`.
--save-conf (bool): Save confidence scores in text labels. Default: `False`.
--save-crop (bool): Save cropped prediction boxes. Default: `False`.
--nosave (bool): Do not save images/videos. Default: `False`.
--classes (list[int]): Filter results by class, e.g., `--classes 0` or `--classes 0 2 3`. Default: `None`.
--agnostic-nms (bool): Perform class-agnostic NMS. Default: `False`.
--augment (bool): Perform augmented inference. Default: `False`.
--visualize (bool): Visualize features. Default: `False`.
--update (bool): Update all models. Default: `False`.
--project (str): Directory to save results, saved as "project/name". Default: `ROOT / "runs/detect"`.
--name (str): Directory name for saving results, e.g., "exp" in "project/name". Default: `"exp"`.
--exist-ok (bool): Allow results to be saved in an existing directory without incrementing. Default: `False`.
--line-thickness (int): Thickness of bounding box lines (in pixels). Default: `3`.
--hide-labels (bool): Hide labels on detected objects. Default: `False`.
--hide-conf (bool): Hide confidence scores on labels. Default: `False`.
--half (bool): Use FP16 half-precision inference. Default: `False`.
--dnn (bool): Use OpenCV DNN backend for ONNX inference. Default: `False`.
--vid-stride (int): Frame-rate stride for video input. Default: `1`.
--weights (list[str]): Model path or Triton URL. Default: ROOT / "yolov3-tiny.pt".
--source (str): Input data source like file/dir/URL/glob/screen/0(webcam). Default: ROOT / "data/images".
--data (str): Optional path to dataset.yaml. Default: ROOT / "data/coco128.yaml".
--imgsz (list[int]): Inference size as height, width. Accepts multiple values. Default: [640].
--conf-thres (float): Confidence threshold for predictions. Default: 0.25.
--iou-thres (float): IoU threshold for Non-Maximum Suppression (NMS). Default: 0.45.
--max-det (int): Maximum number of detections per image. Default: 1000.
--device (str): CUDA device identifier, e.g. "0" or "0,1,2,3" or "cpu". Default: "" (auto-select).
--view-img (bool): Display results. Default: False.
--save-txt (bool): Save results to *.txt files. Default: False.
--save-conf (bool): Save confidence scores in text labels. Default: False.
--save-crop (bool): Save cropped prediction boxes. Default: False.
--nosave (bool): Do not save images/videos. Default: False.
--classes (list[int] | None): Filter results by class, e.g. [0, 2, 3]. Default: None.
--agnostic-nms (bool): Perform class-agnostic NMS. Default: False.
--augment (bool): Apply augmented inference. Default: False.
--visualize (bool): Visualize feature maps. Default: False.
--update (bool): Update all models. Default: False.
--project (str): Directory to save results; results saved to "project/name". Default: ROOT / "runs/detect".
--name (str): Name of the specific run; results saved to "project/name". Default: "exp".
--exist-ok (bool): Allow results to be saved in an existing directory without incrementing. Default: False.
--line-thickness (int): Bounding box line thickness in pixels. Default: 3.
--hide-labels (bool): Hide labels on detections. Default: False.
--hide-conf (bool): Hide confidence scores on labels. Default: False.
--half (bool): Use FP16 half-precision inference. Default: False.
--dnn (bool): Use OpenCV DNN backend for ONNX inference. Default: False.
--vid-stride (int): Frame-rate stride for video input. Default: 1.
Returns:
argparse.Namespace: Parsed command-line arguments for inference configurations.
argparse.Namespace: Parsed command-line arguments for YOLOv3 inference configurations.
Example:
```python
options = parse_opt()
run(**vars(options))
```
"""
parser = argparse.ArgumentParser()
parser.add_argument(
Expand Down Expand Up @@ -359,7 +365,7 @@ def parse_opt():

def main(opt):
"""
Entry point for running the model; checks requirements and calls `run` with parsed options.
Entry point for running the YOLO model; checks requirements and calls `run` with parsed options.
Args:
opt (argparse.Namespace): Parsed command-line options, which include:
Expand Down Expand Up @@ -400,9 +406,10 @@ def main(opt):
opt = parse_opt()
main(opt)
```
Notes:
Run this function as the entry point for using YOLOv3 for object detection on a variety of input sources such
as images, videos, directories, webcams, streams, etc. This function ensures all requirements are checked and
Run this function as the entry point for using YOLO for object detection on a variety of input sources such as
images, videos, directories, webcams, streams, etc. This function ensures all requirements are checked and
subsequently initiates the detection process by calling the `run` function with appropriate options.
"""
check_requirements(ROOT / "requirements.txt", exclude=("tensorboard", "thop"))
Expand Down
Loading

0 comments on commit af56149

Please sign in to comment.