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.76k stars 426 forks source link

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn #242

Closed wanghan0501 closed 5 years ago

wanghan0501 commented 5 years ago
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-26-1467884c8842> in <module>
      1 attack = foolbox.attacks.FGSM(fmodel)
----> 2 adversarial = attack(img, label)
      3 print(np.argmax(model.predictions(adversarial)), label)

~/.pyenv/versions/3.7.1/lib/python3.7/site-packages/foolbox/attacks/base.py in wrapper(self, input_or_adv, label, unpack, **kwargs)
    135         else:
    136             try:
--> 137                 _ = call_fn(self, a, label=None, unpack=None, **kwargs)
    138                 assert _ is None, 'decorated __call__ method must return None'
    139             except StopAttack:

~/.pyenv/versions/3.7.1/lib/python3.7/site-packages/foolbox/attacks/gradient.py in __call__(***failed resolving arguments***)
    146         del unpack
    147 
--> 148         return self._run(a, epsilons=epsilons, max_epsilon=max_epsilon)
    149 
    150     def _gradient(self, a):

~/.pyenv/versions/3.7.1/lib/python3.7/site-packages/foolbox/attacks/gradient.py in _run(self, a, epsilons, max_epsilon)
     23         min_, max_ = a.bounds()
     24 
---> 25         gradient = self._gradient(a)
     26 
     27         if not isinstance(epsilons, Iterable):

~/.pyenv/versions/3.7.1/lib/python3.7/site-packages/foolbox/attacks/gradient.py in _gradient(self, a)
    150     def _gradient(self, a):
    151         min_, max_ = a.bounds()
--> 152         gradient = a.gradient()
    153         gradient = np.sign(gradient) * (max_ - min_)
    154         return gradient

~/.pyenv/versions/3.7.1/lib/python3.7/site-packages/foolbox/adversarial.py in gradient(self, image, label, strict)
    393 
    394         self._total_gradient_calls += 1
--> 395         gradient = self.__model.gradient(image, label)
    396 
    397         assert gradient.shape == image.shape

~/.pyenv/versions/3.7.1/lib/python3.7/site-packages/foolbox/models/base.py in gradient(self, image, label)
    244 
    245         """
--> 246         _, gradient = self.predictions_and_gradient(image, label)
    247         return gradient
    248 

~/.pyenv/versions/3.7.1/lib/python3.7/site-packages/foolbox/models/pytorch.py in predictions_and_gradient(self, image, label)
    127         ce = nn.CrossEntropyLoss()
    128         loss = ce(predictions, target)
--> 129         loss.backward()
    130         grad = images.grad
    131 

~/.pyenv/versions/3.7.1/lib/python3.7/site-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
     91                 products. Defaults to ``False``.
     92         """
---> 93         torch.autograd.backward(self, gradient, retain_graph, create_graph)
     94 
     95     def register_hook(self, hook):

~/.pyenv/versions/3.7.1/lib/python3.7/site-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
     88     Variable._execution_engine.run_backward(
     89         tensors, grad_tensors, retain_graph, create_graph,
---> 90         allow_unreachable=True)  # allow_unreachable flag
     91 
     92 

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

I don't konw why it go into error

wielandbrendel commented 5 years ago

It's hard to tell without the code. Are you sure adversarial is an image (and not e.g. None)?

wanghan0501 commented 5 years ago

I'm sure adversarial is an image.

config = parse_yaml()
config['use_tensorboard'] = False
config['device'] = 'cuda'
config['logger'] = False
model = capsnet_model.Model(config)
model.load('ckpt/mnist/2018Nov24-134539/176.pth')
capsnet = model.net.module.cuda().eval()

fmodel = foolbox.models.PyTorchModel(capsnet, bounds=(0, 1), num_classes=10)
dataset = datasets.MNIST('data/mnist/', train=True,
                         transform=transforms.ToTensor())
img, label = dataset.__getitem__(0)
img = img.data.numpy()
label = label.item()
print('label', label)
print('predicted class', np.argmax(fmodel.predictions(img)))

when I run the above code, it's ok, however, when i run the below code, it's error.

attack  = foolbox.attacks.FGSM(fmodel)
adversarial = attack(img, label)
wielandbrendel commented 5 years ago

Could you do a print(adversarial) and show the result here?

wanghan0501 commented 5 years ago

The result is shown in first comment.

wielandbrendel commented 5 years ago

@wanghan0501 No, in your first comment I just see the traceback. Please do:

print(adversarial) print(adversarial.shape) print(adversarial.dtype)

and show the result here.

wanghan0501 commented 5 years ago
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-428f388bfcfd> in <module>
----> 1 print(adversarial)
      2 print(adversarial.shape)
      3 print(adversarial.dtype)

NameError: name 'adversarial' is not defined
wielandbrendel commented 5 years ago

Please put the print statement below "adversarial = attack(img, label)" ...

wanghan0501 commented 5 years ago

Thanks for your help and I know why my codes go into error. The reason is that when I caculate the logits of CapsNet, I detach the relationship between logits and output.

wielandbrendel commented 5 years ago

Great you could resolve the issue!