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

updating with new requirements

parent c591b4ca
Loading
Loading
Loading
Loading
+5 −1
Changes for Dockerfile: 5 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -26,6 +26,11 @@ COPY notebooks /home/jovyan/notebooks
COPY data /home/jovyan/data
COPY solutions /home/jovyan/solutions
COPY steal_ml /home/jovyan/steal_ml
RUN mkdir /home/jovyan/.keras
RUN mkdir /home/jovyan/.keras/models
RUN apt-get install -y wget
RUN wget 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/

# Allow user to write to directory
USER root
@@ -38,4 +43,3 @@ EXPOSE 8888

# Start the notebook server
CMD jupyter notebook --no-browser --port 8888 --ip=* --NotebookApp.token='' --NotebookApp.disable_check_xsrf=True 
+27 −3
Changes for notebooks/01 - Adversarial Examples.ipynb: 27 added lines, 3 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')
```

%% Output

    Using TensorFlow backend.

    Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5

    ---------------------------------------------------------------------------
    ImportError                               Traceback (most recent call last)
    <ipython-input-1-96dfaef9c3dc> in <module>
          8
          9 keras.backend.set_learning_phase(0)
    ---> 10 kmodel = ResNet50(weights='imagenet')

    ~/.virtualenvs/py3data/lib/python3.6/site-packages/keras/applications/resnet50.py in ResNet50(include_top, weights, input_tensor, input_shape, pooling, classes)
        262                                     cache_subdir='models',
        263                                     md5_hash='a268eb855778b3df3c7506639542a6af')
    --> 264         model.load_weights(weights_path)
        265         if K.backend() == 'theano':
        266             layer_utils.convert_all_kernels_in_model(model)
    ~/.virtualenvs/py3data/lib/python3.6/site-packages/keras/engine/topology.py in load_weights(self, filepath, by_name)
       2529         """
       2530         if h5py is None:
    -> 2531             raise ImportError('`load_weights` requires h5py.')
       2532         f = h5py.File(filepath, mode='r')
       2533         if 'layer_names' not in f.attrs and 'model_weights' in f:
    ImportError: `load_weights` requires h5py.

%% 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
```
+1 −0
Changes for requirements.in: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
foolbox==1.8.0
jupyter
h5py
keras==2.0.4
matplotlib
opencv-python
+3 −2
Changes for requirements.txt: 3 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ entrypoints==0.3 # via nbconvert
foolbox==1.8.0
gitdb2==2.0.5             # via gitpython
gitpython==2.1.11         # via foolbox
h5py==2.9.0
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
@@ -35,7 +36,7 @@ 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, theano
numpy==1.15.4             # via foolbox, h5py, matplotlib, opencv-python, pandas, scikit-learn, scipy, theano
opencv-python==3.4.5.20
pandas==0.23.4
pandocfilters==1.4.2      # via nbconvert
@@ -57,7 +58,7 @@ 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, python-dateutil, theano, traitlets
six==1.12.0               # via bleach, cycler, h5py, keras, prompt-toolkit, python-dateutil, theano, traitlets
sklearn==0.0
smmap2==2.0.5             # via gitdb2
terminado==0.8.1          # via notebook