exporters
sleap_nn.export.exporters
¶
Exporters for serialized model formats.
Modules:
| Name | Description |
|---|---|
onnx_exporter |
ONNX export utilities. |
tensorrt_exporter |
TensorRT export utilities. |
Functions:
| Name | Description |
|---|---|
export_model |
Export a model to the requested format. |
export_to_onnx |
Export a PyTorch model to ONNX. |
export_to_tensorrt |
Export a PyTorch model to TensorRT format. |
export_model(model, save_path, fmt='onnx', input_shape=(1, 1, 512, 512), opset_version=17, output_names=None, verify=True, **kwargs)
¶
Export a model to the requested format.
Source code in sleap_nn/export/exporters/__init__.py
export_to_onnx(model, save_path, input_shape=(1, 1, 512, 512), input_dtype=torch.uint8, opset_version=17, dynamic_axes=None, input_names=None, output_names=None, do_constant_folding=True, verify=True, numerical_check=False, numerical_atol=0.001, numerical_rtol=0.001)
¶
Export a PyTorch model to ONNX.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The PyTorch module to export. |
required |
save_path
|
str | Path
|
Destination path for the |
required |
input_shape
|
Iterable[int]
|
Shape of the dummy input used to trace the graph. |
(1, 1, 512, 512)
|
input_dtype
|
dtype
|
Dtype of the dummy input ( |
uint8
|
opset_version
|
int
|
ONNX opset for the (default) TorchScript exporter. |
17
|
dynamic_axes
|
Optional[Dict[str, Dict[int, str]]]
|
Dynamic-axis spec; defaults to batch/height/width dynamic on
the |
None
|
input_names
|
Optional[List[str]]
|
ONNX input names; defaults to |
None
|
output_names
|
Optional[List[str]]
|
ONNX output names; inferred from a reference forward if
|
None
|
do_constant_folding
|
bool
|
Whether to constant-fold during export. |
True
|
verify
|
bool
|
If |
True
|
numerical_check
|
bool
|
If |
False
|
numerical_atol
|
float
|
Absolute tolerance for the numerical parity check. |
0.001
|
numerical_rtol
|
float
|
Relative tolerance for the numerical parity check. |
0.001
|
Source code in sleap_nn/export/exporters/onnx_exporter.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
export_to_tensorrt(model, save_path, input_shape=(1, 1, 512, 512), input_dtype=torch.uint8, precision='fp16', min_shape=None, opt_shape=None, max_shape=None, workspace_size=2 << 30, method='onnx', verbose=True)
¶
Export a PyTorch model to TensorRT format.
This function supports multiple compilation methods: - "onnx": Exports to ONNX first, then compiles with TensorRT (most reliable) - "jit": Uses torch.jit.trace + torch_tensorrt.compile (alternative)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The PyTorch model to export (typically an ONNX wrapper). |
required |
save_path
|
str | Path
|
Path to save the TensorRT engine (.trt file). |
required |
input_shape
|
Tuple[int, int, int, int]
|
(B, C, H, W) optimal input tensor shape. |
(1, 1, 512, 512)
|
input_dtype
|
dtype
|
Input tensor dtype (torch.uint8 or torch.float32). |
uint8
|
precision
|
str
|
Model precision - "fp32" or "fp16". |
'fp16'
|
min_shape
|
Optional[Tuple[int, int, int, int]]
|
Minimum input shape for dynamic shapes (default: batch=1, H/W halved). |
None
|
opt_shape
|
Optional[Tuple[int, int, int, int]]
|
Optimal input shape (default: same as input_shape). |
None
|
max_shape
|
Optional[Tuple[int, int, int, int]]
|
Maximum input shape (default: batch=16, H/W doubled). |
None
|
workspace_size
|
int
|
TensorRT workspace size in bytes (default 2GB). |
2 << 30
|
method
|
str
|
Compilation method - "onnx" or "jit". |
'onnx'
|
verbose
|
bool
|
Print export info. |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the exported TensorRT engine. |
Note
TensorRT models are NOT cross-platform. The exported model will only work on the same GPU architecture and TensorRT version used for export.