Requirements 
------------
GNU C compiler, ELINA, Gurobi's Python interface,

python3.6 or higher, tensorflow 1.11 or higher, numpy.


Installation
------------

The following dependencies can be installed step by step (sudo rights might be required):

Install m4:
```
wget ftp://ftp.gnu.org/gnu/m4/m4-1.4.1.tar.gz
tar -xvzf m4-1.4.1.tar.gz
cd m4-1.4.1
./configure
make
make install
cp src/m4 /usr/bin
cd ..
rm m4-1.4.1.tar.gz
```

Install gmp:
```
wget https://gmplib.org/download/gmp/gmp-6.1.2.tar.xz
tar -xvf gmp-6.1.2.tar.xz
cd gmp-6.1.2
./configure --enable-cxx
make
make install
cd ..
rm gmp-6.1.2.tar.xz
```

Install mpfr:
```
wget https://www.mpfr.org/mpfr-current/mpfr-4.0.2.tar.xz
tar -xvf mpfr-4.0.2.tar.xz
cd mpfr-4.0.1
./configure
make
make install
cd ..
rm mpfr-4.0.1.tar.xz
```

Install ELINA:
```
git clone https://github.com/eth-sri/ELINA.git
cd ELINA
./configure
make
make install
cd ..
```

Install Gurobi:
```
wget https://packages.gurobi.com/8.1/gurobi8.1.0_linux64.tar.gz
tar -xvf gurobi8.1.0_linux64.tar.gz
cd gurobi810/linux64
sudo python3 setup.py install
sudo cp lib/libgurobi81.so /usr/lib
sudo cp include/gurobi_c.h /usr/include
cd ../../
ldconfig
```

Update environment variables:
```
export GUROBI_HOME="Current_directory/gurobi810/linux64"
export PATH="${PATH}:${GUROBI_HOME}/bin"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:${GUROBI_HOME}/lib

```

Install tensorflow and numpy:
```
pip3 install numpy
pip3 install tensorflow

```

Install krelu implementation: (make sure that you are in `krelu` directory)
```
make
make install
```

We also provide an install script `install.sh` (sudo rights may be required).
Ensure the following are installed before running the script:
gcc, make, python3, pip3, git
After running the script, make sure that these path variables are updated:
Update environment variables:
```
export GUROBI_HOME="Current_directory/gurobi810/linux64"
export PATH="${PATH}:${GUROBI_HOME}/bin"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:${GUROBI_HOME}/lib

```

**IMPORTANT**
Note that to run the framework with Gurobi one needs to obtain an academic license for gurobi from https://user.gurobi.com/download/licenses/free-academic.


Usage
-------------

```
cd tf-verify

python3 . --netname <path to the network file> --epsilon <float between 0 and 1> --domain <deeppoly/refinezono/refinepoly> --dataset <mnist/cifar10> [optional] --use_milp {True/False} --use_2relu {True/False} --use_3relu {True/False} --timeout_lp <float> --timeout_milp <float> --imageno <int>
```

* ```<epsilon>```: specifies bound for the L∞-norm based perturbation (default is 0). This parameter is not required for testing ACAS Xu networks.
* ```<netname>```: network files must be downloaded separately (more in Datasets section)
* ```<imageno>```: a number between 0 and 99 inclusive
* ```<domain>```: refinepoly for krelu combined with the DeepPoly abstraction as in the paper, deeppoly for the DeepPoly domain and refinezono for krelu combined with the Zonotope domain.
* use_milp: default is True, whether to use the MILP solver
* use_2relu: default is False, whether to use 2-ReLU
* use_3relu: default is True, whether to use 3-ReLU
* timeout_lp: default is 1, set to 10 for MNIST CNN and 15 for CIFAR10 CNN
* timeout_milp: default is 1, set to 10 for MNIST CNN and 15 for CIFAR10 CNN
* In the current setting only one of use_3relu or use_2relu should be set to True. 
* One can configure the system to produce the same results as refinezono from the ICLR'19 paper by setting use_milp and use_1relu option to True while setting use_2relu and use_3relu to False and the timeout_lp and timeout_milp to the options reported in the refinezono paper.


Example
-------------

```
python3 . --netname ../netfiles/mnist/mnist_relu_6_100.tf --epsilon 0.027 --domain refinepoly --dataset mnist --use_milp False --use_3relu False --imageno 5
```

Our analyzer will print the following:

* 'Verified' for an image when it can prove the robustness of the network and 'Failed' when it cannot. It will also print an error message when the network misclassifies an image.

* the timing in seconds.

* Bounds that the verifier proves for the last layer in the network

Neural Networks and Datasets
---------------

All the networks used in our experiments are taken from the publicly available database of networks at https://github.com/eth-sri/eran.

We provide the first 100 images from the testset of both MNIST and CIFAR10 datasets in the 'data' folder. Our analyzer first verifies whether the neural network classifies an image correctly before performing robustness analysis. 

Reproducing Results
-------------------
The verifier uses a timeout for LP and MILP solvers. Thus, results may vary depending on processor speed.

Acknowledgement
---------------
This implementation is built on top of [ERAN](https://github.com/eth-sri/eran).


