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_engine.py 4.5 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 YOLO 🚀, AGPL-3.0 license
  2. from ultralytics import YOLO
  3. from ultralytics.cfg import get_cfg
  4. from ultralytics.engine.exporter import Exporter
  5. from ultralytics.models.yolo import classify, detect, segment
  6. from ultralytics.utils import ASSETS, DEFAULT_CFG, WEIGHTS_DIR
  7. CFG_DET = 'yolov8n.yaml'
  8. CFG_SEG = 'yolov8n-seg.yaml'
  9. CFG_CLS = 'yolov8n-cls.yaml' # or 'squeezenet1_0'
  10. CFG = get_cfg(DEFAULT_CFG)
  11. MODEL = WEIGHTS_DIR / 'yolov8n'
  12. def test_func(*args): # noqa
  13. """Test function callback."""
  14. print('callback test passed')
  15. def test_export():
  16. """Test model exporting functionality."""
  17. exporter = Exporter()
  18. exporter.add_callback('on_export_start', test_func)
  19. assert test_func in exporter.callbacks['on_export_start'], 'callback test failed'
  20. f = exporter(model=YOLO(CFG_DET).model)
  21. YOLO(f)(ASSETS) # exported model inference
  22. def test_detect():
  23. """Test object detection functionality."""
  24. overrides = {'data': 'coco8.yaml', 'model': CFG_DET, 'imgsz': 32, 'epochs': 1, 'save': False}
  25. CFG.data = 'coco8.yaml'
  26. CFG.imgsz = 32
  27. # Trainer
  28. trainer = detect.DetectionTrainer(overrides=overrides)
  29. trainer.add_callback('on_train_start', test_func)
  30. assert test_func in trainer.callbacks['on_train_start'], 'callback test failed'
  31. trainer.train()
  32. # Validator
  33. val = detect.DetectionValidator(args=CFG)
  34. val.add_callback('on_val_start', test_func)
  35. assert test_func in val.callbacks['on_val_start'], 'callback test failed'
  36. val(model=trainer.best) # validate best.pt
  37. # Predictor
  38. pred = detect.DetectionPredictor(overrides={'imgsz': [64, 64]})
  39. pred.add_callback('on_predict_start', test_func)
  40. assert test_func in pred.callbacks['on_predict_start'], 'callback test failed'
  41. result = pred(source=ASSETS, model=f'{MODEL}.pt')
  42. assert len(result), 'predictor test failed'
  43. overrides['resume'] = trainer.last
  44. trainer = detect.DetectionTrainer(overrides=overrides)
  45. try:
  46. trainer.train()
  47. except Exception as e:
  48. print(f'Expected exception caught: {e}')
  49. return
  50. Exception('Resume test failed!')
  51. def test_segment():
  52. """Test image segmentation functionality."""
  53. overrides = {'data': 'coco8-seg.yaml', 'model': CFG_SEG, 'imgsz': 32, 'epochs': 1, 'save': False}
  54. CFG.data = 'coco8-seg.yaml'
  55. CFG.imgsz = 32
  56. # YOLO(CFG_SEG).train(**overrides) # works
  57. # Trainer
  58. trainer = segment.SegmentationTrainer(overrides=overrides)
  59. trainer.add_callback('on_train_start', test_func)
  60. assert test_func in trainer.callbacks['on_train_start'], 'callback test failed'
  61. trainer.train()
  62. # Validator
  63. val = segment.SegmentationValidator(args=CFG)
  64. val.add_callback('on_val_start', test_func)
  65. assert test_func in val.callbacks['on_val_start'], 'callback test failed'
  66. val(model=trainer.best) # validate best.pt
  67. # Predictor
  68. pred = segment.SegmentationPredictor(overrides={'imgsz': [64, 64]})
  69. pred.add_callback('on_predict_start', test_func)
  70. assert test_func in pred.callbacks['on_predict_start'], 'callback test failed'
  71. result = pred(source=ASSETS, model=f'{MODEL}-seg.pt')
  72. assert len(result), 'predictor test failed'
  73. # Test resume
  74. overrides['resume'] = trainer.last
  75. trainer = segment.SegmentationTrainer(overrides=overrides)
  76. try:
  77. trainer.train()
  78. except Exception as e:
  79. print(f'Expected exception caught: {e}')
  80. return
  81. Exception('Resume test failed!')
  82. def test_classify():
  83. """Test image classification functionality."""
  84. overrides = {'data': 'imagenet10', 'model': CFG_CLS, 'imgsz': 32, 'epochs': 1, 'save': False}
  85. CFG.data = 'imagenet10'
  86. CFG.imgsz = 32
  87. # YOLO(CFG_SEG).train(**overrides) # works
  88. # Trainer
  89. trainer = classify.ClassificationTrainer(overrides=overrides)
  90. trainer.add_callback('on_train_start', test_func)
  91. assert test_func in trainer.callbacks['on_train_start'], 'callback test failed'
  92. trainer.train()
  93. # Validator
  94. val = classify.ClassificationValidator(args=CFG)
  95. val.add_callback('on_val_start', test_func)
  96. assert test_func in val.callbacks['on_val_start'], 'callback test failed'
  97. val(model=trainer.best)
  98. # Predictor
  99. pred = classify.ClassificationPredictor(overrides={'imgsz': [64, 64]})
  100. pred.add_callback('on_predict_start', test_func)
  101. assert test_func in pred.callbacks['on_predict_start'], 'callback test failed'
  102. result = pred(source=ASSETS, model=trainer.best)
  103. assert len(result), 'predictor test failed'
Tip!

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

Comments

Loading...