models module

Creates DIRESA and AE models out of an encoder and decoder model. Creates DIRESA and AE models from hyperparameters.

Author:

Geert De Paepe

Email:

geert.de.paepe@vub.be

License:

MIT License

  1. Creating AE and Diresa models out of an encoder and decoder model:

  • autoencoder_model(x, encoder, decoder)

  • diresa_model(x, x_twin, encoder, decoder)

  1. Creating AE and Diresa models from hyperparameters

  • build_ae(input_shape, stack, stack_filters, latent_filters, kernel_size=(3, 3), conv_transpose=False, up_first=False, attention=False, residual=False, dense_units=(), dropout_rate=0, activation=’relu’, encoder_activation=’linear’, decoder_activation=’linear’, **kwargs)

  • build_diresa(input_shape, stack, stack_filters, latent_filters, kernel_size=(3, 3), conv_transpose=False, up_first=False, attention=False, residual=False, dense_units=(), dropout_rate=0, activation=’relu’, encoder_activation=’linear’, decoder_activation=’linear’, **kwargs)

Encoder:
  • 0 or more [blocks] with C (Conv2D) or residual units and a P (MaxPooling layer)

  • 0 or 1 [block] of D (Dense layers)

Decoder:
  • 0 or 1 [block] with D (Dense layers)

  • 0 or more [blocks] with C (Conv2D) or residual units and an U (UpSampling layer)

Examples:
  • stack; dense_units; Encoder; Decoder (up_first=True); Decoder (up_first=False)

  • [1]; (); [C-P]-Cout; [U-C]-Cout; [C-U]-Cout

  • [3]; (); [C-C-C-P]-Cout; [U-C-C-C]-Cout; [C-U-C-C]-Cout

  • [1,1]; (); [C-P]-[C-P]-Cout; [U-C]-[U-C]-Cout; [C-U]-[C-U]-Cout

  • (); [20,10]; [D-Dout]; [D-Dout]; [D-Dout]

  • [2]; [20,10]; [C-C-P]-[D-Dout]; [D-D]-[U-C]-Cout; [D-D]-[C-U]-Cout

  • [1,1]; [20,10]; [C-P]-[C-P]-[D-Dout]; [D-D]-[U]-[U-C]-Cout; [D-D]-[U]-[C-U]-Cout

If conv_transpose=True, C is a ConvTranspose layer, only possible for up_first=True. If attention=True, attention layer is added after last C in block. Attention can also be a list of Boolean with length equal the number of blocks. If residual=True, C is a ResNet V1 residual unit with a skip connection, only possible for up_first=True. If attention=True and residual=True, C is ResNet V1 and attention layer with skip connection is added. If dropout_rate!=0, Dropout(dropout_rate) is added between Dense units, only possible if len(dense_units)>0. kwargs are passed to all Conv2D and Dense layers, e.g. they can be used for changing kernel_initializer or kernel_regularizer. Input rank should be 3 if Conv2D blocks, first 2 dimensions of input_shape should be a multiple of 2^len(stack). Input rank should be 1 if only a Dense block.

models.autoencoder_model(x, encoder, decoder)

Creates autoencoder model out of an encoder and a decoder model

Parameters:
  • x – keras input tensor (keras.Input())

  • encoder – encoder functional Keras model

  • decoder – decoder functional Keras model

Returns:

autoencoder model

models.diresa_model(x, x_twin, encoder, decoder, dist_layer=diresa.layers.DistLayer)

Creates a Diresa model out of an encoder and a decoder model

Parameters:
  • x – keras input tensor (keras.Input())

  • x_twin – keras input tensor for shuffled input

  • encoder – encoder functional Keras model

  • decoder – decoder functional Keras model

  • dist_layer – distance layer to be used

Returns:

Diresa model

models.build_ae(input_shape=(), stack=(), stack_filters=(), latent_filters=1, kernel_size=(3, 3), conv_transpose=False, up_first=False, attention=False, residual=False, dropout_rate=0, dense_units=(), activation='relu', activation_layer_param=None, encoder_activation='linear', decoder_activation='linear', **kwargs)

Creates an AE model out of hyperparameters

Parameters:
  • input_shape – rank 3 with Conv2D layers, first 2 dims should be a multiple of 2^len(stack); rank 1 if only Dense layers

  • stack – elements are nbr of Conv2D or residual units in a block

  • stack_filters – elements are nbr of filters in a block

  • latent_filters – nbr of filters in convolutional output (only used if no dense units)

  • kernel_size – kernel size for convolution

  • conv_transpose – if True ConvTranspose is used in decoder, only possible for up_first=True

  • up_first – if True UpSampling is first in decoder block, if False UpSampling is second

  • attention – if True, attention layer is added after last Conv2D layer in block

  • residual – if True, elements in blocks are residual units, if False elements are Conv2D layers

  • dense_units – elements are nbr of nodes of a Dense layer in the dense block

  • dropout_rate – if dropout_rate!=0, Dropout(dropout_rate) is added between Dense units, only if len(dense_units)>0

  • activation – activation function or layer used (except for output of encoder/decoder)

  • activation_layer_param – parameters for activation layer (dictionary)

  • encoder_activation – activation function used for output of encoder

  • decoder_activation – activation function used for output of decoder

  • kwargs – are passed to all Conv2D and Dense layers, e.g. can be used for changing kernel_initializer, kernel_regularizer

Returns:

AE functional Keras model

models.build_diresa(input_shape=(), stack=(), stack_filters=(), latent_filters=1, kernel_size=(3, 3), conv_transpose=False, up_first=False, attention=False, residual=False, dropout_rate=0, dense_units=(), activation='relu', activation_layer_param=None, encoder_activation='linear', decoder_activation='linear', dist_layer=diresa.layers.DistLayer, **kwargs)

Creates a Diresa model out of hyperparameters

Parameters:
  • input_shape – rank 3 with Conv2D layers, first 2 dims should be a multiple of 2^len(stack); rank 1 if only Dense layers

  • stack – elements are nbr of Conv2D or residual units in a block

  • stack_filters – elements are nbr of filters in a block

  • latent_filters – nbr of filters in convolutional output (only used if no dense units)

  • kernel_size – kernel size for convolution

  • conv_transpose – if True ConvTranspose is used in decoder, only possible for up_first=True

  • up_first – if True UpSampling is first in decoder block, if False UpSampling is second

  • attention – if True, attention layer is added after last Conv2D layer in block

  • residual – if True, elements in blocks are residual units, if False elements are Conv2D layers

  • dense_units – elements are nbr of nodes of a Dense layer in the dense block

  • dropout_rate – if dropout_rate!=0, Dropout(dropout_rate) is added between Dense units, only if len(dense_units)>0

  • activation – activation function or layer used (except for output of encoder/decoder)

  • activation_layer_param – parameters for activation layer

  • encoder_activation – activation function used for output of encoder

  • decoder_activation – activation function used for output of decoder

  • dist_layer – distance layer to be used

  • kwargs – are passed to all Conv2D and Dense layers, e.g. can be used for changing kernel_initializer, kernel_regularizer

Returns:

Diresa functional Keras model