Wrapper for Classification Models

Provides class to wrap existing models in different frameworks so that they provide a unified API to the benchmarks.

KerasModel Create a Model instance from a Keras model.
PyTorchModel Creates a Model instance from a PyTorch module.
class perceptron.models.classification.KerasModel(model, bounds, channel_axis=3, preprocessing=(0, 1), predicts='probabilities')[source]

Create a Model instance from a Keras model.

Parameters:
model : keras.model.Model

The Keras model that are loaded.

bounds : tuple

Tuple of lower and upper bound for the pixel values, usually (0, 1) or (0, 255).

channel_axis : int

The index of the axis that represents color channels.

preprocessing: 2-element tuple with floats or numpy arrays

Elementwises preprocessing of input; we first substract the first element of preprocessing from the input and then divide the input by the second element.

predicts : str

Specifies whether the Keras model predicts logits or probabilities. Logits are preferred, but probabilities are the default.

backward(self, gradient, image)[source]

Get gradients w.r.t. the original image.

batch_predictions(self, images)[source]

Batch prediction of images.

model_task(self)[source]

Get the task that the model is used for.

num_classes(self)[source]

Return number of classes.

predictions_and_gradient(self, image, label)[source]

Returns both predictions and gradients.

class perceptron.models.classification.PyTorchModel(model, bounds, num_classes, channel_axis=1, device=None, preprocessing=(0, 1))[source]

Creates a Model instance from a PyTorch module.

Parameters:
model : torch.nn.Module

The PyTorch model that are loaded.

bounds : tuple

Tuple of lower and upper bound for the pixel values, usually (0, 1) or (0, 255).

num_classes : int

Number of classes for which the model will output predictions.

channel_axis : int

The index of the axis that represents color channels.

device : string

A string specifying the device to do computation on. If None, will default to “cuda:0” if torch.cuda.is_available() or “cpu” if not.

preprocessing: 2-element tuple with floats or numpy arrays

Elementwises preprocessing of input; we first subtract the first element of preprocessing from the input and then divide the input by the second element.

backward(self, gradient, image)[source]

Get gradients w.r.t. the original image.

batch_predictions(self, images)[source]

Batch prediction of images.

model_task(self)[source]

Get the task that the model is used for.

num_classes(self)[source]

Return number of classes.

predictions_and_gradient(self, image, label)[source]

Returns both predictions and gradients.