Commit 1b72c143 authored by kjam's avatar kjam
Browse files

updating with final notebook and requirements

parent 9dd4c529
Loading
Loading
Loading
Loading
+1 −1

File changed.

Contains only whitespace changes.

+195 −0
Changes for notebooks/04 - Case Study - Adversarial Detection.ipynb: 195 added lines, 0 removed lines.
Original line number Diff line number Diff line
%% Cell type:markdown id: tags:

### Adversarial Detection using FeatureSqueezing and MagNet

In this notebook, we'll explore adversarial detection using FeatureSqueezing and MagNet. This code has been adapted from [the EvadeML repository](https://github.com/mzweilin/EvadeML-Zoo) with changes to enable it to run properly with Python3 and Jupyter notebooks.

To run locally, I recommend following the latest instructions on the EvadeML repository and the [MagNet repository](https://github.com/Trevillie/MagNet).

%% Cell type:code id: tags:

``` python
%matplotlib inline
```

%% Cell type:code id: tags:

``` python
cd ../EvadeML-Zoo
```

%% Output

    /home/katharine/wrrk/my_classes/oreilly_live/data-security-for-data-science/EvadeML-Zoo

%% Cell type:code id: tags:

``` python
from main import main
```

%% Output

    Using TensorFlow backend.

%% Cell type:code id: tags:

``` python
!python main.py -h
```

%% Cell type:code id: tags:

``` python
main(dataset_name="MNIST",
     model_name="cleverhans",
     detection="MagNet",
     attacks="FGSM?eps=0.1;BIM?eps=0.1&eps_iter=0.02;JSMA?targeted=next"
)
```

%% Cell type:markdown id: tags:

### Let's check out the adversarial examples generated

%% Cell type:code id: tags:

``` python
from IPython.display import Image
Image(filename=
      'results/MNIST_100_1d1b8_cleverhans/MNIST_100_1d1b8_cleverhans_attacks_07450_examples.png')
```

%% Cell type:markdown id: tags:

### And let's take a look at what attacks were not detected!

%% Cell type:code id: tags:

``` python
from IPython.display import Image
Image(filename=
      'results/MNIST_100_1d1b8_cleverhans/attacks_jsma?targeted=next_undetected.png')
```

%% Cell type:markdown id: tags:

#### Your Turn:
- How does FeatureSqueezing compare as a detection mechanism? To use the FeatureSqueezing, you will need to add proper parameters. Run the below cell for a few examples.
- Try a different attack or detection (to see all options, use `main(show_help=True)`).
- Try setting different parameters for the cleverhans attack above.
- Are your new attacks more successful? Why or why not?

%% Cell type:code id: tags:

``` python
```

%% Cell type:code id: tags:

``` python
```

%% Cell type:code id: tags:

``` python
!cat Reproduce_FeatureSqueezing.md | grep FeatureSqueezing
```

%% Cell type:code id: tags:

``` python
main(dataset_name="MNIST",
     model_name="cleverhans",
     detection="FeatureSqueezing?squeezers=bit_depth_5,median_filter_2_2&distance_measure=l1&fpr=0.05",
     attacks="FGSM?eps=0.1;BIM?eps=0.1&eps_iter=0.02;JSMA?targeted=next"
)
```

%% Cell type:code id: tags:

``` python
from IPython.display import Image
from glob import glob

for img in glob('results/MNIST_100_1d1b8_cleverhans/undetected_attacks_*'):
    print(img)
    display(Image(filename=img))
```

%% Cell type:code id: tags:

``` python
main(dataset_name="CIFAR-10",
     model_name="DenseNet",
     attacks="fgsm?eps=0.0156;bim?eps=0.008&eps_iter=0.0012;carlinili?targeted=next&confidence=5;carlinili?targeted=ll&confidence=5;deepfool?overshoot=10;carlinil2?targeted=next&batch_size=100&max_iterations=1000&confidence=5;carlinil2?targeted=ll&batch_size=100&max_iterations=1000&confidence=5;carlinil0?targeted=next&confidence=5;carlinil0?targeted=ll&confidence=5;jsma?targeted=next;jsma?targeted=ll;",
     detection="FeatureSqueezing?squeezers=bit_depth_5,median_filter_2_2,non_local_means_color_13_3_2&distance_measure=l1&threshold=1.7547;MagNet"
)
```

%% Output

    Located Cleverhans
    Located Carlini_nn_robust_attacks
    Located Keras-deep-learning-models
    Located MobileNets
    Located Deepfool/Universal
    Located DenseNet
    Located MagNet
    Flags are {'dataset_name': 'CIFAR-10', 'model_name': 'densenet', 'select': True, 'nb_examples': 100, 'balance_sampling': False, 'test_mode': False, 'attacks': 'fgsm?eps=0.0156;bim?eps=0.008&eps_iter=0.0012;carlinili?targeted=next&confidence=5;carlinili?targeted=ll&confidence=5;deepfool?overshoot=10;carlinil2?targeted=next&batch_size=100&max_iterations=1000&confidence=5;carlinil2?targeted=ll&batch_size=100&max_iterations=1000&confidence=5;carlinil0?targeted=next&confidence=5;carlinil0?targeted=ll&confidence=5;jsma?targeted=next;jsma?targeted=ll;', 'clip': -1, 'visualize': True, 'robustness': '', 'detection': 'FeatureSqueezing?squeezers=bit_depth_5,median_filter_2_2,non_local_means_color_13_3_2&distance_measure=l1&threshold=1.7547;MagNet', 'detection_train_test_mode': True, 'result_folder': 'results', 'verbose': False}
    
    ===Loading CIFAR-10 data...
    Created TensorFlow session and set Keras backend.
    Downloading data from https://github.com/titu1994/DenseNet/releases/download/v2.0/DenseNet-40-12-Tensorflow-Backend-TF-dim-ordering.h5
    
    ===Defined TensorFlow model graph.
    ---Loaded CIFAR-10-densenet model.
    
    Evaluating the pre-trained model...
    Test accuracy on raw legitimate examples 0.9484
    Mean confidence on ground truth classes 0.9215
    Selected 100 examples.
    Selected index in test set (sorted): 0-52:1,54-57:1,59,60,62-67:1,69-77:1,79-99:1,101-105:1
    Test accuracy on selected legitimate examples 1.0000
    Mean confidence on ground truth classes, selected 0.9588
    
    
    Running attack: fgsm {'eps': 0.0156}

    /home/katharine/wrrk/my_classes/oreilly_live/data-security-for-data-science/EvadeML-Zoo/attacks/cleverhans_wrapper.py:31: UserWarning: Ignored arguments: dict_keys(['targeted'])
      warnings.warn("Ignored arguments: %s" % update.keys())

    
    ---Attack (uint8): fgsm?eps=0.0156
    Success rate: 85.00%, Mean confidence of SAEs: 97.29%
    ### Statistics of the SAEs:
    L2 dist: 0.8647, Li dist: 0.0157, L0 dist_value: 99.5%, L0 dist_pixel: 99.8%
    
    Running attack: bim {'eps': 0.008, 'eps_iter': 0.0012}
    
    ---Attack (uint8): bim?eps=0.008&eps_iter=0.0012
    Success rate: 92.00%, Mean confidence of SAEs: 98.40%
    ### Statistics of the SAEs:
    L2 dist: 0.3686, Li dist: 0.0078, L0 dist_value: 92.4%, L0 dist_pixel: 99.5%
    
    Running attack: carlinili {'targeted': 'next', 'confidence': 5.0}
    targeted value: next

%% Cell type:code id: tags:

``` python
from IPython.display import Image
from glob import glob

for img in glob('results/CIFAR*/undetected_attacks_*'):
    print(img)
    display(Image(filename=img))
```

%% Cell type:markdown id: tags:

### Questions for Discussion

- Why might this example have gone undetected?
- Out of the adversarial images, which can you identify?
- As we can see from the detection rate, this works fairly well with MNIST; but it works much less well for color images, why might that be?
+0 −4
Changes for notebooks/04 - Case Study - Adversarial Learning.ipynb: 0 added lines, 4 removed lines.
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
```

requirements.in

0 → 100644
+10 −0
Changes for requirements.in: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
foolbox
jupyter
keras==2.0.4
matplotlib
opencv-python
pandas
pillow
sklearn
tinydb
tensorflow==1.1.0

requirements.txt

0 → 100644
+76 −0
Changes for requirements.txt: 76 added lines, 0 removed lines.
Original line number Diff line number Diff line
#
# This file is autogenerated by pip-compile
# To update, run:
#
#    pip-compile --output-file requirements.txt requirements.in
#
backcall==0.1.0           # via ipython
bleach==3.0.2             # via nbconvert
certifi==2018.11.29       # via requests
chardet==3.0.4            # via requests
cycler==0.10.0            # via matplotlib
decorator==4.3.0          # via ipython, traitlets
defusedxml==0.5.0         # via nbconvert
entrypoints==0.3          # via nbconvert
foolbox==1.8.0
gitdb2==2.0.5             # via gitpython
gitpython==2.1.11         # via foolbox
idna==2.8                 # via requests
ipykernel==5.1.0          # via ipywidgets, jupyter, jupyter-console, notebook, qtconsole
ipython-genutils==0.2.0   # via nbformat, notebook, qtconsole, traitlets
ipython==7.2.0            # via ipykernel, ipywidgets, jupyter-console
ipywidgets==7.4.2         # via jupyter
jedi==0.13.2              # via ipython
jinja2==2.10              # via nbconvert, notebook
jsonschema==2.6.0         # via nbformat
jupyter-client==5.2.4     # via ipykernel, jupyter-console, notebook, qtconsole
jupyter-console==6.0.0    # via jupyter
jupyter-core==4.4.0       # via jupyter-client, nbconvert, nbformat, notebook, qtconsole
jupyter==1.0.0
keras==2.0.4
kiwisolver==1.0.1         # via matplotlib
markupsafe==1.1.0         # via jinja2
matplotlib==3.0.2
mistune==0.8.4            # via nbconvert
nbconvert==5.4.0          # via jupyter, notebook
nbformat==4.4.0           # via ipywidgets, nbconvert, notebook
notebook==5.7.4           # via jupyter, widgetsnbextension
numpy==1.15.4             # via foolbox, matplotlib, opencv-python, pandas, scikit-learn, scipy, tensorflow, theano
opencv-python==3.4.5.20
pandas==0.23.4
pandocfilters==1.4.2      # via nbconvert
parso==0.3.1              # via jedi
pexpect==4.6.0            # via ipython
pickleshare==0.7.5        # via ipython
pillow==5.4.1
prometheus-client==0.5.0  # via notebook
prompt-toolkit==2.0.7     # via ipython, jupyter-console
protobuf==3.6.1           # via tensorflow
ptyprocess==0.6.0         # via pexpect, terminado
pygments==2.3.1           # via ipython, jupyter-console, nbconvert, qtconsole
pyparsing==2.3.0          # via matplotlib
python-dateutil==2.7.5    # via jupyter-client, matplotlib, pandas
pytz==2018.9              # via pandas
pyyaml==3.13              # via keras
pyzmq==17.1.2             # via jupyter-client, notebook
qtconsole==4.4.3          # via jupyter
requests==2.21.0          # via foolbox
scikit-learn==0.20.2      # via sklearn
scipy==1.2.0              # via foolbox, scikit-learn, theano
send2trash==1.5.0         # via notebook
six==1.12.0               # via bleach, cycler, keras, prompt-toolkit, protobuf, python-dateutil, tensorflow, theano, traitlets
sklearn==0.0
smmap2==2.0.5             # via gitdb2
tensorflow==1.1.0
terminado==0.8.1          # via notebook
testpath==0.4.2           # via nbconvert
theano==1.0.3             # via keras
tinydb==3.12.2
tornado==5.1.1            # via ipykernel, jupyter-client, notebook, terminado
traitlets==4.3.2          # via ipykernel, ipython, ipywidgets, jupyter-client, jupyter-core, nbconvert, nbformat, notebook, qtconsole
urllib3==1.24.1           # via requests
wcwidth==0.1.7            # via prompt-toolkit
webencodings==0.5.1       # via bleach
werkzeug==0.14.1          # via tensorflow
wheel==0.32.3             # via tensorflow
widgetsnbextension==3.4.2  # via ipywidgets