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