Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_python.py 25 KB

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

Press p or to see the previous file or, n or to see the next file

Comments

Loading...