Skip to content

Reference for ultralytics/engine/exporter.py

Note

This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/engine/exporter.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!


ultralytics.engine.exporter.Exporter

Exporter(cfg=DEFAULT_CFG, overrides=None, _callbacks=None)

A class for exporting YOLO models to various formats.

This class provides functionality to export YOLO models to different formats including ONNX, TensorRT, CoreML, TensorFlow, and others. It handles format validation, device selection, model preparation, and the actual export process for each supported format.

Attributes:

Name Type Description
args SimpleNamespace

Configuration arguments for the exporter.

callbacks dict

Dictionary of callback functions for different export events.

im Tensor

Input tensor for model inference during export.

model Module

The YOLO model to be exported.

file Path

Path to the model file being exported.

output_shape tuple

Shape of the model output tensor(s).

pretty_name str

Formatted model name for display purposes.

metadata dict

Model metadata including description, author, version, etc.

device device

Device on which the model is loaded.

imgsz tuple

Input image size for the model.

Methods:

Name Description
__call__

Main export method that handles the export process.

get_int8_calibration_dataloader

Build dataloader for INT8 calibration.

export_torchscript

Export model to TorchScript format.

export_onnx

Export model to ONNX format.

export_openvino

Export model to OpenVINO format.

export_paddle

Export model to PaddlePaddle format.

export_mnn

Export model to MNN format.

export_ncnn

Export model to NCNN format.

export_coreml

Export model to CoreML format.

export_engine

Export model to TensorRT format.

export_saved_model

Export model to TensorFlow SavedModel format.

export_pb

Export model to TensorFlow GraphDef format.

export_tflite

Export model to TensorFlow Lite format.

export_edgetpu

Export model to Edge TPU format.

export_tfjs

Export model to TensorFlow.js format.

export_rknn

Export model to RKNN format.

export_imx

Export model to IMX format.

Examples:

Export a YOLOv8 model to ONNX format

>>> from ultralytics.engine.exporter import Exporter
>>> exporter = Exporter()
>>> exporter(model="yolov8n.pt")  # exports to yolov8n.onnx

Export with specific arguments

>>> args = {"format": "onnx", "dynamic": True, "half": True}
>>> exporter = Exporter(overrides=args)
>>> exporter(model="yolov8n.pt")

Parameters:

Name Type Description Default
cfg str

Path to a configuration file.

DEFAULT_CFG
overrides dict

Configuration overrides.

None
_callbacks dict

Dictionary of callback functions.

None
Source code in ultralytics/engine/exporter.py
274
275
276
277
278
279
280
281
282
283
284
285
def __init__(self, cfg=DEFAULT_CFG, overrides=None, _callbacks=None):
    """
    Initialize the Exporter class.

    Args:
        cfg (str, optional): Path to a configuration file.
        overrides (dict, optional): Configuration overrides.
        _callbacks (dict, optional): Dictionary of callback functions.
    """
    self.args = get_cfg(cfg, overrides)
    self.callbacks = _callbacks or callbacks.get_default_callbacks()
    callbacks.add_integration_callbacks(self)

__call__

__call__(model=None) -> str

Return list of exported files/dirs after running callbacks.

