Commit a9a5a146 authored by Katharine Jarmul's avatar Katharine Jarmul
Browse files

updating with fixes from first course

parent 0dbbff08
Loading
Loading
Loading
Loading
+1 −2
Changes for Dockerfile: 1 added line, 2 removed lines.
Original line number Diff line number Diff line
@@ -13,8 +13,6 @@ WORKDIR /home/jovyan/
COPY requirements.txt /home/jovyan/requirements.txt
RUN conda install --yes pip tensorflow==1.8.0
RUN pip install -r /home/jovyan/requirements.txt
RUN conda list
RUN pip freeze

# Add files
COPY notebooks /home/jovyan/notebooks
@@ -34,6 +32,7 @@ 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-* /home/jovyan/.keras/datasets
RUN cp /home/jovyan/.keras/datasets/cifar-10-python.tar.gz /home/jovyan/.keras/datasets/cifar-10-batches-py.tar.gz

# clone evademlzoo and install deps
RUN git clone https://github.com/kjam/EvadeML-Zoo.git 
+2 −2
Changes for README.md: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -26,10 +26,10 @@ You will need to have Docker installed on your system to create images and run c

1) Clone the repository for the class either using the UI or your terminal (see above)..

2) Once you have Docker installed, type the following on your terminal to create a Docker image: `docker build -t ${TITLE} .` (note the period)
2) Once you have Docker installed, type the following on your terminal to create a Docker image: `docker build -t mlsec .` (note the period)

3) That will take a little while to create a Docker image, but once completed, you can run your server with the following:
`docker run -p 8888:8888 ${TITLE}`
`docker run -p 8888:8888 mlsec`

4) Head to `localhost:8888` in your browser and you will be able to access the Jupyter Notebooks.

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

``` python
import foolbox
import keras
import numpy as np
import os
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=None)
```

%% Cell type:code id: tags:

``` python
kmodel.load_weights('/home/jovyan/.keras/models/resnet50_weights_tf_dim_ordering_tf_kernels.h5')
## Note: If you are using connected internet you can use this line instead!

#kmodel=ResNet50(weights='imagenet')
```

%% Cell type:code id: tags:

``` python
fname = os.path.join(os.path.expanduser('~'),
                     '.keras/models/resnet50_weights_tf_dim_ordering_tf_kernels.h5')
```

%% Cell type:code id: tags:

``` python
kmodel.load_weights(fname)
```

%% 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
```

%% 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
```

%% 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

```

%% Cell type:code id: tags:

``` python
```
+2 −1
Changes for notebooks/02 - Poisoning Attack.ipynb: 2 added lines, 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)
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
```

%% 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
```
+15 −353

File changed.

Preview size limit exceeded, changes collapsed.

Loading