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

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

Comments

Loading...