DSE-MSU / DeepRobust

A pytorch adversarial library for attack and defense methods on images and graphs
MIT License
994 stars 192 forks source link

Question: how to use my own image #84

Closed Nevermore-Aino closed 2 years ago

Nevermore-Aino commented 3 years ago

test_loader = torch.utils.data.DataLoader( datasets.CIFAR10('deeprobust/image/data', train = False, download=True, transform = transform_val), batch_size = 1, shuffle=True) #, **kwargs)

classes = np.array(('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'))

xx, yy = next(iter(test_loader)) #<<<<<<<<<<<<<<<------------------------------------------- xx = xx.to('cuda').float()

predict0 = model(xx) predict0= predict0.argmax(dim=1, keepdim=True)

adversary = PGD(model) AdvExArray = adversary.generate(xx, yy, **attack_params['PGD_CIFAR10']).float()


I am a beginner, and DeepRobust helps me a lot.

I want to use a specific image from CIFAR10 or my own custom image instead of xx, yy = next(iter(test_loader)). I did some searching online, but I couldn't find an solution yet.

Sorry to leave a noob question here, but I really could use some help.

Thanks

YaxinLi0-0 commented 3 years ago

Glad you find DeepRobust helpful! To use a specific image, you can try the following lines: IMAGE_ID = 123 ds = datasets.CIFAR10('deeprobust/image/data', train=False, download=True) x, y = ds[IMAGE_ID][0], ds[IMAGE_ID][1] In case you want to create a subset for a specific class, hopefully this will be helpful to you: https://stackoverflow.com/questions/59351910/cifar10-dataset-read-certain-number-of-images-from-a-class

Nevermore-Aino commented 3 years ago

test_loader = torch.utils.data.DataLoader( datasets.CIFAR10('deeprobust/image/data', train = False, download=True, transform = transform_val), batch_size = 1, shuffle=True) #, **kwargs)

classes = np.array(('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'))

IMAGE_ID = 1 #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ds = datasets.CIFAR10('deeprobust/image/data', train=False, download=True) xx, yy = ds[IMAGE_ID][0], ds[IMAGE_ID][1] predict0 = model(xx) #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< predict0= predict0.argmax(dim=1, keepdim=True)


After I tried the giving codes, I got this error below:

File "mypic.py", line 39, in predict0 = model(xx) didn't match because some of the arguments have invalid types: (Image, Parameter, NoneType, tuple, tuple, tuple, int)

I am so excited to read your response. Thanks for helping.