Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ultralytics Code Refactor https://ultralytics.com/actions #2245

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 134 additions & 5 deletions benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,49 @@ def run(
pt_only=False, # test PyTorch only
hard_fail=False, # throw error on benchmark failure
):
"""Run YOLOv3 benchmarks on multiple export formats and validate performance metrics."""
"""
Run YOLOv3 benchmarks on multiple export formats and validate performance metrics.

Args:
weights (str | Path): The path to the model weights file. Default is 'yolov5s.pt'.
imgsz (int): Inference size in pixels. Default is 640.
batch_size (int): Batch size for inference. Default is 1.
data (str | Path): Path to the dataset configuration file in YAML format. Default is 'data/coco128.yaml'.
device (str): Device for inference ('cpu', 'cuda:0', etc.). Default is an empty string, which means auto-detection.
half (bool): Whether to use FP16 half-precision inference. Default is False.
test (bool): If True, only test export functionality without running benchmarks. Default is False.
pt_only (bool): If True, only test PyTorch export format. Default is False.
hard_fail (bool): Whether to throw an error on benchmark failure. Default is False.

Returns:
None: The function executes in-place and prints benchmark results directly to standard output. Results include export
format, model size, mean Average Precision (mAP), and inference time metrics.

Notes:
Export functionality is limited based on system and device compatibility. For example, CoreML inference is only supported
on macOS>=10.13. Edge TPU and TensorFlow.js are not supported for inference.

Example:
```python
run(weights='yolov5s.pt', imgsz=640, batch_size=1, device='cuda:0')
```

See Also:
- `export.py`: Scripts and functions for exporting models to different formats.
- `val.py`: Scripts and functions for validating model performance metrics.

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
$ pip install -U nvidia-tensorrt --index-url https://pypi.ngc.nvidia.com # TensorRT
```

Usage:
```
$ python benchmarks.py --weights yolov5s.pt --img 640
```
"""
y, t = [], time.time()
device = select_device(device)
model_type = type(attempt_load(weights, fuse=False)) # DetectionModel, SegmentationModel, etc.
Expand Down Expand Up @@ -125,7 +167,41 @@ def test(
pt_only=False, # test PyTorch only
hard_fail=False, # throw error on benchmark failure
):
"""Run YOLOv3 export tests for various formats and log the results, including export success status."""
"""
Run YOLOv3 export tests for various formats and log the results, including export success status.

Args:
weights (str | Path): Path to the weights file. Defaults to ROOT / 'yolov5s.pt'.
imgsz (int): Inference image size in pixels. Defaults to 640.
batch_size (int): Number of images per batch. Defaults to 1.
data (str | Path): Path to the dataset YAML file. Defaults to ROOT / 'data/coco128.yaml'.
device (str): Device to run the tests on, e.g., '0' or '0,1,2,3' for CUDA devices or 'cpu' for CPU. Defaults to ''.
half (bool): Use FP16 half-precision inference if True. Defaults to False.
test (bool): If True, test exports only. Defaults to False.
pt_only (bool): If True, test only PyTorch format. Defaults to False.
hard_fail (bool): If True, throws an error if benchmarking fails. Defaults to False.

Returns:
pd.DataFrame: DataFrame containing test results with columns 'Format' and 'Export' representing the format type and
whether the export was successful (True, False).

Notes:
- Ensure required dependencies are installed as per instructions in the documentation.
- Some formats such as Edge TPU and TensorFlow.js are unsupported.
- CoreML inference is only supported on macOS >=10.13.

Example:
```python
results = test(weights='yolov5s.pt', imgsz=640, device='0', half=True)
print(results)
```

Usage:
Run the function to test YOLOv3 exports:
```python
$ python benchmarks.py --weights yolov5s.pt --img 640
```
"""
y, t = [], time.time()
device = select_device(device)
for i, (name, f, suffix, gpu) in export.export_formats().iterrows(): # index, (name, file, suffix, gpu-capable)
Expand All @@ -151,8 +227,35 @@ def test(


def parse_opt():
"""Parses command line arguments for model inference configurations, including weights, image size, and device
options.
"""
Parses command line arguments for model inference configurations, enabling customizable settings for weights, image
size, batch size, device, and other options.

Args:
--weights (str): Path to the weights file. Default is 'ROOT / "yolov3-tiny.pt"'.
--imgsz | --img | --img-size (int): Inference image size in pixels. Default is 640.
--batch-size (int): Batch size for inference. Default is 1.
--data (str): Path to the dataset configuration file (dataset.yaml). Default is 'ROOT / "data/coco128.yaml"'.
--device (str): CUDA device identifier, e.g., '0' for single GPU, '0,1,2,3' for multiple GPUs, or 'cpu' for CPU.
Default is an empty string.
--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.
--pt-only (bool): If set, test only the PyTorch model without exporting to other formats. Default is False.
--hard-fail (str | bool): If set without a value, trigger an exception on error. If set with a string value, enforce
a minimum metric threshold for validation. Default is False.

Returns:
None

Note:
Ensure the dataset configuration path (dataset.yaml) is correctly specified. Use 'check_yaml' to validate the
file format.

Example:
```python
from your_module import parse_opt
parse_opt()
```
"""
parser = argparse.ArgumentParser()
parser.add_argument("--weights", type=str, default=ROOT / "yolov3-tiny.pt", help="weights path")
Expand All @@ -171,7 +274,33 @@ def parse_opt():


def main(opt):
"""Executes tests or main pipeline on provided options, determining behavior based on `opt.test`."""
"""
Executes YOLOv3 benchmarks and export tests based on command line options.

Args:
opt (argparse.Namespace): Parsed command-line options including weights path, image size, batch size, dataset path,
device type, precision, testing flag, PyTorch-only flag, and hard fail condition.

Returns:
pd.DataFrame: DataFrame containing benchmark or export results, with columns for format, size, mAP50-95, and
inference time for benchmarks, or format and export status for tests.

Notes:
See [benchmarks documentation](https://github.com/ultralytics/ultralytics) for detailed usage examples and
export format support.

Examples:
```python
# Execute benchmarks with default options
opt = parse_opt()
main(opt)

# Execute export tests only
opt = parse_opt()
opt.test = True
main(opt)
```
"""
test(**vars(opt)) if opt.test else run(**vars(opt))


Expand Down
100 changes: 97 additions & 3 deletions classify/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,49 @@ def run(
dnn=False, # use OpenCV DNN for ONNX inference
vid_stride=1, # video frame-rate stride
):
"""Performs YOLOv3 classification inference on various input sources and saves or displays results."""
"""
Performs YOLOv3 classification inference on various input sources and saves or displays results.

Args:
weights (Path | str): Path to the model weights file (e.g., 'yolov5s-cls.pt').
source (Path | str): Source for inference, which can be a file, directory, URL, glob, screen, or webcam index.
data (Path | str): Path to the dataset YAML file.
imgsz (tuple): Inference size (height, width). Default is (224, 224).
device (str): Device to run inference on, e.g., '0', '0,1,2,3', or 'cpu'. Default is an empty string (auto-select).
view_img (bool): Whether to display the results. Default is False.
save_txt (bool): Whether to save the results to *.txt files. Default is False.
nosave (bool): If True, do not save images/videos. Default is False.
augment (bool): Whether to use augmented inference. Default is False.
visualize (bool): Whether to visualize features. Default is False.
update (bool): If True, update all models. Default is False.
project (Path | str): Project name to save results to. Default is 'runs/predict-cls'.
name (str): Sub-directory name to save results to. Default is 'exp'.
exist_ok (bool): If True, existing project/name is okay and will not be incremented. Default is False.
half (bool): Use FP16 half-precision inference. Default is False.
dnn (bool): Use OpenCV DNN for ONNX inference. Default is False.
vid_stride (int): Video frame-rate stride. Default is 1.

Returns:
None: This function saves and/or displays the inference results directly.

Examples:
Run inference on an image:
```python
run(weights='yolov5s-cls.pt', source='image.jpg')
```

Run inference on a video:
```python
run(weights='yolov5s-cls.pt', source='video.mp4', view_img=True, nosave=False)
```

Note:
Example sources include webcam (0), image files ('img.jpg'), video files ('vid.mp4'), directories ('path/'),
lists of images ('list.txt'), globs ('path/*.jpg'), YouTube URLs, and RTSP/RTMP/HTTP streams.

Reference:
https://github.com/ultralytics/ultralytics
"""
source = str(source)
save_img = not nosave and not source.endswith(".txt") # save inference images
is_file = Path(source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
Expand Down Expand Up @@ -205,7 +247,37 @@ def run(


def parse_opt():
"""Parses command line arguments for model inference settings, returns a Namespace of options."""
"""
Parses command line arguments for model inference settings, returns a Namespace of options.

Args:
weights (str): Model path(s). Default is 'yolov5s-cls.pt'.
source (str): File/directory/URL/glob/screen/0(webcam). Default is 'data/images'.
data (str): Optional dataset.yaml path. Default is 'data/coco128.yaml'.
imgsz (list of int): Inference size (height, width). Default is [224].
device (str): CUDA device, i.e. '0' or '0,1,2,3' or 'cpu'. Default is ''.
view_img (bool): Flag to show results. Default is False.
save_txt (bool): Flag to save results to a text file. Default is False.
nosave (bool): Flag to not save images/videos. Default is False.
augment (bool): Flag for augmented inference. Default is False.
visualize (bool): Flag to visualize features. Default is False.
update (bool): Flag to update all models. Default is False.
project (str): Save results to project/name. Default is 'runs/predict-cls'.
name (str): Save results to project/name. Default is 'exp'.
exist_ok (bool): Flag to not increment if project/name exists already. Default is False.
half (bool): Flag to use FP16 half-precision inference. Default is False.
dnn (bool): Flag to use OpenCV DNN for ONNX inference. Default is False.
vid_stride (int): Video frame-rate stride. Default is 1.

Returns:
argparse.Namespace: Namespace object containing parsed arguments.

Examples:
```python
opt = parse_opt()
print(opt.weights)
```
"""
parser = argparse.ArgumentParser()
parser.add_argument("--weights", nargs="+", type=str, default=ROOT / "yolov5s-cls.pt", help="model path(s)")
parser.add_argument("--source", type=str, default=ROOT / "data/images", help="file/dir/URL/glob/screen/0(webcam)")
Expand All @@ -231,7 +303,29 @@ def parse_opt():


def main(opt):
"""Entry point for running the model; checks requirements and calls `run` with options parsed from CLI."""
"""
Entry point for running YOLOv3 classification inference; checks requirements and calls the `run` function.

Args:
opt (argparse.Namespace): Command-line arguments parsed from `parse_opt()`. Includes options like weights path,
source for images or videos, device, image size, and other settings.

Returns:
None

Note:
This function ensures that the required dependencies are installed by verifying them against `requirements.txt`,
excluding `tensorboard` and `thop`.

Example:
To run inference on an image using a specific weights file:

```bash
$ python classify/predict.py --weights yolov5s-cls.pt --source img.jpg
```

This will start the YOLOv3 classification inference on `img.jpg` using the specified weights.
"""
check_requirements(ROOT / "requirements.txt", exclude=("tensorboard", "thop"))
run(**vars(opt))

Expand Down
Loading
Loading