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 5.8 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
  1. # Ultralytics YOLO 🚀, AGPL-3.0 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. """Test YOLO model export to TensorRT format for various configurations and run inference."""
  35. file = YOLO(TASK2MODEL[task]).export(
  36. format="engine",
  37. imgsz=32,
  38. dynamic=dynamic,
  39. int8=int8,
  40. half=half,
  41. batch=batch,
  42. data=TASK2DATA[task],
  43. workspace=1, # reduce workspace GB for less resource utilization during testing
  44. simplify=True, # use 'onnxslim'
  45. )
  46. YOLO(file)([SOURCE] * batch, imgsz=64 if dynamic else 32) # exported model inference
  47. Path(file).unlink() # cleanup
  48. Path(file).with_suffix(".cache").unlink() if int8 else None # cleanup INT8 cache
  49. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  50. def test_train():
  51. """Test model training on a minimal dataset using available CUDA devices."""
  52. device = 0 if CUDA_DEVICE_COUNT == 1 else [0, 1]
  53. YOLO(MODEL).train(data="coco8.yaml", imgsz=64, epochs=1, device=device) # requires imgsz>=64
  54. @pytest.mark.slow
  55. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  56. def test_predict_multiple_devices():
  57. """Validate model prediction consistency across CPU and CUDA devices."""
  58. model = YOLO("yolo11n.pt")
  59. model = model.cpu()
  60. assert str(model.device) == "cpu"
  61. _ = model(SOURCE) # CPU inference
  62. assert str(model.device) == "cpu"
  63. model = model.to("cuda:0")
  64. assert str(model.device) == "cuda:0"
  65. _ = model(SOURCE) # CUDA inference
  66. assert str(model.device) == "cuda:0"
  67. model = model.cpu()
  68. assert str(model.device) == "cpu"
  69. _ = model(SOURCE) # CPU inference
  70. assert str(model.device) == "cpu"
  71. model = model.cuda()
  72. assert str(model.device) == "cuda:0"
  73. _ = model(SOURCE) # CUDA inference
  74. assert str(model.device) == "cuda:0"
  75. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  76. def test_autobatch():
  77. """Check optimal batch size for YOLO model training using autobatch utility."""
  78. from ultralytics.utils.autobatch import check_train_batch_size
  79. check_train_batch_size(YOLO(MODEL).model.cuda(), imgsz=128, amp=True)
  80. @pytest.mark.slow
  81. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  82. def test_utils_benchmarks():
  83. """Profile YOLO models for performance benchmarks."""
  84. from ultralytics.utils.benchmarks import ProfileModels
  85. # Pre-export a dynamic engine model to use dynamic inference
  86. YOLO(MODEL).export(format="engine", imgsz=32, dynamic=True, batch=1)
  87. ProfileModels([MODEL], imgsz=32, half=False, min_time=1, num_timed_runs=3, num_warmup_runs=1).profile()
  88. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  89. def test_predict_sam():
  90. """Test SAM model predictions using different prompts, including bounding boxes and point annotations."""
  91. from ultralytics import SAM
  92. from ultralytics.models.sam import Predictor as SAMPredictor
  93. # Load a model
  94. model = SAM(WEIGHTS_DIR / "sam2.1_b.pt")
  95. # Display model information (optional)
  96. model.info()
  97. # Run inference
  98. model(SOURCE, device=0)
  99. # Run inference with bboxes prompt
  100. model(SOURCE, bboxes=[439, 437, 524, 709], device=0)
  101. # Run inference with no labels
  102. model(ASSETS / "zidane.jpg", points=[900, 370], device=0)
  103. # Run inference with 1D points and 1D labels
  104. model(ASSETS / "zidane.jpg", points=[900, 370], labels=[1], device=0)
  105. # Run inference with 2D points and 1D labels
  106. model(ASSETS / "zidane.jpg", points=[[900, 370]], labels=[1], device=0)
  107. # Run inference with multiple 2D points and 1D labels
  108. model(ASSETS / "zidane.jpg", points=[[400, 370], [900, 370]], labels=[1, 1], device=0)
  109. # Run inference with 3D points and 2D labels (multiple points per object)
  110. model(ASSETS / "zidane.jpg", points=[[[900, 370], [1000, 100]]], labels=[[1, 1]], device=0)
  111. # Create SAMPredictor
  112. overrides = dict(conf=0.25, task="segment", mode="predict", imgsz=1024, model=WEIGHTS_DIR / "mobile_sam.pt")
  113. predictor = SAMPredictor(overrides=overrides)
  114. # Set image
  115. predictor.set_image(ASSETS / "zidane.jpg") # set with image file
  116. # predictor(bboxes=[439, 437, 524, 709])
  117. # predictor(points=[900, 370], labels=[1])
  118. # Reset image
  119. predictor.reset_image()
Tip!

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

Comments

Loading...