alibi_detect.models.tensorflow.resnet module

class alibi_detect.models.tensorflow.resnet.LearningRateBatchScheduler(schedule, batch_size, steps_per_epoch)[source]

Bases: Callback

__init__(schedule, batch_size, steps_per_epoch)[source]

Callback to update learning rate on every batch instead of epoch.

Parameters:
  • schedule (Callable) – Function taking the epoch and batch index as input which returns the new learning rate as output.

  • batch_size (int) – Batch size.

  • steps_per_epoch (int) – Number of batches or steps per epoch.

on_batch_begin(batch, logs=None)[source]

Executes before step begins.

on_epoch_begin(epoch, logs=None)[source]
alibi_detect.models.tensorflow.resnet.conv_block(x_in, filters, kernel_size, stage, block, strides=(2, 2), l2_regularisation=True)[source]

Conv block in ResNet with a parameterised skip connection to reduce the width and height controlled by the strides.

Parameters:
  • x_in (Tensor) – Input Tensor.

  • filters (Tuple[int, int]) – Number of filters for each of the 2 conv layers.

  • kernel_size (Union[int, list, Tuple[int]]) – Kernel size for the conv layers.

  • stage (int) – Stage of the block in the ResNet.

  • block (str) – Block within a stage in the ResNet.

  • strides (Tuple[int, int]) – Stride size applied to reduce the image size.

  • l2_regularisation (bool) – Whether to apply L2 regularisation.

Return type:

Tensor

Returns:

Output Tensor of the conv block.

alibi_detect.models.tensorflow.resnet.identity_block(x_in, filters, kernel_size, stage, block, l2_regularisation=True)[source]

Identity block in ResNet.

Parameters:
  • x_in (Tensor) – Input Tensor.

  • filters (Tuple[int, int]) – Number of filters for each of the 2 conv layers.

  • kernel_size (Union[int, list, Tuple[int]]) – Kernel size for the conv layers.

  • stage (int) – Stage of the block in the ResNet.

  • block (str) – Block within a stage in the ResNet.

  • l2_regularisation (bool) – Whether to apply L2 regularisation.

Return type:

Tensor

Returns:

Output Tensor of the identity block.

alibi_detect.models.tensorflow.resnet.l2_regulariser(l2_regularisation=True)[source]

Apply L2 regularisation to kernel.

Parameters:

l2_regularisation (bool) – Whether to apply L2 regularisation.

Returns:

Kernel regularisation.

alibi_detect.models.tensorflow.resnet.learning_rate_schedule(current_epoch, current_batch, batches_per_epoch, batch_size)[source]

Linear learning rate scaling and learning rate decay at specified epochs.

Parameters:
  • current_epoch (int) – Current training epoch.

  • current_batch (int) – Current batch with current epoch, not used.

  • batches_per_epoch (int) – Number of batches or steps in an epoch, not used.

  • batch_size (int) – Batch size.

Return type:

float

Returns:

Adjusted learning rate.

alibi_detect.models.tensorflow.resnet.preprocess_image(x, is_training=True)[source]
Return type:

ndarray

alibi_detect.models.tensorflow.resnet.resnet(num_blocks, classes=10, input_shape=(32, 32, 3))[source]

Define ResNet.

Parameters:
  • num_blocks (int) – Number of ResNet blocks.

  • classes (int) – Number of classification classes.

  • input_shape (Tuple[int, int, int]) – Input shape of an image.

Return type:

Model

Returns:

ResNet as a tf.keras.Model.

alibi_detect.models.tensorflow.resnet.resnet_block(x_in, size, filters, kernel_size, stage, strides=(2, 2), l2_regularisation=True)[source]

Block in ResNet combining a conv block with identity blocks.

Parameters:
  • x_in (Tensor) – Input Tensor.

  • size (int) – The ResNet block consists of 1 conv block and size-1 identity blocks.

  • filters (Tuple[int, int]) – Number of filters for each of the conv layers.

  • kernel_size (Union[int, list, Tuple[int]]) – Kernel size for the conv layers.

  • stage (int) – Stage of the block in the ResNet.

  • strides (Tuple[int, int]) – Stride size applied to reduce the image size.

  • l2_regularisation (bool) – Whether to apply L2 regularisation.

Return type:

Tensor

Returns:

Output Tensor of the conv block.

alibi_detect.models.tensorflow.resnet.run(num_blocks, epochs, batch_size, model_dir, num_classes=10, input_shape=(32, 32, 3), validation_freq=10, verbose=2, seed=1, serving=False)[source]
Return type:

None

alibi_detect.models.tensorflow.resnet.scale_by_instance(x, eps=1e-12)[source]
Return type:

ndarray