Source code in ultralytics/engine/exporter.py
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
def __call__(self, model=None) -> str:
    """Return list of exported files/dirs after running callbacks."""
    t = time.time()
    fmt = self.args.format.lower()  # to lowercase
    if fmt in {"tensorrt", "trt"}:  # 'engine' aliases
        fmt = "engine"
    if fmt in {"mlmodel", "mlpackage", "mlprogram", "apple", "ios", "coreml"}:  # 'coreml' aliases
        fmt = "coreml"
    fmts_dict = export_formats()
    fmts = tuple(fmts_dict["Argument"][1:])  # available export formats
    if fmt not in fmts:
        import difflib

        # Get the closest match if format is invalid
        matches = difflib.get_close_matches(fmt, fmts, n=1, cutoff=0.6)  # 60% similarity required to match
        if not matches:
            raise ValueError(f"Invalid export format='{fmt}'. Valid formats are {fmts}")
        LOGGER.warning(f"Invalid export format='{fmt}', updating to format='{matches[0]}'")
        fmt = matches[0]
    flags = [x == fmt for x in fmts]
    if sum(flags) != 1:
        raise ValueError(f"Invalid export format='{fmt}'. Valid formats are {fmts}")
    (jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs, paddle, mnn, ncnn, imx, rknn) = (
        flags  # export booleans
    )

    is_tf_format = any((saved_model, pb, tflite, edgetpu, tfjs))

    # Device
    dla = None
    if fmt == "engine" and self.args.device is None:
        LOGGER.warning("TensorRT requires GPU export, automatically assigning device=0")
        self.args.device = "0"
    if fmt == "engine" and "dla" in str(self.args.device):  # convert int/list to str first
        dla = self.args.device.rsplit(":", 1)[-1]
        self.args.device = "0"  # update device to "0"
        assert dla in {"0", "1"}, f"Expected self.args.device='dla:0' or 'dla:1, but got {self.args.device}."
    if imx and self.args.device is None and torch.cuda.is_available():
        LOGGER.warning("Exporting on CPU while CUDA is available, setting device=0 for faster export on GPU.")
        self.args.device = "0"  # update device to "0"
    self.device = select_device("cpu" if self.args.device is None else self.args.device)

    # Argument compatibility checks
    fmt_keys = fmts_dict["Arguments"][flags.index(True) + 1]
    validate_args(fmt, self.args, fmt_keys)
    if imx:
        if not self.args.int8:
            LOGGER.warning("IMX export requires int8=True, setting int8=True.")
            self.args.int8 = True
        if model.task != "detect":
            raise ValueError("IMX export only supported for detection models.")
    if not hasattr(model, "names"):
        model.names = default_class_names()
    model.names = check_class_names(model.names)
    if self.args.half and self.args.int8:
        LOGGER.warning("half=True and int8=True are mutually exclusive, setting half=False.")
        self.args.half = False
    if self.args.half and onnx and self.device.type == "cpu":
        LOGGER.warning("half=True only compatible with GPU export, i.e. use device=0")
        self.args.half = False
    self.imgsz = check_imgsz(self.args.imgsz, stride=model.stride, min_dim=2)  # check image size
    if self.args.int8 and engine:
        self.args.dynamic = True  # enforce dynamic to export TensorRT INT8
    if self.args.optimize:
        assert not ncnn, "optimize=True not compatible with format='ncnn', i.e. use optimize=False"
        assert self.device.type == "cpu", "optimize=True not compatible with cuda devices, i.e. use device='cpu'"
    if rknn:
        if not self.args.name:
            LOGGER.warning(
                "Rockchip RKNN export requires a missing 'name' arg for processor type. "
                "Using default name='rk3588'."
            )
            self.args.name = "rk3588"
        self.args.name = self.args.name.lower()
        assert self.args.name in RKNN_CHIPS, (
            f"Invalid processor name '{self.args.name}' for Rockchip RKNN export. Valid names are {RKNN_CHIPS}."
        )
    if self.args.int8 and tflite:
        assert not getattr(model, "end2end", False), "TFLite INT8 export not supported for end2end models."
    if self.args.nms:
        assert not isinstance(model, ClassificationModel), "'nms=True' is not valid for classification models."
        assert not (tflite and ARM64 and LINUX), "TFLite export with NMS unsupported on ARM64 Linux"
        if getattr(model, "end2end", False):
            LOGGER.warning("'nms=True' is not available for end2end models. Forcing 'nms=False'.")
            self.args.nms = False
        self.args.conf = self.args.conf or 0.25  # set conf default value for nms export
    if edgetpu:
        if not LINUX or ARM64:
            raise SystemError(
                "Edge TPU export only supported on non-aarch64 Linux. See https://coral.ai/docs/edgetpu/compiler"
            )
        elif self.args.batch != 1:  # see github.com/ultralytics/ultralytics/pull/13420
            LOGGER.warning("Edge TPU export requires batch size 1, setting batch=1.")
            self.args.batch = 1
    if isinstance(model, WorldModel):
        LOGGER.warning(
            "YOLOWorld (original version) export is not supported to any format. "
            "YOLOWorldv2 models (i.e. 'yolov8s-worldv2.pt') only support export to "
            "(torchscript, onnx, openvino, engine, coreml) formats. "
            "See https://docs.ultralytics.com/models/yolo-world for details."
        )
        model.clip_model = None  # openvino int8 export error: https://github.com/ultralytics/ultralytics/pull/18445
    if self.args.int8 and not self.args.data:
        self.args.data = DEFAULT_CFG.data or TASK2DATA[getattr(model, "task", "detect")]  # assign default data
        LOGGER.warning(
            f"INT8 export requires a missing 'data' arg for calibration. Using default 'data={self.args.data}'."
        )
    if tfjs and (ARM64 and LINUX):
        raise SystemError("TF.js exports are not currently supported on ARM64 Linux")
    # Recommend OpenVINO if export and Intel CPU
    if SETTINGS.get("openvino_msg"):
        if "intel" in get_cpu_info().lower():
            LOGGER.info(
                "💡 ProTip: Export to OpenVINO format for best performance on Intel CPUs."
                " Learn more at https://docs.ultralytics.com/integrations/openvino/"
            )
        SETTINGS["openvino_msg"] = False

    # Input
    im = torch.zeros(self.args.batch, model.yaml.get("channels", 3), *self.imgsz).to(self.device)
    file = Path(
        getattr(model, "pt_path", None) or getattr(model, "yaml_file", None) or model.yaml.get("yaml_file", "")
    )
    if file.suffix in {".yaml", ".yml"}:
        file = Path(file.name)

    # Update model
    model = deepcopy(model).to(self.device)
    for p in model.parameters():
        p.requires_grad = False
    model.eval()
    model.float()
    model = model.fuse()

    if imx:
        from ultralytics.utils.torch_utils import FXModel

        model = FXModel(model)
    for m in model.modules():
        if isinstance(m, Classify):
            m.export = True
        if isinstance(m, (Detect, RTDETRDecoder)):  # includes all Detect subclasses like Segment, Pose, OBB
            m.dynamic = self.args.dynamic
            m.export = True
            m.format = self.args.format
            m.max_det = self.args.max_det
            m.xyxy = self.args.nms and not coreml
        elif isinstance(m, C2f) and not is_tf_format:
            # EdgeTPU does not support FlexSplitV while split provides cleaner ONNX graph
            m.forward = m.forward_split
        if isinstance(m, Detect) and imx:
            from ultralytics.utils.tal import make_anchors

            m.anchors, m.strides = (
                x.transpose(0, 1)
                for x in make_anchors(
                    torch.cat([s / m.stride.unsqueeze(-1) for s in self.imgsz], dim=1), m.stride, 0.5
                )
            )

    y = None
    for _ in range(2):  # dry runs
        y = NMSModel(model, self.args)(im) if self.args.nms and not coreml else model(im)
    if self.args.half and onnx and self.device.type != "cpu":
        im, model = im.half(), model.half()  # to FP16

    # Filter warnings
    warnings.filterwarnings("ignore", category=torch.jit.TracerWarning)  # suppress TracerWarning
    warnings.filterwarnings("ignore", category=UserWarning)  # suppress shape prim::Constant missing ONNX warning
    warnings.filterwarnings("ignore", category=DeprecationWarning)  # suppress CoreML np.bool deprecation warning

    # Assign
    self.im = im
    self.model = model
    self.file = file
    self.output_shape = (
        tuple(y.shape)
        if isinstance(y, torch.Tensor)
        else tuple(tuple(x.shape if isinstance(x, torch.Tensor) else []) for x in y)
    )
    self.pretty_name = Path(self.model.yaml.get("yaml_file", self.file)).stem.replace("yolo", "YOLO")
    data = model.args["data"] if hasattr(model, "args") and isinstance(model.args, dict) else ""
    description = f"Ultralytics {self.pretty_name} model {f'trained on {data}' if data else ''}"
    self.metadata = {
        "description": description,
        "author": "Ultralytics",
        "date": datetime.now().isoformat(),
        "version": __version__,
        "license": "AGPL-3.0 License (https://ultralytics.com/license)",
        "docs": "https://docs.ultralytics.com",
        "stride": int(max(model.stride)),
        "task": model.task,
        "batch": self.args.batch,
        "imgsz": self.imgsz,
        "names": model.names,
        "args": {k: v for k, v in self.args if k in fmt_keys},
        "channels": model.yaml.get("channels", 3),
    }  # model metadata
    if dla is not None:
        self.metadata["dla"] = dla  # make sure `AutoBackend` uses correct dla device if it has one
    if model.task == "pose":
        self.metadata["kpt_shape"] = model.model[-1].kpt_shape

    LOGGER.info(
        f"\n{colorstr('PyTorch:')} starting from '{file}' with input shape {tuple(im.shape)} BCHW and "
        f"output shape(s) {self.output_shape} ({file_size(file):.1f} MB)"
    )
    self.run_callbacks("on_export_start")
    # Exports
    f = [""] * len(fmts)  # exported filenames
    if jit or ncnn:  # TorchScript
        f[0], _ = self.export_torchscript()
    if engine:  # TensorRT required before ONNX
        f[1], _ = self.export_engine(dla=dla)
    if onnx:  # ONNX
        f[2], _ = self.export_onnx()
    if xml:  # OpenVINO
        f[3], _ = self.export_openvino()
    if coreml:  # CoreML
        f[4], _ = self.export_coreml()
    if is_tf_format:  # TensorFlow formats
        self.args.int8 |= edgetpu
        f[5], keras_model = self.export_saved_model()
        if pb or tfjs:  # pb prerequisite to tfjs
            f[6], _ = self.export_pb(keras_model=keras_model)
        if tflite:
            f[7], _ = self.export_tflite()
        if edgetpu:
            f[8], _ = self.export_edgetpu(tflite_model=Path(f[5]) / f"{self.file.stem}_full_integer_quant.tflite")
        if tfjs:
            f[9], _ = self.export_tfjs()
    if paddle:  # PaddlePaddle
        f[10], _ = self.export_paddle()
    if mnn:  # MNN
        f[11], _ = self.export_mnn()
    if ncnn:  # NCNN
        f[12], _ = self.export_ncnn()
    if imx:
        f[13], _ = self.export_imx()
    if rknn:
        f[14], _ = self.export_rknn()

    # Finish
    f = [str(x) for x in f if x]  # filter out '' and None
    if any(f):
        f = str(Path(f[-1]))
        square = self.imgsz[0] == self.imgsz[1]
        s = (
            ""
            if square
            else f"WARNING ⚠️ non-PyTorch val requires square images, 'imgsz={self.imgsz}' will not "
            f"work. Use export 'imgsz={max(self.imgsz)}' if val is required."
        )
        imgsz = self.imgsz[0] if square else str(self.imgsz)[1:-1].replace(" ", "")
        predict_data = f"data={data}" if model.task == "segment" and fmt == "pb" else ""
        q = "int8" if self.args.int8 else "half" if self.args.half else ""  # quantization
        LOGGER.info(
            f"\nExport complete ({time.time() - t:.1f}s)"
            f"\nResults saved to {colorstr('bold', file.parent.resolve())}"
            f"\nPredict:         yolo predict task={model.task} model={f} imgsz={imgsz} {q} {predict_data}"
            f"\nValidate:        yolo val task={model.task} model={f} imgsz={imgsz} data={data} {q} {s}"
            f"\nVisualize:       https://netron.app"
        )

    self.run_callbacks("on_export_end")
    return f  # return list of exported files/dirs

add_callback

add_callback(event: str, callback)

Append the given callback to the specified event.

Source code in ultralytics/engine/exporter.py
1442
1443
1444
def add_callback(self, event: str, callback):
    """Append the given callback to the specified event."""
    self.callbacks[event].append(callback)

export_coreml

export_coreml(prefix=colorstr('CoreML:'))

Export YOLO model to CoreML format.

