Reference for ultralytics/utils/export.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/export.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.export.export_onnx
export_onnx(
torch_model,
im,
onnx_file,
opset=14,
input_names=["images"],
output_names=["output0"],
dynamic=False,
)
Exports a PyTorch model to ONNX format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
torch_model
|
Module
|
The PyTorch model to export. |
required |
im
|
Tensor
|
Example input tensor for the model. |
required |
onnx_file
|
str
|
Path to save the exported ONNX file. |
required |
opset
|
int
|
ONNX opset version to use for export. |
14
|
input_names
|
list
|
List of input tensor names. |
['images']
|
output_names
|
list
|
List of output tensor names. |
['output0']
|
dynamic
|
bool | dict
|
Whether to enable dynamic axes. Defaults to False. |
False
|
Notes
- Setting
do_constant_folding=True
may cause issues with DNN inference for torch>=1.12.
Source code in ultralytics/utils/export.py
ultralytics.utils.export.export_engine
export_engine(
onnx_file,
engine_file=None,
workspace=None,
half=False,
int8=False,
dynamic=False,
shape=(1, 3, 640, 640),
dla=None,
dataset=None,
metadata=None,
verbose=False,
prefix="",
)
Exports a YOLO model to TensorRT engine format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
onnx_file
|
str
|
Path to the ONNX file to be converted. |
required |
engine_file
|
str
|
Path to save the generated TensorRT engine file. |
None
|
workspace
|
int
|
Workspace size in GB for TensorRT. Defaults to None. |
None
|
half
|
bool
|
Enable FP16 precision. Defaults to False. |
False
|
int8
|
bool
|
Enable INT8 precision. Defaults to False. |
False
|
dynamic
|
bool
|
Enable dynamic input shapes. Defaults to False. |
False
|
shape
|
tuple
|
Input shape (batch, channels, height, width). Defaults to (1, 3, 640, 640). |
(1, 3, 640, 640)
|
dla
|
int
|
DLA core to use (Jetson devices only). Defaults to None. |
None
|
dataset
|
InfiniteDataLoader
|
Dataset for INT8 calibration. Defaults to None. |
None
|
metadata
|
dict
|
Metadata to include in the engine file. Defaults to None. |
None
|
verbose
|
bool
|
Enable verbose logging. Defaults to False. |
False
|
prefix
|
str
|
Prefix for log messages. Defaults to "". |
''
|
Raises:
Type | Description |
---|---|
ValueError
|
If DLA is enabled on non-Jetson devices or required precision is not set. |
RuntimeError
|
If the ONNX file cannot be parsed. |
Notes
- TensorRT version compatibility is handled for workspace size and engine building.
- INT8 calibration requires a dataset and generates a calibration cache.
- Metadata is serialized and written to the engine file if provided.
Source code in ultralytics/utils/export.py
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 |
|