Skip to content

Commit

Permalink
fixed subsampling in deepfool #62 (#64)
Browse files Browse the repository at this point in the history
* fixed subsampling in deepfool
  • Loading branch information
wielandbrendel committed Aug 17, 2017
1 parent 799f6e5 commit 6200f0a
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions foolbox/attacks/deepfool.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import warnings
import random

import numpy as np

Expand All @@ -19,7 +18,7 @@ class DeepFoolAttack(Attack):
https://arxiv.org/abs/1511.04599
"""

def _apply(self, a, steps=100, subsample=20):
def _apply(self, a, steps=100, subsample=10):
if not a.has_gradient():
return

Expand All @@ -32,11 +31,19 @@ def _apply(self, a, steps=100, subsample=20):

label = a.original_class

# define labels
logits, _ = a.predictions(a.original_image)
if subsample:
assert isinstance(subsample, int)
# choose the top-k classes
labels = np.argsort(logits)[::-1][:subsample]
else: # pragma: no coverage
labels = np.arange(logits.shape[0])

def get_residual_labels(logits):
"""Get all labels with p < p[target]"""
n = logits.shape[0]
return [
k for k in range(n)
k for k in labels[1:]
if logits[k] < logits[label]]

perturbed = a.original_image
Expand All @@ -55,10 +62,6 @@ def get_residual_labels(logits):
loss = -crossentropy(logits=logits, label=label)

residual_labels = get_residual_labels(logits)
if subsample:
assert isinstance(subsample, int)
random.shuffle(residual_labels)
residual_labels = residual_labels[:subsample]

losses = [
-crossentropy(logits=logits, label=k)
Expand Down

0 comments on commit 6200f0a

Please sign in to comment.