Commit 13e72b65 authored by Katharine Jarmul's avatar Katharine Jarmul
Browse files

updated materials

parent 11c99ade
Loading
Loading
Loading
Loading
+13 −3
Changes for Dockerfile: 13 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@ FROM jupyter/minimal-notebook

USER root
RUN apt-get update
RUN apt-get install -y curl git
RUN apt-get install -y curl wget git

#Set the working directory
USER jovyan
@@ -21,8 +21,18 @@ COPY steal_ml /home/jovyan/steal_ml
RUN mkdir /home/jovyan/.keras
RUN mkdir /home/jovyan/.keras/models
RUN mkdir /home/jovyan/.keras/datasets
RUN curl https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5 --output /home/jovyan/.keras/models/resnet50_weights_tf_dim_ordering_tf_kernels.h5
RUN curl https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz --output /home/jovyan/.keras/datasets/cifar-10-python.tar.gz

# Download weights & datasets
RUN wget -q https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5  
RUN mv resnet50_weights_tf_dim_ordering_tf_kernels.h5 /home/jovyan/.keras/models/
RUN wget -q https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json
RUN mv imagenet_class_index.json /home/jovyan/.keras/models/
RUN wget -q https://s3.amazonaws.com/img-datasets/mnist.npz 
RUN mv mnist.npz /home/jovyan/.keras/datasets
RUN wget -q https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz 
RUN tar -xvzf cifar-10-python.tar.gz && mv cifar-10-python* /home/jovyan/.keras/datasets

# clone evademlzoo and install deps
RUN git clone https://github.com/kjam/EvadeML-Zoo.git 
RUN cd EvadeML-Zoo && git pull
RUN cd EvadeML-Zoo && git submodule init && git submodule update
+32 B (236 KiB)

File changed.

No diff preview for this file type.

+352 B (15 KiB)

File changed.

No diff preview for this file type.

+9 −6
Changes for notebooks/01 - Adversarial Examples.ipynb: 9 added lines, 6 removed lines.
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
import foolbox
import keras
import numpy as np
import matplotlib.pyplot as plt
from keras.applications.resnet50 import ResNet50, \
    preprocess_input, decode_predictions
from keras.preprocessing.image import load_img, img_to_array

keras.backend.set_learning_phase(0)
kmodel = ResNet50(weights='imagenet')
kmodel = ResNet50(weights=None)
```

%% Output
%% Cell type:code id: tags:

    Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5
``` python
kmodel.load_weights('~/.keras/models/resnet50_weights_tf_dim_ordering_tf_kernels.h5')
```

%% Cell type:code id: tags:

``` python
%matplotlib inline

img_path = '../data/img/cat.jpg'
img = load_img(img_path, target_size=(224, 224))

x = img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = kmodel.predict(x)

for pred in decode_predictions(preds, top=3)[0]:
    print(pred)

plt.imshow(img)
plt.axis('off')
```

%% Cell type:markdown id: tags:

### Your Turn

- Try a few of the other photos in the photo directory!

%% Cell type:code id: tags:

``` python
```

%% Cell type:code id: tags:

``` python
%load ../solutions/01_shark.py
# %load ../solutions/01_shark.py
```

%% Cell type:code id: tags:

``` python
# initialize foolbox model
preprocessing = (np.array([104, 116, 123]), 1)
fmodel = foolbox.models.KerasModel(kmodel,
                                   bounds=(0, 255),
                                   preprocessing=preprocessing)

# get source image and label
example_image, label = foolbox.utils.imagenet_example()

# apply attack on source image
# ::-1 reverses the color channels,
# because Keras ResNet50 expects BGR instead of RGB
attack = foolbox.attacks.FGSM(fmodel)
adversarial = attack(example_image[:, :, ::-1], label)
```

%% Cell type:code id: tags:

``` python
%matplotlib inline

plt.figure()

plt.subplot(1, 3, 1)
plt.title('Original')
plt.imshow(example_image / 255)
# division by 255 to convert [0, 255] to [0, 1]
plt.axis('off')

plt.subplot(1, 3, 2)
plt.title('Adversarial')
plt.imshow(adversarial[:, :, ::-1] / 255)
# ::-1 to convert BGR to RGB
plt.axis('off')

