ResNet¶
resnet submodule within the ketos.neural_networks module
This module provides classes to implement Residual Networks (ResNets).
 Contents:
ResNetBlock class ResNet class ResNetInterface class

class
ketos.neural_networks.resnet.
ResNet1DArch
(n_classes, pre_trained_base=None, block_sets=None, initial_filters=16, **kwargs)[source]¶ Bases:
tensorflow.python.keras.engine.training.Model
Implements a 1D (temporal) ResNet architecture, building on top of ResNetBlocks.
 Args:
 block_sets: list of ints
A list specifying the block sets and how many blocks each set contains. Example: [2,2,2] will create a ResNet with 3 block sets, each containing 2 ResNetBlocks (i.e.: a total of 6 residual blocks)
 n_classes:int
The number of classes. The output layer uses a Softmax activation and will contain this number of nodes, resulting in model outputs with this many values summing to 1.0.
 initial_filters:int
The number of filters used in the first ResNetBlock. Subsequent blocks will have two times more filters than their previous block.
 pre_trained_base: instance of ResNet1DArch
A pretrained resnet model from which the residual blocks will be taken. Use by the the clone_with_new_top method when creating a clone for transfer learning
 Returns:
A ResNet1DArch object, which is a tensorflow model.

call
(inputs, training=None)[source]¶ Calls the model on new inputs.
In this case call just reapplies all ops in the graph to the new inputs (e.g. build a new computational graph from the provided inputs).
 Arguments:
inputs: A tensor or list of tensors. training: Boolean or boolean scalar tensor, indicating whether to run
the Network in training mode or inference mode.
 mask: A mask or list of masks. A mask can be
either a tensor or None (no mask).
 Returns:
A tensor if there is a single output, or a list of tensors if there are more than one outputs.

clone_with_new_top
(n_classes=None, freeze_base=True)[source]¶ Clone this instance but replace the original classification top with a new (untrained) one
 Args:
 n_classes:int
The number of classes the new classification top should output. If None(default), the original number of classes will be used.
 freeze_base:bool
If True, the weights of the feature extraction base will be froze (untrainable) in the new model.
 Returns:
 cloned_model: instance of ResNetArch
The new model with the old feature extraction base and new classification top.

freeze_block
(block_ids)[source]¶ Freeze specific residual blocks
 Args:
 blocks_ids: list of ints
The block numbers to be freezed (starting from zero)

get_feature_extraction_base
()[source]¶ Retrive the feature extraction base (initial convolutional layer + residual blocks)
 Returns:
list containing the feature extraction layers

class
ketos.neural_networks.resnet.
ResNet1DBlock
(filters, strides=1, residual_path=False)[source]¶ Bases:
tensorflow.python.keras.engine.training.Model
Residual block for 1D (temporal) ResNet architectures.
 Args:
 filters: int
The number of filters in the block
 strides: int
Strides used in convolutional layers within the block
 residual_path: bool
Whether or not the block will contain a residual path
 Returns:
A ResNetBlock object. The block itself is a tensorflow model and can be used as such.

call
(inputs, training=None)[source]¶ Calls the model on new inputs.
In this case call just reapplies all ops in the graph to the new inputs (e.g. build a new computational graph from the provided inputs).
 Arguments:
inputs: A tensor or list of tensors. training: Boolean or boolean scalar tensor, indicating whether to run
the Network in training mode or inference mode.
 mask: A mask or list of masks. A mask can be
either a tensor or None (no mask).
 Returns:
A tensor if there is a single output, or a list of tensors if there are more than one outputs.

class
ketos.neural_networks.resnet.
ResNet1DInterface
(block_sets=[2, 2, 2], n_classes=2, initial_filters=2, optimizer=Adam ketos recipe, loss_function=CategoricalCrossentropy ketos recipe, metrics=[CategoricalAccuracy ketos recipe, Precision ketos recipe, Recall ketos recipe])[source]¶ Bases:
ketos.neural_networks.resnet.ResNetInterface

classmethod
transform_batch
(x, y, y_fields=['label'], n_classes=2)[source]¶ Transforms a training batch into the format expected by the network.
When this interface is subclassed to make new neural_network classes, this method can be overwritten to accomodate any transformations required. Common operations are reshaping of input arrays and parsing or one hot encoding of the labels.
 Args:
 x:numpy.array
The batch of inputs with shape (batch_size, width, height)
 y:numpy.array
The batch of labels. Each label must be represented as an integer, ranging from zero to n_classes The array is expected to have a field named ‘label’.
 n_classes:int
The number of possible classes for one hot encoding.
 Returns:
 X:numpy.array
The transformed batch of inputs
 Y:numpy.array
The transformed batch of labels
 Examples:
>>> import numpy as np >>> # Create a batch of 10 5x5 arrays >>> inputs = np.random.rand(10,5,5) >>> inputs.shape (10, 5, 5)
>>> # Create a batch of 10 labels (0 or 1) >>> labels = np.random.choice([0,1], size=10).astype([('label','<i4')]) >>> labels.shape (10,)
>>> transformed_inputs, transformed_labels = NNInterface.transform_batch(inputs, labels, n_classes=2) >>> transformed_inputs.shape (10, 5, 5, 1)
>>> transformed_labels.shape (10, 2)

