Home

Awesome

Provably Robust Boosted Decision Stumps and Trees against Adversarial Attacks

<p align="center"><img src="images/abstract.png" width="600"></p>

NeurIPS 2019

Maksym Andriushchenko, Matthias Hein

University of Tübingen

Paper: http://arxiv.org/abs/1906.03526

This repository contains a mini-library for training provably robust boosted decision stumps and trees written in python. Thus, it is easy to modify and experiment with this code, unlike the code of xgboost or lightgbm which are implemented in C++. To foster reproducible research, we also provide code for all main experiments reported in the paper (see exps.sh). Moreover, we also provide code for all figures shown in the paper, each as a separate Jupyter notebook (see folder notebooks). All dependencies are collected in Dockerfile.

Models: All our provably robust models (stumps and trees) are publicly available by this link including our MNIST, FMNIST, and CIFAR-10 models.

Version 2.0 of the repository (corresponds to the NeurIPS'19 camera-ready version):

Main idea of the paper

We propose provable defenses against adversarial attack for boosted decision stumps and trees. Here is the effect of our method on a 2D dataset.

<p align="center"><img src="images/toy2d_stumps_trees.png" width="700"></p>

Provably robust training

We follow the framework of robust optimization aiming at solving the following min-max problem:

<p align="center"><img src="images/general_robust_optimization.png" width="300"></p>

We first derive the robustness certificates. The certification for boosted stumps is exact:

<p align="center"><img src="images/certificate_stumps.png" width="650"></p> For boosted trees, we derive a simple lower bound on the functional margin which, however, becomes tight after robust training. <p align="center"><img src="images/certificate_trees.png" width="550"></p>

Then we integrate these certificates into training which leads to the exact robust loss or to an upper bound on the robust loss for stumps and trees respectively.

How we minimize these robust losses? Surprisingly, it results in a convex optimization problem wrt the parameters of the stumps or trees. We use coordinate descent combined with bisection to solve for w_r and w_l. For more details, see the paper.

Experiments

Experimental results show the efficiency of the robust training methods for boosted stumps and boosted trees:

<p align="center"><img src="images/tables_rte_stumps.png" width="650"></p> <p align="center"><img src="images/tables_rte_trees.png" width="650"></p>

Moreover, although decision trees as weak learners are obviously not suitable for computer vision tasks, our robust boosted trees nonetheless show provable robustness competitive to provably robust CNNs outperforming almost all proposed in the literature approaches:

<p align="center"><img src="images/comparison_cnns.png" width="650"></p>

Effect of robust training

The effect of robust training can be clearly seen based on the splitting thresholds that robust models select

<p align="center"><img src="images/thresholds_histograms.png" width="750"></p>

Exact adversarial examples

Using our exact certification routine, we can also efficiently (without any combinatorial solvers) find provably minimal (exact) adversarial examples wrt Linf-norm for boosted stumps:

<p align="center"><img src="images/exact_adv_examples.png" width="700"></p>

Interpretability

One of the main advantages of boosted trees is their interpretability and transparent decision making. Unlike neural networks, tree ensembles can be directly visualized based on which features they actually use for classification. Here is an example of our provably robust boosted trees on MNIST 2 vs 6:

<!-- ![](images/trees.gif) --> <p align="center"><img src="images/trees.gif" width="500"></p>

Code for training provably robust boosted stumps and trees

Simple example

import numpy as np
import data
from tree_ensemble import TreeEnsemble

n_trees = 50  # total number of trees in the ensemble
model = 'robust_bound'  # robust tree ensemble
X_train, y_train, X_test, y_test, eps = data.all_datasets_dict['breast_cancer']()
X_train, X_test = data.convert_to_float32(X_train), data.convert_to_float32(X_test)

# initialize a tree ensemble with some hyperparameters
ensemble = TreeEnsemble(weak_learner='tree', n_trials_coord=X_train.shape[1], 
                        lr=0.01, min_samples_split=10, min_samples_leaf=5, max_depth=4, 
                        max_weight=1.0, idx_clsf=0)
# initialize gammas, per-example weights which are recalculated each iteration
gamma = np.ones(X_train.shape[0])
for i in range(1, n_trees + 1):
    # fit a new tree in order to minimize the robust loss of the whole ensemble
    weak_learner = ensemble.fit_tree(X_train, y_train, gamma, model, eps, depth=1)
    margin_prev = ensemble.certify_treewise(X_train, y_train, eps)  # needed for pruning
    ensemble.add_weak_learner(weak_learner)
    ensemble.prune_last_tree(X_train, y_train, margin_prev, eps, model)
    # calculate per-example weights for the next iteration
    gamma = np.exp(-ensemble.certify_treewise(X_train, y_train, eps))
    
    # track generalization and robustness
    yf_test = y_test * ensemble.predict(X_test)
    min_yf_test = ensemble.certify_treewise(X_test, y_test, eps)
    if i == 1 or i % 5 == 0:
        print('Iteration: {}, test error: {:.2%}, upper bound on robust test error: {:.2%}'.format(
            i, np.mean(yf_test < 0.0), np.mean(min_yf_test < 0.0)))
Iteration: 1, test error: 2.92%, upper bound on robust test error: 10.95%
Iteration: 5, test error: 2.92%, upper bound on robust test error: 10.95%
Iteration: 10, test error: 2.19%, upper bound on robust test error: 10.22%
Iteration: 15, test error: 2.19%, upper bound on robust test error: 10.22%
Iteration: 20, test error: 2.19%, upper bound on robust test error: 10.22%
Iteration: 25, test error: 2.19%, upper bound on robust test error: 10.22%
Iteration: 30, test error: 1.46%, upper bound on robust test error: 8.03%
Iteration: 35, test error: 1.46%, upper bound on robust test error: 8.03%
Iteration: 40, test error: 1.46%, upper bound on robust test error: 7.30%
Iteration: 45, test error: 1.46%, upper bound on robust test error: 7.30%
Iteration: 50, test error: 0.73%, upper bound on robust test error: 6.57%

Full training

One can train robust stumps or trees using train.py. The full list of possible arguments is available by python train.py --help.

Boosted stumps models:

Boosted trees models:

Note that Linf epsilons for adversarial attacks are specified for each dataset separately in data.py.

See more examples how to train our models on different datasets and in different settings in exps.sh.

Evaluation

eval.py and notebooks/adv_examples.ipynb show how one can restore a trained model in order to evaluate it (e.g., calculate the robustness bounds or to show the adversarial examples).

In order to evaluate the boosted tree models using MILP, we refer to this repository.

Jupyter notebooks to reproduce the figures

Dependencies

All dependencies are collected in Dockerfile. The best way to reproduce our environment is to use Docker. Just build the image and then run the container:

Faster training

Running the code on small- and middle-scale datasets should be fast, however large-scale datasets (especially with many classes) may need some time to train. In case you want to get results faster, there are several options to speed up the training:

Export the model in XGBoost-compatible format

import json

ensembles = []
for i_clsf in range(n_classifiers):
    ensembles.append(TreeEnsemble(weak_learner, 0, 0, 0, 0, 0, 0, 0, 0, 0))

model_ova = OneVsAllClassifier(ensembles)
model_ova.load('{}/{}.model.npy'.format(exp_folder, args.model_path), iteration=iter_to_take)

model_list_of_dicts = model.dump_model()
with open('model.json', 'w') as f:
    json.dump(model_list_of_dicts, f)

Contact

Do you have a problem or question regarding the code? Please don't hesitate to open an issue or contact Maksym Andriushchenko directly. We would also like to hear from you if you find the code useful in some applications.

Citation

@article{andriushchenko2019provably,
  title={Provably Robust Boosted Decision Stumps and Trees against Adversarial Attacks},
  author={Andriushchenko, Maksym and Hein, Matthias},
  conference={Advances in Neural Information Processing Systems},
  year={2019}
}