mlair.model_modules

Collection of all modules that are related to a model.

Submodules

Package Contents

Classes

AbstractModelClass

The AbstractModelClass provides a unified skeleton for any model provided to the machine learning workflow.

class mlair.model_modules.AbstractModelClass(input_shape, output_shape)

Bases: abc.ABC

The AbstractModelClass provides a unified skeleton for any model provided to the machine learning workflow.

The model can always be accessed by calling ModelClass.model or directly by an model method without parsing the model attribute name (e.g. ModelClass.model.compile -> ModelClass.compile). Beside the model, this class provides the corresponding loss function.

_requirements = []
load_model(self, name: str, compile: bool = False)None
__getattr__(self, name: str) → Any

Is called if __getattribute__ is not able to find requested attribute.

Normally, the model class is saved into a variable like model = ModelClass(). To bypass a call like model.model to access the _model attribute, this method tries to search for the named attribute in the self.model namespace and returns this attribute if available. Therefore, following expression is true: ModelClass().compile == ModelClass().model.compile as long the called attribute/method is not part if the ModelClass itself.

Parameters

name – name of the attribute or method to call

Returns

attribute or method from self.model namespace

property model(self) → tensorflow.keras.Model

The model property containing a keras.Model instance.

Returns

the keras model

property custom_objects(self) → Dict

The custom objects property collects all non-keras utilities that are used in the model class.

To load such a customised and already compiled model (e.g. from local disk), this information is required.

Returns

custom objects in a dictionary

property compile_options(self) → Dict

The compile options property allows the user to use all keras.compile() arguments. They can ether be passed as dictionary (1), as attribute, without setting compile_options (2) or as mixture (partly defined as instance attributes and partly parsing a dictionary) of both of them (3). The method will raise an Error when the same parameter is set differently.

Example (1) Recommended (includes check for valid keywords which are used as args in keras.compile) .. code-block:: python

def set_compile_options(self):
self.compile_options = {“optimizer”: keras.optimizers.SGD(),

“loss”: keras.losses.mean_squared_error, “metrics”: [“mse”, “mae”]}

Example (2) .. code-block:: python

def set_compile_options(self):

self.optimizer = keras.optimizers.SGD() self.loss = keras.losses.mean_squared_error self.metrics = [“mse”, “mae”]

Example (3) Correct: .. code-block:: python

def set_compile_options(self):

self.optimizer = keras.optimizers.SGD() self.loss = keras.losses.mean_squared_error self.compile_options = {“metrics”: [“mse”, “mae”]}

Incorrect: (Will raise an error) .. code-block:: python

def set_compile_options(self):

self.optimizer = keras.optimizers.SGD() self.loss = keras.losses.mean_squared_error self.compile_options = {“optimizer”: keras.optimizers.Adam(), “metrics”: [“mse”, “mae”]}

Note: * As long as the attribute and the dict value have exactly the same values, the setter method will not raise an error * For example (2) there is no check implemented, if the attributes are valid compile options

Returns

static __extract_from_tuple(tup)

Return element of tuple if it contains only a single element.

static __compare_keras_optimizers(first, second)

Compares if optimiser and all settings of the optimisers are exactly equal.

:return True if optimisers are interchangeable, or False if optimisers are distinguishable.

get_settings(self) → Dict

Get all class attributes that are not protected in the AbstractModelClass as dictionary.

Returns

all class attributes

abstract set_model(self)

Abstract method to set model.

abstract set_compile_options(self)

This method only has to be defined in child class, when additional compile options should be used () (other options than optimizer and loss) Has to be set as dictionary: {‘optimizer’: None,

‘loss’: None, ‘metrics’: None, ‘loss_weights’: None, ‘sample_weight_mode’: None, ‘weighted_metrics’: None, ‘target_tensors’: None }

Returns

set_custom_objects(self, **kwargs)None

Set custom objects that are not part of keras framework.

These custom objects are needed if an already compiled model is loaded from disk. There is a special treatment for the Padding2D class, which is a base class for different padding types. For a correct behaviour, all supported subclasses are added as custom objects in addition to the given ones.

Parameters

kwargs – all custom objects, that should be saved

classmethod requirements(cls)

Return requirements and own arguments without duplicates.

classmethod own_args(cls, *args)

Return all arguments (including kwonlyargs).

classmethod super_args(cls)