ultralytics 8.3.117__py3-none-any.whl → 8.3.119__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. tests/__init__.py +22 -0
  2. tests/conftest.py +83 -0
  3. tests/test_cli.py +128 -0
  4. tests/test_cuda.py +164 -0
  5. tests/test_engine.py +131 -0
  6. tests/test_exports.py +231 -0
  7. tests/test_integrations.py +154 -0
  8. tests/test_python.py +695 -0
  9. tests/test_solutions.py +176 -0
  10. ultralytics/__init__.py +1 -1
  11. ultralytics/cfg/__init__.py +1 -0
  12. ultralytics/cfg/default.yaml +1 -0
  13. ultralytics/data/augment.py +122 -7
  14. ultralytics/data/base.py +9 -2
  15. ultralytics/data/dataset.py +7 -5
  16. ultralytics/engine/exporter.py +10 -91
  17. ultralytics/engine/tuner.py +2 -1
  18. ultralytics/models/rtdetr/val.py +1 -0
  19. ultralytics/models/yolo/detect/predict.py +1 -1
  20. ultralytics/models/yolo/model.py +2 -3
  21. ultralytics/models/yolo/obb/train.py +1 -1
  22. ultralytics/models/yolo/pose/predict.py +1 -1
  23. ultralytics/models/yolo/pose/train.py +1 -1
  24. ultralytics/models/yolo/pose/val.py +1 -1
  25. ultralytics/models/yolo/segment/train.py +3 -3
  26. ultralytics/nn/autobackend.py +2 -5
  27. ultralytics/nn/text_model.py +97 -13
  28. ultralytics/utils/benchmarks.py +1 -1
  29. ultralytics/utils/downloads.py +1 -0
  30. ultralytics/utils/ops.py +1 -1
  31. ultralytics/utils/tuner.py +2 -1
  32. {ultralytics-8.3.117.dist-info → ultralytics-8.3.119.dist-info}/METADATA +6 -7
  33. {ultralytics-8.3.117.dist-info → ultralytics-8.3.119.dist-info}/RECORD +37 -28
  34. {ultralytics-8.3.117.dist-info → ultralytics-8.3.119.dist-info}/WHEEL +1 -1
  35. {ultralytics-8.3.117.dist-info → ultralytics-8.3.119.dist-info}/entry_points.txt +0 -0
  36. {ultralytics-8.3.117.dist-info → ultralytics-8.3.119.dist-info}/licenses/LICENSE +0 -0
  37. {ultralytics-8.3.117.dist-info → ultralytics-8.3.119.dist-info}/top_level.txt +0 -0
