def test_samples_large_batch(fmodel_and_data: ModelAndData, batchsize: int, dataset: str) -> None: fmodel, _, _ = fmodel_and_data data_format = getattr(fmodel, "data_format", "channels_first") with pytest.warns(UserWarning, match="only 20 samples"): x, y = fbn.samples(fmodel, dataset=dataset, batchsize=batchsize, data_format=data_format) assert len(x) == len(y) == batchsize assert not ep.istensor(x) assert not ep.istensor(y)
def test_forward_wrapped(fmodel_and_data: ModelAndData) -> None: fmodel, x, y = fmodel_and_data assert ep.istensor(x) logits = fmodel(x) assert ep.istensor(logits) assert logits.ndim == 2 assert len(logits) == len(x) == len(y) _, num_classes = logits.shape assert (y >= 0).all() assert (y < num_classes).all() if hasattr(logits.raw, "requires_grad"): assert not logits.raw.requires_grad
def test_call_one_epsilon( fmodel_and_data_ext_for_attacks: ModelDescriptionAndData, attack: fbn.Attack, ) -> None: (fmodel, x, y), _ = fmodel_and_data_ext_for_attacks assert ep.istensor(x) assert ep.istensor(y) raw, clipped, success = attack(fmodel, x, y, epsilons=1.0) assert ep.istensor(raw) assert ep.istensor(clipped) assert ep.istensor(success) assert raw.shape == x.shape assert clipped.shape == x.shape assert success.shape == (len(x),)
def wrap(*args): """Wraps all inputs as EagerPy tensors if they are not already wrapped and returns a function restoring the original format""" if len(args) == 0: return args restore = wrap_ if ep.istensor(args[0]) else unwrap_ result = wrap_(*args) return (result, restore) if len(args) == 1 else (*result, restore)
def test_call_one_epsilon( fmodel_and_data_ext_for_attacks: Tuple[Tuple[fbn.Model, ep.Tensor, ep.Tensor], bool], attack: fbn.Attack, ) -> None: (fmodel, x, y), _ = fmodel_and_data_ext_for_attacks assert ep.istensor(x) assert ep.istensor(y) raw, clipped, success = attack(fmodel, x, y, epsilons=1.0) assert ep.istensor(raw) assert ep.istensor(clipped) assert ep.istensor(success) assert raw.shape == x.shape assert clipped.shape == x.shape assert success.shape == (len(x), )
def test_samples(fmodel_and_data: ModelAndData, batchsize: int, dataset: str) -> None: fmodel, _, _ = fmodel_and_data if hasattr(fmodel, "data_format"): data_format = fmodel.data_format # type: ignore x, y = fbn.samples(fmodel, dataset=dataset, batchsize=batchsize) assert len(x) == len(y) == batchsize assert not ep.istensor(x) assert not ep.istensor(y) x, y = fbn.samples(fmodel, batchsize=batchsize, data_format=data_format) assert len(x) == len(y) == batchsize assert not ep.istensor(x) assert not ep.istensor(y) with pytest.raises(ValueError): data_format = { "channels_first": "channels_last", "channels_last": "channels_first", }[data_format] fbn.samples(fmodel, batchsize=batchsize, data_format=data_format) else: x, y = fbn.samples(fmodel, batchsize=batchsize, data_format="channels_first") assert len(x) == len(y) == batchsize assert not ep.istensor(x) assert not ep.istensor(y) with pytest.raises(ValueError): fbn.samples(fmodel, batchsize=batchsize)
def get_numpy_kwargs(kwargs: Any) -> Dict: return { k: ep.astensor(t.numpy()) if ep.istensor(t) else t for k, t in kwargs.items() }
def test_module() -> None: assert ep.istensor(ep.numpy.tanh([3, 5])) assert not ep.istensor(ep.numpy.tanh(3))
def unwrap_(*args): """Unwraps all EagerPy tensors if they are not already unwrapped""" result = tuple(t.tensor if ep.istensor(t) else t for t in args) return result[0] if len(args) == 1 else result