Commit 0dbbff08 authored by Katharine Jarmul's avatar Katharine Jarmul
Browse files

updating filepath for docker

parent 3c662e38
Loading
Loading
Loading
Loading
+1 −1
Changes for notebooks/01 - Adversarial Examples.ipynb: 1 added line, 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 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('~/.keras/models/resnet50_weights_tf_dim_ordering_tf_kernels.h5')
kmodel.load_weights('/home/jovyan/.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
```

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