mltk.core.tflite_micro.TfliteMicro¶
- class TfliteMicro[source]¶
This class wraps the TF-Lite Micro C++ library
This class allows for loading a .tflite model file into the TF-Lite Micro (TFLM) C++ library and running inference using either the TFLM reference kernels or hardware accelerated kernels.
Methods
__init__
Return if the given accelerator is supported
Add an accelerator search path
Return the TFLM API version number.
Return an instance to the specified accelerator wrapper
Return the C++ wrapper's logging level
Return the wrapper's Python logger
Return a list of supported accelerators by name
Return the GIT hash of the MLTK repo used to compile the wrapper library
Load the TF-Lite Micro interpreter with the given .tflite model
Given a case-insensitive accelerator name, normalize the name to the format used by the C++ library
Profile the given model in the simulator and optionally determine metric estimates
Run one inference and record each model layer's input/output tensors
Register a TFLM accelerator instance
Set the C++ wrapper logging level
Set the wrapper's Python logger
Unload a previously loaded model
- static git_hash()[source]¶
Return the GIT hash of the MLTK repo used to compile the wrapper library
- Return type:
str
- static api_version()[source]¶
Return the TFLM API version number. This is used to ensure accelerator wrappers are compatible with this TFLM wrapper
- Return type:
int
- static set_log_level(level)[source]¶
Set the C++ wrapper logging level
- NOTE: This sets the level in the C++ wrapper, NOT the Python logger.
Increasing the logging level can help with throughput as each log generated by the wrapper needs to be forwarded to the Python logger.
- Return type:
str
- Returns:
The previous log level
- Parameters:
level (str) –
- static get_log_level()[source]¶
Return the C++ wrapper’s logging level
NOTE: This returns the C++ wrapper’s logging level, NOT the Python logger.
- Return type:
str
- static set_logger(logger)[source]¶
Set the wrapper’s Python logger
This logger will be invoked by the C++ wrapper’s logging callback.
- Parameters:
logger (Logger) –
- static normalize_accelerator_name(accelerator)[source]¶
Given a case-insensitive accelerator name, normalize the name to the format used by the C++ library
- Return type:
str
- Returns:
Normalized name of accelerator or None if accelerator is unknown
- Parameters:
accelerator (str) –
- static get_supported_accelerators()[source]¶
Return a list of supported accelerators by name
- Return type:
List
[str
]
- static accelerator_is_supported(accelerator)[source]¶
Return if the given accelerator is supported
- Return type:
bool
- Parameters:
accelerator (str) –
- static load_tflite_model(model, accelerator=None, enable_profiler=False, enable_recorder=False, enable_tensor_recorder=False, force_buffer_overlap=False, runtime_buffer_size=None, **kwargs)[source]¶
Load the TF-Lite Micro interpreter with the given .tflite model
NOTE: - Only 1 model may be loaded at a time - You must call unload_model() when the model is no longer needed
- Return type:
- Parameters:
model (Union[str, TfliteModel]) –
accelerator (str) –
runtime_buffer_size (int) –
- static unload_model(model)[source]¶
Unload a previously loaded model
- Parameters:
model (TfliteMicroModel) –
- static profile_model(model, accelerator=None, return_estimates=False, disable_simulator_backend=False, runtime_buffer_size=-1, input_data=None, **kwargs)[source]¶
Profile the given model in the simulator and optionally determine metric estimates
- Parameters:
model (Union[str, TfliteModel]) –
accelerator (str) –
input_data (Union[ndarray, List[ndarray]]) –
- static record_model(model, input_data=None, accelerator=None, enable_accelerator_recorder=False, disable_simulator_backend=False, enable_tensor_recorder=True, return_model_details=False, update_input_model=False, layer_callback=None)[source]¶
Run one inference and record each model layer’s input/output tensors
- Parameters:
model (
Union
[str
,TfliteModel
]) – path to .tflite model file or TfliteModel instanceinput_data (
Union
[ndarray
,List
[ndarray
]]) – Model input0 data as numpy array or list of numpy arrays for each model inputaccelerator (
str
) – Optional accelerator to use for inferenceenable_accelerator_recorder – If enabled, record the data/instructions generated by the hardware accelerator The recorded data with be stored in each layers’ metadata property, .e.g.:
layer.metadata['accelerator_data']
. Each layers’ recorded data is a dictionary with the entries specific to the hardware accelerator.disable_simulator_backend – Disable the simulator backend while running the accelerator recorder. This can greatly improve execution time, however, the generated data output (i.e. output tensors) is invalid
enable_tensor_recorder – Record the input/output tensors of each layer
return_model_details – Also return the recorded model’s TfliteMicroModelDetails
layer_callback (Callable[[TfliteLayer], bool]) –
- Return type:
Union
[List
[TfliteLayer
],Tuple
[List
[TfliteLayer
],TfliteMicroModelDetails
]]- Returns:
If return_model_details=False, then return a list of TfliteLayers with the tensor data updated with the recorded values from the previous inference If return_model_details=True, then return a tuple(list(TfliteLayers), TfliteMicroModelDetails)
- static register_accelerator(accelerator)[source]¶
Register a TFLM accelerator instance
- Parameters:
accelerator (TfliteMicroAccelerator) –