Source code in ultralytics/engine/exporter.py
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
@try_export
def export_coreml(self, prefix=colorstr("CoreML:")):
    """Export YOLO model to CoreML format."""
    mlmodel = self.args.format.lower() == "mlmodel"  # legacy *.mlmodel export format requested
    check_requirements("coremltools>=8.0")
    import coremltools as ct  # noqa

    LOGGER.info(f"\n{prefix} starting export with coremltools {ct.__version__}...")
    assert not WINDOWS, "CoreML export is not supported on Windows, please run on macOS or Linux."
    assert self.args.batch == 1, "CoreML batch sizes > 1 are not supported. Please retry at 'batch=1'."
    f = self.file.with_suffix(".mlmodel" if mlmodel else ".mlpackage")
    if f.is_dir():
        shutil.rmtree(f)

    bias = [0.0, 0.0, 0.0]
    scale = 1 / 255
    classifier_config = None
    if self.model.task == "classify":
        classifier_config = ct.ClassifierConfig(list(self.model.names.values()))
        model = self.model
    elif self.model.task == "detect":
        model = IOSDetectModel(self.model, self.im) if self.args.nms else self.model
    else:
        if self.args.nms:
            LOGGER.warning(f"{prefix} 'nms=True' is only available for Detect models like 'yolo11n.pt'.")
            # TODO CoreML Segment and Pose model pipelining
        model = self.model
    ts = torch.jit.trace(model.eval(), self.im, strict=False)  # TorchScript model

    # Based on apple's documentation it is better to leave out the minimum_deployment target and let that get set
    # Internally based on the model conversion and output type.
    # Setting minimum_depoloyment_target >= iOS16 will require setting compute_precision=ct.precision.FLOAT32.
    # iOS16 adds in better support for FP16, but none of the CoreML NMS specifications handle FP16 as input.
    ct_model = ct.convert(
        ts,
        inputs=[ct.ImageType("image", shape=self.im.shape, scale=scale, bias=bias)],  # expects ct.TensorType
        classifier_config=classifier_config,
        convert_to="neuralnetwork" if mlmodel else "mlprogram",
    )
    bits, mode = (8, "kmeans") if self.args.int8 else (16, "linear") if self.args.half else (32, None)
    if bits < 32:
        if "kmeans" in mode:
            check_requirements("scikit-learn")  # scikit-learn package required for k-means quantization
        if mlmodel:
            ct_model = ct.models.neural_network.quantization_utils.quantize_weights(ct_model, bits, mode)
        elif bits == 8:  # mlprogram already quantized to FP16
            import coremltools.optimize.coreml as cto

            op_config = cto.OpPalettizerConfig(mode="kmeans", nbits=bits, weight_threshold=512)
            config = cto.OptimizationConfig(global_config=op_config)
            ct_model = cto.palettize_weights(ct_model, config=config)
    if self.args.nms and self.model.task == "detect":
        if mlmodel:
            weights_dir = None
        else:
            ct_model.save(str(f))  # save otherwise weights_dir does not exist
            weights_dir = str(f / "Data/com.apple.CoreML/weights")
        ct_model = self._pipeline_coreml(ct_model, weights_dir=weights_dir)

    m = self.metadata  # metadata dict
    ct_model.short_description = m.pop("description")
    ct_model.author = m.pop("author")
    ct_model.license = m.pop("license")
    ct_model.version = m.pop("version")
    ct_model.user_defined_metadata.update({k: str(v) for k, v in m.items()})
    if self.model.task == "classify":
        ct_model.user_defined_metadata.update({"com.apple.coreml.model.preview.type": "imageClassifier"})

    try:
        ct_model.save(str(f))  # save *.mlpackage
    except Exception as e:
        LOGGER.warning(
            f"{prefix} CoreML export to *.mlpackage failed ({e}), reverting to *.mlmodel export. "
            f"Known coremltools Python 3.11 and Windows bugs https://github.com/apple/coremltools/issues/1928."
        )
        f = f.with_suffix(".mlmodel")
        ct_model.save(str(f))
    return f, ct_model

export_edgetpu

export_edgetpu(tflite_model='', prefix=colorstr('Edge TPU:'))

Export YOLO model to Edge TPU format https://coral.ai/docs/edgetpu/models-intro/.

Source code in ultralytics/engine/exporter.py
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
@try_export
def export_edgetpu(self, tflite_model="", prefix=colorstr("Edge TPU:")):
    """Export YOLO model to Edge TPU format https://coral.ai/docs/edgetpu/models-intro/."""
    cmd = "edgetpu_compiler --version"
    help_url = "https://coral.ai/docs/edgetpu/compiler/"
    assert LINUX, f"export only supported on Linux. See {help_url}"
    if subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True).returncode != 0:
        LOGGER.info(f"\n{prefix} export requires Edge TPU compiler. Attempting install from {help_url}")
        for c in (
            "curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -",
            'echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | '
            "sudo tee /etc/apt/sources.list.d/coral-edgetpu.list",
            "sudo apt-get update",
            "sudo apt-get install edgetpu-compiler",
        ):
            subprocess.run(c if is_sudo_available() else c.replace("sudo ", ""), shell=True, check=True)
    ver = subprocess.run(cmd, shell=True, capture_output=True, check=True).stdout.decode().rsplit(maxsplit=1)[-1]

    LOGGER.info(f"\n{prefix} starting export with Edge TPU compiler {ver}...")
    f = str(tflite_model).replace(".tflite", "_edgetpu.tflite")  # Edge TPU model

    cmd = (
        "edgetpu_compiler "
        f'--out_dir "{Path(f).parent}" '
        "--show_operations "
        "--search_delegate "
        "--delegate_search_step 30 "
        "--timeout_sec 180 "
        f'"{tflite_model}"'
    )
    LOGGER.info(f"{prefix} running '{cmd}'")
    subprocess.run(cmd, shell=True)
    self._add_tflite_metadata(f)
    return f, None

export_engine

export_engine(dla=None, prefix=colorstr('TensorRT:'))

Export YOLO model to TensorRT format https://developer.nvidia.com/tensorrt.

Source code in ultralytics/engine/exporter.py
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
@try_export
def export_engine(self, dla=None, prefix=colorstr("TensorRT:")):
    """Export YOLO model to TensorRT format https://developer.nvidia.com/tensorrt."""
    assert self.im.device.type != "cpu", "export running on CPU but must be on GPU, i.e. use 'device=0'"
    f_onnx, _ = self.export_onnx()  # run before TRT import https://github.com/ultralytics/ultralytics/issues/7016

    try:
        import tensorrt as trt  # noqa
    except ImportError:
        if LINUX:
            check_requirements("tensorrt>7.0.0,!=10.1.0")
        import tensorrt as trt  # noqa
    check_version(trt.__version__, ">=7.0.0", hard=True)
    check_version(trt.__version__, "!=10.1.0", msg="https://github.com/ultralytics/ultralytics/pull/14239")

    # Setup and checks
    LOGGER.info(f"\n{prefix} starting export with TensorRT {trt.__version__}...")
    assert Path(f_onnx).exists(), f"failed to export ONNX file: {f_onnx}"
    f = self.file.with_suffix(".engine")  # TensorRT engine file
    export_engine(
        f_onnx,
        f,
        self.args.workspace,
        self.args.half,
        self.args.int8,
        self.args.dynamic,
        self.im.shape,
        dla=dla,
        dataset=self.get_int8_calibration_dataloader(prefix) if self.args.int8 else None,
        metadata=self.metadata,
        verbose=self.args.verbose,
        prefix=prefix,
    )

    return f, None

export_imx

export_imx(prefix=colorstr('IMX:'))

Export YOLO model to IMX format.

