Harry24k / adversarial-attacks-pytorch

PyTorch implementation of adversarial attacks [torchattacks]
https://adversarial-attacks-pytorch.readthedocs.io/en/latest/index.html
MIT License
1.9k stars 352 forks source link

[BUG] Can't generate CW attacks on the GPU #120

Closed MoiseRousseau closed 1 year ago

MoiseRousseau commented 1 year ago

✨ Short description of the bug [tl;dr]

I am running several attacks on different neural net. I first load the dataset on the GPU and then perform the generation on the GPU too. While this works well for many attacks (FGSM, BIM, PGD, VANILA, ...), this fails only for the CW attack. I work with torchattacks-3.3.0.

💬 Detailed code and results

The CW attack fails with the following error:

generating adversarial examples for DeepFool
saving adversarial examples #this works
generating adversarial examples for CW
Traceback (most recent call last):
  File "/home/moise/mlp_study_exp.py", line 331, in generating_adversarial_examples
    adv_images = atk(images, labels)
  File "/home/moise/env/lib/python3.9/site-packages/torchattacks/attack.py", line 429, in __call__
    adv_inputs = self.forward(inputs, labels, *args, **kwargs)
  File "/home/moise/env/lib/python3.9/site-packages/torchattacks/attacks/cw.py", line 84, in forward
    f_loss = self.f(outputs, labels).sum()
  File "/home/moise/env/lib/python3.9/site-packages/torchattacks/attacks/cw.py", line 125, in f
    one_hot_labels = torch.eye(len(outputs[0]))[labels].to(self.device)
RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (cpu)

Changing the faulty line (125 in attacks/cw.py) by:

one_hot_labels = torch.eye(len(outputs[0]), device=self.device)[labels]

solve the problem.

I can send a minimal reproducible example if you need it.

Moise

MoiseRousseau commented 1 year ago

Or, at least, changing by:

one_hot_labels = (torch.eye(len(outputs[0]).to(self.device))[labels]
rikonaka commented 1 year ago

Or, at least, changing by:

one_hot_labels = (torch.eye(len(outputs[0]).to(self.device))[labels]

Hi @MoiseRousseau , this bug has actually been fixed, but since the new version has not been released, the problem still exists for those installing via pip.

MoiseRousseau commented 1 year ago

Hi @rikonaka ,

I indeed installed torckattacks with PyPi, thanks for keeping me informed!

Moise