perceptron.models

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

Model Base class to provide metrics with a unified interface to models.
DifferentiableModel Base class for differentiable models that provide gradients.

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.
AipModel Base class for models hosted on Baidu AIP platform.
AipAntiPornModel Create a Model instance from an AipAntiPorn model.
GoogleCloudModel Base class for models in Google Cloud.
GoogleSafeSearchModel Create a :class: Model instance from a GoogleSafeSearchModel model.
GoogleObjectDetectionModel Create a :class: Model instance from a GoogleObjectDetectionModel model.
KerasYOLOv3Model
KerasSSD300Model

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

class perceptron.models.Model(bounds, channel_axis, preprocessing=(0, 1))[source]

Base class to provide metrics with a unified interface to models.

predictions(self, image)[source]

Calculate prediction for a single image.

class perceptron.models.DifferentiableModel(bounds, channel_axis, preprocessing=(0, 1))[source]

Base class for differentiable models that provide gradients.

The DifferentiableModel class can be used as a base class for models that provide gradients. Subclasses must implement predictions_and_gradient().

backward(self, gradient, image)[source]

Backpropagates the gradient of some loss w.r.t. the logits through the network and returns the gradient of that loss w.r.t. the input image.

Parameters:
gradient : numpy.ndarray

Gradient of some loss w.r.t. the logits.

image : numpy.ndarry

Image with shape (height, width, channels).

Returns:
gradient : numpy.ndarray

The gradient w.r.t. the image.

batch_predictions(self, images)[source]

Calculates predictions for a batch of images.

Parameters:
images : numpy.ndarray

Batch of images with shape (batch size, height, width, channels).

Returns:
numpy.ndarray

Predictions (logits, or with bounding boxes).

gradient(self, image, label)[source]

Calculates the gradient of the cross-entropy loss w.r.t. the image.

The default implementation calls predictions_and_gradient. Subclasses can provide more efficient implementations that only calculate the gradient.

Parameters:
image : numpy.ndarray

The gradient of the cross-entropy loss w.r.t. the image. Will have the same shape as the image.

num_classes(self)[source]

Determines the number of classes.

predictions(self, image)[source]

Convenience method that calculates predictions for a single image.

predictions_and_gradient(self, image, label)[source]

Calculates predictions for an image and the gradient of the cross-entropy loss w.r.t. the image.

Parameters:
image : numpy.ndarray

Image with shape (height, width, channels).

label : int

Reference label used to calculate the gradient.

Returns:
predictions : numpy.ndarray

Vector of predictions. (logits, or with bounding boxes).

gradient : numpy.ndarray

The gradient of the cross-entropy loss w.r.t. the image. Will have the same shape as the image.