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

How to load datasets that are not among the sample? #732

Open DeepOceanDeep opened 8 months ago

DeepOceanDeep commented 8 months ago

Hi,

I'd like to use attacks available in foolbox to run them over the full cifar10, 100, imagenet, and not the few images that are available from the sample folder. The tutorial only explains that and when I tested questions on issues, I was not successful in loading.

Basically I want to load a pretrained model, and load a batch of size 128, and see accuracies.`Any help would help a lot and appreciated. Thanks in advance.

model = timm.create_model("resnet18", pretrained=True, num_class=10)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=128, shuffle=False, num_workers=2)
processing =dict( mean=[0.491, 0.482, 0.446],std=[0.247, 0.243, 0.261], axis=-3)
bounds = ((0,1))
fmodel = fb.PyTorchModel(model, bounds=bounds, preprocessing=processing)
assert fmodel.bounds == (0, 1)
images, labels = next(iter(testloader))
fb.utils.accuracy(fmodel, images, labels)

attack = fb.attacks.LinfDeepFoolAttack()
raw, clipped, is_adv = attack(fmodel, images, labels, epsilons=0.03)
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

Even when I send everything to cuda, same error happens!! is there any miminal working code that I uses the full cifar10 that I use it as the start code?