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
Return path to model archive file (.mdk.zip)
Return all model attributes
Return a list of the class name strings this model expects
Custom command CLI
A description of this model and how it should be use.
Return path to .h5 model file automatically extracted from model archive file (.mdk.zip)
Path to .h5 model file that is generated in the log directory at the end of training
Return the image input shape as a tuple of integers
Get/set custom objects that should be loaded with the Keras model
Path to directory where logs will be generated
Dictionary of model parameters to include in the generated .tflite
Return the absolute path to the model's specification python script
Return the number of classes this model expects
The name of this model, this the filename of the model's Python script.
Return if testing mode has been enabled
Return path to .tflite model file automatically extracted from model's archive file (.mdk.zip)
Path to .tflite model file that is generated in the log directory at the end of training (if quantization is enabled)
Return a list of registered metadata entries that will be included in the generated .tflite
Path to unquantized/float32 .tflite model file that is generated in the log directory at the end of training (if enabled)
The model version, e.g.
Methods
Add given log directory to the model archive
Add given log file in the log directory to the model archive
Register an event handler
Add a callback that populates the tflite model parameters The callback is invoked just before the model parameters are serialized
Add an entry to the metadata that will be included in the generated .tflite
Return if the model archive file is writable
Create a directory for storing model log files
Create a logger for this 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.
Enable testing mode
Extract a directory from the model's archive file
Extract a file from the model's archive file
Return attribute value or None if unknown
Generate a summary of the .tflite model's metadata
has_attribute
Populate the model parameters.
Serialize each metadata entry registered with this model
Insert a custom entry into the tflite model paremeters
Return a summary of the model
Trigger all handlers for the given event
- property attributes¶
Return all model attributes
- 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
- 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 theMltkModelEvent
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
) – TheMltkModelEvent
on which the handler will be invokedhandler (
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