Harry24k / adversarial-attacks-pytorch

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

one pixel numpy, tensor incompatiblity #162

Closed hosnahoseini closed 8 months ago

hosnahoseini commented 11 months ago

✨ Short description of the bug [tl;dr]

In torchattacks/attacks/onepixel.py. there is a place where it wants to convert tensor to numpy but it needs to fist transfer the tensor to cpu which is not implemented I think :-?

💬 Detailed code and results

I use tensors on cuda as input to onepixel attack:

attack = torchattacks.OnePixel(self.model, pixels=1, steps=10, popsize=10, inf_batch=128)
adv_images = attack((xs/torch.max(xs)), target)

but got the following error:

File "/usr/local/lib/python3.10/dist-packages/torchattacks/attacks/onepixel.py", line 100, in _loss
    prob = self._get_prob(adv_images)[:, label]
  File "/usr/local/lib/python3.10/dist-packages/torch/_tensor.py", line 970, in __array__
    return self.numpy()
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
rikonaka commented 11 months ago

Hi @hosnahoseini , It doesn't matter which device the input is on, I guess it's something else going wrong in your code. 😉😉😉

image

device = 'cuda'