Skip to content

Commit

Permalink
Merge pull request #523 from bethgelab/genattacktests
Browse files Browse the repository at this point in the history
full rewrite of gen_attack_utils tests, fixes #522
  • Loading branch information
jonasrauber committed Mar 22, 2020
2 parents 64dc1ea + b630721 commit a950ed1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 61 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ testskipslow:
pytest --pdb --skipslow --cov=foolbox --cov-append --backend jax
pytest --pdb --skipslow --cov=foolbox --cov-append --backend numpy

.PHONY: testskipslowrev
testskipslowrev:
pytest --pdb --skipslow --cov=foolbox --cov-append --backend numpy
pytest --pdb --skipslow --cov=foolbox --cov-append --backend jax
pytest --pdb --skipslow --cov=foolbox --cov-append --backend tensorflow
pytest --pdb --skipslow --cov=foolbox --cov-append --backend pytorch
pytest --pdb --skipslow --cov=foolbox

.PHONY: testattacks
testattacks:
pytest --pdb --cov=foolbox.attacks tests/test_attacks.py
Expand Down
83 changes: 22 additions & 61 deletions tests/test_gen_attack_utils.py
Original file line number Diff line number Diff line change
@@ -1,81 +1,42 @@
import eagerpy as ep
import numpy as np
import pytest
from typing import Any

from foolbox.attacks.gen_attack_utils import rescale_images


def test_pytorch_numpy_compatibility() -> None:
import numpy as np
import torch
def test_rescale_axis(request: Any, dummy: ep.Tensor) -> None:
backend = request.config.option.backend
if backend == "numpy":
pytest.skip()

x_np = np.random.uniform(0.0, 1.0, size=(16, 3, 64, 64))
x_torch = torch.from_numpy(x_np)

x_np_ep = ep.astensor(x_np)
x_torch_ep = ep.astensor(x_torch)

x_up_np_ep = rescale_images(x_np_ep, (16, 3, 128, 128), 1)
x_up_torch_ep = rescale_images(x_torch_ep, (16, 3, 128, 128), 1)

x_up_np = x_up_np_ep.numpy()
x_up_torch = x_up_torch_ep.numpy()

assert np.allclose(x_up_np, x_up_torch)
x = ep.from_numpy(dummy, x_np)
x_ep = ep.astensor(x)
x_up_ep = rescale_images(x_ep, (16, 3, 128, 128), 1)
x_up = x_up_ep.numpy()

assert np.allclose(x_up_np, x_up)

def test_pytorch_numpy_compatibility_different_axis() -> None:
import numpy as np
import torch

x_np = np.random.uniform(0.0, 1.0, size=(16, 64, 64, 3))
x_torch = torch.from_numpy(x_np)
def test_rescale_axis_nhwc(request: Any, dummy: ep.Tensor) -> None:
backend = request.config.option.backend
if backend == "numpy":
pytest.skip()

x_np = np.random.uniform(0.0, 1.0, size=(16, 64, 64, 3))
x_np_ep = ep.astensor(x_np)
x_torch_ep = ep.astensor(x_torch)

x_up_np_ep = rescale_images(x_np_ep, (16, 128, 128, 3), -1)
x_up_torch_ep = rescale_images(x_torch_ep, (16, 128, 128, 3), -1)

x_up_np = x_up_np_ep.numpy()
x_up_torch = x_up_torch_ep.numpy()

assert np.allclose(x_up_np, x_up_torch)


def test_pytorch_tensorflow_compatibility() -> None:
import numpy as np
import torch
import tensorflow as tf

x_np = np.random.uniform(0.0, 1.0, size=(16, 3, 64, 64))
x_torch = torch.from_numpy(x_np)
x_tf = tf.convert_to_tensor(x_np)

x_tf_ep = ep.astensor(x_tf)
x_torch_ep = ep.astensor(x_torch)

x_up_tf_ep = rescale_images(x_tf_ep, (16, 3, 128, 128), 1)
x_up_torch_ep = rescale_images(x_torch_ep, (16, 3, 128, 128), 1)

x_up_tf = x_up_tf_ep.numpy()
x_up_torch = x_up_torch_ep.numpy()

assert np.allclose(x_up_tf, x_up_torch)


def test_jax_numpy_compatibility() -> None:
import numpy as np
import jax.numpy as jnp

x_np = np.random.uniform(0.0, 1.0, size=(16, 3, 64, 64))
x_jax = jnp.array(x_np)

x_np_ep = ep.astensor(x_np)
x_jax_ep = ep.astensor(x_jax)

x_up_np_ep = rescale_images(x_np_ep, (16, 3, 128, 128), 1)
x_up_jax_ep = rescale_images(x_jax_ep, (16, 3, 128, 128), 1)

x_up_np = x_up_np_ep.numpy()
x_up_jax = x_up_jax_ep.numpy()
x = ep.from_numpy(dummy, x_np)
x_ep = ep.astensor(x)
x_up_ep = rescale_images(x_ep, (16, 128, 128, 3), -1)
x_up = x_up_ep.numpy()

assert np.allclose(x_up_np, x_up_jax)
assert np.allclose(x_up_np, x_up)

0 comments on commit a950ed1

Please sign in to comment.