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.9 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
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. import subprocess
  3. import pytest
  4. from ultralytics.utils import ASSETS, WEIGHTS_DIR
  5. from ultralytics.utils.checks import cuda_device_count, cuda_is_available
  6. CUDA_IS_AVAILABLE = cuda_is_available()
  7. CUDA_DEVICE_COUNT = cuda_device_count()
  8. TASK_ARGS = [
  9. ('detect', 'yolov8n', 'coco8.yaml'),
  10. ('segment', 'yolov8n-seg', 'coco8-seg.yaml'),
  11. ('classify', 'yolov8n-cls', 'imagenet10'),
  12. ('pose', 'yolov8n-pose', 'coco8-pose.yaml'), ] # (task, model, data)
  13. EXPORT_ARGS = [
  14. ('yolov8n', 'torchscript'),
  15. ('yolov8n-seg', 'torchscript'),
  16. ('yolov8n-cls', 'torchscript'),
  17. ('yolov8n-pose', 'torchscript'), ] # (model, format)
  18. def run(cmd):
  19. """Execute a shell command using subprocess."""
  20. subprocess.run(cmd.split(), check=True)
  21. def test_special_modes():
  22. """Test various special command modes of YOLO."""
  23. run('yolo help')
  24. run('yolo checks')
  25. run('yolo version')
  26. run('yolo settings reset')
  27. run('yolo cfg')
  28. @pytest.mark.parametrize('task,model,data', TASK_ARGS)
  29. def test_train(task, model, data):
  30. """Test YOLO training for a given task, model, and data."""
  31. run(f'yolo train {task} model={model}.yaml data={data} imgsz=32 epochs=1 cache=disk')
  32. @pytest.mark.parametrize('task,model,data', TASK_ARGS)
  33. def test_val(task, model, data):
  34. """Test YOLO validation for a given task, model, and data."""
  35. run(f'yolo val {task} model={WEIGHTS_DIR / model}.pt data={data} imgsz=32 save_txt save_json')
  36. @pytest.mark.parametrize('task,model,data', TASK_ARGS)
  37. def test_predict(task, model, data):
  38. """Test YOLO prediction on sample assets for a given task and model."""
  39. run(f'yolo predict model={WEIGHTS_DIR / model}.pt source={ASSETS} imgsz=32 save save_crop save_txt')
  40. @pytest.mark.parametrize('model,format', EXPORT_ARGS)
  41. def test_export(model, format):
  42. """Test exporting a YOLO model to different formats."""
  43. run(f'yolo export model={WEIGHTS_DIR / model}.pt format={format} imgsz=32')
  44. def test_rtdetr(task='detect', model='yolov8n-rtdetr.yaml', data='coco8.yaml'):
  45. """Test the RTDETR functionality with the Ultralytics framework."""
  46. # Warning: MUST use imgsz=640
  47. run(f'yolo train {task} model={model} data={data} --imgsz= 640 epochs =1, cache = disk') # add coma, spaces to args
  48. run(f"yolo predict {task} model={model} source={ASSETS / 'bus.jpg'} imgsz=640 save save_crop save_txt")
  49. def test_fastsam(task='segment', model=WEIGHTS_DIR / 'FastSAM-s.pt', data='coco8-seg.yaml'):
  50. """Test FastSAM segmentation functionality within Ultralytics."""
  51. source = ASSETS / 'bus.jpg'
  52. run(f'yolo segment val {task} model={model} data={data} imgsz=32')
  53. run(f'yolo segment predict model={model} source={source} imgsz=32 save save_crop save_txt')
  54. from ultralytics import FastSAM
  55. from ultralytics.models.fastsam import FastSAMPrompt
  56. from ultralytics.models.sam import Predictor
  57. # Create a FastSAM model
  58. sam_model = FastSAM(model) # or FastSAM-x.pt
  59. # Run inference on an image
  60. everything_results = sam_model(source, device='cpu', retina_masks=True, imgsz=1024, conf=0.4, iou=0.9)
  61. # Remove small regions
  62. new_masks, _ = Predictor.remove_small_regions(everything_results[0].masks.data, min_area=20)
  63. # Everything prompt
  64. prompt_process = FastSAMPrompt(source, everything_results, device='cpu')
  65. ann = prompt_process.everything_prompt()
  66. # Bbox default shape [0,0,0,0] -> [x1,y1,x2,y2]
  67. ann = prompt_process.box_prompt(bbox=[200, 200, 300, 300])
  68. # Text prompt
  69. ann = prompt_process.text_prompt(text='a photo of a dog')
  70. # Point prompt
  71. # Points default [[0,0]] [[x1,y1],[x2,y2]]
  72. # Point_label default [0] [1,0] 0:background, 1:foreground
  73. ann = prompt_process.point_prompt(points=[[200, 200]], pointlabel=[1])
  74. prompt_process.plot(annotations=ann, output='./')
  75. def test_mobilesam():
  76. """Test MobileSAM segmentation functionality using Ultralytics."""
  77. from ultralytics import SAM
  78. # Load the model
  79. model = SAM(WEIGHTS_DIR / 'mobile_sam.pt')
  80. # Source
  81. source = ASSETS / 'zidane.jpg'
  82. # Predict a segment based on a point prompt
  83. model.predict(source, points=[900, 370], labels=[1])
  84. # Predict a segment based on a box prompt
  85. model.predict(source, bboxes=[439, 437, 524, 709])
  86. # Predict all
  87. # model(source)
  88. # Slow Tests -----------------------------------------------------------------------------------------------------------
  89. @pytest.mark.slow
  90. @pytest.mark.parametrize('task,model,data', TASK_ARGS)
  91. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason='CUDA is not available')
  92. @pytest.mark.skipif(CUDA_DEVICE_COUNT < 2, reason='DDP is not available')
  93. def test_train_gpu(task, model, data):
  94. """Test YOLO training on GPU(s) for various tasks and models."""
  95. run(f'yolo train {task} model={model}.yaml data={data} imgsz=32 epochs=1 device=0') # single GPU
  96. run(f'yolo train {task} model={model}.pt 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...