Source code in ultralytics/engine/exporter.py
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
@try_export
def export_imx(self, prefix=colorstr("IMX:")):
    """Export YOLO model to IMX format."""
    gptq = False
    assert LINUX, (
        "export only supported on Linux. "
        "See https://developer.aitrios.sony-semicon.com/en/raspberrypi-ai-camera/documentation/imx500-converter"
    )
    if getattr(self.model, "end2end", False):
        raise ValueError("IMX export is not supported for end2end models.")
    check_requirements(("model-compression-toolkit>=2.3.0", "sony-custom-layers>=0.3.0", "edge-mdt-tpc>=1.1.0"))
    check_requirements("imx500-converter[pt]>=3.16.1")  # Separate requirements for imx500-converter

    import model_compression_toolkit as mct
    import onnx
    from edgemdt_tpc import get_target_platform_capabilities
    from sony_custom_layers.pytorch import multiclass_nms

    LOGGER.info(f"\n{prefix} starting export with model_compression_toolkit {mct.__version__}...")

    # Install Java>=17
    try:
        java_output = subprocess.run(["java", "--version"], check=True, capture_output=True).stdout.decode()
        version_match = re.search(r"(?:openjdk|java) (\d+)", java_output)
        java_version = int(version_match.group(1)) if version_match else 0
        assert java_version >= 17, "Java version too old"
    except (FileNotFoundError, subprocess.CalledProcessError, AssertionError):
        cmd = (["sudo"] if is_sudo_available() else []) + ["apt", "install", "-y", "openjdk-21-jre"]
        subprocess.run(cmd, check=True)

    def representative_dataset_gen(dataloader=self.get_int8_calibration_dataloader(prefix)):
        for batch in dataloader:
            img = batch["img"]
            img = img / 255.0
            yield [img]

    tpc = get_target_platform_capabilities(tpc_version="4.0", device_type="imx500")

    bit_cfg = mct.core.BitWidthConfig()
    if "C2PSA" in self.model.__str__():  # YOLO11
        layer_names = ["sub", "mul_2", "add_14", "cat_21"]
        weights_memory = 2585350.2439
        n_layers = 238  # 238 layers for fused YOLO11n
    else:  # YOLOv8
        layer_names = ["sub", "mul", "add_6", "cat_17"]
        weights_memory = 2550540.8
        n_layers = 168  # 168 layers for fused YOLOv8n

    # Check if the model has the expected number of layers
    if len(list(self.model.modules())) != n_layers:
        raise ValueError("IMX export only supported for YOLOv8n and YOLO11n models.")

    for layer_name in layer_names:
        bit_cfg.set_manual_activation_bit_width([mct.core.common.network_editors.NodeNameFilter(layer_name)], 16)

    config = mct.core.CoreConfig(
        mixed_precision_config=mct.core.MixedPrecisionQuantizationConfig(num_of_images=10),
        quantization_config=mct.core.QuantizationConfig(concat_threshold_update=True),
        bit_width_config=bit_cfg,
    )

    resource_utilization = mct.core.ResourceUtilization(weights_memory=weights_memory)

    quant_model = (
        mct.gptq.pytorch_gradient_post_training_quantization(  # Perform Gradient-Based Post Training Quantization
            model=self.model,
            representative_data_gen=representative_dataset_gen,
            target_resource_utilization=resource_utilization,
            gptq_config=mct.gptq.get_pytorch_gptq_config(
                n_epochs=1000, use_hessian_based_weights=False, use_hessian_sample_attention=False
            ),
            core_config=config,
            target_platform_capabilities=tpc,
        )[0]
        if gptq
        else mct.ptq.pytorch_post_training_quantization(  # Perform post training quantization
            in_module=self.model,
            representative_data_gen=representative_dataset_gen,
            target_resource_utilization=resource_utilization,
            core_config=config,
            target_platform_capabilities=tpc,
        )[0]
    )

    class NMSWrapper(torch.nn.Module):
        """Wrap PyTorch Module with multiclass_nms layer from sony_custom_layers."""

        def __init__(
            self,
            model: torch.nn.Module,
            score_threshold: float = 0.001,
            iou_threshold: float = 0.7,
            max_detections: int = 300,
        ):
            """
            Initialize NMSWrapper with PyTorch Module and NMS parameters.

            Args:
                model (torch.nn.Module): Model instance.
                score_threshold (float): Score threshold for non-maximum suppression.
                iou_threshold (float): Intersection over union threshold for non-maximum suppression.
                max_detections (int): The number of detections to return.
            """
            super().__init__()
            self.model = model
            self.score_threshold = score_threshold
            self.iou_threshold = iou_threshold
            self.max_detections = max_detections

        def forward(self, images):
            """Forward pass with model inference and NMS post-processing."""
            # model inference
            outputs = self.model(images)

            boxes = outputs[0]
            scores = outputs[1]
            nms = multiclass_nms(
                boxes=boxes,
                scores=scores,
                score_threshold=self.score_threshold,
                iou_threshold=self.iou_threshold,
                max_detections=self.max_detections,
            )
            return nms

    quant_model = NMSWrapper(
        model=quant_model,
        score_threshold=self.args.conf or 0.001,
        iou_threshold=self.args.iou,
        max_detections=self.args.max_det,
    ).to(self.device)

    f = Path(str(self.file).replace(self.file.suffix, "_imx_model"))
    f.mkdir(exist_ok=True)
    onnx_model = f / Path(str(self.file.name).replace(self.file.suffix, "_imx.onnx"))  # js dir
    mct.exporter.pytorch_export_model(
        model=quant_model, save_model_path=onnx_model, repr_dataset=representative_dataset_gen
    )

    model_onnx = onnx.load(onnx_model)  # load onnx model
    for k, v in self.metadata.items():
        meta = model_onnx.metadata_props.add()
        meta.key, meta.value = k, str(v)

    onnx.save(model_onnx, onnx_model)

    subprocess.run(
        ["imxconv-pt", "-i", str(onnx_model), "-o", str(f), "--no-input-persistency", "--overwrite-output"],
        check=True,
    )

    # Needed for imx models.
    with open(f / "labels.txt", "w", encoding="utf-8") as file:
        file.writelines([f"{name}\n" for _, name in self.model.names.items()])

    return f, None

export_mnn

export_mnn(prefix=colorstr('MNN:'))

Export YOLO model to MNN format using MNN https://github.com/alibaba/MNN.

Source code in ultralytics/engine/exporter.py
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
@try_export
def export_mnn(self, prefix=colorstr("MNN:")):
    """Export YOLO model to MNN format using MNN https://github.com/alibaba/MNN."""
    f_onnx, _ = self.export_onnx()  # get onnx model first

    check_requirements("MNN>=2.9.6")
    import MNN  # noqa
    from MNN.tools import mnnconvert

    # Setup and checks
    LOGGER.info(f"\n{prefix} starting export with MNN {MNN.version()}...")
    assert Path(f_onnx).exists(), f"failed to export ONNX file: {f_onnx}"
    f = str(self.file.with_suffix(".mnn"))  # MNN model file
    args = ["", "-f", "ONNX", "--modelFile", f_onnx, "--MNNModel", f, "--bizCode", json.dumps(self.metadata)]
    if self.args.int8:
        args.extend(("--weightQuantBits", "8"))
    if self.args.half:
        args.append("--fp16")
    mnnconvert.convert(args)
    # remove scratch file for model convert optimize
    convert_scratch = Path(self.file.parent / ".__convert_external_data.bin")
    if convert_scratch.exists():
        convert_scratch.unlink()
    return f, None

export_ncnn

export_ncnn(prefix=colorstr('NCNN:'))

Export YOLO model to NCNN format using PNNX https://github.com/pnnx/pnnx.

Source code in ultralytics/engine/exporter.py
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
@try_export
def export_ncnn(self, prefix=colorstr("NCNN:")):
    """Export YOLO model to NCNN format using PNNX https://github.com/pnnx/pnnx."""
    check_requirements("ncnn")
    import ncnn  # noqa

    LOGGER.info(f"\n{prefix} starting export with NCNN {ncnn.__version__}...")
    f = Path(str(self.file).replace(self.file.suffix, f"_ncnn_model{os.sep}"))
    f_ts = self.file.with_suffix(".torchscript")

    name = Path("pnnx.exe" if WINDOWS else "pnnx")  # PNNX filename
    pnnx = name if name.is_file() else (ROOT / name)
    if not pnnx.is_file():
        LOGGER.warning(
            f"{prefix} PNNX not found. Attempting to download binary file from "
            "https://github.com/pnnx/pnnx/.\nNote PNNX Binary file must be placed in current working directory "
            f"or in {ROOT}. See PNNX repo for full installation instructions."
        )
        system = "macos" if MACOS else "windows" if WINDOWS else "linux-aarch64" if ARM64 else "linux"
        try:
            release, assets = get_github_assets(repo="pnnx/pnnx")
            asset = [x for x in assets if f"{system}.zip" in x][0]
            assert isinstance(asset, str), "Unable to retrieve PNNX repo assets"  # i.e. pnnx-20240410-macos.zip
            LOGGER.info(f"{prefix} successfully found latest PNNX asset file {asset}")
        except Exception as e:
            release = "20240410"
            asset = f"pnnx-{release}-{system}.zip"
            LOGGER.warning(f"{prefix} PNNX GitHub assets not found: {e}, using default {asset}")
        unzip_dir = safe_download(f"https://github.com/pnnx/pnnx/releases/download/{release}/{asset}", delete=True)
        if check_is_path_safe(Path.cwd(), unzip_dir):  # avoid path traversal security vulnerability
            shutil.move(src=unzip_dir / name, dst=pnnx)  # move binary to ROOT
            pnnx.chmod(0o777)  # set read, write, and execute permissions for everyone
            shutil.rmtree(unzip_dir)  # delete unzip dir

    ncnn_args = [
        f"ncnnparam={f / 'model.ncnn.param'}",
        f"ncnnbin={f / 'model.ncnn.bin'}",
        f"ncnnpy={f / 'model_ncnn.py'}",
    ]

    pnnx_args = [
        f"pnnxparam={f / 'model.pnnx.param'}",
        f"pnnxbin={f / 'model.pnnx.bin'}",
        f"pnnxpy={f / 'model_pnnx.py'}",
        f"pnnxonnx={f / 'model.pnnx.onnx'}",
    ]

    cmd = [
        str(pnnx),
        str(f_ts),
        *ncnn_args,
        *pnnx_args,
        f"fp16={int(self.args.half)}",
        f"device={self.device.type}",
        f'inputshape="{[self.args.batch, 3, *self.imgsz]}"',
    ]
    f.mkdir(exist_ok=True)  # make ncnn_model directory
    LOGGER.info(f"{prefix} running '{' '.join(cmd)}'")
    subprocess.run(cmd, check=True)

    # Remove debug files
    pnnx_files = [x.rsplit("=", 1)[-1] for x in pnnx_args]
    for f_debug in ("debug.bin", "debug.param", "debug2.bin", "debug2.param", *pnnx_files):
        Path(f_debug).unlink(missing_ok=True)

    YAML.save(f / "metadata.yaml", self.metadata)  # add metadata.yaml
    return str(f), None

