mltk.core.MltkModel

class MltkModel[source]

The root MLTK Model object

This must be defined in a model specification file.

Refer to the Model Specification guide fore more details.

Properties

archive_path

Return path to model archive file (.mdk.zip)

attributes

Return all model attributes

classes

Return a list of the class name strings this model expects

cli

Custom command CLI

description

A description of this model and how it should be use.

h5_archive_path

Return path to .h5 model file automatically extracted from model archive file (.mdk.zip)

h5_log_dir_path

Path to .h5 model file that is generated in the log directory at the end of training

input_shape

Return the image input shape as a tuple of integers

keras_custom_objects

Get/set custom objects that should be loaded with the Keras model

log_dir

Path to directory where logs will be generated

model_parameters

Dictionary of model parameters to include in the generated .tflite

model_specification_path

Return the absolute path to the model's specification python script

n_classes

Return the number of classes this model expects

name

The name of this model, this the filename of the model's Python script.

test_mode_enabled

Return if testing mode has been enabled

tflite_archive_path

Return path to .tflite model file automatically extracted from model's archive file (.mdk.zip)

tflite_log_dir_path

Path to .tflite model file that is generated in the log directory at the end of training (if quantization is enabled)

tflite_metadata_entries

Return a list of registered metadata entries that will be included in the generated .tflite

unquantized_tflite_log_dir_path

Path to unquantized/float32 .tflite model file that is generated in the log directory at the end of training (if enabled)

version

The model version, e.g.

Methods

__init__

add_archive_dir

Add given log directory to the model archive

add_archive_file

Add given log file in the log directory to the model archive

add_event_handler

Register an event handler

add_model_parameter_populate_callback

Add a callback that populates the tflite model parameters The callback is invoked just before the model parameters are serialized

add_tflite_metadata_entry

Add an entry to the metadata that will be included in the generated .tflite

check_archive_file_is_writable

Return if the model archive file is writable

create_log_dir

Create a directory for storing model log files

create_logger

Create a logger for this model

deserialize_tflite_metadata

Load metadata from the given .tflite flatbuffer file The given tflite_model argument can be the .tflite's flatbuffer bytes, a file path to the .tflite file, or a TfliteModel object.

enable_test_mode

Enable testing mode

get_archive_dir

Extract a directory from the model's archive file

get_archive_file

Extract a file from the model's archive file

get_attribute

Return attribute value or None if unknown

get_tflite_metadata_summary

Generate a summary of the .tflite model's metadata

has_attribute

populate_model_parameters

Populate the model parameters.

serialize_tflite_metadata

Serialize each metadata entry registered with this model

set_model_parameter

Insert a custom entry into the tflite model paremeters

summary

Return a summary of the model

trigger_event

Trigger all handlers for the given event

__init__(model_script_path=None)[source]
Parameters:

model_script_path (str) –

property attributes

Return all model attributes

get_attribute(name)[source]

Return attribute value or None if unknown

Parameters:

name (str) –

property cli

Custom command CLI

This is used to register custom commands The commands may be invoked with: mltk custom <model name> [command args]

property model_specification_path

Return the absolute path to the model’s specification python script

property name

The name of this model, this the filename of the model’s Python script.

property version

The model version, e.g. 3

property description

A description of this model and how it should be use. This is added to the .tflite model flatbuffer “description” field

property log_dir

Path to directory where logs will be generated

create_log_dir(suffix='', delete_existing=False)[source]

Create a directory for storing model log files

Return type:

str

Parameters:

suffix (str) –

create_logger(name, parent=None)[source]

Create a logger for this model

Return type:

Logger

Parameters:

parent (Logger) –

property h5_log_dir_path

Path to .h5 model file that is generated in the log directory at the end of training

property tflite_log_dir_path

Path to .tflite model file that is generated in the log directory at the end of training (if quantization is enabled)

property unquantized_tflite_log_dir_path

Path to unquantized/float32 .tflite model file that is generated in the log directory at the end of training (if enabled)

property classes

Return a list of the class name strings this model expects

property n_classes

Return the number of classes this model expects

property input_shape

