Skip to content

cli

sleap_nn.export.cli

CLI entry points for export workflows.

Functions:

Name Description
export

Export trained models to ONNX/TensorRT formats.

export(model_paths, output, fmt, opset_version, max_instances, max_batch_size, input_scale, input_height, input_width, crop_size, max_peaks_per_node, n_line_points, max_edge_length_ratio, dist_penalty_weight, device, precision, peak_threshold, workspace_size_gb, verify)

Export trained models to ONNX/TensorRT formats.

Source code in sleap_nn/export/cli.py
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 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
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 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
 654
 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
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 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
 839
 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
 918
 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
 954
 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
@click.command(context_settings=CONTEXT_SETTINGS)
@click.argument(
    "model_paths",
    nargs=-1,
    type=click.Path(exists=True, file_okay=False, path_type=Path),
)
@click.option(
    "--output",
    "-o",
    type=click.Path(file_okay=False, path_type=Path),
    default=None,
    help="Output directory for exported model files.",
)
@click.option(
    "--format",
    "-f",
    "fmt",
    type=click.Choice(["onnx", "tensorrt", "both"], case_sensitive=False),
    default="onnx",
    show_default=True,
)
@click.option("--opset-version", type=int, default=17, show_default=True)
@click.option("--max-instances", type=int, default=20, show_default=True)
@click.option("--max-batch-size", type=int, default=8, show_default=True)
@click.option("--input-scale", type=float, default=None)
@click.option("--input-height", type=int, default=None)
@click.option("--input-width", type=int, default=None)
@click.option("--crop-size", type=int, default=None)
@click.option("--max-peaks-per-node", type=int, default=20, show_default=True)
@click.option("--n-line-points", type=int, default=10, show_default=True)
@click.option("--max-edge-length-ratio", type=float, default=0.25, show_default=True)
@click.option("--dist-penalty-weight", type=float, default=1.0, show_default=True)
@click.option("--device", type=str, default="cpu", show_default=True)
@click.option(
    "--precision",
    type=click.Choice(["fp32", "fp16", "tf32"], case_sensitive=False),
    default="fp16",
    show_default=True,
    help="TensorRT precision mode.",
)
@click.option(
    "--peak-threshold",
    type=float,
    default=0.2,
    show_default=True,
    help="Minimum confidence threshold for peak detection (baked into ONNX graph).",
)
@click.option(
    "--workspace-size-gb",
    type=float,
    default=None,
    help="TensorRT builder workspace size in GB. Uses exporter default if unset.",
)
@click.option("--verify/--no-verify", default=True, show_default=True)
def export(
    model_paths: tuple[Path, ...],
    output: Optional[Path],
    fmt: str,
    opset_version: int,
    max_instances: int,
    max_batch_size: int,
    input_scale: Optional[float],
    input_height: Optional[int],
    input_width: Optional[int],
    crop_size: Optional[int],
    max_peaks_per_node: int,
    n_line_points: int,
    max_edge_length_ratio: float,
    dist_penalty_weight: float,
    device: str,
    precision: str,
    peak_threshold: float,
    workspace_size_gb: Optional[float],
    verify: bool,
) -> None:
    """Export trained models to ONNX/TensorRT formats."""
    import torch

    from sleap_nn.export.exporters import export_to_onnx, export_to_tensorrt
    from sleap_nn.export.exporters.onnx_exporter import has_rope
    from sleap_nn.export.metadata import (
        build_base_metadata,
        embed_metadata_in_onnx,
        hash_file,
    )
    from sleap_nn.export.utils import (
        load_training_config,
        warn_on_tiled_export,
        resolve_anchor_part,
        resolve_backbone_source,
        resolve_centroid_method,
        resolve_backbone_type,
        resolve_background_fill,
        resolve_burn_in,
        resolve_class_maps_output_stride,
        resolve_class_names,
        resolve_crop_centering,
        resolve_crop_size,
        resolve_edge_inds,
        resolve_embedding_dim,
        resolve_embedding_input_channels,
        resolve_input_channels,
        resolve_input_scale,
        resolve_input_shape,
        resolve_max_hw,
        resolve_model_type,
        resolve_n_classes,
        resolve_node_names,
        resolve_normalize,
        resolve_output_stride,
        resolve_pafs_output_stride,
    )
    from sleap_nn.export.wrappers import (
        BBoxONNXWrapper,
        BottomUpMultiClassONNXWrapper,
        BottomUpONNXWrapper,
        CenteredInstanceONNXWrapper,
        CentroidONNXWrapper,
        EmbeddingONNXWrapper,
        SingleInstanceONNXWrapper,
        TopDownMultiClassCombinedONNXWrapper,
        TopDownMultiClassONNXWrapper,
        TopDownONNXWrapper,
    )

    fmt = fmt.lower()

    trt_workspace_kwargs = (
        {"workspace_size": int(workspace_size_gb * (1 << 30))}
        if workspace_size_gb is not None
        else {}
    )

    if not model_paths:
        raise click.ClickException("Provide at least one model path to export.")

    model_paths = list(model_paths)
    cfgs = [load_training_config(path) for path in model_paths]
    for cfg in cfgs:
        warn_on_tiled_export(cfg)
    model_types = [resolve_model_type(cfg) for cfg in cfgs]
    backbone_types = [resolve_backbone_type(cfg) for cfg in cfgs]

    # A standalone centroid model is exported from a SINGLE directory; passing two
    # centroid dirs is almost always a mistake (the user likely meant centroid +
    # centered_instance for a top-down bundle). Catch it with a clear message
    # rather than letting it fall through to the generic combination error.
    if len(model_paths) == 2 and all(mt == "centroid" for mt in model_types):
        raise click.ClickException(
            "Received two centroid model directories. A standalone centroid model "
            "is exported from a single directory; a top-down bundle pairs a centroid "
            "directory with a centered_instance (or multi_class_topdown) directory. "
            "Pass one centroid directory for a standalone centroid export."
        )

    if len(model_paths) >= 2 and {"bbox_centernet", "bbox_ssd"} & set(model_types):
        raise click.ClickException(
            "A bounding-box detector + top-down model pair cannot be exported as one "
            "bundle yet. Export each directory on its own (the detector exports its "
            "network; the centered-instance model exports a network that takes "
            "crops), or run the pair from its checkpoints with `sleap-nn predict "
            "--model_paths <bbox_dir> --model_paths <centered_instance_dir>`."
        )

    from sleap_nn.data.instance_cropping import BBoxCropSpec

    box_scaled = [p for p, c in zip(model_paths, cfgs) if BBoxCropSpec.from_config(c)]
    if box_scaled and len(model_paths) >= 2:
        raise click.ClickException(
            f"{box_scaled[0]} crops each instance's bounding box "
            "(preprocessing.crop_mode='bbox'); combined top-down bundles cut fixed "
            "windows around centroids, so it cannot be exported in one. Export it "
            "alone (the network takes crops) or run it with a box detector via "
            "`sleap-nn predict`."
        )
    if box_scaled:
        logger.warning(
            f"{box_scaled[0]} is a box-scaled centered-instance model "
            "(preprocessing.crop_mode='bbox'): the exported network takes crops "
            "cut by its crop rule, which is recorded in the exported "
            "training_config (data_config.preprocessing.crop_*)."
        )

    if len(model_paths) == 1:
        model_path = model_paths[0]
        cfg = cfgs[0]
        model_type = model_types[0]
        backbone_type = backbone_types[0]

        if model_type not in (
            "centroid",
            "centered_instance",
            "bottomup",
            "single_instance",
            "multi_class_topdown",
            "multi_class_bottomup",
            "embedding",
            "bbox_centernet",
            "bbox_ssd",
        ):
            raise click.ClickException(
                f"Model type '{model_type}' is not supported for export yet."
            )

        ckpt_path = model_path / "best.ckpt"
        if not ckpt_path.exists():
            raise click.ClickException(f"Checkpoint not found: {ckpt_path}")

        lightning_model = _load_lightning_model(
            model_type=model_type,
            backbone_type=backbone_type,
            cfg=cfg,
            ckpt_path=ckpt_path,
            device=device,
        )

        torch_model = lightning_model.model
        torch_model.eval()
        torch_model.to(device)

        export_dir = output or (model_path / "exported")
        export_dir.mkdir(parents=True, exist_ok=True)

        resolved_scale = (
            input_scale if input_scale is not None else resolve_input_scale(cfg)
        )
        output_stride = resolve_output_stride(cfg, model_type)
        resolved_crop_size = (
            (crop_size, crop_size) if crop_size is not None else resolve_crop_size(cfg)
        )
        metadata_max_instances = None
        metadata_max_peaks = None
        metadata_n_classes = None
        metadata_class_names = None
        metadata_embedding_dim = None
        metadata_normalize = None
        metadata_backbone_source = None
        metadata_burn_in = None
        metadata_background_fill = None
        metadata_crop_centering = None
        metadata_max_hw = (None, None)
        metadata_normalization = "0_to_1"

        if model_type == "centroid":
            wrapper = CentroidONNXWrapper(
                torch_model,
                max_instances=max_instances,
                output_stride=output_stride,
                input_scale=resolved_scale,
                peak_threshold=peak_threshold,
            )
            output_names = ["centroids", "centroid_vals", "instance_valid"]
            metadata_max_instances = max_instances
            node_names = resolve_node_names(cfg, model_type)
            edge_inds = resolve_edge_inds(cfg, node_names)
        elif model_type == "centered_instance":
            wrapper = CenteredInstanceONNXWrapper(
                torch_model,
                output_stride=output_stride,
                input_scale=resolved_scale,
                peak_threshold=peak_threshold,
            )
            output_names = ["peaks", "peak_vals"]
            node_names = resolve_node_names(cfg, model_type)
            edge_inds = resolve_edge_inds(cfg, node_names)
        elif model_type == "bottomup":
            node_names = resolve_node_names(cfg, model_type)
            edge_inds = resolve_edge_inds(cfg, node_names)
            pafs_output_stride = resolve_pafs_output_stride(cfg)
            wrapper = BottomUpONNXWrapper(
                torch_model,
                skeleton_edges=edge_inds,
                n_nodes=len(node_names),
                max_peaks_per_node=max_peaks_per_node,
                n_line_points=n_line_points,
                cms_output_stride=output_stride,
                pafs_output_stride=pafs_output_stride,
                max_edge_length_ratio=max_edge_length_ratio,
                dist_penalty_weight=dist_penalty_weight,
                input_scale=resolved_scale,
                peak_threshold=peak_threshold,
            )
            output_names = [
                "peaks",
                "peak_vals",
                "peak_mask",
                "line_scores",
                "candidate_mask",
            ]
            metadata_max_peaks = max_peaks_per_node
        elif model_type == "single_instance":
            wrapper = SingleInstanceONNXWrapper(
                torch_model,
                output_stride=output_stride,
                input_scale=resolved_scale,
                peak_threshold=peak_threshold,
            )
            output_names = ["peaks", "peak_vals"]
            node_names = resolve_node_names(cfg, model_type)
            edge_inds = resolve_edge_inds(cfg, node_names)
        elif model_type == "multi_class_topdown":
            n_classes = resolve_n_classes(cfg, model_type)
            class_names = resolve_class_names(cfg, model_type)
            wrapper = TopDownMultiClassONNXWrapper(
                torch_model,
                output_stride=output_stride,
                input_scale=resolved_scale,
                n_classes=n_classes,
                peak_threshold=peak_threshold,
            )
            output_names = ["peaks", "peak_vals", "class_logits"]
            node_names = resolve_node_names(cfg, model_type)
            edge_inds = resolve_edge_inds(cfg, node_names)
            metadata_n_classes = n_classes
            metadata_class_names = class_names
        elif model_type == "multi_class_bottomup":
            node_names = resolve_node_names(cfg, model_type)
            edge_inds = resolve_edge_inds(cfg, node_names)
            n_classes = resolve_n_classes(cfg, model_type)
            class_names = resolve_class_names(cfg, model_type)
            class_maps_output_stride = resolve_class_maps_output_stride(cfg)
            wrapper = BottomUpMultiClassONNXWrapper(
                torch_model,
                n_nodes=len(node_names),
                n_classes=n_classes,
                max_peaks_per_node=max_peaks_per_node,
                cms_output_stride=output_stride,
                class_maps_output_stride=class_maps_output_stride,
                input_scale=resolved_scale,
                peak_threshold=peak_threshold,
            )
            output_names = ["peaks", "peak_vals", "peak_mask", "class_probs"]
            metadata_max_peaks = max_peaks_per_node
            metadata_n_classes = n_classes
            metadata_class_names = class_names
        elif model_type == "embedding":
            # Re-ID head: crop -> appearance vector. Simplest wrapper (single output,
            # no peak finding, no skeleton). Input is the grayscale crop the embedder
            # standardizes per-crop (the wrapper does not /255).
            wrapper = EmbeddingONNXWrapper(
                torch_model, normalize=resolve_normalize(cfg)
            )
            output_names = ["embedding"]
            # No skeleton semantics for an appearance model.
            node_names = []
            edge_inds = []
            metadata_embedding_dim = resolve_embedding_dim(cfg)
            metadata_normalize = resolve_normalize(cfg)
            metadata_backbone_source = resolve_backbone_source(cfg)
            metadata_burn_in = resolve_burn_in(cfg)
            metadata_background_fill = resolve_background_fill(cfg)
            metadata_crop_centering = resolve_crop_centering(cfg)
            metadata_max_hw = resolve_max_hw(cfg)
            metadata_normalization = "per_crop_standardize"
            trained_crop = resolve_crop_size(cfg)
            if crop_size is not None and trained_crop != (crop_size, crop_size):
                # Native inference always crops at the TRAINED size (it rejects
                # overriding it); an export at another size feeds the embedder
                # crops it was never fitted on.
                logger.warning(
                    f"--crop-size {crop_size} does not match the crop size this "
                    f"embedding model was trained on ({trained_crop}). The exported "
                    "graph will embed crops of a different size -- the animal "
                    "appears at a different scale than in training -- and its "
                    "vectors will not match native `sleap-nn predict` inference. "
                    "Omit --crop-size to export at the trained size."
                )
            if metadata_burn_in:
                # The single-input ONNX graph standardizes over the WHOLE crop; a
                # burn_in model's native inference standardizes over the foreground
                # (mask) only and fills the background. The exported embeddings will
                # therefore DIVERGE from native masked inference. Recorded in metadata.
                logger.warning(
                    "Exporting a mask-burn-in embedding model "
                    f"(background_fill='{metadata_background_fill}'): the ONNX graph "
                    "does a MASKLESS whole-crop standardize and cannot reproduce the "
                    "masked (foreground-only) standardize used at training/native "
                    "inference, so exported embeddings will diverge. Use the native "
                    "`sleap-nn predict ... --save_embeddings slp` path for exact "
                    "parity, or train/export a burn_in=False model for a faithful "
                    "single-input ONNX embedder."
                )
        elif model_type in ("bbox_centernet", "bbox_ssd"):
            # Detectors export the network only (decoder-ready head outputs);
            # `ExportedBBoxLayer` decodes with the same BBoxDecoder as the
            # checkpoint path, rebuilt from the copied training config.
            from sleap_nn.data.bboxes import bbox_head_categories
            from sleap_nn.export.wrappers.bbox import BBOX_EXPORT_OUTPUTS

            # The graph takes model-input-sized frames; `ExportedBBoxLayer` applies
            # `input_scale` and the stride padding on the host (the metadata keeps
            # the scale for it).
            wrapper = BBoxONNXWrapper(torch_model, model_type=model_type)
            output_names = BBOX_EXPORT_OUTPUTS[model_type]
            node_names = []
            edge_inds = []
            metadata_class_names = bbox_head_categories(
                model_type, cfg.model_config.head_configs
            )
            metadata_n_classes = len(metadata_class_names)
            metadata_max_instances = max_instances
            metadata_max_hw = resolve_max_hw(cfg)
        else:
            raise click.ClickException(
                f"Model type '{model_type}' is not supported for export yet."
            )

        wrapper.eval()
        wrapper.to(device)

        input_shape = resolve_input_shape(
            cfg, input_height=input_height, input_width=input_width
        )
        input_channels = resolve_input_channels(cfg)
        if model_type == "embedding":
            # The embedder consumes a fixed-size grayscale CROP, not a full frame:
            # size the export input from the crop size + the (grayscale) data
            # channels. A 3ch ImageNet backbone repeats gray->3ch internally.
            if resolved_crop_size is None:
                raise click.ClickException(
                    "Embedding export requires a crop size. Provide --crop-size or "
                    "set data_config.preprocessing.crop_size."
                )
            crop_h, crop_w = resolved_crop_size
            input_channels = resolve_embedding_input_channels(cfg)
            input_shape = (1, input_channels, crop_h, crop_w)
        elif (
            model_type in ("centered_instance", "multi_class_topdown")
            and resolved_crop_size is not None
            and input_height is None
            and input_width is None
        ):
            # These wrappers take crops: trace at the crop size, the only size the
            # model sees -- and the size a RoPE backbone's static-H/W export fixes.
            input_shape = (input_shape[0], input_shape[1], *resolved_crop_size)
        if model_type in ("bbox_centernet", "bbox_ssd"):
            # Trace on a model-input-sized frame (scaled, stride-padded).
            max_stride = int(
                cfg.model_config.backbone_config[backbone_type]["max_stride"]
            )
            _, c, h, w = input_shape
            h = math.ceil(h * resolved_scale / max_stride) * max_stride
            w = math.ceil(w * resolved_scale / max_stride) * max_stride
            input_shape = (1, c, h, w)
        model_out_path = export_dir / "model.onnx"

        export_to_onnx(
            wrapper,
            model_out_path,
            input_shape=input_shape,
            input_dtype=torch.uint8,
            opset_version=opset_version,
            output_names=output_names,
            verify=verify,
        )

        training_config_path = _copy_training_config(model_path, export_dir, None)
        if training_config_path is not None:
            training_config_hash = hash_file(training_config_path)
            training_config_text = training_config_path.read_text()
        else:
            training_config_hash = ""
            training_config_text = None

        metadata = build_base_metadata(
            export_format="onnx",
            model_type=model_type,
            model_name=model_path.name,
            checkpoint_path=str(ckpt_path),
            backbone=backbone_type,
            n_nodes=len(node_names),
            n_edges=len(edge_inds),
            node_names=node_names,
            edge_inds=edge_inds,
            input_scale=resolved_scale,
            input_channels=input_channels,
            output_stride=output_stride,
            crop_size=resolved_crop_size,
            max_instances=metadata_max_instances,
            max_peaks_per_node=metadata_max_peaks,
            max_batch_size=max_batch_size,
            precision="fp32",
            training_config_hash=training_config_hash,
            training_config_embedded=training_config_text is not None,
            input_dtype="uint8",
            normalization=metadata_normalization,
            n_classes=metadata_n_classes,
            class_names=metadata_class_names,
            peak_threshold=peak_threshold,
            anchor_part=resolve_anchor_part(cfg, model_type),
            embedding_dim=metadata_embedding_dim,
            normalize=metadata_normalize,
            backbone_source=metadata_backbone_source,
            burn_in=metadata_burn_in,
            background_fill=metadata_background_fill,
            centroid_method=resolve_centroid_method(cfg, model_type),
            crop_centering=metadata_crop_centering,
            max_height=metadata_max_hw[0],
            max_width=metadata_max_hw[1],
        )

        metadata.save(export_dir / "export_metadata.json")

        if training_config_text is not None:
            try:
                embed_metadata_in_onnx(model_out_path, metadata, training_config_text)
            except ImportError:
                pass

        # Export to TensorRT if requested
        if fmt in ("tensorrt", "both"):
            trt_out_path = export_dir / "model.trt"
            B, C, H, W = input_shape

            # For centered_instance, single_instance, and embedding models, use the
            # crop size for TensorRT shape profiles since inference uses cropped inputs
            if model_type in ("centered_instance", "single_instance", "embedding"):
                if resolved_crop_size is not None:
                    crop_h, crop_w = resolved_crop_size
                    trt_input_shape = (1, C, crop_h, crop_w)
                    # Use crop size for min/opt, allow flexibility for max
                    trt_min_shape = (1, C, crop_h, crop_w)
                    trt_opt_shape = (1, C, crop_h, crop_w)
                    trt_max_shape = (max_batch_size, C, crop_h * 2, crop_w * 2)
                else:
                    trt_input_shape = input_shape
                    trt_min_shape = None
                    trt_opt_shape = None
                    trt_max_shape = (max_batch_size, C, H * 2, W * 2)
            else:
                trt_input_shape = input_shape
                trt_min_shape = None
                trt_opt_shape = None
                trt_max_shape = (max_batch_size, C, H * 2, W * 2)

            export_to_tensorrt(
                wrapper,
                trt_out_path,
                input_shape=trt_input_shape,
                input_dtype=torch.uint8,
                precision=precision,
                min_shape=trt_min_shape,
                opt_shape=trt_opt_shape,
                max_shape=trt_max_shape,
                **trt_workspace_kwargs,
                verbose=True,
            )
            # Update metadata for TensorRT
            trt_metadata = build_base_metadata(
                export_format="tensorrt",
                model_type=model_type,
                model_name=model_path.name,
                checkpoint_path=str(ckpt_path),
                backbone=backbone_type,
                n_nodes=len(node_names),
                n_edges=len(edge_inds),
                node_names=node_names,
                edge_inds=edge_inds,
                input_scale=resolved_scale,
                input_channels=input_channels,
                output_stride=output_stride,
                crop_size=resolved_crop_size,
                max_instances=metadata_max_instances,
                max_peaks_per_node=metadata_max_peaks,
                max_batch_size=max_batch_size,
                precision=precision,
                training_config_hash=training_config_hash,
                training_config_embedded=training_config_text is not None,
                input_dtype="uint8",
                normalization=metadata_normalization,
                n_classes=metadata_n_classes,
                class_names=metadata_class_names,
                peak_threshold=peak_threshold,
                anchor_part=resolve_anchor_part(cfg, model_type),
                embedding_dim=metadata_embedding_dim,
                normalize=metadata_normalize,
                backbone_source=metadata_backbone_source,
                burn_in=metadata_burn_in,
                background_fill=metadata_background_fill,
                centroid_method=resolve_centroid_method(cfg, model_type),
                crop_centering=metadata_crop_centering,
                max_height=metadata_max_hw[0],
                max_width=metadata_max_hw[1],
            )
            trt_metadata.save(export_dir / "model.trt.metadata.json")
        return

    if len(model_paths) == 2 and set(model_types) == {
        "centroid",
        "centered_instance",
    }:
        centroid_idx = model_types.index("centroid")
        instance_idx = model_types.index("centered_instance")

        centroid_path = model_paths[centroid_idx]
        instance_path = model_paths[instance_idx]
        centroid_cfg = cfgs[centroid_idx]
        instance_cfg = cfgs[instance_idx]
        centroid_backbone = backbone_types[centroid_idx]
        instance_backbone = backbone_types[instance_idx]

        centroid_ckpt = centroid_path / "best.ckpt"
        instance_ckpt = instance_path / "best.ckpt"
        if not centroid_ckpt.exists():
            raise click.ClickException(f"Checkpoint not found: {centroid_ckpt}")
        if not instance_ckpt.exists():
            raise click.ClickException(f"Checkpoint not found: {instance_ckpt}")

        centroid_model = _load_lightning_model(
            model_type="centroid",
            backbone_type=centroid_backbone,
            cfg=centroid_cfg,
            ckpt_path=centroid_ckpt,
            device=device,
        ).model
        instance_model = _load_lightning_model(
            model_type="centered_instance",
            backbone_type=instance_backbone,
            cfg=instance_cfg,
            ckpt_path=instance_ckpt,
            device=device,
        ).model

        centroid_model.eval()
        instance_model.eval()
        centroid_model.to(device)
        instance_model.to(device)

        export_dir = output or (centroid_path / "exported_topdown")
        export_dir.mkdir(parents=True, exist_ok=True)

        centroid_scale = (
            input_scale
            if input_scale is not None
            else resolve_input_scale(centroid_cfg)
        )
        instance_scale = (
            input_scale
            if input_scale is not None
            else resolve_input_scale(instance_cfg)
        )
        centroid_stride = resolve_output_stride(centroid_cfg, "centroid")
        instance_stride = resolve_output_stride(instance_cfg, "centered_instance")

        resolved_crop = resolve_crop_size(instance_cfg)
        if crop_size is not None:
            resolved_crop = (crop_size, crop_size)
        if resolved_crop is None:
            raise click.ClickException(
                "Top-down export requires crop_size. Provide --crop-size or ensure "
                "data_config.preprocessing.crop_size is set."
            )

        node_names = resolve_node_names(instance_cfg, "centered_instance")
        edge_inds = resolve_edge_inds(instance_cfg, node_names)

        wrapper = TopDownONNXWrapper(
            centroid_model=centroid_model,
            instance_model=instance_model,
            max_instances=max_instances,
            crop_size=resolved_crop,
            centroid_output_stride=centroid_stride,
            instance_output_stride=instance_stride,
            centroid_input_scale=centroid_scale,
            instance_input_scale=instance_scale,
            n_nodes=len(node_names),
            centroid_peak_threshold=peak_threshold,
            instance_peak_threshold=peak_threshold,
        )
        wrapper.eval()
        wrapper.to(device)

        input_shape = resolve_input_shape(
            centroid_cfg, input_height=input_height, input_width=input_width
        )
        model_out_path = export_dir / "model.onnx"

        export_to_onnx(
            wrapper,
            model_out_path,
            input_shape=input_shape,
            input_dtype=torch.uint8,
            opset_version=opset_version,
            output_names=[
                "centroids",
                "centroid_vals",
                "peaks",
                "peak_vals",
                "instance_valid",
            ],
            verify=verify,
            # Only the centroid model sees the frame size; the instance model
            # always receives fixed-size crops, so its RoPE grid never changes.
            static_hw=has_rope(centroid_model),
        )

        centroid_cfg_path = _copy_training_config(centroid_path, export_dir, "centroid")
        instance_cfg_path = _copy_training_config(
            instance_path, export_dir, "centered_instance"
        )
        config_payload = {}
        config_hashes = []
        if centroid_cfg_path is not None:
            config_payload["centroid"] = centroid_cfg_path.read_text()
            config_hashes.append(f"centroid:{hash_file(centroid_cfg_path)}")
        if instance_cfg_path is not None:
            config_payload["centered_instance"] = instance_cfg_path.read_text()
            config_hashes.append(f"centered_instance:{hash_file(instance_cfg_path)}")

        training_config_hash = ";".join(config_hashes) if config_hashes else ""
        training_config_text = json.dumps(config_payload) if config_payload else None

        metadata = build_base_metadata(
            export_format="onnx",
            model_type="topdown",
            model_name=f"{centroid_path.name}+{instance_path.name}",
            checkpoint_path=(
                f"centroid:{centroid_ckpt};centered_instance:{instance_ckpt}"
            ),
            backbone=(
                f"centroid:{centroid_backbone};centered_instance:{instance_backbone}"
            ),
            n_nodes=len(node_names),
            n_edges=len(edge_inds),
            node_names=node_names,
            edge_inds=edge_inds,
            input_scale=centroid_scale,
            input_channels=resolve_input_channels(centroid_cfg),
            output_stride=instance_stride,
            crop_size=resolved_crop,
            max_instances=max_instances,
            max_batch_size=max_batch_size,
            precision="fp32",
            training_config_hash=training_config_hash,
            training_config_embedded=training_config_text is not None,
            input_dtype="uint8",
            normalization="0_to_1",
            peak_threshold=peak_threshold,
            anchor_part=resolve_anchor_part(centroid_cfg, "centroid"),
            centroid_method=resolve_centroid_method(centroid_cfg, "centroid"),
        )

        metadata.save(export_dir / "export_metadata.json")

        if training_config_text is not None:
            try:
                embed_metadata_in_onnx(model_out_path, metadata, training_config_text)
            except ImportError:
                pass

        # Export to TensorRT if requested
        if fmt in ("tensorrt", "both"):
            trt_out_path = export_dir / "model.trt"
            B, C, H, W = input_shape
            export_to_tensorrt(
                wrapper,
                trt_out_path,
                input_shape=input_shape,
                input_dtype=torch.uint8,
                precision=precision,
                max_shape=(max_batch_size, C, H * 2, W * 2),
                **trt_workspace_kwargs,
                verbose=True,
                # As for the ONNX graph: only the centroid model sees the frame.
                static_hw=has_rope(centroid_model),
            )
            # Update metadata for TensorRT
            trt_metadata = build_base_metadata(
                export_format="tensorrt",
                model_type="topdown",
                model_name=f"{centroid_path.name}+{instance_path.name}",
                checkpoint_path=(
                    f"centroid:{centroid_ckpt};centered_instance:{instance_ckpt}"
                ),
                backbone=(
                    f"centroid:{centroid_backbone};centered_instance:{instance_backbone}"
                ),
                n_nodes=len(node_names),
                n_edges=len(edge_inds),
                node_names=node_names,
                edge_inds=edge_inds,
                input_scale=centroid_scale,
                input_channels=resolve_input_channels(centroid_cfg),
                output_stride=instance_stride,
                crop_size=resolved_crop,
                max_instances=max_instances,
                max_batch_size=max_batch_size,
                precision=precision,
                training_config_hash=training_config_hash,
                training_config_embedded=training_config_text is not None,
                input_dtype="uint8",
                normalization="0_to_1",
                peak_threshold=peak_threshold,
                anchor_part=resolve_anchor_part(centroid_cfg, "centroid"),
                centroid_method=resolve_centroid_method(centroid_cfg, "centroid"),
            )
            trt_metadata.save(export_dir / "model.trt.metadata.json")
        return

    # Combined multiclass top-down export (centroid + multi_class_topdown)
    if len(model_paths) == 2 and set(model_types) == {
        "centroid",
        "multi_class_topdown",
    }:
        centroid_idx = model_types.index("centroid")
        instance_idx = model_types.index("multi_class_topdown")

        centroid_path = model_paths[centroid_idx]
        instance_path = model_paths[instance_idx]
        centroid_cfg = cfgs[centroid_idx]
        instance_cfg = cfgs[instance_idx]
        centroid_backbone = backbone_types[centroid_idx]
        instance_backbone = backbone_types[instance_idx]

        centroid_ckpt = centroid_path / "best.ckpt"
        instance_ckpt = instance_path / "best.ckpt"
        if not centroid_ckpt.exists():
            raise click.ClickException(f"Checkpoint not found: {centroid_ckpt}")
        if not instance_ckpt.exists():
            raise click.ClickException(f"Checkpoint not found: {instance_ckpt}")

        centroid_model = _load_lightning_model(
            model_type="centroid",
            backbone_type=centroid_backbone,
            cfg=centroid_cfg,
            ckpt_path=centroid_ckpt,
            device=device,
        ).model
        instance_model = _load_lightning_model(
            model_type="multi_class_topdown",
            backbone_type=instance_backbone,
            cfg=instance_cfg,
            ckpt_path=instance_ckpt,
            device=device,
        ).model

        centroid_model.eval()
        instance_model.eval()
        centroid_model.to(device)
        instance_model.to(device)

        export_dir = output or (centroid_path / "exported_multi_class_topdown")
        export_dir.mkdir(parents=True, exist_ok=True)

        centroid_scale = (
            input_scale
            if input_scale is not None
            else resolve_input_scale(centroid_cfg)
        )
        instance_scale = (
            input_scale
            if input_scale is not None
            else resolve_input_scale(instance_cfg)
        )
        centroid_stride = resolve_output_stride(centroid_cfg, "centroid")
        instance_stride = resolve_output_stride(instance_cfg, "multi_class_topdown")

        resolved_crop = resolve_crop_size(instance_cfg)
        if crop_size is not None:
            resolved_crop = (crop_size, crop_size)
        if resolved_crop is None:
            raise click.ClickException(
                "Multiclass top-down export requires crop_size. Provide --crop-size or "
                "ensure data_config.preprocessing.crop_size is set."
            )

        node_names = resolve_node_names(instance_cfg, "multi_class_topdown")
        edge_inds = resolve_edge_inds(instance_cfg, node_names)
        n_classes = resolve_n_classes(instance_cfg, "multi_class_topdown")
        class_names = resolve_class_names(instance_cfg, "multi_class_topdown")

        wrapper = TopDownMultiClassCombinedONNXWrapper(
            centroid_model=centroid_model,
            instance_model=instance_model,
            max_instances=max_instances,
            crop_size=resolved_crop,
            centroid_output_stride=centroid_stride,
            instance_output_stride=instance_stride,
            centroid_input_scale=centroid_scale,
            instance_input_scale=instance_scale,
            n_nodes=len(node_names),
            n_classes=n_classes,
            centroid_peak_threshold=peak_threshold,
            instance_peak_threshold=peak_threshold,
        )
        wrapper.eval()
        wrapper.to(device)

        input_shape = resolve_input_shape(
            centroid_cfg, input_height=input_height, input_width=input_width
        )
        model_out_path = export_dir / "model.onnx"

        export_to_onnx(
            wrapper,
            model_out_path,
            input_shape=input_shape,
            input_dtype=torch.uint8,
            opset_version=opset_version,
            output_names=[
                "centroids",
                "centroid_vals",
                "peaks",
                "peak_vals",
                "class_logits",
                "instance_valid",
            ],
            verify=verify,
            # As for top-down: only the centroid model sees the frame size.
            static_hw=has_rope(centroid_model),
        )

        centroid_cfg_path = _copy_training_config(centroid_path, export_dir, "centroid")
        instance_cfg_path = _copy_training_config(
            instance_path, export_dir, "multi_class_topdown"
        )
        config_payload = {}
        config_hashes = []
        if centroid_cfg_path is not None:
            config_payload["centroid"] = centroid_cfg_path.read_text()
            config_hashes.append(f"centroid:{hash_file(centroid_cfg_path)}")
        if instance_cfg_path is not None:
            config_payload["multi_class_topdown"] = instance_cfg_path.read_text()
            config_hashes.append(f"multi_class_topdown:{hash_file(instance_cfg_path)}")

        training_config_hash = ";".join(config_hashes) if config_hashes else ""
        training_config_text = json.dumps(config_payload) if config_payload else None

        metadata = build_base_metadata(
            export_format="onnx",
            model_type="multi_class_topdown_combined",
            model_name=f"{centroid_path.name}+{instance_path.name}",
            checkpoint_path=(
                f"centroid:{centroid_ckpt};multi_class_topdown:{instance_ckpt}"
            ),
            backbone=(
                f"centroid:{centroid_backbone};multi_class_topdown:{instance_backbone}"
            ),
            n_nodes=len(node_names),
            n_edges=len(edge_inds),
            node_names=node_names,
            edge_inds=edge_inds,
            input_scale=centroid_scale,
            input_channels=resolve_input_channels(centroid_cfg),
            output_stride=instance_stride,
            crop_size=resolved_crop,
            max_instances=max_instances,
            max_batch_size=max_batch_size,
            training_config_hash=training_config_hash,
            training_config_embedded=training_config_text is not None,
            input_dtype="uint8",
            normalization="0_to_1",
            n_classes=n_classes,
            class_names=class_names,
            peak_threshold=peak_threshold,
            anchor_part=resolve_anchor_part(centroid_cfg, "centroid"),
            centroid_method=resolve_centroid_method(centroid_cfg, "centroid"),
        )
        metadata.save(export_dir / "export_metadata.json")
        click.echo(f"ONNX model exported to: {model_out_path}")
        click.echo(f"Metadata saved to: {export_dir / 'export_metadata.json'}")

        # TensorRT export for combined multiclass top-down
        if fmt in ("tensorrt", "both"):
            trt_out_path = export_dir / "model.trt"
            B, C, H, W = input_shape
            export_to_tensorrt(
                wrapper,
                trt_out_path,
                input_shape=input_shape,
                input_dtype=torch.uint8,
                precision=precision,
                max_shape=(max_batch_size, C, H * 2, W * 2),
                **trt_workspace_kwargs,
                verbose=True,
                # As for the ONNX graph: only the centroid model sees the frame.
                static_hw=has_rope(centroid_model),
            )
            trt_metadata = build_base_metadata(
                export_format="tensorrt",
                model_type="multi_class_topdown_combined",
                model_name=f"{centroid_path.name}+{instance_path.name}",
                checkpoint_path=(
                    f"centroid:{centroid_ckpt};multi_class_topdown:{instance_ckpt}"
                ),
                backbone=(
                    f"centroid:{centroid_backbone};multi_class_topdown:{instance_backbone}"
                ),
                n_nodes=len(node_names),
                n_edges=len(edge_inds),
                node_names=node_names,
                edge_inds=edge_inds,
                input_scale=centroid_scale,
                input_channels=resolve_input_channels(centroid_cfg),
                output_stride=instance_stride,
                crop_size=resolved_crop,
                max_instances=max_instances,
                max_batch_size=max_batch_size,
                precision=precision,
                training_config_hash=training_config_hash,
                training_config_embedded=training_config_text is not None,
                input_dtype="uint8",
                normalization="0_to_1",
                n_classes=n_classes,
                class_names=class_names,
                peak_threshold=peak_threshold,
                anchor_part=resolve_anchor_part(centroid_cfg, "centroid"),
                centroid_method=resolve_centroid_method(centroid_cfg, "centroid"),
            )
            trt_metadata.save(export_dir / "model.trt.metadata.json")
        return

    raise click.ClickException(
        "Provide one model path for centroid/centered-instance/bottom-up export, "
        "or two paths (centroid + centered_instance or centroid + multi_class_topdown) "
        "for combined top-down export."
    )