def test_genattack_numpy(request: Any) -> None: class Model: def __call__(self, inputs: Any) -> Any: return inputs.mean(axis=(2, 3)) model = Model() with pytest.raises(ValueError): fbn.NumPyModel(model, bounds=(0, 1), data_format="foo") fmodel = fbn.NumPyModel(model, bounds=(0, 1)) x, y = ep.astensors( *fbn.samples( fmodel, dataset="imagenet", batchsize=16, data_format="channels_first" ) ) with pytest.raises(ValueError, match="data_format"): fbn.attacks.GenAttack(reduced_dims=(2, 2)).run( fmodel, x, fbn.TargetedMisclassification(y), epsilon=0.3 ) with pytest.raises(ValueError, match="channel_axis"): fbn.attacks.GenAttack(channel_axis=2, reduced_dims=(2, 2)).run( fmodel, x, fbn.TargetedMisclassification(y), epsilon=0.3 )
def test_blur_numpy(request: Any) -> None: class Model: def __call__(self, inputs: Any) -> Any: return inputs.mean(axis=(2, 3)) model = Model() with pytest.raises(ValueError): fbn.NumPyModel(model, bounds=(0, 1), data_format="foo") fmodel = fbn.NumPyModel(model, bounds=(0, 1)) x, y = ep.astensors(*fbn.samples( fmodel, dataset="imagenet", batchsize=16, data_format="channels_first")) with pytest.raises(ValueError, match="data_format"): fbn.attacks.GaussianBlurAttack()(fmodel, x, y, epsilons=None)
def numpy_simple_model(request: Any) -> ModelAndData: class Model: def __call__(self, inputs: Any) -> Any: return inputs.mean(axis=(2, 3)) model = Model() with pytest.raises(ValueError): fbn.NumPyModel(model, bounds=(0, 1), data_format="foo") fmodel = fbn.NumPyModel(model, bounds=(0, 1)) with pytest.raises(ValueError, match="data_format"): x, _ = fbn.samples(fmodel, dataset="imagenet", batchsize=16) fmodel = fbn.NumPyModel(model, bounds=(0, 1), data_format="channels_first") with pytest.warns(UserWarning, match="returning NumPy arrays"): x, _ = fbn.samples(fmodel, dataset="imagenet", batchsize=16) x = ep.astensor(x) y = fmodel(x).argmax(axis=-1) return fmodel, x, y