mlair.model_modules.convolutional_networks

Module Contents

Classes

CNNfromConfig

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

CNN

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

CNN_16_32_64

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

Attributes

__author__

__date__

mlair.model_modules.convolutional_networks.__author__ = Lukas Leufen
mlair.model_modules.convolutional_networks.__date__ = 2021-02-
class mlair.model_modules.convolutional_networks.CNNfromConfig(input_shape: list, output_shape: list, layer_configuration: list, optimizer='adam', batch_normalization=False, **kwargs)

Bases: mlair.model_modules.AbstractModelClass

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.

_activation
_initializer
_optimizer
_regularizer
_requirements = ['lr', 'beta_1', 'beta_2', 'epsilon', 'decay', 'amsgrad', 'momentum', 'nesterov', 'l1', 'l2']

Use this class like the following. Note that all keys must match the corresponding tf/keras keys of the layer

```python

input_shape = [(65,1,9)] output_shape = [(4, )] layer_configuration=[

{“type”: “Conv2D”, “activation”: “relu”, “kernel_size”: (1, 1), “filters”: 8}, {“type”: “Dropout”, “rate”: 0.2}, {“type”: “Conv2D”, “activation”: “relu”, “kernel_size”: (5, 1), “filters”: 16}, {“type”: “Dropout”, “rate”: 0.2}, {“type”: “MaxPooling2D”, “pool_size”: (8, 1), “strides”: (1, 1)}, {“type”: “Conv2D”, “activation”: “relu”, “kernel_size”: (1, 1), “filters”: 16}, {“type”: “Dropout”, “rate”: 0.2}, {“type”: “Conv2D”, “activation”: “relu”, “kernel_size”: (5, 1), “filters”: 32}, {“type”: “Dropout”, “rate”: 0.2}, {“type”: “MaxPooling2D”, “pool_size”: (8, 1), “strides”: (1, 1)}, {“type”: “Conv2D”, “activation”: “relu”, “kernel_size”: (1, 1), “filters”: 32}, {“type”: “Dropout”, “rate”: 0.2}, {“type”: “Conv2D”, “activation”: “relu”, “kernel_size”: (5, 1), “filters”: 64}, {“type”: “Dropout”, “rate”: 0.2}, {“type”: “MaxPooling2D”, “pool_size”: (8, 1), “strides”: (1, 1)}, {“type”: “Conv2D”, “activation”: “relu”, “kernel_size”: (1, 1), “filters”: 64}, {“type”: “Dropout”, “rate”: 0.2}, {“type”: “Flatten”}, # {“type”: “Dense”, “units”: 128, “activation”: “relu”}

] model = CNNfromConfig(input_shape, output_shape, layer_configuration)

```

set_model(self)

Abstract method to set model.

static _get_layer_name(layer: tensorflow.keras.layers, layer_kwargs: Union[dict, None], pos: int, *args)
_set_optimizer(self, optimizer, **kwargs)
_set_regularizer(self, regularizer, **kwargs)
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

_extract_layer_conf(self, layer_opts)
class mlair.model_modules.convolutional_networks.CNN(input_shape: list, output_shape: list, activation='relu', activation_output='linear', optimizer='adam', regularizer=None, kernel_size=7, dropout=None, dropout_freq=None, pooling_freq=None, pooling_type='max', n_layer=1, n_filter=10, layer_configuration=None, pooling_size=None, dense_layer_configuration=None, **kwargs)

Bases: mlair.model_modules.AbstractModelClass

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.

_activation
_initializer
_optimizer
_regularizer
_requirements = ['lr', 'beta_1', 'beta_2', 'epsilon', 'decay', 'amsgrad', 'momentum', 'nesterov', 'l1', 'l2']
_dropout
_pooling

Define CNN model as in the following examples:

  • use same kernel for all layers and use in total 3 conv layers, no dropout or pooling is applied

    `python model=CNN, kernel_size=5, n_layer=3, dense_layer_configuration=[128, 64], `

  • specify the kernel sizes, make sure len of kernel size parameter matches number of layers

    `python model=CNN, kernel_size=[3, 7, 11], n_layer=3, dense_layer_configuration=[128, 64], `

  • use different number of filters in each layer (can be combined either with fixed or individual kernel sizes), make sure that lengths match. Using layer_configuration always overwrites any value given to n_layers parameter.

    `python model=CNN, kernel_size=[3, 7, 11], layer_configuration=[24, 48, 48], `

  • now specify individual kernel sizes and number of filters for each layer

    `python model=CNN, layer_configuration=[(16, 3), (32, 7), (64, 11)], dense_layer_configuration=[128, 64], `

  • add also some dropout and pooling every 2nd layer, dropout is applied after the conv layer, pooling before. Note that pooling will not used in the init layer whereas dropout is already applied there.

    `python model=CNN, dropout_freq=2, dropout=0.3, pooling_type="max", pooling_freq=2, pooling_size=3, layer_configuration=[(16, 3), (32, 7), (64, 11)], dense_layer_configuration=[128, 64], `

_set_pooling(self, pooling)
_set_layer_freq(self, param)
_set_activation(self, activation)
_set_optimizer(self, optimizer, **kwargs)
_set_regularizer(self, regularizer, **kwargs)
_set_dropout(self, activation, dropout_rate)
set_model(self)

Build the model.

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

class mlair.model_modules.convolutional_networks.CNN_16_32_64(input_shape: list, output_shape: list, activation='relu', activation_output='linear', optimizer='adam', regularizer=None, kernel_size=7, dropout=None, dropout_freq=None, pooling_freq=None, pooling_type='max', n_layer=1, n_filter=10, layer_configuration=None, pooling_size=None, dense_layer_configuration=None, **kwargs)

Bases: CNN

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.

set_model(self)

Build the model.