bethgelab / foolbox

A Python toolbox to create adversarial examples that fool neural networks in PyTorch, TensorFlow, and JAX
https://foolbox.jonasrauber.de
MIT License
2.75k stars 425 forks source link

foolbox error for vector based inputs (non squared input) #334

Closed cod3r0k closed 5 years ago

cod3r0k commented 5 years ago

Hi! Today I faced a problem with foolbox. My data is vectors with related tags. But each attack faced the same problem as below: fmodel = foolbox.models.KerasModel(model, bounds=(-1, 1)) attack = foolbox.attacks.FGSM(fmodel) attack in this line I faced the problem: adversarial = attack(x_test[0:1], y_test[0:1]) the error is : `AssertionError Traceback (most recent call last)

in () ----> 1 adversarial = attack(x_test[0:1], y_test[0:1]) 2 # if the attack fails, adversarial will be None and a warning will be printed 3 frames /usr/local/lib/python3.6/dist-packages/foolbox/adversarial.py in __is_adversarial(self, image, predictions, in_bounds) 225 predictions, self.__original_class) 226 assert isinstance(is_adversarial, bool) or \ --> 227 isinstance(is_adversarial, np.bool_) 228 if is_adversarial: 229 is_best, distance = self.__new_adversarial( AssertionError: `
zimmerrol commented 5 years ago

Hi @cod3r0k, the attacks in foolbox expect you to pass them exactly one example, i.e. the x array should have rank 3 and the shape (W, H, C). Judging from the way how you call attack(...) right now, it does look like you are including a batch dimension of size 1. Just make sure that your data has an appropriate shape - then your problem should go away.

jonasrauber commented 5 years ago

Seems to be solved.