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

updating with fixes for fresh install

parent 7b3349fe
Loading
Loading
Loading
Loading
+0 −3
Changes for .gitignore: 0 added lines, 3 removed lines.
Original line number Diff line number Diff line
<<<<<<< HEAD
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
@@ -103,6 +102,4 @@ venv.bak/

# mypy
.mypy_cache/
=======
.ipynb*
>>>>>>> oreilly/oreilly
+7 −1
Changes for Dockerfile: 7 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ WORKDIR /home/jovyan/
# Modules
COPY requirements.txt /home/jovyan/requirements.txt
RUN pip install -r /home/jovyan/requirements.txt
RUN conda install tensorflow==1.1.0
RUN conda install tensorflow==1.8.0
RUN git init
COPY .gitmodules /home/jovyan/.gitmodules
RUN git submodule init
@@ -26,9 +26,15 @@ COPY solutions /home/jovyan/solutions
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 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 wget https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
RUN mv resnet50_weights_tf_dim_ordering_tf_kernels.h5 /home/jovyan/.keras/models/
RUN mv cifar-10-python.tar.gz /home/jovyan/.keras/models/
RUN cd /home/jovyan/EvadeML-Zoo && git submodule init && git submodule update
RUN cd /home/jovyan/EvadeML-ZOO && mkdir downloads; curl -sL https://github.com/mzweilin/EvadeML-Zoo/releases/download/v0.1/downloads.tar.gz | tar xzv -C downloads


# Allow user to write to directory
USER root
Compare f505f51f to 27e171f8
Changes for EvadeML-Zoo: 1 added line, 1 removed line.
Original line number Diff line number Diff line
Subproject commit f505f51fae3e466d751a784433dad69ffe1c1013
Subproject commit 27e171f8779d2f76fa19897c44e4fa94b6384acd
+1 −23
Changes for notebooks/01 - Adversarial Examples.ipynb: 1 added line, 23 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
```
+216 −69

File changed.

Preview size limit exceeded, changes collapsed.

Loading