export_onnx

export_onnx(prefix=colorstr('ONNX:'))

Export YOLO model to ONNX format.

Source code in ultralytics/engine/exporter.py
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
@try_export
def export_onnx(self, prefix=colorstr("ONNX:")):
    """Export YOLO model to ONNX format."""
    requirements = ["onnx>=1.12.0,<1.18.0"]
    if self.args.simplify:
        requirements += ["onnxslim>=0.1.53", "onnxruntime" + ("-gpu" if torch.cuda.is_available() else "")]
    check_requirements(requirements)
    import onnx  # noqa

    opset_version = self.args.opset or get_latest_opset()
    LOGGER.info(f"\n{prefix} starting export with onnx {onnx.__version__} opset {opset_version}...")
    f = str(self.file.with_suffix(".onnx"))
    output_names = ["output0", "output1"] if isinstance(self.model, SegmentationModel) else ["output0"]
    dynamic = self.args.dynamic
    if dynamic:
        dynamic = {"images": {0: "batch", 2: "height", 3: "width"}}  # shape(1,3,640,640)
        if isinstance(self.model, SegmentationModel):
            dynamic["output0"] = {0: "batch", 2: "anchors"}  # shape(1, 116, 8400)
            dynamic["output1"] = {0: "batch", 2: "mask_height", 3: "mask_width"}  # shape(1,32,160,160)
        elif isinstance(self.model, DetectionModel):
            dynamic["output0"] = {0: "batch", 2: "anchors"}  # shape(1, 84, 8400)
        if self.args.nms:  # only batch size is dynamic with NMS
            dynamic["output0"].pop(2)
    if self.args.nms and self.model.task == "obb":
        self.args.opset = opset_version  # for NMSModel

    with arange_patch(self.args):
        export_onnx(
            NMSModel(self.model, self.args) if self.args.nms else self.model,
            self.im,
            f,
            opset=opset_version,
            input_names=["images"],
            output_names=output_names,
            dynamic=dynamic or None,
        )

    # Checks
    model_onnx = onnx.load(f)  # load onnx model

    # Simplify
    if self.args.simplify:
        try:
            import onnxslim

            LOGGER.info(f"{prefix} slimming with onnxslim {onnxslim.__version__}...")
            model_onnx = onnxslim.slim(model_onnx)

        except Exception as e:
            LOGGER.warning(f"{prefix} simplifier failure: {e}")

    # Metadata
    for k, v in self.metadata.items():
        meta = model_onnx.metadata_props.add()
        meta.key, meta.value = k, str(v)

    onnx.save(model_onnx, f)
    return f, model_onnx

export_openvino

export_openvino(prefix=colorstr('OpenVINO:'))

Export YOLO model to OpenVINO format.

Source code in ultralytics/engine/exporter.py
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
@try_export
def export_openvino(self, prefix=colorstr("OpenVINO:")):
    """Export YOLO model to OpenVINO format."""
    if MACOS:
        msg = "OpenVINO error in macOS>=15.4 https://github.com/openvinotoolkit/openvino/issues/30023"
        check_version(MACOS_VERSION, "<15.4", name="macOS ", hard=True, msg=msg)
    check_requirements("openvino>=2024.0.0")
    import openvino as ov

    LOGGER.info(f"\n{prefix} starting export with openvino {ov.__version__}...")
    assert TORCH_1_13, f"OpenVINO export requires torch>=1.13.0 but torch=={torch.__version__} is installed"
    ov_model = ov.convert_model(
        NMSModel(self.model, self.args) if self.args.nms else self.model,
        input=None if self.args.dynamic else [self.im.shape],
        example_input=self.im,
    )

    def serialize(ov_model, file):
        """Set RT info, serialize, and save metadata YAML."""
        ov_model.set_rt_info("YOLO", ["model_info", "model_type"])
        ov_model.set_rt_info(True, ["model_info", "reverse_input_channels"])
        ov_model.set_rt_info(114, ["model_info", "pad_value"])
        ov_model.set_rt_info([255.0], ["model_info", "scale_values"])
        ov_model.set_rt_info(self.args.iou, ["model_info", "iou_threshold"])
        ov_model.set_rt_info([v.replace(" ", "_") for v in self.model.names.values()], ["model_info", "labels"])
        if self.model.task != "classify":
            ov_model.set_rt_info("fit_to_window_letterbox", ["model_info", "resize_type"])

        ov.save_model(ov_model, file, compress_to_fp16=self.args.half)
        YAML.save(Path(file).parent / "metadata.yaml", self.metadata)  # add metadata.yaml

    if self.args.int8:
        fq = str(self.file).replace(self.file.suffix, f"_int8_openvino_model{os.sep}")
        fq_ov = str(Path(fq) / self.file.with_suffix(".xml").name)
        # INT8 requires nncf, nncf requires packaging>=23.2 https://github.com/openvinotoolkit/nncf/issues/3463
        check_requirements("packaging>=23.2")  # must be installed first to build nncf wheel
        check_requirements("nncf>=2.14.0")
        import nncf

        def transform_fn(data_item) -> np.ndarray:
            """Quantization transform function."""
            data_item: torch.Tensor = data_item["img"] if isinstance(data_item, dict) else data_item
            assert data_item.dtype == torch.uint8, "Input image must be uint8 for the quantization preprocessing"
            im = data_item.numpy().astype(np.float32) / 255.0  # uint8 to fp16/32 and 0-255 to 0.0-1.0
            return np.expand_dims(im, 0) if im.ndim == 3 else im

        # Generate calibration data for integer quantization
        ignored_scope = None
        if isinstance(self.model.model[-1], Detect):
            # Includes all Detect subclasses like Segment, Pose, OBB, WorldDetect, YOLOEDetect
            head_module_name = ".".join(list(self.model.named_modules())[-1][0].split(".")[:2])
            ignored_scope = nncf.IgnoredScope(  # ignore operations
                patterns=[
                    f".*{head_module_name}/.*/Add",
                    f".*{head_module_name}/.*/Sub*",
                    f".*{head_module_name}/.*/Mul*",
                    f".*{head_module_name}/.*/Div*",
                    f".*{head_module_name}\\.dfl.*",
                ],
                types=["Sigmoid"],
            )

        quantized_ov_model = nncf.quantize(
            model=ov_model,
            calibration_dataset=nncf.Dataset(self.get_int8_calibration_dataloader(prefix), transform_fn),
            preset=nncf.QuantizationPreset.MIXED,
            ignored_scope=ignored_scope,
        )
        serialize(quantized_ov_model, fq_ov)
        return fq, None

    f = str(self.file).replace(self.file.suffix, f"_openvino_model{os.sep}")
    f_ov = str(Path(f) / self.file.with_suffix(".xml").name)

    serialize(ov_model, f_ov)
    return f, None

export_paddle

export_paddle(prefix=colorstr('PaddlePaddle:'))

Export YOLO model to PaddlePaddle format.