classmethod

class
ketos.neural_networks.resnet.
ResNetArch
(n_classes, pre_trained_base=None, block_sets=None, initial_filters=16, **kwargs)[source]¶ Bases:
tensorflow.python.keras.engine.training.Model
Implements a ResNet architecture, building on top of ResNetBlocks.
 Args:
 block_sets: list of ints
A list specifying the block sets and how many blocks each set contains. Example: [2,2,2] will create a ResNet with 3 block sets, each containing 2 ResNetBlocks (i.e.: a total of 6 residual blocks)
 n_classes:int
The number of classes. The output layer uses a Softmax activation and will contain this number of nodes, resulting in model outputs with this many values summing to 1.0.
 initial_filters:int
The number of filters used in the first ResNetBlock. Subsequent blocks will have two times more filters than their previous block.
 pre_trained_base: instance of ResNetArch
A pretrained resnet model from which the residual blocks will be taken. Use by the the clone_with_new_top method when creating a clone for transfer learning
 Returns:
A ResNetArch object, which is a tensorflow model.

call
(inputs, training=None)[source]¶ Calls the model on new inputs.
In this case call just reapplies all ops in the graph to the new inputs (e.g. build a new computational graph from the provided inputs).
 Arguments:
inputs: A tensor or list of tensors. training: Boolean or boolean scalar tensor, indicating whether to run
the Network in training mode or inference mode.
 mask: A mask or list of masks. A mask can be
either a tensor or None (no mask).
 Returns:
A tensor if there is a single output, or a list of tensors if there are more than one outputs.

clone_with_new_top
(n_classes=None, freeze_base=True)[source]¶ Clone this instance but replace the original classification top with a new (untrained) one
 Args:
 n_classes:int
The number of classes the new classification top should output. If None(default), the original number of classes will be used.
 freeze_base:bool
If True, the weights of the feature extraction base will be froze (untrainable) in the new model.
 Returns:
 cloned_model: instance of ResNetArch
The new model with the old feature extraction base and new classification top.

freeze_block
(block_ids)[source]¶ Freeze specific residual blocks
 Args:
 blocks_ids: int
The block number to be freezed (starting from zero)

get_feature_extraction_base
()[source]¶ Retrive the feature extraction base (initial convolutional layer + residual blocks)
 Returns:
list containing the feature extraction layers

class
ketos.neural_networks.resnet.
ResNetBlock
(filters, strides=1, residual_path=False)[source]¶ Bases:
tensorflow.python.keras.engine.training.Model
Residual block for ResNet architectures.
 Args:
 filters: int
The number of filters in the block
 strides: int
Strides used in convolutional layers within the block
 residual_path: bool
Whether or not the block will contain a residual path
 Returns:
A ResNetBlock object. The block itself is a tensorflow model and can be used as such.

call
(inputs, training=None)[source]¶ Calls the model on new inputs.
In this case call just reapplies all ops in the graph to the new inputs (e.g. build a new computational graph from the provided inputs).
 Arguments:
inputs: A tensor or list of tensors. training: Boolean or boolean scalar tensor, indicating whether to run
the Network in training mode or inference mode.
 mask: A mask or list of masks. A mask can be
either a tensor or None (no mask).
 Returns:
A tensor if there is a single output, or a list of tensors if there are more than one outputs.

class
ketos.neural_networks.resnet.
ResNetInterface
(block_sets=[2, 2, 2], n_classes=2, initial_filters=16, optimizer=Adam ketos recipe, loss_function=BinaryCrossentropy ketos recipe, metrics=[BinaryAccuracy ketos recipe, Precision ketos recipe, Recall ketos recipe])[source]¶ Bases:
ketos.neural_networks.dev_utils.nn_interface.NNInterface
Creates a ResNet model with the standardized Ketos interface.
 Args:
 block_sets: list of ints
A list specifying the block sets and how many blocks each set contains. Example: [2,2,2] will create a ResNet with 3 block sets, each containing 2 ResNetBlocks (i.e.: a total of 6 residual blocks)
 n_classes:int
The number of classes. The output layer uses a Softmax activation and will contain this number of nodes, resulting in model outputs with this many values summing to 1.0.
 initial_filters:int
The number of filters used in the first ResNetBlock. Subsequent blocks will have two times more filters than their previous block.
 optimizer: ketos.neural_networks.RecipeCompat object
A recipe compatible optimizer (i.e.: wrapped by the ketos.neural_networksRecipeCompat class)
 loss_function: ketos.neural_networks.RecipeCompat object
A recipe compatible loss_function (i.e.: wrapped by the ketos.neural_networksRecipeCompat class)
 metrics: list of ketos.neural_networks.RecipeCompat objects
A list of recipe compatible metrics (i.e.: wrapped by the ketos.neural_networksRecipeCompat class). These metrics will be computed on each batch during training.
 secondary_metrics: list of ketos.neural_networks.RecipeCompat objects
A list of recipe compatible metrics (i.e.: wrapped by the ketos.neural_networksRecipeCompat class). These can be used as additional metrics. Computed at each batch during training but only printed or logged as the average at the end of the epoch