Spaces:
Running
Running
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license | |
import shutil | |
import uuid | |
from itertools import product | |
from pathlib import Path | |
import pytest | |
from tests import MODEL, SOURCE | |
from ultralytics import YOLO | |
from ultralytics.cfg import TASK2DATA, TASK2MODEL, TASKS | |
from ultralytics.utils import ( | |
IS_RASPBERRYPI, | |
LINUX, | |
MACOS, | |
WINDOWS, | |
checks, | |
) | |
from ultralytics.utils.torch_utils import TORCH_1_9, TORCH_1_13 | |
def test_export_torchscript(): | |
"""Test YOLO model exporting to TorchScript format for compatibility and correctness.""" | |
file = YOLO(MODEL).export(format="torchscript", optimize=False, imgsz=32) | |
YOLO(file)(SOURCE, imgsz=32) # exported model inference | |
def test_export_onnx(): | |
"""Test YOLO model export to ONNX format with dynamic axes.""" | |
file = YOLO(MODEL).export(format="onnx", dynamic=True, imgsz=32) | |
YOLO(file)(SOURCE, imgsz=32) # exported model inference | |
def test_export_openvino(): | |
"""Test YOLO exports to OpenVINO format for model inference compatibility.""" | |
file = YOLO(MODEL).export(format="openvino", imgsz=32) | |
YOLO(file)(SOURCE, imgsz=32) # exported model inference | |
def test_export_openvino_matrix(task, dynamic, int8, half, batch): | |
"""Test YOLO model exports to OpenVINO under various configuration matrix conditions.""" | |
file = YOLO(TASK2MODEL[task]).export( | |
format="openvino", | |
imgsz=32, | |
dynamic=dynamic, | |
int8=int8, | |
half=half, | |
batch=batch, | |
data=TASK2DATA[task], | |
) | |
if WINDOWS: | |
# Use unique filenames due to Windows file permissions bug possibly due to latent threaded use | |
# See https://github.com/ultralytics/ultralytics/actions/runs/8957949304/job/24601616830?pr=10423 | |
file = Path(file) | |
file = file.rename(file.with_stem(f"{file.stem}-{uuid.uuid4()}")) | |
YOLO(file)([SOURCE] * batch, imgsz=64 if dynamic else 32) # exported model inference | |
shutil.rmtree(file, ignore_errors=True) # retry in case of potential lingering multi-threaded file usage errors | |
def test_export_onnx_matrix(task, dynamic, int8, half, batch, simplify): | |
"""Test YOLO exports to ONNX format with various configurations and parameters.""" | |
file = YOLO(TASK2MODEL[task]).export( | |
format="onnx", | |
imgsz=32, | |
dynamic=dynamic, | |
int8=int8, | |
half=half, | |
batch=batch, | |
simplify=simplify, | |
) | |
YOLO(file)([SOURCE] * batch, imgsz=64 if dynamic else 32) # exported model inference | |
Path(file).unlink() # cleanup | |
def test_export_torchscript_matrix(task, dynamic, int8, half, batch): | |
"""Tests YOLO model exports to TorchScript format under varied configurations.""" | |
file = YOLO(TASK2MODEL[task]).export( | |
format="torchscript", | |
imgsz=32, | |
dynamic=dynamic, | |
int8=int8, | |
half=half, | |
batch=batch, | |
) | |
YOLO(file)([SOURCE] * 3, imgsz=64 if dynamic else 32) # exported model inference at batch=3 | |
Path(file).unlink() # cleanup | |
def test_export_coreml_matrix(task, dynamic, int8, half, batch): | |
"""Test YOLO exports to CoreML format with various parameter configurations.""" | |
file = YOLO(TASK2MODEL[task]).export( | |
format="coreml", | |
imgsz=32, | |
dynamic=dynamic, | |
int8=int8, | |
half=half, | |
batch=batch, | |
) | |
YOLO(file)([SOURCE] * batch, imgsz=32) # exported model inference at batch=3 | |
shutil.rmtree(file) # cleanup | |
def test_export_tflite_matrix(task, dynamic, int8, half, batch): | |
"""Test YOLO exports to TFLite format considering various export configurations.""" | |
file = YOLO(TASK2MODEL[task]).export( | |
format="tflite", | |
imgsz=32, | |
dynamic=dynamic, | |
int8=int8, | |
half=half, | |
batch=batch, | |
) | |
YOLO(file)([SOURCE] * batch, imgsz=32) # exported model inference at batch=3 | |
Path(file).unlink() # cleanup | |
# RuntimeError: BlobWriter not loaded | |
def test_export_coreml(): | |
"""Test YOLO exports to CoreML format, optimized for macOS only.""" | |
if MACOS: | |
file = YOLO(MODEL).export(format="coreml", imgsz=32) | |
YOLO(file)(SOURCE, imgsz=32) # model prediction only supported on macOS for nms=False models | |
else: | |
YOLO(MODEL).export(format="coreml", nms=True, imgsz=32) | |
def test_export_tflite(): | |
"""Test YOLO exports to TFLite format under specific OS and Python version conditions.""" | |
model = YOLO(MODEL) | |
file = model.export(format="tflite", imgsz=32) | |
YOLO(file)(SOURCE, imgsz=32) | |
def test_export_pb(): | |
"""Test YOLO exports to TensorFlow's Protobuf (*.pb) format.""" | |
model = YOLO(MODEL) | |
file = model.export(format="pb", imgsz=32) | |
YOLO(file)(SOURCE, imgsz=32) | |
def test_export_paddle(): | |
"""Test YOLO exports to Paddle format, noting protobuf conflicts with ONNX.""" | |
YOLO(MODEL).export(format="paddle", imgsz=32) | |
def test_export_mnn(): | |
"""Test YOLO exports to MNN format (WARNING: MNN test must precede NCNN test or CI error on Windows).""" | |
file = YOLO(MODEL).export(format="mnn", imgsz=32) | |
YOLO(file)(SOURCE, imgsz=32) # exported model inference | |
def test_export_ncnn(): | |
"""Test YOLO exports to NCNN format.""" | |
file = YOLO(MODEL).export(format="ncnn", imgsz=32) | |
YOLO(file)(SOURCE, imgsz=32) # exported model inference | |
def test_export_imx(): | |
"""Test YOLOv8n exports to IMX format.""" | |
model = YOLO("yolov8n.pt") | |
file = model.export(format="imx", imgsz=32) | |
YOLO(file)(SOURCE, imgsz=32) | |