Return the image input shape as a tuple of integers

property keras_custom_objects

Get/set custom objects that should be loaded with the Keras model

See https://keras.io/guides/serialization_and_saving/#custom-objects for more details.

property test_mode_enabled

Return if testing mode has been enabled

enable_test_mode()[source]

Enable testing mode

summary()[source]

Return a summary of the model

Return type:

str

add_event_handler(event, handler, _raise_exception=False, **kwargs)[source]

Register an event handler

Register a handler that will be invoked on the corresponding MltkModelEvent

The given handler should have the signature:

def my_event_handler(mltk_model:MltkModel, logger:logging.Logger, **kwargs):
    ...

Where kwargs will contain keyword arguments specific to the MltkModelEvent as well as any keyword arguments passed to this API.

Note

  • By default, exceptions raised by the handler will be logged, but not stop further execution

    Use _raise_exception` to cause the handler to raise the exception.

  • Event handlers are invoked in the order in which they are registered

Parameters:
  • event (MltkModelEvent) – The MltkModelEvent on which the handler will be invoked

  • handler (Callable[[MltkModel, Logger, Any], None]) – Function to be invoked for the given event

  • _raise_exception – By default, exceptions are only logged. This allows for raising the handler exception.

  • kwargs – Additional keyword arguments to provided to the handler NOTE: These keyword arguments must not collide with the event-specific keyword arguments

trigger_event(event, **kwargs)[source]

Trigger all handlers for the given event

This is used internally by the MLTK.

Parameters:

event (MltkModelEvent) –

add_archive_dir(base_dir, create_new=False, recursive=False)

Add given log directory to the model archive

add_archive_file(name)

Add given log file in the log directory to the model archive

add_model_parameter_populate_callback(callback)

Add a callback that populates the tflite model parameters The callback is invoked just before the model parameters are serialized

NOTE: This is typically called by other MltkModel mixins

Parameters:

callback (Callable) –

add_tflite_metadata_entry(entry)

Add an entry to the metadata that will be included in the generated .tflite

Parameters:

entry (TfliteModelMetadataEntry) –

property archive_path

Return path to model archive file (.mdk.zip)

check_archive_file_is_writable(throw_exception=False)

Return if the model archive file is writable

Return type:

bool

deserialize_tflite_metadata(tflite_model)

Load metadata from the given .tflite flatbuffer file The given tflite_model argument can be the .tflite’s flatbuffer bytes, a file path to the .tflite file, or a TfliteModel object.

Parameters:

tflite_model (Union[bytes, str, TfliteModel]) –

get_archive_dir(name, dest_dir=None)

Extract a directory from the model’s archive file

Return type:

str

Parameters:
  • name (str) –

  • dest_dir (str) –

get_archive_file(name, dest_dir=None)

Extract a file from the model’s archive file

Return type:

str

Parameters:
  • name (str) –

  • dest_dir (str) –

get_tflite_metadata_summary(include_tag=None, exclude_tag=None)

Generate a summary of the .tflite model’s metadata

Return type:

str

property h5_archive_path

Return path to .h5 model file automatically extracted from model archive file (.mdk.zip)

property model_parameters

Dictionary of model parameters to include in the generated .tflite

populate_model_parameters()

Populate the model parameters.

This is done by invoking callbacks that were registered by mixins like the AudioDatasetMixin which populates the TfliteModelParameters with the AudioFeatureGenerator settings

This is automatically called by APIs like quantize_model() and update_model_parameters()

serialize_tflite_metadata()

Serialize each metadata entry registered with this model

This is invoked by the quantize_model() API when generated the model’s .tflite file.

Return type:

List[Tuple[str, bytes]]

Returns:

list(tuple(<metedata tag>, <metadata serialized bytes>))

set_model_parameter(key, value, dtype=None)

Insert a custom entry into the tflite model paremeters

Parameters:
  • key (str) –

  • value (Any) –

  • dtype (str) –

property tflite_archive_path

Return path to .tflite model file automatically extracted from model’s archive file (.mdk.zip)

property tflite_metadata_entries

Return a list of registered metadata entries that will be included in the generated .tflite