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_exports.py 8.0 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. import shutil
  3. import uuid
  4. from itertools import product
  5. from pathlib import Path
  6. import pytest
  7. from tests import MODEL, SOURCE
  8. from ultralytics import YOLO
  9. from ultralytics.cfg import TASK2DATA, TASK2MODEL, TASKS
  10. from ultralytics.utils import (
  11. IS_RASPBERRYPI,
  12. LINUX,
  13. MACOS,
  14. WINDOWS,
  15. checks,
  16. )
  17. from ultralytics.utils.torch_utils import TORCH_1_9, TORCH_1_13
  18. def test_export_torchscript():
  19. """Test YOLO model exporting to TorchScript format for compatibility and correctness."""
  20. file = YOLO(MODEL).export(format="torchscript", optimize=False, imgsz=32)
  21. YOLO(file)(SOURCE, imgsz=32) # exported model inference
  22. def test_export_onnx():
  23. """Test YOLO model export to ONNX format with dynamic axes."""
  24. file = YOLO(MODEL).export(format="onnx", dynamic=True, imgsz=32)
  25. YOLO(file)(SOURCE, imgsz=32) # exported model inference
  26. @pytest.mark.skipif(checks.IS_PYTHON_3_12, reason="OpenVINO not supported in Python 3.12")
  27. @pytest.mark.skipif(not TORCH_1_13, reason="OpenVINO requires torch>=1.13")
  28. def test_export_openvino():
  29. """Test YOLO exports to OpenVINO format for model inference compatibility."""
  30. file = YOLO(MODEL).export(format="openvino", imgsz=32)
  31. YOLO(file)(SOURCE, imgsz=32) # exported model inference
  32. @pytest.mark.slow
  33. @pytest.mark.skipif(checks.IS_PYTHON_3_12, reason="OpenVINO not supported in Python 3.12")
  34. @pytest.mark.skipif(not TORCH_1_13, reason="OpenVINO requires torch>=1.13")
  35. @pytest.mark.parametrize(
  36. "task, dynamic, int8, half, batch",
  37. [ # generate all combinations but exclude those where both int8 and half are True
  38. (task, dynamic, int8, half, batch)
  39. for task, dynamic, int8, half, batch in product(TASKS, [True, False], [True, False], [True, False], [1, 2])
  40. if not (int8 and half) # exclude cases where both int8 and half are True
  41. ],
  42. )
  43. def test_export_openvino_matrix(task, dynamic, int8, half, batch):
  44. """Test YOLO model exports to OpenVINO under various configuration matrix conditions."""
  45. file = YOLO(TASK2MODEL[task]).export(
  46. format="openvino",
  47. imgsz=32,
  48. dynamic=dynamic,
  49. int8=int8,
  50. half=half,
  51. batch=batch,
  52. data=TASK2DATA[task],
  53. )
  54. if WINDOWS:
  55. # Use unique filenames due to Windows file permissions bug possibly due to latent threaded use
  56. # See https://github.com/ultralytics/ultralytics/actions/runs/8957949304/job/24601616830?pr=10423
  57. file = Path(file)
  58. file = file.rename(file.with_stem(f"{file.stem}-{uuid.uuid4()}"))
  59. YOLO(file)([SOURCE] * batch, imgsz=64 if dynamic else 32) # exported model inference
  60. shutil.rmtree(file, ignore_errors=True) # retry in case of potential lingering multi-threaded file usage errors
  61. @pytest.mark.slow
  62. @pytest.mark.parametrize(
  63. "task, dynamic, int8, half, batch, simplify", product(TASKS, [True, False], [False], [False], [1, 2], [True, False])
  64. )
  65. def test_export_onnx_matrix(task, dynamic, int8, half, batch, simplify):
  66. """Test YOLO exports to ONNX format with various configurations and parameters."""
  67. file = YOLO(TASK2MODEL[task]).export(
  68. format="onnx",
  69. imgsz=32,
  70. dynamic=dynamic,
  71. int8=int8,
  72. half=half,
  73. batch=batch,
  74. simplify=simplify,
  75. )
  76. YOLO(file)([SOURCE] * batch, imgsz=64 if dynamic else 32) # exported model inference
  77. Path(file).unlink() # cleanup
  78. @pytest.mark.slow
  79. @pytest.mark.parametrize("task, dynamic, int8, half, batch", product(TASKS, [False], [False], [False], [1, 2]))
  80. def test_export_torchscript_matrix(task, dynamic, int8, half, batch):
  81. """Tests YOLO model exports to TorchScript format under varied configurations."""
  82. file = YOLO(TASK2MODEL[task]).export(
  83. format="torchscript",
  84. imgsz=32,
  85. dynamic=dynamic,
  86. int8=int8,
  87. half=half,
  88. batch=batch,
  89. )
  90. YOLO(file)([SOURCE] * 3, imgsz=64 if dynamic else 32) # exported model inference at batch=3
  91. Path(file).unlink() # cleanup
  92. @pytest.mark.slow
  93. @pytest.mark.skipif(not MACOS, reason="CoreML inference only supported on macOS")
  94. @pytest.mark.skipif(not TORCH_1_9, reason="CoreML>=7.2 not supported with PyTorch<=1.8")
  95. @pytest.mark.skipif(checks.IS_PYTHON_3_12, reason="CoreML not supported in Python 3.12")
  96. @pytest.mark.parametrize(
  97. "task, dynamic, int8, half, batch",
  98. [ # generate all combinations but exclude those where both int8 and half are True
  99. (task, dynamic, int8, half, batch)
  100. for task, dynamic, int8, half, batch in product(TASKS, [False], [True, False], [True, False], [1])
  101. if not (int8 and half) # exclude cases where both int8 and half are True
  102. ],
  103. )
  104. def test_export_coreml_matrix(task, dynamic, int8, half, batch):
  105. """Test YOLO exports to CoreML format with various parameter configurations."""
  106. file = YOLO(TASK2MODEL[task]).export(
  107. format="coreml",
  108. imgsz=32,
  109. dynamic=dynamic,
  110. int8=int8,
  111. half=half,
  112. batch=batch,
  113. )
  114. YOLO(file)([SOURCE] * batch, imgsz=32) # exported model inference at batch=3
  115. shutil.rmtree(file) # cleanup
  116. @pytest.mark.slow
  117. @pytest.mark.skipif(not checks.IS_PYTHON_MINIMUM_3_10, reason="TFLite export requires Python>=3.10")
  118. @pytest.mark.skipif(not LINUX, reason="Test disabled as TF suffers from install conflicts on Windows and macOS")
  119. @pytest.mark.parametrize(
  120. "task, dynamic, int8, half, batch",
  121. [ # generate all combinations but exclude those where both int8 and half are True
  122. (task, dynamic, int8, half, batch)
  123. for task, dynamic, int8, half, batch in product(TASKS, [False], [True, False], [True, False], [1])
  124. if not (int8 and half) # exclude cases where both int8 and half are True
  125. ],
  126. )
  127. def test_export_tflite_matrix(task, dynamic, int8, half, batch):
  128. """Test YOLO exports to TFLite format considering various export configurations."""
  129. file = YOLO(TASK2MODEL[task]).export(
  130. format="tflite",
  131. imgsz=32,
  132. dynamic=dynamic,
  133. int8=int8,
  134. half=half,
  135. batch=batch,
  136. )
  137. YOLO(file)([SOURCE] * batch, imgsz=32) # exported model inference at batch=3
  138. Path(file).unlink() # cleanup
  139. @pytest.mark.skipif(not TORCH_1_9, reason="CoreML>=7.2 not supported with PyTorch<=1.8")
  140. @pytest.mark.skipif(WINDOWS, reason="CoreML not supported on Windows") # RuntimeError: BlobWriter not loaded
  141. @pytest.mark.skipif(IS_RASPBERRYPI, reason="CoreML not supported on Raspberry Pi")
  142. @pytest.mark.skipif(checks.IS_PYTHON_3_12, reason="CoreML not supported in Python 3.12")
  143. def test_export_coreml():
  144. """Test YOLO exports to CoreML format, optimized for macOS only."""
  145. if MACOS:
  146. file = YOLO(MODEL).export(format="coreml", imgsz=32)
  147. YOLO(file)(SOURCE, imgsz=32) # model prediction only supported on macOS for nms=False models
  148. else:
  149. YOLO(MODEL).export(format="coreml", nms=True, imgsz=32)
  150. @pytest.mark.skipif(not checks.IS_PYTHON_MINIMUM_3_10, reason="TFLite export requires Python>=3.10")
  151. @pytest.mark.skipif(not LINUX, reason="Test disabled as TF suffers from install conflicts on Windows and macOS")
  152. def test_export_tflite():
  153. """Test YOLO exports to TFLite format under specific OS and Python version conditions."""
  154. model = YOLO(MODEL)
  155. file = model.export(format="tflite", imgsz=32)
  156. YOLO(file)(SOURCE, imgsz=32)
  157. @pytest.mark.skipif(True, reason="Test disabled")
  158. @pytest.mark.skipif(not LINUX, reason="TF suffers from install conflicts on Windows and macOS")
  159. def test_export_pb():
  160. """Test YOLO exports to TensorFlow's Protobuf (*.pb) format."""
  161. model = YOLO(MODEL)
  162. file = model.export(format="pb", imgsz=32)
  163. YOLO(file)(SOURCE, imgsz=32)
  164. @pytest.mark.skipif(True, reason="Test disabled as Paddle protobuf and ONNX protobuf requirementsk conflict.")
  165. def test_export_paddle():
  166. """Test YOLO exports to Paddle format, noting protobuf conflicts with ONNX."""
  167. YOLO(MODEL).export(format="paddle", imgsz=32)
  168. @pytest.mark.slow
  169. def test_export_ncnn():
  170. """Test YOLO exports to NCNN format."""
  171. file = YOLO(MODEL).export(format="ncnn", imgsz=32)
  172. YOLO(file)(SOURCE, imgsz=32) # exported model inference
Tip!

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

Comments

Loading...