models

Easily define U-net-like architectures using Keras layers

ct_segnet.model_utils.models.build_Unet_flex(img_shape, n_depth=1, n_pools=4, activation='lrelu', batch_norm=True, kern_size=(3, 3), kern_size_upconv=(2, 2), pool_size=(2, 2), dropout_level=1.0, loss='binary_crossentropy', stdinput=True)[source]

Define your own Unet-like architecture, based on the arguments provided. Checks that the architecture complies with the converging-diverging paths and ensures the output image size is the same as input image size.

Returns

a keras model for a U-net-like architecture

Return type

tf.Keras.model

Parameters
  • img_shape (tuple) – input image shape (ny,nx,1)

  • n_depth (int or list) – Option 1: a list of the number of filters in each convolutional layer upstream of each pooling layer. Length must equal number of max pooling layers.

    Option 2: an integer that multiplies the values in this list: [16, 32, …]. E.g. n_depth = 2 creates [32, 64, …]

  • n_pools (int) – Number of max pooling layers

  • activation (str or tf.Keras.layers.Activation) – name of custom activation or Keras activation layer

  • batch_norm (bool) – True to insert BN layer after the convolutional layers

  • kern_size (list or tuple) – kernel size, e.g. (3,3). Provide a list (length = n_pools) of tuples to apply a different kernel size to each block.

  • kern_size_upconv (list or tuple) – kernel size for upconv, e.g. (2,2). Provide a list (length = n_pools) of tuples to apply a different kernel size to each block

  • pool_size (list or tuple) – max pool size, e.g. (2,2). Provide a list (length = n_pools) of tuples to apply a different size to each block

  • dropout_level (float or list) – Option 1: a list (length = n_pools) of dropout values to apply separately in each block

    Option 2: a float (0..1) that multiples the values in this list: [0.1, 0.1, 0.2, 0.2, 0.3]

  • loss (str) – The loss function of your choice. The following are implemented: ‘weighted_crossentropy’, ‘focal_loss’, ‘binary_crossentropy’

  • stdinput (bool) – If True, the input image will be normalized into [0,1]

ct_segnet.model_utils.models.conv_layer(tensor_in, n_filters, kern_size=None, activation=None, kern_init='he_normal', padding='same', dropout=0.1, batch_norm=False)[source]

Define a block of two convolutional layers

Returns

tensor of rank 4 (batch_size, n_rows, n_cols, n_channels)

Parameters
  • tensor_in (tensor) – input tensor

  • n_filters (int) – number of filters in each convolutional layer

  • kern_size (tuple) – kernel size, e.g. (3,3)

  • activation (str or tf.Keras.layers.Activation) – name of custom activation or Keras activation layer

  • kern_init (str) – kernel initialization method

  • padding (str) – type of padding

  • dropout (float) – dropout fraction

  • batch_norm (bool) – True to insert a BN layer

ct_segnet.model_utils.models.insert_activation(tensor_in, activation)[source]
Returns

tensor of rank 4 (batch_size, n_rows, n_cols, n_channels)

Parameters
  • tensor_in (tensor) – input tensor

  • activation (str or tf.Keras.layers.Activation) – name of custom activation or Keras activation layer

ct_segnet.model_utils.models.pool_layer(tensor_in, n_filters, pool_size, dropout=None, activation=None, batch_norm=False, kern_size=None)[source]

Define a block of 2 convolutional layer followed by a pooling layer

Returns

tensor of rank 4 (batch_size, n_rows, n_cols, n_channels)

Parameters
  • tensor_in (tensor) – input tensor

  • n_filters (int) – number of filters in each convolutional layer

  • pool_size (tuple) – max pooling (2,2)

  • dropout (float) – fraction of dropout

  • activation (str or tf.Keras.layers.Activation) – name of custom activation or Keras activation layer

  • batch_norm (bool) – True to insert a BN layer

  • kern_size (tuple) – kernel size for conv layer, e.g. (3,3)

ct_segnet.model_utils.models.upconv_layer(tensor_in, concat_tensor, n_filters=None, activation=None, kern_size=None, strides=None, padding='same', batch_norm=False)[source]

Define an upconvolutional layer and concatenate the output with a conv layer from the contracting path

Returns

tensor of rank 4 (batch_size, n_rows, n_cols, n_channels)

Parameters
  • tensor_in (tensor) – input tensor

  • concat_tensor (tensor) – this will be concatenated to the output of the upconvolutional layer

  • n_filters (int) – number of filters in each convolutional layer

  • kern_size (tuple) – kernel size for upconv, e.g. (2,2)

  • activation (str or tf.Keras.layers.Activation) – name of custom activation or Keras activation layer

  • kern_init (str) – kernel initialization method

  • strides (tuple) – strides e.g. (2,2)

  • padding (str) – type of padding

  • batch_norm (bool) – True to insert a BN layer