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 3.4 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
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. import pytest
  3. import torch
  4. from ultralytics import YOLO
  5. from ultralytics.utils import ASSETS, WEIGHTS_DIR, checks
  6. CUDA_IS_AVAILABLE = checks.cuda_is_available()
  7. CUDA_DEVICE_COUNT = checks.cuda_device_count()
  8. MODEL = WEIGHTS_DIR / 'path with spaces' / 'yolov8n.pt' # test spaces in path
  9. DATA = 'coco8.yaml'
  10. BUS = ASSETS / 'bus.jpg'
  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_train():
  17. """Test model training on a minimal dataset."""
  18. device = 0 if CUDA_DEVICE_COUNT == 1 else [0, 1]
  19. YOLO(MODEL).train(data=DATA, imgsz=64, epochs=1, device=device) # requires imgsz>=64
  20. @pytest.mark.slow
  21. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason='CUDA is not available')
  22. def test_predict_multiple_devices():
  23. """Validate model prediction on multiple devices."""
  24. model = YOLO('yolov8n.pt')
  25. model = model.cpu()
  26. assert str(model.device) == 'cpu'
  27. _ = model(BUS) # CPU inference
  28. assert str(model.device) == 'cpu'
  29. model = model.to('cuda:0')
  30. assert str(model.device) == 'cuda:0'
  31. _ = model(BUS) # CUDA inference
  32. assert str(model.device) == 'cuda:0'
  33. model = model.cpu()
  34. assert str(model.device) == 'cpu'
  35. _ = model(BUS) # CPU inference
  36. assert str(model.device) == 'cpu'
  37. model = model.cuda()
  38. assert str(model.device) == 'cuda:0'
  39. _ = model(BUS) # CUDA inference
  40. assert str(model.device) == 'cuda:0'
  41. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason='CUDA is not available')
  42. def test_autobatch():
  43. """Check batch size for YOLO model using autobatch."""
  44. from ultralytics.utils.autobatch import check_train_batch_size
  45. check_train_batch_size(YOLO(MODEL).model.cuda(), imgsz=128, amp=True)
  46. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason='CUDA is not available')
  47. def test_utils_benchmarks():
  48. """Profile YOLO models for performance benchmarks."""
  49. from ultralytics.utils.benchmarks import ProfileModels
  50. # Pre-export a dynamic engine model to use dynamic inference
  51. YOLO(MODEL).export(format='engine', imgsz=32, dynamic=True, batch=1)
  52. ProfileModels([MODEL], imgsz=32, half=False, min_time=1, num_timed_runs=3, num_warmup_runs=1).profile()
  53. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason='CUDA is not available')
  54. def test_predict_sam():
  55. """Test SAM model prediction with various prompts."""
  56. from ultralytics import SAM
  57. from ultralytics.models.sam import Predictor as SAMPredictor
  58. # Load a model
  59. model = SAM(WEIGHTS_DIR / 'sam_b.pt')
  60. # Display model information (optional)
  61. model.info()
  62. # Run inference
  63. model(BUS, device=0)
  64. # Run inference with bboxes prompt
  65. model(BUS, bboxes=[439, 437, 524, 709], device=0)
  66. # Run inference with points prompt
  67. model(ASSETS / 'zidane.jpg', points=[900, 370], labels=[1], device=0)
  68. # Create SAMPredictor
  69. overrides = dict(conf=0.25, task='segment', mode='predict', imgsz=1024, model=WEIGHTS_DIR / 'mobile_sam.pt')
  70. predictor = SAMPredictor(overrides=overrides)
  71. # Set image
  72. predictor.set_image(ASSETS / 'zidane.jpg') # set with image file
  73. # predictor(bboxes=[439, 437, 524, 709])
  74. # predictor(points=[900, 370], labels=[1])
  75. # Reset image
  76. predictor.reset_image()
Tip!

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

Comments

Loading...