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 5.3 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
  1. # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/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 ARM64, ASSETS, LINUX, 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: str) -> None:
  13. """Execute a shell command using subprocess."""
  14. subprocess.run(cmd.split(), check=True)
  15. def test_special_modes() -> None:
  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: str, model: str, data: str) -> None:
  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: str, model: str, data: str) -> None:
  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: str, model: str, data: str) -> None:
  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: str) -> None:
  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: str = "detect", model: str = "yolov8n-rtdetr.yaml", data: str = "coco8.yaml") -> None:
  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") # spaces
  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. weights = WEIGHTS_DIR / "rtdetr-l.pt"
  45. run(f"yolo predict {task} model={weights} source={ASSETS / 'bus.jpg'} imgsz=160 save save_crop save_txt")
  46. @pytest.mark.skipif(checks.IS_PYTHON_3_12, reason="MobileSAM with CLIP is not supported in Python 3.12")
  47. @pytest.mark.skipif(
  48. checks.IS_PYTHON_3_8 and LINUX and ARM64,
  49. reason="MobileSAM with CLIP is not supported in Python 3.8 and aarch64 Linux",
  50. )
  51. def test_fastsam(
  52. task: str = "segment", model: str = WEIGHTS_DIR / "FastSAM-s.pt", data: str = "coco8-seg.yaml"
  53. ) -> None:
  54. """Test FastSAM model for segmenting objects in images using various prompts within Ultralytics."""
  55. source = ASSETS / "bus.jpg"
  56. run(f"yolo segment val {task} model={model} data={data} imgsz=32")
  57. run(f"yolo segment predict model={model} source={source} imgsz=32 save save_crop save_txt")
  58. from ultralytics import FastSAM
  59. from ultralytics.models.sam import Predictor
  60. # Create a FastSAM model
  61. sam_model = FastSAM(model) # or FastSAM-x.pt
  62. # Run inference on an image
  63. for s in (source, Image.open(source)):
  64. everything_results = sam_model(s, device="cpu", retina_masks=True, imgsz=320, conf=0.4, iou=0.9)
  65. # Remove small regions
  66. new_masks, _ = Predictor.remove_small_regions(everything_results[0].masks.data, min_area=20)
  67. # Run inference with bboxes and points and texts prompt at the same time
  68. sam_model(source, bboxes=[439, 437, 524, 709], points=[[200, 200]], labels=[1], texts="a photo of a dog")
  69. def test_mobilesam() -> None:
  70. """Test MobileSAM segmentation with point prompts using Ultralytics."""
  71. from ultralytics import SAM
  72. # Load the model
  73. model = SAM(WEIGHTS_DIR / "mobile_sam.pt")
  74. # Source
  75. source = ASSETS / "zidane.jpg"
  76. # Predict a segment based on a 1D point prompt and 1D labels.
  77. model.predict(source, points=[900, 370], labels=[1])
  78. # Predict a segment based on 3D points and 2D labels (multiple points per object).
  79. model.predict(source, points=[[[900, 370], [1000, 100]]], labels=[[1, 1]])
  80. # Predict a segment based on a box prompt
  81. model.predict(source, bboxes=[439, 437, 524, 709], save=True)
  82. # Predict all
  83. # model(source)
  84. # Slow Tests -----------------------------------------------------------------------------------------------------------
  85. @pytest.mark.slow
  86. @pytest.mark.parametrize("task,model,data", TASK_MODEL_DATA)
  87. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  88. @pytest.mark.skipif(CUDA_DEVICE_COUNT < 2, reason="DDP is not available")
  89. def test_train_gpu(task: str, model: str, data: str) -> None:
  90. """Test YOLO training on GPU(s) for various tasks and models."""
  91. run(f"yolo train {task} model={model} data={data} imgsz=32 epochs=1 device=0") # single GPU
  92. 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...