Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
Glenn Jocher 2024-06-30 22:09:02 +02:00 committed by GitHub
parent ff63a56a42
commit 691b5daccb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 124 additions and 113 deletions

View file

@ -38,7 +38,7 @@ def test_model_forward():
def test_model_methods():
"""Test various methods and properties of the YOLO model."""
"""Test various methods and properties of the YOLO model to ensure correct functionality."""
model = YOLO(MODEL)
# Model methods
@ -58,7 +58,7 @@ def test_model_methods():
def test_model_profile():
"""Test profiling of the YOLO model with 'profile=True' argument."""
"""Test profiling of the YOLO model with `profile=True` to assess performance and resource usage."""
from ultralytics.nn.tasks import DetectionModel
model = DetectionModel() # build model
@ -68,7 +68,7 @@ def test_model_profile():
@pytest.mark.skipif(not IS_TMP_WRITEABLE, reason="directory is not writeable")
def test_predict_txt():
"""Test YOLO predictions with sources (file, dir, glob, recursive glob) specified in a text file."""
"""Tests YOLO predictions with file, directory, and pattern sources listed in a text file."""
txt_file = TMP / "sources.txt"
with open(txt_file, "w") as f:
for x in [ASSETS / "bus.jpg", ASSETS, ASSETS / "*", ASSETS / "**/*.jpg"]:
@ -78,7 +78,7 @@ def test_predict_txt():
@pytest.mark.parametrize("model_name", MODELS)
def test_predict_img(model_name):
"""Test YOLO prediction on various types of image sources."""
"""Test YOLO model predictions on various image input types and sources, including online images."""
model = YOLO(WEIGHTS_DIR / model_name)
im = cv2.imread(str(SOURCE)) # uint8 numpy array
assert len(model(source=Image.open(SOURCE), save=True, verbose=True, imgsz=32)) == 1 # PIL
@ -100,12 +100,12 @@ def test_predict_img(model_name):
@pytest.mark.parametrize("model", MODELS)
def test_predict_visualize(model):
"""Test model predict methods with 'visualize=True' arguments."""
"""Test model prediction methods with 'visualize=True' to generate and display prediction visualizations."""
YOLO(WEIGHTS_DIR / model)(SOURCE, imgsz=32, visualize=True)
def test_predict_grey_and_4ch():
"""Test YOLO prediction on SOURCE converted to greyscale and 4-channel images."""
"""Test YOLO prediction on SOURCE converted to greyscale and 4-channel images with various filenames."""
im = Image.open(SOURCE)
directory = TMP / "im4"
directory.mkdir(parents=True, exist_ok=True)
@ -132,11 +132,7 @@ def test_predict_grey_and_4ch():
@pytest.mark.slow
@pytest.mark.skipif(not ONLINE, reason="environment is offline")
def test_youtube():
"""
Test YouTube inference.
Note: ConnectionError may occur during this test due to network instability or YouTube server availability.
"""
"""Test YOLO model on a YouTube video stream, handling potential network-related errors."""
model = YOLO(MODEL)
try:
model.predict("https://youtu.be/G17sBkb38XQ", imgsz=96, save=True)
@ -149,9 +145,9 @@ def test_youtube():
@pytest.mark.skipif(not IS_TMP_WRITEABLE, reason="directory is not writeable")
def test_track_stream():
"""
Test streaming tracking (short 10 frame video) with non-default ByteTrack tracker.
Tests streaming tracking on a short 10 frame video using ByteTrack tracker and different GMC methods.
Note imgsz=160 required for tracking for higher confidence and better matches
Note imgsz=160 required for tracking for higher confidence and better matches.
"""
video_url = "https://ultralytics.com/assets/decelera_portrait_min.mov"
model = YOLO(MODEL)
@ -175,21 +171,21 @@ def test_val():
def test_train_scratch():
"""Test training the YOLO model from scratch."""
"""Test training the YOLO model from scratch using the provided configuration."""
model = YOLO(CFG)
model.train(data="coco8.yaml", epochs=2, imgsz=32, cache="disk", batch=-1, close_mosaic=1, name="model")
model(SOURCE)
def test_train_pretrained():
"""Test training the YOLO model from a pre-trained state."""
"""Test training of the YOLO model starting from a pre-trained checkpoint."""
model = YOLO(WEIGHTS_DIR / "yolov8n-seg.pt")
model.train(data="coco8-seg.yaml", epochs=1, imgsz=32, cache="ram", copy_paste=0.5, mixup=0.5, name=0)
model(SOURCE)
def test_all_model_yamls():
"""Test YOLO model creation for all available YAML configurations."""
"""Test YOLO model creation for all available YAML configurations in the `cfg/models` directory."""
for m in (ROOT / "cfg" / "models").rglob("*.yaml"):
if "rtdetr" in m.name:
if TORCH_1_9: # torch<=1.8 issue - TypeError: __init__() got an unexpected keyword argument 'batch_first'
@ -208,7 +204,7 @@ def test_workflow():
def test_predict_callback_and_setup():
"""Test callback functionality during YOLO prediction."""
"""Test callback functionality during YOLO prediction setup and execution."""
def on_predict_batch_end(predictor):
"""Callback function that handles operations at the end of a prediction batch."""
@ -232,7 +228,7 @@ def test_predict_callback_and_setup():
@pytest.mark.parametrize("model", MODELS)
def test_results(model):
"""Test various result formats for the YOLO model."""
"""Ensure YOLO model predictions can be processed and printed in various formats."""
results = YOLO(WEIGHTS_DIR / model)([SOURCE, SOURCE], imgsz=160)
for r in results:
r = r.cpu().numpy()
@ -247,7 +243,7 @@ def test_results(model):
def test_labels_and_crops():
"""Test output from prediction args for saving detection labels and crops."""
"""Test output from prediction args for saving YOLO detection labels and crops; ensures accurate saving."""
imgs = [SOURCE, ASSETS / "zidane.jpg"]
results = YOLO(WEIGHTS_DIR / "yolov8n.pt")(imgs, imgsz=160, save_txt=True, save_crop=True)
save_path = Path(results[0].save_dir)
@ -270,7 +266,7 @@ def test_labels_and_crops():
@pytest.mark.skipif(not ONLINE, reason="environment is offline")
def test_data_utils():
"""Test utility functions in ultralytics/data/utils.py."""
"""Test utility functions in ultralytics/data/utils.py, including dataset stats and auto-splitting."""
from ultralytics.data.utils import HUBDatasetStats, autosplit
from ultralytics.utils.downloads import zip_directory
@ -290,7 +286,7 @@ def test_data_utils():
@pytest.mark.skipif(not ONLINE, reason="environment is offline")
def test_data_converter():
"""Test dataset converters."""
"""Test dataset conversion functions from COCO to YOLO format and class mappings."""
from ultralytics.data.converter import coco80_to_coco91_class, convert_coco
file = "instances_val2017.json"
@ -300,7 +296,7 @@ def test_data_converter():
def test_data_annotator():
"""Test automatic data annotation."""
"""Automatically annotate data using specified detection and segmentation models."""
from ultralytics.data.annotator import auto_annotate
auto_annotate(
@ -323,7 +319,7 @@ def test_events():
def test_cfg_init():
"""Test configuration initialization utilities."""
"""Test configuration initialization utilities from the 'ultralytics.cfg' module."""
from ultralytics.cfg import check_dict_alignment, copy_default_cfg, smart_value
with contextlib.suppress(SyntaxError):
@ -334,7 +330,7 @@ def test_cfg_init():
def test_utils_init():
"""Test initialization utilities."""
"""Test initialization utilities in the Ultralytics library."""
from ultralytics.utils import get_git_branch, get_git_origin_url, get_ubuntu_version, is_github_action_running
get_ubuntu_version()
@ -344,7 +340,7 @@ def test_utils_init():
def test_utils_checks():
"""Test various utility checks."""
"""Test various utility checks for filenames, git status, requirements, image sizes, and versions."""
checks.check_yolov5u_filename("yolov5n.pt")
checks.git_describe(ROOT)
checks.check_requirements() # check requirements.txt
@ -356,14 +352,14 @@ def test_utils_checks():
@pytest.mark.skipif(WINDOWS, reason="Windows profiling is extremely slow (cause unknown)")
def test_utils_benchmarks():
"""Test model benchmarking."""
"""Benchmark model performance using 'ProfileModels' from 'ultralytics.utils.benchmarks'."""
from ultralytics.utils.benchmarks import ProfileModels
ProfileModels(["yolov8n.yaml"], imgsz=32, min_time=1, num_timed_runs=3, num_warmup_runs=1).profile()
def test_utils_torchutils():
"""Test Torch utility functions."""
"""Test Torch utility functions including profiling and FLOP calculations."""
from ultralytics.nn.modules.conv import Conv
from ultralytics.utils.torch_utils import get_flops_with_torch_profiler, profile, time_sync
@ -378,14 +374,14 @@ def test_utils_torchutils():
@pytest.mark.slow
@pytest.mark.skipif(not ONLINE, reason="environment is offline")
def test_utils_downloads():
"""Test file download utilities."""
"""Test file download utilities from ultralytics.utils.downloads."""
from ultralytics.utils.downloads import get_google_drive_file_info
get_google_drive_file_info("https://drive.google.com/file/d/1cqT-cJgANNrhIHCrEufUYhQ4RqiWG_lJ/view?usp=drive_link")
def test_utils_ops():
"""Test various operations utilities."""
"""Test utility operations functions for coordinate transformation and normalization."""
from ultralytics.utils.ops import (
ltwh2xywh,
ltwh2xyxy,
@ -414,7 +410,7 @@ def test_utils_ops():
def test_utils_files():
"""Test file handling utilities."""
"""Test file handling utilities including file age, date, and paths with spaces."""
from ultralytics.utils.files import file_age, file_date, get_latest_run, spaces_in_path
file_age(SOURCE)
@ -429,7 +425,7 @@ def test_utils_files():
@pytest.mark.slow
def test_utils_patches_torch_save():
"""Test torch_save backoff when _torch_save throws RuntimeError."""
"""Test torch_save backoff when _torch_save raises RuntimeError to ensure robustness."""
from unittest.mock import MagicMock, patch
from ultralytics.utils.patches import torch_save
@ -444,7 +440,7 @@ def test_utils_patches_torch_save():
def test_nn_modules_conv():
"""Test Convolutional Neural Network modules."""
"""Test Convolutional Neural Network modules including CBAM, Conv2, and ConvTranspose."""
from ultralytics.nn.modules.conv import CBAM, Conv2, ConvTranspose, DWConvTranspose2d, Focus
c1, c2 = 8, 16 # input and output channels
@ -463,7 +459,7 @@ def test_nn_modules_conv():
def test_nn_modules_block():
"""Test Neural Network block modules."""
"""Test various blocks in neural network modules including C1, C3TR, BottleneckCSP, C3Ghost, and C3x."""
from ultralytics.nn.modules.block import C1, C3TR, BottleneckCSP, C3Ghost, C3x
c1, c2 = 8, 16 # input and output channels
@ -479,7 +475,7 @@ def test_nn_modules_block():
@pytest.mark.skipif(not ONLINE, reason="environment is offline")
def test_hub():
"""Test Ultralytics HUB functionalities."""
"""Test Ultralytics HUB functionalities (e.g. export formats, logout)."""
from ultralytics.hub import export_fmts_hub, logout
from ultralytics.hub.utils import smart_request
@ -490,7 +486,7 @@ def test_hub():
@pytest.fixture
def image():
"""Loads an image from a predefined source using OpenCV."""
"""Load and return an image from a predefined source using OpenCV."""
return cv2.imread(str(SOURCE))
@ -504,7 +500,7 @@ def image():
],
)
def test_classify_transforms_train(image, auto_augment, erasing, force_color_jitter):
"""Tests classification transforms during training with various augmentation settings."""
"""Tests classification transforms during training with various augmentations to ensure proper functionality."""
from ultralytics.data.augment import classify_augmentations
transform = classify_augmentations(
@ -533,7 +529,7 @@ def test_classify_transforms_train(image, auto_augment, erasing, force_color_jit
@pytest.mark.slow
@pytest.mark.skipif(not ONLINE, reason="environment is offline")
def test_model_tune():
"""Tune YOLO model for performance."""
"""Tune YOLO model for performance improvement."""
YOLO("yolov8n-pose.pt").tune(data="coco8-pose.yaml", plots=False, imgsz=32, epochs=1, iterations=2, device="cpu")
YOLO("yolov8n-cls.pt").tune(data="imagenet10", plots=False, imgsz=32, epochs=1, iterations=2, device="cpu")
@ -550,7 +546,7 @@ def test_model_embeddings():
@pytest.mark.skipif(checks.IS_PYTHON_3_12, reason="YOLOWorld with CLIP is not supported in Python 3.12")
def test_yolo_world():
"""Tests YOLO world models with different configurations, including classes, detection, and training scenarios."""
"""Tests YOLO world models with CLIP support, including detection and training scenarios."""
model = YOLO("yolov8s-world.pt") # no YOLOv8n-world model yet
model.set_classes(["tree", "window"])
model(SOURCE, conf=0.01)
@ -581,7 +577,7 @@ def test_yolo_world():
def test_yolov10():
"""A simple test for yolov10 for now."""
"""Test YOLOv10 model training, validation, and prediction steps with minimal configurations."""
model = YOLO("yolov10n.yaml")
# train/val/predict
model.train(data="coco8.yaml", epochs=1, imgsz=32, close_mosaic=1, cache="disk")