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_cli.py 4.7 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
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. import subprocess
  3. import pytest
  4. from PIL import Image
  5. from tests import CUDA_DEVICE_COUNT, CUDA_IS_AVAILABLE
  6. from ultralytics.cfg import TASK2DATA, TASK2MODEL, TASKS
  7. from ultralytics.utils import ASSETS, WEIGHTS_DIR, checks
  8. from ultralytics.utils.torch_utils import TORCH_1_9
  9. # Constants
  10. TASK_MODEL_DATA = [(task, WEIGHTS_DIR / TASK2MODEL[task], TASK2DATA[task]) for task in TASKS]
  11. MODELS = [WEIGHTS_DIR / TASK2MODEL[task] for task in TASKS]
  12. def run(cmd):
  13. """Execute a shell command using subprocess."""
  14. subprocess.run(cmd.split(), check=True)
  15. def test_special_modes():
  16. """Test various special command-line modes for YOLO functionality."""
  17. run("yolo help")
  18. run("yolo checks")
  19. run("yolo version")
  20. run("yolo settings reset")
  21. run("yolo cfg")
  22. @pytest.mark.parametrize("task,model,data", TASK_MODEL_DATA)
  23. def test_train(task, model, data):
  24. """Test YOLO training for different tasks, models, and datasets."""
  25. run(f"yolo train {task} model={model} data={data} imgsz=32 epochs=1 cache=disk")
  26. @pytest.mark.parametrize("task,model,data", TASK_MODEL_DATA)
  27. def test_val(task, model, data):
  28. """Test YOLO validation process for specified task, model, and data using a shell command."""
  29. run(f"yolo val {task} model={model} data={data} imgsz=32 save_txt save_json")
  30. @pytest.mark.parametrize("task,model,data", TASK_MODEL_DATA)
  31. def test_predict(task, model, data):
  32. """Test YOLO prediction on provided sample assets for specified task and model."""
  33. run(f"yolo predict model={model} source={ASSETS} imgsz=32 save save_crop save_txt")
  34. @pytest.mark.parametrize("model", MODELS)
  35. def test_export(model):
  36. """Test exporting a YOLO model to TorchScript format."""
  37. run(f"yolo export model={model} format=torchscript imgsz=32")
  38. def test_rtdetr(task="detect", model="yolov8n-rtdetr.yaml", data="coco8.yaml"):
  39. """Test the RTDETR functionality within Ultralytics for detection tasks using specified model and data."""
  40. # Warning: must use imgsz=640 (note also add coma, spaces, fraction=0.25 args to test single-image training)
  41. run(f"yolo train {task} model={model} data={data} --imgsz= 160 epochs =1, cache = disk fraction=0.25")
  42. run(f"yolo predict {task} model={model} source={ASSETS / 'bus.jpg'} imgsz=160 save save_crop save_txt")
  43. if TORCH_1_9:
  44. run(f"yolo predict {task} model='rtdetr-l.pt' source={ASSETS / 'bus.jpg'} imgsz=160 save save_crop save_txt")
  45. @pytest.mark.skipif(checks.IS_PYTHON_3_12, reason="MobileSAM with CLIP is not supported in Python 3.12")
  46. def test_fastsam(task="segment", model=WEIGHTS_DIR / "FastSAM-s.pt", data="coco8-seg.yaml"):
  47. """Test FastSAM model for segmenting objects in images using various prompts within Ultralytics."""
  48. source = ASSETS / "bus.jpg"
  49. run(f"yolo segment val {task} model={model} data={data} imgsz=32")
  50. run(f"yolo segment predict model={model} source={source} imgsz=32 save save_crop save_txt")
  51. from ultralytics import FastSAM
  52. from ultralytics.models.sam import Predictor
  53. # Create a FastSAM model
  54. sam_model = FastSAM(model) # or FastSAM-x.pt
  55. # Run inference on an image
  56. for s in (source, Image.open(source)):
  57. everything_results = sam_model(s, device="cpu", retina_masks=True, imgsz=320, conf=0.4, iou=0.9)
  58. # Remove small regions
  59. new_masks, _ = Predictor.remove_small_regions(everything_results[0].masks.data, min_area=20)
  60. # Run inference with bboxes and points and texts prompt at the same time
  61. sam_model(source, bboxes=[439, 437, 524, 709], points=[[200, 200]], labels=[1], texts="a photo of a dog")
  62. def test_mobilesam():
  63. """Test MobileSAM segmentation with point prompts using Ultralytics."""
  64. from ultralytics import SAM
  65. # Load the model
  66. model = SAM(WEIGHTS_DIR / "mobile_sam.pt")
  67. # Source
  68. source = ASSETS / "zidane.jpg"
  69. # Predict a segment based on a point prompt
  70. model.predict(source, points=[900, 370], labels=[1])
  71. # Predict a segment based on a box prompt
  72. model.predict(source, bboxes=[439, 437, 524, 709], save=True)
  73. # Predict all
  74. # model(source)
  75. # Slow Tests -----------------------------------------------------------------------------------------------------------
  76. @pytest.mark.slow
  77. @pytest.mark.parametrize("task,model,data", TASK_MODEL_DATA)
  78. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  79. @pytest.mark.skipif(CUDA_DEVICE_COUNT < 2, reason="DDP is not available")
  80. def test_train_gpu(task, model, data):
  81. """Test YOLO training on GPU(s) for various tasks and models."""
  82. run(f"yolo train {task} model={model} data={data} imgsz=32 epochs=1 device=0") # single GPU
  83. run(f"yolo train {task} model={model} data={data} imgsz=32 epochs=1 device=0,1") # multi GPU
Tip!

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

Comments

Loading...