tests/test_python.py ADDED
@@ -0,0 +1,695 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
3
+ import contextlib
4
+ import csv
5
+ import urllib
6
+ from copy import copy
7
+ from pathlib import Path
8
+
9
+ import cv2
10
+ import numpy as np
11
+ import pytest
12
+ import torch
13
+ import yaml
14
+ from PIL import Image
15
+
16
+ from tests import CFG, MODEL, SOURCE, SOURCES_LIST, TMP
17
+ from ultralytics import RTDETR, YOLO
18
+ from ultralytics.cfg import MODELS, TASK2DATA, TASKS
19
+ from ultralytics.data.build import load_inference_source
20
+ from ultralytics.utils import (
21
+ ARM64,
22
+ ASSETS,
23
+ DEFAULT_CFG,
24
+ DEFAULT_CFG_PATH,
25
+ LINUX,
26
+ LOGGER,
27
+ ONLINE,
28
+ ROOT,
29
+ WEIGHTS_DIR,
30
+ WINDOWS,
31
+ checks,
32
+ is_dir_writeable,
33
+ is_github_action_running,
34
+ )
35
+ from ultralytics.utils.downloads import download
36
+ from ultralytics.utils.torch_utils import TORCH_1_9
37
+
38
+ IS_TMP_WRITEABLE = is_dir_writeable(TMP) # WARNING: must be run once tests start as TMP does not exist on tests/init
39
+
40
+
41
+ def test_model_forward():
42
+ """Test the forward pass of the YOLO model."""
43
+ model = YOLO(CFG)
44
+ model(source=None, imgsz=32, augment=True) # also test no source and augment
45
+
46
+
47
+ def test_model_methods():
48
+ """Test various methods and properties of the YOLO model to ensure correct functionality."""
49
+ model = YOLO(MODEL)
50
+
51
+ # Model methods
52
+ model.info(verbose=True, detailed=True)
53
+ model = model.reset_weights()
54
+ model = model.load(MODEL)
55
+ model.to("cpu")
56
+ model.fuse()
57
+ model.clear_callback("on_train_start")
58
+ model.reset_callbacks()
59
+
60
+ # Model properties
61
+ _ = model.names
62
+ _ = model.device
63
+ _ = model.transforms
64
+ _ = model.task_map
65
+
66
+
67
+ def test_model_profile():
68
+ """Test profiling of the YOLO model with `profile=True` to assess performance and resource usage."""
69
+ from ultralytics.nn.tasks import DetectionModel
70
+
71
+ model = DetectionModel() # build model
72
+ im = torch.randn(1, 3, 64, 64) # requires min imgsz=64
73
+ _ = model.predict(im, profile=True)
74
+
75
+
76
+ @pytest.mark.skipif(not IS_TMP_WRITEABLE, reason="directory is not writeable")
77
+ def test_predict_txt():
78
+ """Test YOLO predictions with file, directory, and pattern sources listed in a text file."""
79
+ file = TMP / "sources_multi_row.txt"
80
+ with open(file, "w") as f:
81
+ for src in SOURCES_LIST:
82
+ f.write(f"{src}\n")
83
+ results = YOLO(MODEL)(source=file, imgsz=32)
84
+ assert len(results) == 7 # 1 + 2 + 2 + 2 = 7 images
85
+
86
+
87
+ @pytest.mark.skipif(True, reason="disabled for testing")
88
+ @pytest.mark.skipif(not IS_TMP_WRITEABLE, reason="directory is not writeable")
89
+ def test_predict_csv_multi_row():
90
+ """Test YOLO predictions with sources listed in multiple rows of a CSV file."""
91
+ file = TMP / "sources_multi_row.csv"
92
+ with open(file, "w", newline="") as f:
93
+ writer = csv.writer(f)
94
+ writer.writerow(["source"])
95
+ writer.writerows([[src] for src in SOURCES_LIST])
96
+ results = YOLO(MODEL)(source=file, imgsz=32)
97
+ assert len(results) == 7 # 1 + 2 + 2 + 2 = 7 images
98
+
99
+
100
+ @pytest.mark.skipif(True, reason="disabled for testing")
101
+ @pytest.mark.skipif(not IS_TMP_WRITEABLE, reason="directory is not writeable")
102
+ def test_predict_csv_single_row():
103
+ """Test YOLO predictions with sources listed in a single row of a CSV file."""
104
+ file = TMP / "sources_single_row.csv"
105
+ with open(file, "w", newline="") as f:
106
+ writer = csv.writer(f)
107
+ writer.writerow(SOURCES_LIST)
108
+ results = YOLO(MODEL)(source=file, imgsz=32)
109
+ assert len(results) == 7 # 1 + 2 + 2 + 2 = 7 images
110
+
111
+
112
+ @pytest.mark.parametrize("model_name", MODELS)
113
+ def test_predict_img(model_name):
114
+ """Test YOLO model predictions on various image input types and sources, including online images."""
115
+ model = YOLO(WEIGHTS_DIR / model_name)
116
+ im = cv2.imread(str(SOURCE)) # uint8 numpy array
117
+ assert len(model(source=Image.open(SOURCE), save=True, verbose=True, imgsz=32)) == 1 # PIL
118
+ assert len(model(source=im, save=True, save_txt=True, imgsz=32)) == 1 # ndarray
119
+ assert len(model(torch.rand((2, 3, 32, 32)), imgsz=32)) == 2 # batch-size 2 Tensor, FP32 0.0-1.0 RGB order
120
+ assert len(model(source=[im, im], save=True, save_txt=True, imgsz=32)) == 2 # batch
121
+ assert len(list(model(source=[im, im], save=True, stream=True, imgsz=32))) == 2 # stream
122
+ assert len(model(torch.zeros(320, 640, 3).numpy().astype(np.uint8), imgsz=32)) == 1 # tensor to numpy
123
+ batch = [
124
+ str(SOURCE), # filename
125
+ Path(SOURCE), # Path
126
+ "https://github.com/ultralytics/assets/releases/download/v0.0.0/zidane.jpg" if ONLINE else SOURCE, # URI
127
+ cv2.imread(str(SOURCE)), # OpenCV
128
+ Image.open(SOURCE), # PIL
129
+ np.zeros((320, 640, 3), dtype=np.uint8), # numpy
130
+ ]
131
+ assert len(model(batch, imgsz=32, classes=0)) == len(batch) # multiple sources in a batch
132
+
133
+
134
+ @pytest.mark.parametrize("model", MODELS)
135
+ def test_predict_visualize(model):
136
+ """Test model prediction methods with 'visualize=True' to generate and display prediction visualizations."""
137
+ YOLO(WEIGHTS_DIR / model)(SOURCE, imgsz=32, visualize=True)
138
+
139
+
140
+ def test_predict_grey_and_4ch():
141
+ """Test YOLO prediction on SOURCE converted to greyscale and 4-channel images with various filenames."""
142
+ im = Image.open(SOURCE)
143
+ directory = TMP / "im4"
144
+ directory.mkdir(parents=True, exist_ok=True)
145
+
146
+ source_greyscale = directory / "greyscale.jpg"
147
+ source_rgba = directory / "4ch.png"
148
+ source_non_utf = directory / "non_UTF_测试文件_tést_image.jpg"
149
+ source_spaces = directory / "image with spaces.jpg"
150
+
151
+ im.convert("L").save(source_greyscale) # greyscale
152
+ im.convert("RGBA").save(source_rgba) # 4-ch PNG with alpha
153
+ im.save(source_non_utf) # non-UTF characters in filename
154
+ im.save(source_spaces) # spaces in filename
155
+
156
+ # Inference
157
+ model = YOLO(MODEL)
158
+ for f in source_rgba, source_greyscale, source_non_utf, source_spaces:
159
+ for source in Image.open(f), cv2.imread(str(f)), f:
160
+ results = model(source, save=True, verbose=True, imgsz=32)
161
+ assert len(results) == 1 # verify that an image was run
162
+ f.unlink() # cleanup
163
+
164
+
165
+ @pytest.mark.slow
166
+ @pytest.mark.skipif(not ONLINE, reason="environment is offline")
167
+ @pytest.mark.skipif(is_github_action_running(), reason="No auth https://github.com/JuanBindez/pytubefix/issues/166")
168
+ def test_youtube():
169
+ """Test YOLO model on a YouTube video stream, handling potential network-related errors."""
170
+ model = YOLO(MODEL)
171
+ try:
172
+ model.predict("https://youtu.be/G17sBkb38XQ", imgsz=96, save=True)
173
+ # Handle internet connection errors and 'urllib.error.HTTPError: HTTP Error 429: Too Many Requests'
174
+ except (urllib.error.HTTPError, ConnectionError) as e:
175
+ LOGGER.error(f"YouTube Test Error: {e}")
176
+
177
+
178
+ @pytest.mark.skipif(not ONLINE, reason="environment is offline")
179
+ @pytest.mark.skipif(not IS_TMP_WRITEABLE, reason="directory is not writeable")
180
+ def test_track_stream():
181
+ """
182
+ Test streaming tracking on a short 10 frame video using ByteTrack tracker and different GMC methods.
183
+
184
+ Note imgsz=160 required for tracking for higher confidence and better matches.
185
+ """
186
+ video_url = "https://github.com/ultralytics/assets/releases/download/v0.0.0/decelera_portrait_min.mov"
187
+ model = YOLO(MODEL)
188
+ model.track(video_url, imgsz=160, tracker="bytetrack.yaml")
189
+ model.track(video_url, imgsz=160, tracker="botsort.yaml", save_frames=True) # test frame saving also
190
+
191
+ # Test Global Motion Compensation (GMC) methods
192
+ for gmc in "orb", "sift", "ecc":
193
+ with open(ROOT / "cfg/trackers/botsort.yaml", encoding="utf-8") as f:
194
+ data = yaml.safe_load(f)
195
+ tracker = TMP / f"botsort-{gmc}.yaml"
196
+ data["gmc_method"] = gmc
197
+ with open(tracker, "w", encoding="utf-8") as f:
198
+ yaml.safe_dump(data, f)
199
+ model.track(video_url, imgsz=160, tracker=tracker)
200
+
201
+
202
+ def test_val():
203
+ """Test the validation mode of the YOLO model."""
204
+ YOLO(MODEL).val(data="coco8.yaml", imgsz=32)
205
+
206
+
207
+ def test_train_scratch():
208
+ """Test training the YOLO model from scratch using the provided configuration."""
209
+ model = YOLO(CFG)
210
+ model.train(data="coco8.yaml", epochs=2, imgsz=32, cache="disk", batch=-1, close_mosaic=1, name="model")
211
+ model(SOURCE)
212
+
213
+
214
+ @pytest.mark.parametrize("scls", [False, True])
215
+ def test_train_pretrained(scls):
216
+ """Test training of the YOLO model starting from a pre-trained checkpoint."""
217
+ model = YOLO(WEIGHTS_DIR / "yolo11n-seg.pt")
218
+ model.train(
219
+ data="coco8-seg.yaml", epochs=1, imgsz=32, cache="ram", copy_paste=0.5, mixup=0.5, name=0, single_cls=scls
220
+ )
221
+ model(SOURCE)
222
+
223
+
224
+ def test_all_model_yamls():
225
+ """Test YOLO model creation for all available YAML configurations in the `cfg/models` directory."""
226
+ for m in (ROOT / "cfg" / "models").rglob("*.yaml"):
227
+ if "rtdetr" in m.name:
228
+ if TORCH_1_9: # torch<=1.8 issue - TypeError: __init__() got an unexpected keyword argument 'batch_first'
229
+ _ = RTDETR(m.name)(SOURCE, imgsz=640) # must be 640
230
+ else:
231
+ YOLO(m.name)
232
+
233
+
234
+ @pytest.mark.skipif(WINDOWS, reason="Windows slow CI export bug https://github.com/ultralytics/ultralytics/pull/16003")
235
+ def test_workflow():
236
+ """Test the complete workflow including training, validation, prediction, and exporting."""
237
+ model = YOLO(MODEL)
238
+ model.train(data="coco8.yaml", epochs=1, imgsz=32, optimizer="SGD")
239
+ model.val(imgsz=32)
240
+ model.predict(SOURCE, imgsz=32)
241
+ model.export(format="torchscript") # WARNING: Windows slow CI export bug
242
+
243
+
244
+ def test_predict_callback_and_setup():
245
+ """Test callback functionality during YOLO prediction setup and execution."""
246
+
247
+ def on_predict_batch_end(predictor):
248
+ """Callback function that handles operations at the end of a prediction batch."""
249
+ path, im0s, _ = predictor.batch
250
+ im0s = im0s if isinstance(im0s, list) else [im0s]
251
+ bs = [predictor.dataset.bs for _ in range(len(path))]
252
+ predictor.results = zip(predictor.results, im0s, bs) # results is List[batch_size]
253
+
254
+ model = YOLO(MODEL)
255
+ model.add_callback("on_predict_batch_end", on_predict_batch_end)
256
+
257
+ dataset = load_inference_source(source=SOURCE)
258
+ bs = dataset.bs # noqa access predictor properties
259
+ results = model.predict(dataset, stream=True, imgsz=160) # source already setup
260
+ for r, im0, bs in results:
261
+ print("test_callback", im0.shape)
262
+ print("test_callback", bs)
263
+ boxes = r.boxes # Boxes object for bbox outputs
264
+ print(boxes)
265
+
266
+
267
+ @pytest.mark.parametrize("model", MODELS)
268
+ def test_results(model):
269
+ """Test YOLO model results processing and output in various formats."""
270
+ results = YOLO(WEIGHTS_DIR / model)([SOURCE, SOURCE], imgsz=160)
271
+ for r in results:
272
+ r = r.cpu().numpy()
273
+ print(r, len(r), r.path) # print numpy attributes
274
+ r = r.to(device="cpu", dtype=torch.float32)
275
+ r.save_txt(txt_file=TMP / "runs/tests/label.txt", save_conf=True)
276
+ r.save_crop(save_dir=TMP / "runs/tests/crops/")
277
+ r.to_json(normalize=True)
278
+ r.to_df(decimals=3)
279
+ r.to_csv()
280
+ r.to_xml()
281
+ r.plot(pil=True, save=True, filename=TMP / "results_plot_save.jpg")
282
+ r.plot(conf=True, boxes=True)
283
+ print(r, len(r), r.path) # print after methods
284
+
285
+
286
+ def test_labels_and_crops():
287
+ """Test output from prediction args for saving YOLO detection labels and crops."""
288
+ imgs = [SOURCE, ASSETS / "zidane.jpg"]
289
+ results = YOLO(WEIGHTS_DIR / "yolo11n.pt")(imgs, imgsz=160, save_txt=True, save_crop=True)
290
+ save_path = Path(results[0].save_dir)
291
+ for r in results:
292
+ im_name = Path(r.path).stem
293
+ cls_idxs = r.boxes.cls.int().tolist()
294
+ # Check correct detections
295
+ assert cls_idxs == ([0, 7, 0, 0] if r.path.endswith("bus.jpg") else [0, 0, 0]) # bus.jpg and zidane.jpg classes
296
+ # Check label path
297
+ labels = save_path / f"labels/{im_name}.txt"
298
+ assert labels.exists()
299
+ # Check detections match label count
300
+ assert len(r.boxes.data) == len([line for line in labels.read_text().splitlines() if line])
301
+ # Check crops path and files
302
+ crop_dirs = list((save_path / "crops").iterdir())
303
+ crop_files = [f for p in crop_dirs for f in p.glob("*")]
304
+ # Crop directories match detections
305
+ assert all(r.names.get(c) in {d.name for d in crop_dirs} for c in cls_idxs)
306
+ # Same number of crops as detections
307
+ assert len([f for f in crop_files if im_name in f.name]) == len(r.boxes.data)
308
+
309
+
310
+ @pytest.mark.skipif(not ONLINE, reason="environment is offline")
311
+ def test_data_utils():
312
+ """Test utility functions in ultralytics/data/utils.py, including dataset stats and auto-splitting."""
313
+ from ultralytics.data.split import autosplit
314
+ from ultralytics.data.utils import HUBDatasetStats
315
+ from ultralytics.utils.downloads import zip_directory
316
+
317
+ # from ultralytics.utils.files import WorkingDirectory
318
+ # with WorkingDirectory(ROOT.parent / 'tests'):
319
+
320
+ for task in TASKS:
321
+ file = Path(TASK2DATA[task]).with_suffix(".zip") # i.e. coco8.zip
322
+ download(f"https://github.com/ultralytics/hub/raw/main/example_datasets/{file}", unzip=False, dir=TMP)
323
+ stats = HUBDatasetStats(TMP / file, task=task)
324
+ stats.get_json(save=True)
325
+ stats.process_images()
326
+
327
+ autosplit(TMP / "coco8")
328
+ zip_directory(TMP / "coco8/images/val") # zip
329
+
330
+
331
+ @pytest.mark.skipif(not ONLINE, reason="environment is offline")
332
+ def test_data_converter():
333
+ """Test dataset conversion functions from COCO to YOLO format and class mappings."""
334
+ from ultralytics.data.converter import coco80_to_coco91_class, convert_coco
335
+
336
+ file = "instances_val2017.json"
337
+ download(f"https://github.com/ultralytics/assets/releases/download/v0.0.0/{file}", dir=TMP)
338
+ convert_coco(labels_dir=TMP, save_dir=TMP / "yolo_labels", use_segments=True, use_keypoints=False, cls91to80=True)
339
+ coco80_to_coco91_class()
340
+
341
+
342
+ def test_data_annotator():
343
+ """Test automatic annotation of data using detection and segmentation models."""
344
+ from ultralytics.data.annotator import auto_annotate
345
+
346
+ auto_annotate(
347
+ ASSETS,
348
+ det_model=WEIGHTS_DIR / "yolo11n.pt",
349
+ sam_model=WEIGHTS_DIR / "mobile_sam.pt",
350
+ output_dir=TMP / "auto_annotate_labels",
351
+ )
352
+
353
+
354
+ def test_events():
355
+ """Test event sending functionality."""
356
+ from ultralytics.hub.utils import Events
357
+
358
+ events = Events()
359
+ events.enabled = True
360
+ cfg = copy(DEFAULT_CFG) # does not require deepcopy
361
+ cfg.mode = "test"
362
+ events(cfg)
363
+
364
+
365
+ def test_cfg_init():
366
+ """Test configuration initialization utilities from the 'ultralytics.cfg' module."""
367
+ from ultralytics.cfg import check_dict_alignment, copy_default_cfg, smart_value
368
+
369
+ with contextlib.suppress(SyntaxError):
370
+ check_dict_alignment({"a": 1}, {"b": 2})
371
+ copy_default_cfg()
372
+ (Path.cwd() / DEFAULT_CFG_PATH.name.replace(".yaml", "_copy.yaml")).unlink(missing_ok=False)
373
+ [smart_value(x) for x in ["none", "true", "false"]]
374
+
375
+
376
+ def test_utils_init():
377
+ """Test initialization utilities in the Ultralytics library."""
378
+ from ultralytics.utils import get_git_branch, get_git_origin_url, get_ubuntu_version, is_github_action_running
379
+
380
+ get_ubuntu_version()
381
+ is_github_action_running()
382
+ get_git_origin_url()
383
+ get_git_branch()
384
+
385
+
386
+ def test_utils_checks():
387
+ """Test various utility checks for filenames, git status, requirements, image sizes, and versions."""
388
+ checks.check_yolov5u_filename("yolov5n.pt")
389
+ checks.git_describe(ROOT)
390
+ checks.check_requirements() # check requirements.txt
391
+ checks.check_imgsz([600, 600], max_dim=1)
392
+ checks.check_imshow(warn=True)
393
+ checks.check_version("ultralytics", "8.0.0")
394
+ checks.print_args()
395
+
396
+
397
+ @pytest.mark.skipif(WINDOWS, reason="Windows profiling is extremely slow (cause unknown)")
398
+ def test_utils_benchmarks():
399
+ """Benchmark model performance using 'ProfileModels' from 'ultralytics.utils.benchmarks'."""
400
+ from ultralytics.utils.benchmarks import ProfileModels
401
+
402
+ ProfileModels(["yolo11n.yaml"], imgsz=32, min_time=1, num_timed_runs=3, num_warmup_runs=1).profile()
403
+
404
+
405
+ def test_utils_torchutils():
406
+ """Test Torch utility functions including profiling and FLOP calculations."""
407
+ from ultralytics.nn.modules.conv import Conv
408
+ from ultralytics.utils.torch_utils import get_flops_with_torch_profiler, profile, time_sync
409
+
410
+ x = torch.randn(1, 64, 20, 20)
411
+ m = Conv(64, 64, k=1, s=2)
412
+
413
+ profile(x, [m], n=3)
414
+ get_flops_with_torch_profiler(m)
415
+ time_sync()
416
+
417
+
418
+ def test_utils_ops():
419
+ """Test utility operations for coordinate transformations and normalizations."""
420
+ from ultralytics.utils.ops import (
421
+ ltwh2xywh,
422
+ ltwh2xyxy,
423
+ make_divisible,
424
+ xywh2ltwh,
425
+ xywh2xyxy,
426
+ xywhn2xyxy,
427
+ xywhr2xyxyxyxy,
428
+ xyxy2ltwh,
429
+ xyxy2xywh,
430
+ xyxy2xywhn,
431
+ xyxyxyxy2xywhr,
432
+ )
433
+
434
+ make_divisible(17, torch.tensor([8]))
435
+
436
+ boxes = torch.rand(10, 4) # xywh
437
+ torch.allclose(boxes, xyxy2xywh(xywh2xyxy(boxes)))
438
+ torch.allclose(boxes, xyxy2xywhn(xywhn2xyxy(boxes)))
439
+ torch.allclose(boxes, ltwh2xywh(xywh2ltwh(boxes)))
440
+ torch.allclose(boxes, xyxy2ltwh(ltwh2xyxy(boxes)))
441
+
442
+ boxes = torch.rand(10, 5) # xywhr for OBB
443
+ boxes[:, 4] = torch.randn(10) * 30
444
+ torch.allclose(boxes, xyxyxyxy2xywhr(xywhr2xyxyxyxy(boxes)), rtol=1e-3)
445
+
446
+
447
+ def test_utils_files():
448
+ """Test file handling utilities including file age, date, and paths with spaces."""
449
+ from ultralytics.utils.files import file_age, file_date, get_latest_run, spaces_in_path
450
+
451
+ file_age(SOURCE)
452
+ file_date(SOURCE)
453
+ get_latest_run(ROOT / "runs")
454
+
455
+ path = TMP / "path/with spaces"
456
+ path.mkdir(parents=True, exist_ok=True)
457
+ with spaces_in_path(path) as new_path:
458
+ print(new_path)
459
+
460
+
461
+ @pytest.mark.slow
462
+ def test_utils_patches_torch_save():
463
+ """Test torch_save backoff when _torch_save raises RuntimeError."""
464
+ from unittest.mock import MagicMock, patch
465
+
466
+ from ultralytics.utils.patches import torch_save
467
+
468
+ mock = MagicMock(side_effect=RuntimeError)
469
+
470
+ with patch("ultralytics.utils.patches._torch_save", new=mock):
471
+ with pytest.raises(RuntimeError):
472
+ torch_save(torch.zeros(1), TMP / "test.pt")
473
+
474
+ assert mock.call_count == 4, "torch_save was not attempted the expected number of times"
475
+
476
+
477
+ def test_nn_modules_conv():
478
+ """Test Convolutional Neural Network modules including CBAM, Conv2, and ConvTranspose."""
479
+ from ultralytics.nn.modules.conv import CBAM, Conv2, ConvTranspose, DWConvTranspose2d, Focus
480
+
481
+ c1, c2 = 8, 16 # input and output channels
482
+ x = torch.zeros(4, c1, 10, 10) # BCHW
483
+
484
+ # Run all modules not otherwise covered in tests
485
+ DWConvTranspose2d(c1, c2)(x)
486
+ ConvTranspose(c1, c2)(x)
487
+ Focus(c1, c2)(x)
488
+ CBAM(c1)(x)
489
+
490
+ # Fuse ops
491
+ m = Conv2(c1, c2)
492
+ m.fuse_convs()
493
+ m(x)
494
+
495
+
496
+ def test_nn_modules_block():
497
+ """Test various neural network block modules."""
498
+ from ultralytics.nn.modules.block import C1, C3TR, BottleneckCSP, C3Ghost, C3x
499
+
500
+ c1, c2 = 8, 16 # input and output channels
501
+ x = torch.zeros(4, c1, 10, 10) # BCHW
502
+
503
+ # Run all modules not otherwise covered in tests
504
+ C1(c1, c2)(x)
505
+ C3x(c1, c2)(x)
506
+ C3TR(c1, c2)(x)
507
+ C3Ghost(c1, c2)(x)
508
+ BottleneckCSP(c1, c2)(x)
509
+
510
+
511
+ @pytest.mark.skipif(not ONLINE, reason="environment is offline")
512
+ def test_hub():
513
+ """Test Ultralytics HUB functionalities."""
514
+ from ultralytics.hub import export_fmts_hub, logout
515
+ from ultralytics.hub.utils import smart_request
516
+
517
+ export_fmts_hub()
518
+ logout()
519
+ smart_request("GET", "https://github.com", progress=True)
520
+
521
+
522
+ @pytest.fixture
523
+ def image():
524
+ """Load and return an image from a predefined source."""
525
+ return cv2.imread(str(SOURCE))
526
+
527
+
528
+ @pytest.mark.parametrize(
529
+ "auto_augment, erasing, force_color_jitter",
530
+ [
531
+ (None, 0.0, False),
532
+ ("randaugment", 0.5, True),
533
+ ("augmix", 0.2, False),
534
+ ("autoaugment", 0.0, True),
535
+ ],
536
+ )
537
+ def test_classify_transforms_train(image, auto_augment, erasing, force_color_jitter):
538
+ """Test classification transforms during training with various augmentations."""
539
+ from ultralytics.data.augment import classify_augmentations
540
+
541
+ transform = classify_augmentations(
542
+ size=224,
543
+ mean=(0.5, 0.5, 0.5),
544
+ std=(0.5, 0.5, 0.5),
545
+ scale=(0.08, 1.0),
546
+ ratio=(3.0 / 4.0, 4.0 / 3.0),
547
+ hflip=0.5,
548
+ vflip=0.5,
549
+ auto_augment=auto_augment,
550
+ hsv_h=0.015,
551
+ hsv_s=0.4,
552
+ hsv_v=0.4,
553
+ force_color_jitter=force_color_jitter,
554
+ erasing=erasing,
555
+ )
556
+
557
+ transformed_image = transform(Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)))
558
+
559
+ assert transformed_image.shape == (3, 224, 224)
560
+ assert torch.is_tensor(transformed_image)
561
+ assert transformed_image.dtype == torch.float32
562
+
563
+
564
+ @pytest.mark.slow
565
+ @pytest.mark.skipif(not ONLINE, reason="environment is offline")
566
+ def test_model_tune():
567
+ """Tune YOLO model for performance improvement."""
568
+ YOLO("yolo11n-pose.pt").tune(data="coco8-pose.yaml", plots=False, imgsz=32, epochs=1, iterations=2, device="cpu")
569
+ YOLO("yolo11n-cls.pt").tune(data="imagenet10", plots=False, imgsz=32, epochs=1, iterations=2, device="cpu")
570
+
571
+
572
+ def test_model_embeddings():
573
+ """Test YOLO model embeddings extraction functionality."""
574
+ model_detect = YOLO(MODEL)
575
+ model_segment = YOLO(WEIGHTS_DIR / "yolo11n-seg.pt")
576
+
577
+ for batch in [SOURCE], [SOURCE, SOURCE]: # test batch size 1 and 2
578
+ assert len(model_detect.embed(source=batch, imgsz=32)) == len(batch)
579
+ assert len(model_segment.embed(source=batch, imgsz=32)) == len(batch)
580
+
581
+
582
+ @pytest.mark.skipif(checks.IS_PYTHON_3_12, reason="YOLOWorld with CLIP is not supported in Python 3.12")
583
+ @pytest.mark.skipif(
584
+ checks.IS_PYTHON_3_8 and LINUX and ARM64,
585
+ reason="YOLOWorld with CLIP is not supported in Python 3.8 and aarch64 Linux",
586
+ )
587
+ def test_yolo_world():
588
+ """Test YOLO world models with CLIP support."""
589
+ model = YOLO(WEIGHTS_DIR / "yolov8s-world.pt") # no YOLO11n-world model yet
590
+ model.set_classes(["tree", "window"])
591
+ model(SOURCE, conf=0.01)
592
+
593
+ model = YOLO(WEIGHTS_DIR / "yolov8s-worldv2.pt") # no YOLO11n-world model yet
594
+ # Training from a pretrained model. Eval is included at the final stage of training.
595
+ # Use dota8.yaml which has fewer categories to reduce the inference time of CLIP model
596
+ model.train(
597
+ data="dota8.yaml",
598
+ epochs=1,
599
+ imgsz=32,
600
+ cache="disk",
601
+ close_mosaic=1,
602
+ )
603
+
604
+ # test WorWorldTrainerFromScratch
605
+ from ultralytics.models.yolo.world.train_world import WorldTrainerFromScratch
606
+
607
+ model = YOLO("yolov8s-worldv2.yaml") # no YOLO11n-world model yet
608
+ model.train(
609
+ data={"train": {"yolo_data": ["dota8.yaml"]}, "val": {"yolo_data": ["dota8.yaml"]}},
610
+ epochs=1,
611
+ imgsz=32,
612
+ cache="disk",
613
+ close_mosaic=1,
614
+ trainer=WorldTrainerFromScratch,
615
+ )
616
+
617
+
618
+ @pytest.mark.skipif(checks.IS_PYTHON_3_12 or not TORCH_1_9, reason="YOLOE with CLIP is not supported in Python 3.12")
619
+ @pytest.mark.skipif(
620
+ checks.IS_PYTHON_3_8 and LINUX and ARM64,
621
+ reason="YOLOE with CLIP is not supported in Python 3.8 and aarch64 Linux",
622
+ )
623
+ def test_yoloe():
624
+ """Test YOLOE models with MobileClip support."""
625
+ # Predict
626
+ # text-prompts
627
+ model = YOLO(WEIGHTS_DIR / "yoloe-11s-seg.pt")
628
+ names = ["person", "bus"]
629
+ model.set_classes(names, model.get_text_pe(names))
630
+ model(SOURCE, conf=0.01)
631
+
632
+ import numpy as np
633
+
634
+ from ultralytics import YOLOE
635
+ from ultralytics.models.yolo.yoloe import YOLOEVPSegPredictor
636
+
637
+ # visual-prompts
638
+ visuals = dict(
639
+ bboxes=np.array(
640
+ [[221.52, 405.8, 344.98, 857.54], [120, 425, 160, 445]],
641
+ ),
642
+ cls=np.array([0, 1]),
643
+ )
644
+ model.predict(
645
+ SOURCE,
646
+ visual_prompts=visuals,
647
+ predictor=YOLOEVPSegPredictor,
648
+ )
649
+
650
+ # Val
651
+ model = YOLOE(WEIGHTS_DIR / "yoloe-11s-seg.pt")
652
+ # text prompts
653
+ model.val(data="coco128-seg.yaml", imgsz=32)
654
+ # visual prompts
655
+ model.val(data="coco128-seg.yaml", load_vp=True, imgsz=32)
656
+
657
+ # Train, fine-tune
658
+ from ultralytics.models.yolo.yoloe import YOLOEPESegTrainer
659
+
660
+ model = YOLOE("yoloe-11s-seg.pt")
661
+ model.train(
662
+ data="coco128-seg.yaml",
663
+ epochs=1,
664
+ close_mosaic=1,
665
+ trainer=YOLOEPESegTrainer,
666
+ imgsz=32,
667
+ )
668
+
669
+ # prompt-free
670
+ # predict
671
+ model = YOLOE(WEIGHTS_DIR / "yoloe-11s-seg-pf.pt")
672
+ model.predict(SOURCE)
673
+ # val
674
+ model = YOLOE("yoloe-11s-seg.pt") # or select yoloe-m/l-seg.pt for different sizes
675
+ model.val(data="coco128-seg.yaml", imgsz=32)
676
+
677
+
678
+ def test_yolov10():
679
+ """Test YOLOv10 model training, validation, and prediction functionality."""
680
+ model = YOLO("yolov10n.yaml")
681
+ # train/val/predict
682
+ model.train(data="coco8.yaml", epochs=1, imgsz=32, close_mosaic=1, cache="disk")
683
+ model.val(data="coco8.yaml", imgsz=32)
684
+ model.predict(imgsz=32, save_txt=True, save_crop=True, augment=True)
685
+ model(SOURCE)
686
+
687
+
688
+ def test_multichannel():
689
+ """Test YOLO model multi-channel training, validation, and prediction functionality."""
690
+ model = YOLO("yolo11n.pt")
691
+ model.train(data="coco8-multispectral.yaml", epochs=1, imgsz=32, close_mosaic=1, cache="disk")
692
+ model.val(data="coco8-multispectral.yaml")
693
+ im = np.zeros((32, 32, 10), dtype=np.uint8)
694
+ model.predict(source=im, imgsz=32, save_txt=True, save_crop=True, augment=True)
695
+ model.export(format="onnx")