converter
TFLite Converter API
This module handles converting models to TensorFlow Lite format.
Classes:
-
QuantizationType
–Enum class for quantization types.
-
TfLiteKerasConverter
–TensorFlow Lite model converter.
-
ConversionType
–Enum class for conversion types.
Classes
QuantizationType
Supported quantization formats
Attributes:
-
FP32
–FP32 quantization
-
FP16
–FP16 quantization
-
INT8
–INT8 quantization
-
INT16X8
–INT16X8 quantization
ConversionType
Supported conversion types
Attributes:
-
KERAS
–Use Keras model directly
-
SAVED_MODEL
–Use TF Saved model format
-
CONCRETE
–Lower to TF Concrete functions
TfLiteKerasConverter
Converts Keras model to TFLite model.
Parameters:
-
model
Model
) –Keras model
Example:
# Create simple dataset
test_x = np.random.rand(1000, 64).astype(np.float32)
test_y = np.random.randint(0, 10, 1000).astype(np.int32)
# Create a dense model and train
model = keras.Sequential([
keras.layers.Dense(64, activation="relu", input_shape=(64,)),
keras.layers.Dense(32, activation="relu"),
keras.layers.Dense(10, activation="softmax"),
])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(test_x, test_y, epochs=1, validation_split=0.2)
# Create converter and convert to TFLite w/ FP32 quantization
converter = nse.converters.tflite.TfLiteKerasConverter(model=model)
tflite_content = converter.convert(
test_x,
quantization=nse.converters.tflite.QuantizationType.FP32,
io_type="float32"
)
y_pred_tfl = converter.predict(test_x)
y_pred_tf = model.predict(test_x)
print(np.allclose(y_pred_tf, y_pred_tfl, atol=1e-3))
Source code in neuralspot_edge/converters/tflite/converter.py
Functions
from_saved_model
classmethod
Create converter from saved keras model
Parameters:
-
model_path
Path
) –Path to saved model
Returns:
-
TfLiteKerasConverter
(TfLiteKerasConverter
) –Converter
Source code in neuralspot_edge/converters/tflite/converter.py
convert
convert(test_x: npt.NDArray | None = None, quantization: QuantizationType = QuantizationType.FP32, io_type: str | None = None, mode: ConversionType = ConversionType.KERAS, strict: bool = True, verbose: int = 2) -> str
Convert TF model into TFLite model content
Parameters:
-
test_x
NDArray | None
, default:None
) –Test dataset. Defaults to None.
-
quantization
QuantizationType
, default:FP32
) –Quantization type. Defaults to QuantizationType.FP32.
-
io_type
str | None
, default:None
) –Input/Output type. Defaults to None.
-
mode
ConversionType
, default:KERAS
) –Conversion mode. Defaults to ConversionType.KERAS.
-
strict
bool
, default:True
) –Strict mode. Defaults to True.
-
verbose
int
, default:2
) –Verbosity level (0,1,2). Defaults to 2.
Returns:
-
str
(str
) –TFLite content
Source code in neuralspot_edge/converters/tflite/converter.py
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 |
|
debug_quantization
Debug quantized TFLite model content.
Source code in neuralspot_edge/converters/tflite/converter.py
evaluate
evaluate(x: npt.NDArray, y: npt.NDArray, input_name: str | None = None, output_name: str | None = None) -> npt.NDArray
Evaluate TFLite model
Parameters:
-
x
NDArray
) –Input samples
-
y
NDArray
) –Input labels
-
input_name
str | None
, default:None
) –Input layer name. Defaults to None.
-
output_name
str | None
, default:None
) –Output layer name. Defaults to None.
Returns:
-
NDArray
–npt.NDArray: Loss values
Source code in neuralspot_edge/converters/tflite/converter.py
export
Export TFLite model content to file
Parameters:
-
tflite_path
str
) –TFLite file path
Source code in neuralspot_edge/converters/tflite/converter.py
export_header
Export TFLite model as C header file.
Parameters:
-
header_path
str
) –Header file path
-
name
str
, default:'model'
) –Variable name. Defaults to "model".