Reference for ultralytics/utils/export/tensorflow.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/export/tensorflow.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.export.tensorflow.tf_wrapper
tf_wrapper(model: Module) -> torch.nn.Module
A wrapper to add TensorFlow compatible inference methods to Detect and Pose layers.
Source code in ultralytics/utils/export/tensorflow.py
17 18 19 20 21 22 23 24 25 26 27 | |
ultralytics.utils.export.tensorflow._tf_inference
_tf_inference(self, x: list[Tensor]) -> tuple[torch.Tensor]
Decode boxes and cls scores for tf object detection.
Source code in ultralytics/utils/export/tensorflow.py
30 31 32 33 34 35 36 37 38 39 40 41 42 | |
ultralytics.utils.export.tensorflow.tf_kpts_decode
tf_kpts_decode(self, bs: int, kpts: Tensor) -> torch.Tensor
Decode keypoints for tf pose estimation.
Source code in ultralytics/utils/export/tensorflow.py
45 46 47 48 49 50 51 52 53 54 55 56 57 | |
ultralytics.utils.export.tensorflow.onnx2saved_model
onnx2saved_model(
onnx_file: str,
output_dir: Path,
int8: bool = False,
images: ndarray = None,
disable_group_convolution: bool = False,
prefix="",
)
Convert a ONNX model to TensorFlow SavedModel format via ONNX.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
onnx_file
|
str
|
ONNX file path. |
required |
output_dir
|
Path
|
Output directory path for the SavedModel. |
required |
int8
|
bool
|
Enable INT8 quantization. Defaults to False. |
False
|
images
|
ndarray
|
Calibration images for INT8 quantization in BHWC format. |
None
|
disable_group_convolution
|
bool
|
Disable group convolution optimization. Defaults to False. |
False
|
prefix
|
str
|
Logging prefix. Defaults to "". |
''
|
Returns:
| Type | Description |
|---|---|
Model
|
Converted Keras model. |
Note
Requires onnx2tf package. Downloads calibration data if INT8 quantization is enabled. Removes temporary files and renames quantized models after conversion.
Source code in ultralytics/utils/export/tensorflow.py
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 | |
ultralytics.utils.export.tensorflow.keras2pb
keras2pb(keras_model, file: Path, prefix='')
Convert a Keras model to TensorFlow GraphDef (.pb) format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keras_model
|
tf_keras
|
Keras model to convert to frozen graph format. |
required |
file
|
Path
|
Output file path (suffix will be changed to .pb). |
required |
prefix
|
str
|
Logging prefix. Defaults to "". |
''
|
Note
Creates a frozen graph by converting variables to constants for inference optimization.
Source code in ultralytics/utils/export/tensorflow.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
ultralytics.utils.export.tensorflow.tflite2edgetpu
tflite2edgetpu(
tflite_file: str | Path, output_dir: str | Path, prefix: str = ""
)
Convert a TensorFlow Lite model to Edge TPU format using the Edge TPU compiler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tflite_file
|
str | Path
|
Path to the input TensorFlow Lite (.tflite) model file. |
required |
output_dir
|
str | Path
|
Output directory path for the compiled Edge TPU model. |
required |
prefix
|
str
|
Logging prefix. Defaults to "". |
''
|
Note
Requires the Edge TPU compiler to be installed. The function compiles the TFLite model for optimal performance on Google's Edge TPU hardware accelerator.
Source code in ultralytics/utils/export/tensorflow.py
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 | |
ultralytics.utils.export.tensorflow.pb2tfjs
pb2tfjs(
pb_file: str,
output_dir: str,
half: bool = False,
int8: bool = False,
prefix: str = "",
)
Convert a TensorFlow GraphDef (.pb) model to TensorFlow.js format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pb_file
|
str
|
Path to the input TensorFlow GraphDef (.pb) model file. |
required |
output_dir
|
str
|
Output directory path for the converted TensorFlow.js model. |
required |
half
|
bool
|
Enable FP16 quantization. Defaults to False. |
False
|
int8
|
bool
|
Enable INT8 quantization. Defaults to False. |
False
|
prefix
|
str
|
Logging prefix. Defaults to "". |
''
|
Note
Requires tensorflowjs package. Uses tensorflowjs_converter command-line tool for conversion. Handles spaces in file paths and warns if output directory contains spaces.
Source code in ultralytics/utils/export/tensorflow.py
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 | |
ultralytics.utils.export.tensorflow.gd_outputs
gd_outputs(gd)
Return TensorFlow GraphDef model output node names.
Source code in ultralytics/utils/export/tensorflow.py
215 216 217 218 219 220 221 | |