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_cuda.py 6.1 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
  1. # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. from itertools import product
  3. from pathlib import Path
  4. import pytest
  5. import torch
  6. from tests import CUDA_DEVICE_COUNT, CUDA_IS_AVAILABLE, MODEL, SOURCE
  7. from ultralytics import YOLO
  8. from ultralytics.cfg import TASK2DATA, TASK2MODEL, TASKS
  9. from ultralytics.utils import ASSETS, WEIGHTS_DIR
  10. from ultralytics.utils.checks import check_amp
  11. def test_checks():
  12. """Validate CUDA settings against torch CUDA functions."""
  13. assert torch.cuda.is_available() == CUDA_IS_AVAILABLE
  14. assert torch.cuda.device_count() == CUDA_DEVICE_COUNT
  15. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  16. def test_amp():
  17. """Test AMP training checks."""
  18. model = YOLO("yolo11n.pt").model.cuda()
  19. assert check_amp(model)
  20. @pytest.mark.slow
  21. @pytest.mark.skipif(True, reason="CUDA export tests disabled pending additional Ultralytics GPU server availability")
  22. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  23. @pytest.mark.parametrize(
  24. "task, dynamic, int8, half, batch",
  25. [ # generate all combinations but exclude those where both int8 and half are True
  26. (task, dynamic, int8, half, batch)
  27. # Note: tests reduced below pending compute availability expansion as GPU CI runner utilization is high
  28. # for task, dynamic, int8, half, batch in product(TASKS, [True, False], [True, False], [True, False], [1, 2])
  29. for task, dynamic, int8, half, batch in product(TASKS, [True], [True], [False], [2])
  30. if not (int8 and half) # exclude cases where both int8 and half are True
  31. ],
  32. )
  33. def test_export_engine_matrix(task, dynamic, int8, half, batch):
  34. """
  35. Test YOLO model export to TensorRT format for various configurations and run inference.
  36. Args:
  37. task (str): Task type like 'detect', 'segment', etc.
  38. dynamic (bool): Whether to use dynamic input size.
  39. int8 (bool): Whether to use INT8 precision.
  40. half (bool): Whether to use FP16 precision.
  41. batch (int): Batch size for export.
  42. """
  43. file = YOLO(TASK2MODEL[task]).export(
  44. format="engine",
  45. imgsz=32,
  46. dynamic=dynamic,
  47. int8=int8,
  48. half=half,
  49. batch=batch,
  50. data=TASK2DATA[task],
  51. workspace=1, # reduce workspace GB for less resource utilization during testing
  52. simplify=True, # use 'onnxslim'
  53. )
  54. YOLO(file)([SOURCE] * batch, imgsz=64 if dynamic else 32) # exported model inference
  55. Path(file).unlink() # cleanup
  56. Path(file).with_suffix(".cache").unlink() if int8 else None # cleanup INT8 cache
  57. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  58. def test_train():
  59. """Test model training on a minimal dataset using available CUDA devices."""
  60. device = 0 if CUDA_DEVICE_COUNT == 1 else [0, 1]
  61. YOLO(MODEL).train(data="coco8.yaml", imgsz=64, epochs=1, device=device) # requires imgsz>=64
  62. @pytest.mark.slow
  63. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  64. def test_predict_multiple_devices():
  65. """Validate model prediction consistency across CPU and CUDA devices."""
  66. model = YOLO("yolo11n.pt")
  67. model = model.cpu()
  68. assert str(model.device) == "cpu"
  69. _ = model(SOURCE) # CPU inference
  70. assert str(model.device) == "cpu"
  71. model = model.to("cuda:0")
  72. assert str(model.device) == "cuda:0"
  73. _ = model(SOURCE) # CUDA inference
  74. assert str(model.device) == "cuda:0"
  75. model = model.cpu()
  76. assert str(model.device) == "cpu"
  77. _ = model(SOURCE) # CPU inference
  78. assert str(model.device) == "cpu"
  79. model = model.cuda()
  80. assert str(model.device) == "cuda:0"
  81. _ = model(SOURCE) # CUDA inference
  82. assert str(model.device) == "cuda:0"
  83. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  84. def test_autobatch():
  85. """Check optimal batch size for YOLO model training using autobatch utility."""
  86. from ultralytics.utils.autobatch import check_train_batch_size
  87. check_train_batch_size(YOLO(MODEL).model.cuda(), imgsz=128, amp=True)
  88. @pytest.mark.slow
  89. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  90. def test_utils_benchmarks():
  91. """Profile YOLO models for performance benchmarks."""
  92. from ultralytics.utils.benchmarks import ProfileModels
  93. # Pre-export a dynamic engine model to use dynamic inference
  94. YOLO(MODEL).export(format="engine", imgsz=32, dynamic=True, batch=1)
  95. ProfileModels([MODEL], imgsz=32, half=False, min_time=1, num_timed_runs=3, num_warmup_runs=1).profile()
  96. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  97. def test_predict_sam():
  98. """Test SAM model predictions using different prompts, including bounding boxes and point annotations."""
  99. from ultralytics import SAM
  100. from ultralytics.models.sam import Predictor as SAMPredictor
  101. # Load a model
  102. model = SAM(WEIGHTS_DIR / "sam2.1_b.pt")
  103. # Display model information (optional)
  104. model.info()
  105. # Run inference
  106. model(SOURCE, device=0)
  107. # Run inference with bboxes prompt
  108. model(SOURCE, bboxes=[439, 437, 524, 709], device=0)
  109. # Run inference with no labels
  110. model(ASSETS / "zidane.jpg", points=[900, 370], device=0)
  111. # Run inference with 1D points and 1D labels
  112. model(ASSETS / "zidane.jpg", points=[900, 370], labels=[1], device=0)
  113. # Run inference with 2D points and 1D labels
  114. model(ASSETS / "zidane.jpg", points=[[900, 370]], labels=[1], device=0)
  115. # Run inference with multiple 2D points and 1D labels
  116. model(ASSETS / "zidane.jpg", points=[[400, 370], [900, 370]], labels=[1, 1], device=0)
  117. # Run inference with 3D points and 2D labels (multiple points per object)
  118. model(ASSETS / "zidane.jpg", points=[[[900, 370], [1000, 100]]], labels=[[1, 1]], device=0)
  119. # Create SAMPredictor
  120. overrides = dict(conf=0.25, task="segment", mode="predict", imgsz=1024, model=WEIGHTS_DIR / "mobile_sam.pt")
  121. predictor = SAMPredictor(overrides=overrides)
  122. # Set image
  123. predictor.set_image(ASSETS / "zidane.jpg") # set with image file
  124. # predictor(bboxes=[439, 437, 524, 709])
  125. # predictor(points=[900, 370], labels=[1])
  126. # Reset image
  127. predictor.reset_image()
Tip!

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

Comments

Loading...