Skip to content

Instantly share code, notes, and snippets.

@neale
Last active January 12, 2018 23:27
Show Gist options
  • Save neale/d5ed607d00a1ef8b76c704e25bcc70f6 to your computer and use it in GitHub Desktop.
Save neale/d5ed607d00a1ef8b76c704e25bcc70f6 to your computer and use it in GitHub Desktop.
lbfgs_test
import foolbox
import keras
import numpy
from keras.applications.resnet50 import ResNet50
import matplotlib.pyplot as plt
import scipy.misc
# instantiate model
keras.backend.set_learning_phase(0)
kmodel = ResNet50(weights='imagenet')
preprocessing = (numpy.array([104, 116, 123]), 1)
fmodel = foolbox.models.KerasModel(kmodel, bounds=(0, 255), preprocessing=preprocessing)
path = 'ILSVRC2012_val_00017762.JPEG'
# get source image and label
image = scipy.misc.imread(path)
image = scipy.misc.imresize(image, (224, 224))
label = numpy.argmax(fmodel.predictions(image))
attack = foolbox.attacks.LBFGSAttack(fmodel)
adversarial = attack(image[:,:,::-1], label, unpack=False)
#attack not applied
plt.imshow(adversarial.image[:,:,::-1])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment