Repository navigation
Home
Borche edited this page Oct 27, 2019
·
4 revisions
Table of contents:
- Attacking with single algorithm, x times.
- Attacking with all available algorithms, x times.
- Attacking with all available algorithms, using the random strategy
All examples are attacking ResNet50, using the sample image from Foolbox.
The single function can be used to create attack with only 1 algorithm.
from loops.Attacks import AttackTypes
from loops.Loops import LoopAttack
import foolbox
import keras
from keras.applications.resnet50 import ResNet50
# instantiate model
keras.backend.set_learning_phase(0)
kmodel = ResNet50(weights='imagenet')
# get source image and label
image, _ = foolbox.utils.imagenet_example()
x = 100
loopAttack = LoopAttack(model=kmodel,bounds=(0,255),loops=x)
result = loopAttack.single(data=image,attack=AttackTypes.ADefAttack)all is the function that will attack with all available algorithms, using the default strategy.
from loops.Attacks import AttackTypes
from loops.Loops import LoopAttack
import foolbox
import keras
from keras.applications.resnet50 import ResNet50
# instantiate model
keras.backend.set_learning_phase(0)
kmodel = ResNet50(weights='imagenet')
# get source image and label
image, _ = foolbox.utils.imagenet_example()
x = 100
loopAttack = LoopAttack(model=kmodel,bounds=(0,255),loops=x)
result = loopAttack.all(data=image)all_random is the function that will attack with all available algorithms, using the random strategy.
from loops.Attacks import AttackTypes
from loops.Loops import LoopAttack
import foolbox
import keras
from keras.applications.resnet50 import ResNet50
# instantiate model
keras.backend.set_learning_phase(0)
kmodel = ResNet50(weights='imagenet')
# get source image and label
image, _ = foolbox.utils.imagenet_example()
x = 100
loopAttack = LoopAttack(model=kmodel,bounds=(0,255),loops=x)
result = loopAttack.all_random(data=image)any_of_random takes a list of approved algorithms and performs the attack using the random strategy.
from loops.Attacks import AttackTypes
from loops.Loops import LoopAttack
import foolbox
import keras
from keras.applications.resnet50 import ResNet50
# instantiate model
keras.backend.set_learning_phase(0)
kmodel = ResNet50(weights='imagenet')
# get source image and label
image, _ = foolbox.utils.imagenet_example()
x = 100
selectedAttacks = [AttackTypes.ADefAttack, AttackTypes.DeepFoolAttack]
loopAttack = LoopAttack(model=kmodel, bounds=(0, 255), loops=x)
result = loopAttack.any_of_random(data=image, attacks=selectedAttacks)from loops.Attacks import AttackTypes
from loops.Loops import LoopAttack
import foolbox
import keras
from keras.applications.resnet50 import ResNet50
import numpy as np
def mse(imageA, imageB):
err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
err /= float(imageA.shape[0] * imageA.shape[1])
print("Compare result: ", err)
return err
def compare_examples(result):
old = result["input"]
adversarial = result["adversarialExample"]
return mse(old, adversarial)
# instantiate model
keras.backend.set_learning_phase(0)
kmodel = ResNet50(weights='imagenet')
# get source image and label
image, _ = foolbox.utils.imagenet_example()
x = 100
excludedAttacks = [AttackTypes.BoundaryAttack, AttackTypes.NewtonFoolAttack, AttackTypes.LocalSearchAttack]
loopAttack = LoopAttack(model=kmodel, bounds=(0, 255), loops=x)
result = loopAttack.all_except_best_of(data=image, excludedAttacks=excludedAttacks, compare=compare_examples)