IO

class plugins.train.model._base.io.IO(plugin: ModelBase, model_dir: str, is_predict: bool, save_optimizer: T.Literal['never', 'always', 'exit'])

Bases: object

Model saving and loading functions.

Handles the loading and saving of the plugin model from disk as well as the model backup and snapshot functions.

Parameters:
  • plugin (ModelBase) – The parent plugin class that owns the IO functions.

  • model_dir (str) – The full path to the model save location

  • is_predict (bool) – True if the model is being loaded for inference. False if the model is being loaded for training.

  • save_optimizer (T.Literal['never', 'always', 'exit']) – When to save the optimizer weights. “never” never saves the optimizer weights. “always” always saves the optimizer weights. “exit” only saves the optimizer weights on an exit request.

Attributes Summary

filename

The filename for this model.

history

list of loss history for the current save iteration.

model_dir

The full path to the model folder

model_exists

True if a model of the type being loaded exists within the model folder location otherwise False.

multiple_models_in_folder

If there are multiple model types in the requested folder, or model types that don't correspond to the requested plugin type, then returns the list of plugin names that exist in the folder, otherwise returns None

Methods Summary

load()

Loads the model from disk

load_optimizer()

Load the optimizer's state_dict from the .keras model file

save([optimizer, is_exit])

Backup and save the model and state file.

snapshot()

Perform a model snapshot.

Attributes Documentation

filename

The filename for this model.

history

list of loss history for the current save iteration.

model_dir

The full path to the model folder

model_exists

True if a model of the type being loaded exists within the model folder location otherwise False.

multiple_models_in_folder

If there are multiple model types in the requested folder, or model types that don’t correspond to the requested plugin type, then returns the list of plugin names that exist in the folder, otherwise returns None

Methods Documentation

load() Model

Loads the model from disk

If the predict function is to be called and the model cannot be found in the model folder then an error is logged and the process exits.

When loading the model, the plugin model folder is scanned for custom layers which are added to Keras’ custom objects.

Return type:

The saved model loaded from disk

load_optimizer() dict[str, Any] | None

Load the optimizer’s state_dict from the .keras model file

Return type:

The saved optimizer state_dict or None if it does not exist

save(optimizer: Optimizer | None = None, is_exit: bool = False) None

Backup and save the model and state file.

Parameters:
  • optimizer (Optimizer | None) – The current optimizer in use for the model if it should be saved. Default: None

  • is_exit (bool) – True if the save request has come from an exit process request otherwise False. Default: False

Return type:

None

snapshot() None

Perform a model snapshot.

Notes

Snapshot function is called 1 iteration after the model was saved, so that it is built from the latest save, hence iteration being reduced by 1.

Return type:

None

property filename: str

The filename for this model.

property history: list[float]

list of loss history for the current save iteration.

load() Model

Loads the model from disk

If the predict function is to be called and the model cannot be found in the model folder then an error is logged and the process exits.

When loading the model, the plugin model folder is scanned for custom layers which are added to Keras’ custom objects.

Return type:

The saved model loaded from disk

load_optimizer() dict[str, Any] | None

Load the optimizer’s state_dict from the .keras model file

Return type:

The saved optimizer state_dict or None if it does not exist

property model_dir: str

The full path to the model folder

property model_exists: bool

True if a model of the type being loaded exists within the model folder location otherwise False.

property multiple_models_in_folder: list[str] | None

If there are multiple model types in the requested folder, or model types that don’t correspond to the requested plugin type, then returns the list of plugin names that exist in the folder, otherwise returns None

save(optimizer: Optimizer | None = None, is_exit: bool = False) None

Backup and save the model and state file.

Parameters:
  • optimizer (Optimizer | None) – The current optimizer in use for the model if it should be saved. Default: None

  • is_exit (bool) – True if the save request has come from an exit process request otherwise False. Default: False

Return type:

None

snapshot() None

Perform a model snapshot.

Notes

Snapshot function is called 1 iteration after the model was saved, so that it is built from the latest save, hence iteration being reduced by 1.

Return type:

None