Source code in ultralytics/engine/exporter.py
732
733
734
735
736
737
738
739
740
741
742
743
744
745
@try_export
def export_paddle(self, prefix=colorstr("PaddlePaddle:")):
    """Export YOLO model to PaddlePaddle format."""
    assert not IS_JETSON, "Jetson Paddle exports not supported yet"
    check_requirements(("paddlepaddle-gpu" if torch.cuda.is_available() else "paddlepaddle>=3.0.0", "x2paddle"))
    import x2paddle  # noqa
    from x2paddle.convert import pytorch2paddle  # noqa

    LOGGER.info(f"\n{prefix} starting export with X2Paddle {x2paddle.__version__}...")
    f = str(self.file).replace(self.file.suffix, f"_paddle_model{os.sep}")

    pytorch2paddle(module=self.model, save_dir=f, jit_type="trace", input_examples=[self.im])  # export
    YAML.save(Path(f) / "metadata.yaml", self.metadata)  # add metadata.yaml
    return f, None

export_pb

export_pb(keras_model, prefix=colorstr('TensorFlow GraphDef:'))

Export YOLO model to TensorFlow GraphDef *.pb format https://github.com/leimao/Frozen-Graph-TensorFlow.

Source code in ultralytics/engine/exporter.py
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
@try_export
def export_pb(self, keras_model, prefix=colorstr("TensorFlow GraphDef:")):
    """Export YOLO model to TensorFlow GraphDef *.pb format https://github.com/leimao/Frozen-Graph-TensorFlow."""
    import tensorflow as tf  # noqa
    from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2  # noqa

    LOGGER.info(f"\n{prefix} starting export with tensorflow {tf.__version__}...")
    f = self.file.with_suffix(".pb")

    m = tf.function(lambda x: keras_model(x))  # full model
    m = m.get_concrete_function(tf.TensorSpec(keras_model.inputs[0].shape, keras_model.inputs[0].dtype))
    frozen_func = convert_variables_to_constants_v2(m)
    frozen_func.graph.as_graph_def()
    tf.io.write_graph(graph_or_graph_def=frozen_func.graph, logdir=str(f.parent), name=f.name, as_text=False)
    return f, None

export_rknn

export_rknn(prefix=colorstr('RKNN:'))

Export YOLO model to RKNN format.

Source code in ultralytics/engine/exporter.py
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
@try_export
def export_rknn(self, prefix=colorstr("RKNN:")):
    """Export YOLO model to RKNN format."""
    LOGGER.info(f"\n{prefix} starting export with rknn-toolkit2...")

    check_requirements("rknn-toolkit2")
    if IS_COLAB:
        # Prevent 'exit' from closing the notebook https://github.com/airockchip/rknn-toolkit2/issues/259
        import builtins

        builtins.exit = lambda: None

    from rknn.api import RKNN

    f, _ = self.export_onnx()
    export_path = Path(f"{Path(f).stem}_rknn_model")
    export_path.mkdir(exist_ok=True)

    rknn = RKNN(verbose=False)
    rknn.config(mean_values=[[0, 0, 0]], std_values=[[255, 255, 255]], target_platform=self.args.name)
    rknn.load_onnx(model=f)
    rknn.build(do_quantization=False)  # TODO: Add quantization support
    f = f.replace(".onnx", f"-{self.args.name}.rknn")
    rknn.export_rknn(f"{export_path / f}")
    YAML.save(export_path / "metadata.yaml", self.metadata)
    return export_path, None

export_saved_model

export_saved_model(prefix=colorstr('TensorFlow SavedModel:'))

Export YOLO model to TensorFlow SavedModel format.

Source code in ultralytics/engine/exporter.py
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
@try_export
def export_saved_model(self, prefix=colorstr("TensorFlow SavedModel:")):
    """Export YOLO model to TensorFlow SavedModel format."""
    cuda = torch.cuda.is_available()
    try:
        import tensorflow as tf  # noqa
    except ImportError:
        check_requirements("tensorflow>=2.0.0")
        import tensorflow as tf  # noqa
    check_requirements(
        (
            "tf_keras",  # required by 'onnx2tf' package
            "sng4onnx>=1.0.1",  # required by 'onnx2tf' package
            "onnx_graphsurgeon>=0.3.26",  # required by 'onnx2tf' package
            "ai-edge-litert>=1.2.0",  # required by 'onnx2tf' package
            "onnx>=1.12.0,<1.18.0",
            "onnx2tf>=1.26.3",
            "onnxslim>=0.1.53",
            "onnxruntime-gpu" if cuda else "onnxruntime",
            "protobuf>=5",
        ),
        cmds="--extra-index-url https://pypi.ngc.nvidia.com",  # onnx_graphsurgeon only on NVIDIA
    )

    LOGGER.info(f"\n{prefix} starting export with tensorflow {tf.__version__}...")
    check_version(
        tf.__version__,
        ">=2.0.0",
        name="tensorflow",
        verbose=True,
        msg="https://github.com/ultralytics/ultralytics/issues/5161",
    )
    f = Path(str(self.file).replace(self.file.suffix, "_saved_model"))
    if f.is_dir():
        shutil.rmtree(f)  # delete output folder

    # Pre-download calibration file to fix https://github.com/PINTO0309/onnx2tf/issues/545
    onnx2tf_file = Path("calibration_image_sample_data_20x128x128x3_float32.npy")
    if not onnx2tf_file.exists():
        attempt_download_asset(f"{onnx2tf_file}.zip", unzip=True, delete=True)

    # Export to ONNX
    self.args.simplify = True
    f_onnx, _ = self.export_onnx()

    # Export to TF
    np_data = None
    if self.args.int8:
        tmp_file = f / "tmp_tflite_int8_calibration_images.npy"  # int8 calibration images file
        if self.args.data:
            f.mkdir()
            images = [batch["img"] for batch in self.get_int8_calibration_dataloader(prefix)]
            images = torch.nn.functional.interpolate(torch.cat(images, 0).float(), size=self.imgsz).permute(
                0, 2, 3, 1
            )
            np.save(str(tmp_file), images.numpy().astype(np.float32))  # BHWC
            np_data = [["images", tmp_file, [[[[0, 0, 0]]]], [[[[255, 255, 255]]]]]]

    import onnx2tf  # scoped for after ONNX export for reduced conflict during import

    LOGGER.info(f"{prefix} starting TFLite export with onnx2tf {onnx2tf.__version__}...")
    keras_model = onnx2tf.convert(
        input_onnx_file_path=f_onnx,
        output_folder_path=str(f),
        not_use_onnxsim=True,
        verbosity="error",  # note INT8-FP16 activation bug https://github.com/ultralytics/ultralytics/issues/15873
        output_integer_quantized_tflite=self.args.int8,
        quant_type="per-tensor",  # "per-tensor" (faster) or "per-channel" (slower but more accurate)
        custom_input_op_name_np_data_path=np_data,
        enable_batchmatmul_unfold=True,  # fix lower no. of detected objects on GPU delegate
        output_signaturedefs=True,  # fix error with Attention block group convolution
        disable_group_convolution=self.args.format == "tfjs",  # fix TF.js error with group convolution
        optimization_for_gpu_delegate=True,
    )
    YAML.save(f / "metadata.yaml", self.metadata)  # add metadata.yaml

    # Remove/rename TFLite models
    if self.args.int8:
        tmp_file.unlink(missing_ok=True)
        for file in f.rglob("*_dynamic_range_quant.tflite"):
            file.rename(file.with_name(file.stem.replace("_dynamic_range_quant", "_int8") + file.suffix))
        for file in f.rglob("*_integer_quant_with_int16_act.tflite"):
            file.unlink()  # delete extra fp16 activation TFLite files

    # Add TFLite metadata
    for file in f.rglob("*.tflite"):
        f.unlink() if "quant_with_int16_act.tflite" in str(f) else self._add_tflite_metadata(file)

    return str(f), keras_model  # or keras_model = tf.saved_model.load(f, tags=None, options=None)

export_tfjs

export_tfjs(prefix=colorstr('TensorFlow.js:'))

Export YOLO model to TensorFlow.js format.

