Skip to content

Commit

Permalink
improved test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Rauber committed Jun 23, 2017
1 parent e98eaa3 commit 53f5600
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
23 changes: 14 additions & 9 deletions foolbox/attacks/localsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ def unnormalize(im):
channels = I.shape[channel_axis]

def random_locations():
locations = np.random.permutation(h * w)[:int(0.1 * h * w)]
n = int(0.1 * h * w)
n = min(n, 128)
locations = np.random.permutation(h * w)[:n]
p_x = locations % w
p_y = locations // w
pxy = list(zip(p_x, p_y))
Expand Down Expand Up @@ -119,17 +121,20 @@ def cyclic(r, Ibxy):

for _ in range(R):
# Computing the function g using the neighborhood
# IMPORTANT: random subset for efficiency
PxPy = PxPy[np.random.permutation(len(PxPy))[:128]]
L = [pert(Ii, p, x, y) for x, y in PxPy]

# TODO: use batch predictions
def score(It):
logits, _ = a.predictions(unnormalize(It), strict=False)
probs = softmax(logits)
return probs[cI]
def score(Its):
Its = np.stack(Its)
Its = unnormalize(Its)
batch_logits, _ = a.batch_predictions(Its, strict=False)
scores = [softmax(logits)[cI] for logits in batch_logits]
return scores

scores = [score(It) for It in L]
scores = score(L)

indices = np.argsort(scores)[::-1][:t]
indices = np.argsort(scores)[:t]

PxPy_star = PxPy[indices]

Expand All @@ -143,7 +148,7 @@ def score(It):

# Check whether the perturbed image Ii is an adversarial image
_, is_adv = a.predictions(unnormalize(Ii))
if is_adv:
if is_adv: # pragma: no cover
return

# Update a neighborhood of pixel locations for the next round
Expand Down
4 changes: 2 additions & 2 deletions foolbox/tests/test_adversarial.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_adversarial(model, criterion, image, label):
# image = bn_image
# label = bn_label

adversarial = Adversarial(model, criterion, image, label)
adversarial = Adversarial(model, criterion, image, label, verbose=False)

assert not adversarial.predictions(image)[1]

Expand All @@ -31,7 +31,7 @@ def test_adversarial(model, criterion, image, label):
assert adversarial.normalized_distance(image).value == 0

label = 22 # wrong label
adversarial = Adversarial(model, criterion, image, label)
adversarial = Adversarial(model, criterion, image, label, verbose=True)

assert adversarial.image is not None
assert adversarial.distance == MSE(value=0)
Expand Down

0 comments on commit 53f5600

Please sign in to comment.