Provably robust neural networks

A repository for training provably robust neural networks by optimizing convex outer bounds on the adversarial polytope. Created by Eric Wong and Zico Kolter. Link to the original arXiv paper. The method has been further extended to be fully modular, scalable, and use cascades to improve robust error. Check out our new paper on arXiv: Scaling provable adversarial defenses.

News

Installation & Usage

You can install this repository with pip install convex_adversarial

If you wish to have the version of code that reflects the first paper, use pip install convex_adversal=0.2, or clone the 0.2 release on Github.

The package contains the following functions:

Why do we need robust networks?

While networks are capable of representing highly complex functions. For example, with today's networks it is an easy task to achieve 99% accuracy on the MNIST digit recognition dataset, and we can quickly train a small network that can accurately predict that the following image is a 7.

However, the versatility of neural networks comes at a cost: these networks are highly susceptible to small perturbations, or adversarial attacks (e.g. the fast gradient sign method and projected gradient descent)! While most of us can recognize that the following image is still a 7, the same network that could correctly classify the above image instead classifies the following image as a 3.

While this is a relatively harmless example, one can easily think of situations where such adversarial perturbations can be dangerous and costly (e.g. autonomous driving).

What are robust networks?

Robust networks are networks that are trained to protect against any sort of adversarial perturbation. Specifically, for any seen training example, the network is robust if it is impossible to cause the network to incorrectly classify the example by adding a small perturbation.

How do we do this?

The short version: we use the dual of a convex relaxation of the network over the adversarial polytope to lower bound the output. This lower bound can be expressed as another deep network with the same model parameters, and optimizing this lower bound allows us to guarantee robustness of the network.

The long version: see our original paper, Provable defenses against adversarial examples via the convex outer adversarial polytope.

For our updated version which is scalable, modular, and achieves even better robust performance, see our new paper, Scaling provable adversarial defenses.

What difference does this make?

We illustrate the power of training robust networks in the following two scenarios: 2D toy case for a visualization, and on the MNIST dataset. More experiments are in the paper.

2D toy example

To illustrate the difference, consider a binary classification task on 2D space, separating red dots from blue dots. Optimizing a neural network in the usual fashion gives us the following classifier on the left, and our robust method gives the classifier on the right. The squares around each example represent the adversarial region of perturbations.

For the standard classifier, a number of the examples have perturbation regions that contain both red and blue. These examples are susceptible to adversarial attacks that will flip the output of the neural network. On the other hand, the robust network has all perturbation regions fully contained in the either red or blue, and so this network is robust: we are guaranteed that there is no possible adversarial perturbation to flip the label of any example.

Robustness to adversarial attacks: MNIST classification

As mentioned before, it is easy to fool networks trained on the MNIST dataset when using attacks such as the fast gradient sign method (FGS) and projected gradient descent (PGD). We observe that PGD can almost always fool the MNIST trained network.

Base error FGS error PGD Error Robust Error
Original 1.1% 50.0% 81.7% 100%
Robust 1.8% 3.9% 4.1% 5.8%

On the other hand, the robust network is significantly less affected by these attacks. In fact, when optimizing the robust loss, we can additionally calculate a robust error which gives an provable upper bound on the error caused by any adversarial perturbation. In this case, the robust network has a robust error of 5.8%, and so we are guaranteed that no adversarial attack can ever get an error rate of larger than 5.8%. In comparison, the robust error of the standard network is 100%. More results on HAR, Fashion-MNIST, and SVHN can be found in the paper. Results for the scalable version with random projections on residual networks and on the CIFAR10 dataset can be found in our second paper.

Modularity

Dual operations

The package currently has dual operators for the following constrained input spaces and layers. These are defined in dual_inputs.py and dual_layers.py.

Dual input spaces

Dual layers

Due to the modularity of the implementation, it is easy to extend the methodology to additional dual layers. A dual input or dual layer can be implemented by filling in the following signature:

class DualObject(nn.Module, metaclass=ABCMeta): 
    @abstractmethod
    def __init__(self): 
        """ Initialize a dual layer by initializing the variables needed to
        compute this layer's contribution to the upper and lower bounds. 

        In the paper, if this object is at layer i, this is initializing `h'
        with the required cached values when nu[i]=I and nu[i]=-I. 
        """pass

    @abstractmethod
    def apply(self, dual_layer):
        """ Advance cached variables initialized in this class by the given
        dual layer.  """
        raise NotImplementedError

    @abstractmethod
    def bounds(self): 
        """ Return this layers contribution to the upper and lower bounds. In
        the paper, this is the `h' upper bound where nu is implicitly given by
        c=I and c=-I. """
        raise NotImplementedError

    @abstractmethod
    def objective(self, *nus): 
        """ Return this layers contribution to the objective, given some
        backwards pass. In the paper, this is the `h' upper bound evaluated on a
        the given nu variables. 

        If this is layer i, then we get as input nu[k] through nu[i]. 
        So non-residual layers will only need nu[-1] and nu[-2]. """
        raise NotImplementedError

class DualLayer(DualObject): 
    @abstractmethod
    def forward(self, *xs): 
        """ Given previous inputs, apply the affine layer (forward pass) """ 
        raise NotImplementedError

    @abstractmethod
    def T(self, *xs): 
        """ Given previous inputs, apply the transposed affine layer 
        (backward pass) """
        raise NotImplementedError

Residual networks / skip connections

To create sequential PyTorch modules with skip connections, we provide a generalization of the PyTorch module nn.Sequential. Specifically, we have a DenseSequential module that is identical to nn.Sequential but also takes in Dense' modules. TheDense' modules consist of m layers, and applies these m layers to the last m outputs of the network.

As an example, the following is a simple two layer network with a single skip connection. The first layer is identical to a normal nn.Conv2d layer. The second layer has a skip connection from the layer with 16 filters and also a normal convolutional layer from the previous layer with 32 filters.

residual_block = DenseSequential([
    Dense(nn.Conv2d(16,32,...)),
    nn.ReLU(), 
    Dense(nn.Conv2d(16,32,...), None, nn.Conv2d(32,32,...))
])

What is in this repository?