Source code in ultralytics/engine/exporter.py
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
@try_export
def export_tfjs(self, prefix=colorstr("TensorFlow.js:")):
    """Export YOLO model to TensorFlow.js format."""
    check_requirements("tensorflowjs")
    import tensorflow as tf
    import tensorflowjs as tfjs  # noqa

    LOGGER.info(f"\n{prefix} starting export with tensorflowjs {tfjs.__version__}...")
    f = str(self.file).replace(self.file.suffix, "_web_model")  # js dir
    f_pb = str(self.file.with_suffix(".pb"))  # *.pb path

    gd = tf.Graph().as_graph_def()  # TF GraphDef
    with open(f_pb, "rb") as file:
        gd.ParseFromString(file.read())
    outputs = ",".join(gd_outputs(gd))
    LOGGER.info(f"\n{prefix} output node names: {outputs}")

    quantization = "--quantize_float16" if self.args.half else "--quantize_uint8" if self.args.int8 else ""
    with spaces_in_path(f_pb) as fpb_, spaces_in_path(f) as f_:  # exporter can not handle spaces in path
        cmd = (
            "tensorflowjs_converter "
            f'--input_format=tf_frozen_model {quantization} --output_node_names={outputs} "{fpb_}" "{f_}"'
        )
        LOGGER.info(f"{prefix} running '{cmd}'")
        subprocess.run(cmd, shell=True)

    if " " in f:
        LOGGER.warning(f"{prefix} your model may not work correctly with spaces in path '{f}'.")

    # Add metadata
    YAML.save(Path(f) / "metadata.yaml", self.metadata)  # add metadata.yaml
    return f, None

export_tflite

export_tflite(prefix=colorstr('TensorFlow Lite:'))

Export YOLO model to TensorFlow Lite format.

Source code in ultralytics/engine/exporter.py
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
@try_export
def export_tflite(self, prefix=colorstr("TensorFlow Lite:")):
    """Export YOLO model to TensorFlow Lite format."""
    # BUG https://github.com/ultralytics/ultralytics/issues/13436
    import tensorflow as tf  # noqa

    LOGGER.info(f"\n{prefix} starting export with tensorflow {tf.__version__}...")
    saved_model = Path(str(self.file).replace(self.file.suffix, "_saved_model"))
    if self.args.int8:
        f = saved_model / f"{self.file.stem}_int8.tflite"  # fp32 in/out
    elif self.args.half:
        f = saved_model / f"{self.file.stem}_float16.tflite"  # fp32 in/out
    else:
        f = saved_model / f"{self.file.stem}_float32.tflite"
    return str(f), None

export_torchscript

export_torchscript(prefix=colorstr('TorchScript:'))

Export YOLO model to TorchScript format.

Source code in ultralytics/engine/exporter.py
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
@try_export
def export_torchscript(self, prefix=colorstr("TorchScript:")):
    """Export YOLO model to TorchScript format."""
    LOGGER.info(f"\n{prefix} starting export with torch {torch.__version__}...")
    f = self.file.with_suffix(".torchscript")

    ts = torch.jit.trace(NMSModel(self.model, self.args) if self.args.nms else self.model, self.im, strict=False)
    extra_files = {"config.txt": json.dumps(self.metadata)}  # torch._C.ExtraFilesMap()
    if self.args.optimize:  # https://pytorch.org/tutorials/recipes/mobile_interpreter.html
        LOGGER.info(f"{prefix} optimizing for mobile...")
        from torch.utils.mobile_optimizer import optimize_for_mobile

        optimize_for_mobile(ts)._save_for_lite_interpreter(str(f), _extra_files=extra_files)
    else:
        ts.save(str(f), _extra_files=extra_files)
    return f, None

get_int8_calibration_dataloader

get_int8_calibration_dataloader(prefix='')

Build and return a dataloader for calibration of INT8 models.

Source code in ultralytics/engine/exporter.py
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
def get_int8_calibration_dataloader(self, prefix=""):
    """Build and return a dataloader for calibration of INT8 models."""
    LOGGER.info(f"{prefix} collecting INT8 calibration images from 'data={self.args.data}'")
    data = (check_cls_dataset if self.model.task == "classify" else check_det_dataset)(self.args.data)
    # TensorRT INT8 calibration should use 2x batch size
    batch = self.args.batch * (2 if self.args.format == "engine" else 1)
    dataset = YOLODataset(
        data[self.args.split or "val"],
        data=data,
        fraction=self.args.fraction,
        task=self.model.task,
        imgsz=self.imgsz[0],
        augment=False,
        batch_size=batch,
    )
    n = len(dataset)
    if n < self.args.batch:
        raise ValueError(
            f"The calibration dataset ({n} images) must have at least as many images as the batch size "
            f"('batch={self.args.batch}')."
        )
    elif n < 300:
        LOGGER.warning(f"{prefix} >300 images recommended for INT8 calibration, found {n} images.")
    return build_dataloader(dataset, batch=batch, workers=0)  # required for batch loading

run_callbacks

run_callbacks(event: str)

Execute all callbacks for a given event.

Source code in ultralytics/engine/exporter.py
1446
1447
1448
1449
def run_callbacks(self, event: str):
    """Execute all callbacks for a given event."""
    for callback in self.callbacks.get(event, []):
        callback(self)





ultralytics.engine.exporter.IOSDetectModel

IOSDetectModel(model, im)

Bases: Module

Wrap an Ultralytics YOLO model for Apple iOS CoreML export.

Parameters:

Name Type Description Default
model Module

The YOLO model to wrap.

required
im Tensor

Example input tensor with shape (B, C, H, W).

required
Source code in ultralytics/engine/exporter.py
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
def __init__(self, model, im):
    """
    Initialize the IOSDetectModel class with a YOLO model and example image.

    Args:
        model (torch.nn.Module): The YOLO model to wrap.
        im (torch.Tensor): Example input tensor with shape (B, C, H, W).
    """
    super().__init__()
    _, _, h, w = im.shape  # batch, channel, height, width
    self.model = model
    self.nc = len(model.names)  # number of classes
    if w == h:
        self.normalize = 1.0 / w  # scalar
    else:
        self.normalize = torch.tensor([1.0 / w, 1.0 / h, 1.0 / w, 1.0 / h])  # broadcast (slower, smaller)

forward

forward(x)

Normalize predictions of object detection model with input size-dependent factors.

Source code in ultralytics/engine/exporter.py
1472
1473
1474
1475
def forward(self, x):
    """Normalize predictions of object detection model with input size-dependent factors."""
    xywh, cls = self.model(x)[0].transpose(0, 1).split((4, self.nc), 1)
    return cls, xywh * self.normalize  # confidence (3780, 80), coordinates (3780, 4)





ultralytics.engine.exporter.NMSModel

NMSModel(model, args)

Bases: Module

Model wrapper with embedded NMS for Detect, Segment, Pose and OBB.

Parameters:

Name Type Description Default
model Module

The model to wrap with NMS postprocessing.

required
args Namespace

The export arguments.

required
Source code in ultralytics/engine/exporter.py
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
def __init__(self, model, args):
    """
    Initialize the NMSModel.

    Args:
        model (torch.nn.Module): The model to wrap with NMS postprocessing.
        args (Namespace): The export arguments.
    """
    super().__init__()
    self.model = model
    self.args = args
    self.obb = model.task == "obb"
    self.is_tf = self.args.format in frozenset({"saved_model", "tflite", "tfjs"})

forward

forward(x)

Perform inference with NMS post-processing. Supports Detect, Segment, OBB and Pose.

Parameters:

Name Type Description Default
x Tensor

The preprocessed tensor with shape (N, 3, H, W).

required

Returns:

Type Description
Tensor

List of detections, each an (N, max_det, 4 + 2 + extra_shape) Tensor where N is the number of detections after NMS.