plt.subplot(1, 3, 3)
plt.title('Difference')
difference = adversarial[:, :, ::-1] - example_image
plt.imshow(difference / abs(difference).max() * 0.2 + 0.5)
plt.axis('off')

plt.show()
```

%% Cell type:markdown id: tags:

## Your Turn

- Did it work? What was the predicted label?

%% Cell type:code id: tags:

``` python
```

%% Cell type:code id: tags:

``` python
%load ../solutions/02_adversarial_vs_original_predictions.py
# %load ../solutions/02_adversarial_vs_original_predictions.py
```

%% Cell type:markdown id: tags:

## Your Turn

- Now try and make an adversarial attack using a different attack method. See all attacks here: https://foolbox.readthedocs.io/en/latest/modules/attacks.html
- You may also try a targeted class attack: (see: https://foolbox.readthedocs.io/en/latest/modules/criteria.html#foolbox.criteria.TargetClass and classes with labels https://gist.github.com/yrevar/942d3a0ac09ec9e5eb3a)

%% Cell type:code id: tags:

``` python
```

%% Cell type:code id: tags:

``` python
%load ../solutions/03_deepfool_attack.py
# %load ../solutions/03_deepfool_attack.py

```

%% Cell type:code id: tags:

``` python
```
+1 −1
Changes for notebooks/02 - Poisoning Attack.ipynb: 1 added line, 1 removed line.
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
from sklearn.externals import joblib
```

%% Cell type:code id: tags:

``` python
spam_model = joblib.load('../data/bayes_spam_pipeline.pkl')
```

%% Cell type:code id: tags:

``` python
spam_model.steps
```

%% Cell type:markdown id: tags:

### Investigating top spam and ham features

%% Cell type:code id: tags:

``` python
feature_names = spam_model.steps[0][1].get_feature_names()
top_spam = sorted(zip(spam_model.steps[1][1].coef_[0],
                       feature_names), reverse=True)[:10]
top_spam
```

%% Cell type:code id: tags:

``` python
top_ham = sorted(zip(spam_model.steps[1][1].coef_[0],
                       feature_names))[:10]
top_ham
```

%% Cell type:markdown id: tags:

### Poisoning the  well...

%% Cell type:code id: tags:

``` python
my_poisoned_data = [
    ('Guaranteed to make you sleep better', 0),
    ('Hi, my name is Katharine', 1),
    ('Free prize to the first to reply', 0),
    ('I like free things', 0),
    ('How are you?', 1),
    ('free pizza in the lobby', 0),
    ('I can guarantee you it is a free prize', 0),
    ('You can win your free prize now!', 0),
    ('You can win your free prize today!', 0),
    ('everyone likes free things.', 0)
]
```

%% Cell type:code id: tags:

``` python
spam_model.predict([x[0] for x in my_poisoned_data])
```

%% Cell type:code id: tags:

``` python
X_poisoned = spam_model.steps[0][1].transform(
    [x[0] for x in my_poisoned_data])
```

%% Cell type:code id: tags:

``` python
X_poisoned
```

%% Cell type:code id: tags:

``` python
y_poisoned = [x[1] for x in my_poisoned_data]
```

%% Cell type:markdown id: tags:

### Active learning environments (or batch training on new data)

%% Cell type:code id: tags:

``` python
spam_model.steps[1][1].partial_fit(X_poisoned, y_poisoned)
```

%% Cell type:markdown id: tags:

### Evaluating our poisoning

%% Cell type:code id: tags:

``` python
feature_names = spam_model.steps[0][1].get_feature_names()
top_poisoned_spam = sorted(zip(spam_model.steps[1][1].coef_[0],
                       feature_names), reverse=True)[:10]
top_poisoned_spam
```

%% Cell type:markdown id: tags:

### Your Turn

- Did we influence the model weights? How can you tell?
- Do our spam examples now pass?
- How can we increase our influence? Try moving the coefficient for the word "free" at least .01 in the direction of ham.

%% Cell type:code id: tags:

``` python
```

%% Cell type:code id: tags:

``` python
%load ../solutions/04_poisoning.py
# %load ../solutions/04_poisoning.py
```

%% Cell type:markdown id: tags:

### Did we fool the model?

%% Cell type:code id: tags:

``` python
my_poisoned_data
```

%% Cell type:code id: tags:

``` python
spam_model.predict([x[0] for x in my_poisoned_data])
```

%% Cell type:code id: tags:

``` python
```
Loading