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 11 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
  1. # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. import io
  3. import shutil
  4. import uuid
  5. from contextlib import redirect_stderr, redirect_stdout
  6. from itertools import product
  7. from pathlib import Path
  8. import pytest
  9. from tests import MODEL, SOURCE
  10. from ultralytics import YOLO
  11. from ultralytics.cfg import TASK2DATA, TASK2MODEL, TASKS
  12. from ultralytics.utils import (
  13. ARM64,
  14. IS_RASPBERRYPI,
  15. LINUX,
  16. MACOS,
  17. WINDOWS,
  18. checks,
  19. )
  20. from ultralytics.utils.torch_utils import TORCH_1_9, TORCH_1_13
  21. def test_export_torchscript():
  22. """Test YOLO model export to TorchScript format for compatibility and correctness."""
  23. file = YOLO(MODEL).export(format="torchscript", optimize=False, imgsz=32)
  24. YOLO(file)(SOURCE, imgsz=32) # exported model inference
  25. def test_export_onnx():
  26. """Test YOLO model export to ONNX format with dynamic axes."""
  27. file = YOLO(MODEL).export(format="onnx", dynamic=True, imgsz=32)
  28. YOLO(file)(SOURCE, imgsz=32) # exported model inference
  29. @pytest.mark.skipif(not TORCH_1_13, reason="OpenVINO requires torch>=1.13")
  30. def test_export_openvino():
  31. """Test YOLO export to OpenVINO format for model inference compatibility."""
  32. file = YOLO(MODEL).export(format="openvino", imgsz=32)
  33. YOLO(file)(SOURCE, imgsz=32) # exported model inference
  34. @pytest.mark.slow
  35. @pytest.mark.skipif(not TORCH_1_13, reason="OpenVINO requires torch>=1.13")
  36. @pytest.mark.parametrize(
  37. "task, dynamic, int8, half, batch, nms",
  38. [ # generate all combinations except for exclusion cases
  39. (task, dynamic, int8, half, batch, nms)
  40. for task, dynamic, int8, half, batch, nms in product(
  41. TASKS, [True, False], [True, False], [True, False], [1, 2], [True, False]
  42. )
  43. if not ((int8 and half) or (task == "classify" and nms))
  44. ],
  45. )
  46. def test_export_openvino_matrix(task, dynamic, int8, half, batch, nms):
  47. """Test YOLO model export to OpenVINO under various configuration matrix conditions."""
  48. file = YOLO(TASK2MODEL[task]).export(
  49. format="openvino",
  50. imgsz=32,
  51. dynamic=dynamic,
  52. int8=int8,
  53. half=half,
  54. batch=batch,
  55. data=TASK2DATA[task],
  56. nms=nms,
  57. )
  58. if WINDOWS:
  59. # Use unique filenames due to Windows file permissions bug possibly due to latent threaded use
  60. # See https://github.com/ultralytics/ultralytics/actions/runs/8957949304/job/24601616830?pr=10423
  61. file = Path(file)
  62. file = file.rename(file.with_stem(f"{file.stem}-{uuid.uuid4()}"))
  63. YOLO(file)([SOURCE] * batch, imgsz=64 if dynamic else 32, batch=batch) # exported model inference
  64. shutil.rmtree(file, ignore_errors=True) # retry in case of potential lingering multi-threaded file usage errors
  65. @pytest.mark.slow
  66. @pytest.mark.parametrize(
  67. "task, dynamic, int8, half, batch, simplify, nms",
  68. [ # generate all combinations except for exclusion cases
  69. (task, dynamic, int8, half, batch, simplify, nms)
  70. for task, dynamic, int8, half, batch, simplify, nms in product(
  71. TASKS, [True, False], [False], [False], [1, 2], [True, False], [True, False]
  72. )
  73. if not ((int8 and half) or (task == "classify" and nms) or (task == "obb" and nms and not TORCH_1_13))
  74. ],
  75. )
  76. def test_export_onnx_matrix(task, dynamic, int8, half, batch, simplify, nms):
  77. """Test YOLO export to ONNX format with various configurations and parameters."""
  78. file = YOLO(TASK2MODEL[task]).export(
  79. format="onnx", imgsz=32, dynamic=dynamic, int8=int8, half=half, batch=batch, simplify=simplify, nms=nms
  80. )
  81. YOLO(file)([SOURCE] * batch, imgsz=64 if dynamic else 32) # exported model inference
  82. Path(file).unlink() # cleanup
  83. @pytest.mark.slow
  84. @pytest.mark.parametrize(
  85. "task, dynamic, int8, half, batch, nms",
  86. [ # generate all combinations except for exclusion cases
  87. (task, dynamic, int8, half, batch, nms)
  88. for task, dynamic, int8, half, batch, nms in product(
  89. TASKS, [False, True], [False], [False], [1, 2], [True, False]
  90. )
  91. if not (task == "classify" and nms)
  92. ],
  93. )
  94. def test_export_torchscript_matrix(task, dynamic, int8, half, batch, nms):
  95. """Test YOLO model export to TorchScript format under varied configurations."""
  96. file = YOLO(TASK2MODEL[task]).export(
  97. format="torchscript", imgsz=32, dynamic=dynamic, int8=int8, half=half, batch=batch, nms=nms
  98. )
  99. YOLO(file)([SOURCE] * batch, imgsz=64 if dynamic else 32) # exported model inference
  100. Path(file).unlink() # cleanup
  101. @pytest.mark.slow
  102. @pytest.mark.skipif(not MACOS, reason="CoreML inference only supported on macOS")
  103. @pytest.mark.skipif(not TORCH_1_9, reason="CoreML>=7.2 not supported with PyTorch<=1.8")
  104. @pytest.mark.skipif(checks.IS_PYTHON_3_13, reason="CoreML not supported in Python 3.13")
  105. @pytest.mark.parametrize(
  106. "task, dynamic, int8, half, batch",
  107. [ # generate all combinations except for exclusion cases
  108. (task, dynamic, int8, half, batch)
  109. for task, dynamic, int8, half, batch in product(TASKS, [False], [True, False], [True, False], [1])
  110. if not (int8 and half)
  111. ],
  112. )
  113. def test_export_coreml_matrix(task, dynamic, int8, half, batch):
  114. """Test YOLO export to CoreML format with various parameter configurations."""
  115. file = YOLO(TASK2MODEL[task]).export(
  116. format="coreml",
  117. imgsz=32,
  118. dynamic=dynamic,
  119. int8=int8,
  120. half=half,
  121. batch=batch,
  122. )
  123. YOLO(file)([SOURCE] * batch, imgsz=32) # exported model inference
  124. shutil.rmtree(file) # cleanup
  125. @pytest.mark.slow
  126. @pytest.mark.skipif(not checks.IS_PYTHON_MINIMUM_3_10, reason="TFLite export requires Python>=3.10")
  127. @pytest.mark.skipif(
  128. not LINUX or IS_RASPBERRYPI,
  129. reason="Test disabled as TF suffers from install conflicts on Windows, macOS and Raspberry Pi",
  130. )
  131. @pytest.mark.parametrize(
  132. "task, dynamic, int8, half, batch, nms",
  133. [ # generate all combinations except for exclusion cases
  134. (task, dynamic, int8, half, batch, nms)
  135. for task, dynamic, int8, half, batch, nms in product(
  136. TASKS, [False], [True, False], [True, False], [1], [True, False]
  137. )
  138. if not ((int8 and half) or (task == "classify" and nms) or (ARM64 and nms))
  139. ],
  140. )
  141. def test_export_tflite_matrix(task, dynamic, int8, half, batch, nms):
  142. """Test YOLO export to TFLite format considering various export configurations."""
  143. file = YOLO(TASK2MODEL[task]).export(
  144. format="tflite", imgsz=32, dynamic=dynamic, int8=int8, half=half, batch=batch, nms=nms
  145. )
  146. YOLO(file)([SOURCE] * batch, imgsz=32) # exported model inference
  147. Path(file).unlink() # cleanup
  148. @pytest.mark.skipif(not TORCH_1_9, reason="CoreML>=7.2 not supported with PyTorch<=1.8")
  149. @pytest.mark.skipif(WINDOWS, reason="CoreML not supported on Windows") # RuntimeError: BlobWriter not loaded
  150. @pytest.mark.skipif(LINUX and ARM64, reason="CoreML not supported on aarch64 Linux")
  151. @pytest.mark.skipif(checks.IS_PYTHON_3_13, reason="CoreML not supported in Python 3.13")
  152. def test_export_coreml():
  153. """Test YOLO export to CoreML format and check for errors."""
  154. # Capture stdout and stderr
  155. stdout, stderr = io.StringIO(), io.StringIO()
  156. with redirect_stdout(stdout), redirect_stderr(stderr):
  157. YOLO(MODEL).export(format="coreml", nms=True, imgsz=32)
  158. if MACOS:
  159. file = YOLO(MODEL).export(format="coreml", imgsz=32)
  160. YOLO(file)(SOURCE, imgsz=32) # model prediction only supported on macOS for nms=False models
  161. # Check captured output for errors
  162. output = stdout.getvalue() + stderr.getvalue()
  163. assert "Error" not in output, f"CoreML export produced errors: {output}"
  164. assert "You will not be able to run predict()" not in output, "CoreML export has predict() error"
  165. @pytest.mark.skipif(not checks.IS_PYTHON_MINIMUM_3_10, reason="TFLite export requires Python>=3.10")
  166. @pytest.mark.skipif(not LINUX, reason="Test disabled as TF suffers from install conflicts on Windows and macOS")
  167. def test_export_tflite():
  168. """Test YOLO export to TFLite format under specific OS and Python version conditions."""
  169. model = YOLO(MODEL)
  170. file = model.export(format="tflite", imgsz=32)
  171. YOLO(file)(SOURCE, imgsz=32)
  172. @pytest.mark.skipif(True, reason="Test disabled")
  173. @pytest.mark.skipif(not LINUX, reason="TF suffers from install conflicts on Windows and macOS")
  174. def test_export_pb():
  175. """Test YOLO export to TensorFlow's Protobuf (*.pb) format."""
  176. model = YOLO(MODEL)
  177. file = model.export(format="pb", imgsz=32)
  178. YOLO(file)(SOURCE, imgsz=32)
  179. @pytest.mark.skipif(True, reason="Test disabled as Paddle protobuf and ONNX protobuf requirements conflict.")
  180. def test_export_paddle():
  181. """Test YOLO export to Paddle format, noting protobuf conflicts with ONNX."""
  182. YOLO(MODEL).export(format="paddle", imgsz=32)
  183. @pytest.mark.slow
  184. def test_export_mnn():
  185. """Test YOLO export to MNN format (WARNING: MNN test must precede NCNN test or CI error on Windows)."""
  186. file = YOLO(MODEL).export(format="mnn", imgsz=32)
  187. YOLO(file)(SOURCE, imgsz=32) # exported model inference
  188. @pytest.mark.slow
  189. @pytest.mark.parametrize(
  190. "task, int8, half, batch",
  191. [ # generate all combinations except for exclusion cases
  192. (task, int8, half, batch)
  193. for task, int8, half, batch in product(TASKS, [True, False], [True, False], [1, 2])
  194. if not (int8 and half)
  195. ],
  196. )
  197. def test_export_mnn_matrix(task, int8, half, batch):
  198. """Test YOLO export to MNN format considering various export configurations."""
  199. file = YOLO(TASK2MODEL[task]).export(format="mnn", imgsz=32, int8=int8, half=half, batch=batch)
  200. YOLO(file)([SOURCE] * batch, imgsz=32) # exported model inference
  201. Path(file).unlink() # cleanup
  202. @pytest.mark.slow
  203. def test_export_ncnn():
  204. """Test YOLO export to NCNN format."""
  205. file = YOLO(MODEL).export(format="ncnn", imgsz=32)
  206. YOLO(file)(SOURCE, imgsz=32) # exported model inference
  207. @pytest.mark.slow
  208. @pytest.mark.parametrize(
  209. "task, half, batch",
  210. [ # generate all combinations except for exclusion cases
  211. (task, half, batch) for task, half, batch in product(TASKS, [True, False], [1])
  212. ],
  213. )
  214. def test_export_ncnn_matrix(task, half, batch):
  215. """Test YOLO export to NCNN format considering various export configurations."""
  216. file = YOLO(TASK2MODEL[task]).export(format="ncnn", imgsz=32, half=half, batch=batch)
  217. YOLO(file)([SOURCE] * batch, imgsz=32) # exported model inference
  218. shutil.rmtree(file, ignore_errors=True) # retry in case of potential lingering multi-threaded file usage errors
  219. @pytest.mark.skipif(True, reason="Test disabled as keras and tensorflow version conflicts with TFlite export.")
  220. @pytest.mark.skipif(not LINUX or MACOS, reason="Skipping test on Windows and Macos")
  221. def test_export_imx():
  222. """Test YOLO export to IMX format."""
  223. model = YOLO("yolov8n.pt")
  224. file = model.export(format="imx", imgsz=32)
  225. YOLO(file)(SOURCE, imgsz=32)
Tip!

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

Comments

Loading...