Source code in ultralytics/engine/exporter.py
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
def forward(self, x):
    """
    Perform inference with NMS post-processing. Supports Detect, Segment, OBB and Pose.

    Args:
        x (torch.Tensor): The preprocessed tensor with shape (N, 3, H, W).

    Returns:
        (torch.Tensor): List of detections, each an (N, max_det, 4 + 2 + extra_shape) Tensor where N is the
            number of detections after NMS.
    """
    from functools import partial

    from torchvision.ops import nms

    preds = self.model(x)
    pred = preds[0] if isinstance(preds, tuple) else preds
    kwargs = dict(device=pred.device, dtype=pred.dtype)
    bs = pred.shape[0]
    pred = pred.transpose(-1, -2)  # shape(1,84,6300) to shape(1,6300,84)
    extra_shape = pred.shape[-1] - (4 + len(self.model.names))  # extras from Segment, OBB, Pose
    if self.args.dynamic and self.args.batch > 1:  # batch size needs to always be same due to loop unroll
        pad = torch.zeros(torch.max(torch.tensor(self.args.batch - bs), torch.tensor(0)), *pred.shape[1:], **kwargs)
        pred = torch.cat((pred, pad))
    boxes, scores, extras = pred.split([4, len(self.model.names), extra_shape], dim=2)
    scores, classes = scores.max(dim=-1)
    self.args.max_det = min(pred.shape[1], self.args.max_det)  # in case num_anchors < max_det
    # (N, max_det, 4 coords + 1 class score + 1 class label + extra_shape).
    out = torch.zeros(bs, self.args.max_det, boxes.shape[-1] + 2 + extra_shape, **kwargs)
    for i in range(bs):
        box, cls, score, extra = boxes[i], classes[i], scores[i], extras[i]
        mask = score > self.args.conf
        if self.is_tf:
            # TFLite GatherND error if mask is empty
            score *= mask
            # Explicit length otherwise reshape error, hardcoded to `self.args.max_det * 5`
            mask = score.topk(min(self.args.max_det * 5, score.shape[0])).indices
        box, score, cls, extra = box[mask], score[mask], cls[mask], extra[mask]
        nmsbox = box.clone()
        # `8` is the minimum value experimented to get correct NMS results for obb
        multiplier = 8 if self.obb else 1
        # Normalize boxes for NMS since large values for class offset causes issue with int8 quantization
        if self.args.format == "tflite":  # TFLite is already normalized
            nmsbox *= multiplier
        else:
            nmsbox = multiplier * nmsbox / torch.tensor(x.shape[2:], **kwargs).max()
        if not self.args.agnostic_nms:  # class-specific NMS
            end = 2 if self.obb else 4
            # fully explicit expansion otherwise reshape error
            # large max_wh causes issues when quantizing
            cls_offset = cls.reshape(-1, 1).expand(nmsbox.shape[0], end)
            offbox = nmsbox[:, :end] + cls_offset * multiplier
            nmsbox = torch.cat((offbox, nmsbox[:, end:]), dim=-1)
        nms_fn = (
            partial(
                nms_rotated,
                use_triu=not (
                    self.is_tf
                    or (self.args.opset or 14) < 14
                    or (self.args.format == "openvino" and self.args.int8)  # OpenVINO int8 error with triu
                ),
            )
            if self.obb
            else nms
        )
        keep = nms_fn(
            torch.cat([nmsbox, extra], dim=-1) if self.obb else nmsbox,
            score,
            self.args.iou,
        )[: self.args.max_det]
        dets = torch.cat(
            [box[keep], score[keep].view(-1, 1), cls[keep].view(-1, 1).to(out.dtype), extra[keep]], dim=-1
        )
        # Zero-pad to max_det size to avoid reshape error
        pad = (0, 0, 0, self.args.max_det - dets.shape[0])
        out[i] = torch.nn.functional.pad(dets, pad)
    return (out[:bs], preds[1]) if self.model.task == "segment" else out[:bs]





ultralytics.engine.exporter.export_formats

export_formats()

Return a dictionary of Ultralytics YOLO export formats.

Source code in ultralytics/engine/exporter.py
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
def export_formats():
    """Return a dictionary of Ultralytics YOLO export formats."""
    x = [
        ["PyTorch", "-", ".pt", True, True, []],
        ["TorchScript", "torchscript", ".torchscript", True, True, ["batch", "optimize", "half", "nms"]],
        ["ONNX", "onnx", ".onnx", True, True, ["batch", "dynamic", "half", "opset", "simplify", "nms"]],
        [
            "OpenVINO",
            "openvino",
            "_openvino_model",
            True,
            False,
            ["batch", "dynamic", "half", "int8", "nms", "fraction"],
        ],
        [
            "TensorRT",
            "engine",
            ".engine",
            False,
            True,
            ["batch", "dynamic", "half", "int8", "simplify", "nms", "fraction"],
        ],
        ["CoreML", "coreml", ".mlpackage", True, False, ["batch", "half", "int8", "nms"]],
        ["TensorFlow SavedModel", "saved_model", "_saved_model", True, True, ["batch", "int8", "keras", "nms"]],
        ["TensorFlow GraphDef", "pb", ".pb", True, True, ["batch"]],
        ["TensorFlow Lite", "tflite", ".tflite", True, False, ["batch", "half", "int8", "nms", "fraction"]],
        ["TensorFlow Edge TPU", "edgetpu", "_edgetpu.tflite", True, False, []],
        ["TensorFlow.js", "tfjs", "_web_model", True, False, ["batch", "half", "int8", "nms"]],
        ["PaddlePaddle", "paddle", "_paddle_model", True, True, ["batch"]],
        ["MNN", "mnn", ".mnn", True, True, ["batch", "half", "int8"]],
        ["NCNN", "ncnn", "_ncnn_model", True, True, ["batch", "half"]],
        ["IMX", "imx", "_imx_model", True, True, ["int8", "fraction"]],
        ["RKNN", "rknn", "_rknn_model", False, False, ["batch", "name"]],
    ]
    return dict(zip(["Format", "Argument", "Suffix", "CPU", "GPU", "Arguments"], zip(*x)))





ultralytics.engine.exporter.validate_args

validate_args(format, passed_args, valid_args)

Validate arguments based on the export format.

Parameters:

Name Type Description Default
format str

The export format.

required
passed_args Namespace

The arguments used during export.

required
valid_args list

List of valid arguments for the format.

required

Raises:

Type Description
AssertionError

If an unsupported argument is used, or if the format lacks supported argument listings.

Source code in ultralytics/engine/exporter.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
def validate_args(format, passed_args, valid_args):
    """
    Validate arguments based on the export format.

    Args:
        format (str): The export format.
        passed_args (Namespace): The arguments used during export.
        valid_args (list): List of valid arguments for the format.

    Raises:
        AssertionError: If an unsupported argument is used, or if the format lacks supported argument listings.
    """
    export_args = ["half", "int8", "dynamic", "keras", "nms", "batch", "fraction"]

    assert valid_args is not None, f"ERROR ❌️ valid arguments for '{format}' not listed."
    custom = {"batch": 1, "data": None, "device": None}  # exporter defaults
    default_args = get_cfg(DEFAULT_CFG, custom)
    for arg in export_args:
        not_default = getattr(passed_args, arg, None) != getattr(default_args, arg, None)
        if not_default:
            assert arg in valid_args, f"ERROR ❌️ argument '{arg}' is not supported for format='{format}'"





ultralytics.engine.exporter.gd_outputs

gd_outputs(gd)

Return TensorFlow GraphDef model output node names.

Source code in ultralytics/engine/exporter.py
173
174
175
176
177
178
179
def gd_outputs(gd):
    """Return TensorFlow GraphDef model output node names."""
    name_list, input_list = [], []
    for node in gd.node:  # tensorflow.core.framework.node_def_pb2.NodeDef
        name_list.append(node.name)
        input_list.extend(node.input)
    return sorted(f"{x}:0" for x in list(set(name_list) - set(input_list)) if not x.startswith("NoOp"))





ultralytics.engine.exporter.try_export

try_export(inner_func)

YOLO export decorator, i.e. @try_export.

Source code in ultralytics/engine/exporter.py
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
def try_export(inner_func):
    """YOLO export decorator, i.e. @try_export."""
    inner_args = get_default_args(inner_func)

    def outer_func(*args, **kwargs):
        """Export a model."""
        prefix = inner_args["prefix"]
        dt = 0.0
        try:
            with Profile() as dt:
                f, model = inner_func(*args, **kwargs)
            LOGGER.info(f"{prefix} export success ✅ {dt.t:.1f}s, saved as '{f}' ({file_size(f):.1f} MB)")
            return f, model
        except Exception as e:
            LOGGER.error(f"{prefix} export failure {dt.t:.1f}s: {e}")
            raise e

    return outer_func





ultralytics.engine.exporter.arange_patch

arange_patch(args)

Workaround for ONNX torch.arange incompatibility with FP16.

https://github.com/pytorch/pytorch/issues/148041.

Source code in ultralytics/engine/exporter.py
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
@contextmanager
def arange_patch(args):
    """
    Workaround for ONNX torch.arange incompatibility with FP16.

    https://github.com/pytorch/pytorch/issues/148041.
    """
    if args.dynamic and args.half and args.format == "onnx":
        func = torch.arange

        def arange(*args, dtype=None, **kwargs):
            """Return a 1-D tensor of size with values from the interval and common difference."""
            return func(*args, **kwargs).to(dtype)  # cast to dtype instead of passing dtype

        torch.arange = arange  # patch
        yield
        torch.arange = func  # unpatch
    else:
        yield





📅 Created 1 year ago ✏️ Updated 2 months ago