Neural network attack foolbox (FGSM)

Viewed 223

I am trying to attack a keras neural network model located in a file (model.h5) and per foolbox's documentation, TensorFlowModel supports keras models. However, when I apply this to my keras model, I get an error. I wonder if it is due to the foolbox version I am using?

Code:

import foolbox
import numpy as np
import tensorflow as tf 

from foolbox.attacks import FGSM
from foolbox.criteria import Misclassification


############## Loading the model and preprocessing #####################  
tf.keras.backend.set_learning_phase(False)
model = tf.keras.load_model("model.h5") 
_, (images, labels) = tf.keras.datasets.mnist.load_data()
images = images.reshape(images.shape[0], 28, 28, 1)
images = images / 255
images = images.astype(np.float32)

######################### Attacking the model ##########################
fmodel = foolbox.models.TensorFlowModel(model, bounds=(0, 1))
attack = foolbox.attacks.FGSM(fmodel, criterion=Misclassification())
adversarial = np.array([attack(images[0], label=labels[0])])

model_predictions = model.predict(adversarial)
print('real label: {}, label prediction; {}'.format(
    labels[0], np.argmax(model_predictions)))

Error:


TypeError                                 Traceback (most recent call last)

<ipython-input-28-29f5e9da9c7e> in <module>()
     27 ######################### Attacking the model ##########################
     28 
---> 29 attack = foolbox.attacks.FGSM(fmodel, criterion=Misclassification())
     30 adversarial = np.array([attack(images[0], label=labels[0])])
     31 

TypeError: __init__() missing 1 required positional argument: 'labels'
1 Answers

I ran into this just today and was sad to see that there is no answer here. But I figured it out, so here we go.

Yes, this is due to the version of foolbox you are using.

In between version 2.4.0 and version 3.0.0b1, foolbox changed Misclassification from a function with no parameters to one that needs a 'labels' parameter. Using foolbox 2.4.0 should fix this problem.

Related