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

AttributeError: 'PyTorchModel' object has no attribute 'forward_one' #328

Closed haim-barad closed 5 years ago

haim-barad commented 5 years ago

I'm trying to use a resnet50 (imagenet) and perform some simple attacks - however, I can't even get the samples in the docs working:

(Pdb) print(fmodel)

<foolbox.models.pytorch.PyTorchModel object at 0x7ff7e59b3be0> (Pdb) print(dir(fmodel)) ['abstractmethods', 'class', 'delattr', 'dict', 'dir', 'doc', 'enter', 'eq', 'exit', 'format', 'ge', 'getattribute', 'gt', 'hash', 'init', 'init_subclass', 'le', 'lt', 'module', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof', 'str', 'subclasshook', 'weakref', '_abc_cache', '_abc_negative_cache', '_abc_negative_cache_version', '_abc_registry', '_bounds', '_channel_axis', '_loss_fn', '_model', '_num_classes', '_old_pytorch', '_preprocessing', '_process_gradient', '_process_input', 'backward', 'batch_predictions', 'bounds', 'channel_axis', 'device', 'gradient', 'num_classes', 'predictions', 'predictions_and_gradient'] (Pdb)

and then when I try to do

print('predicted class', np.argmax(fmodel.forward_one(fimage)))

I get the error

AttributeError: 'PyTorchModel' object has no attribute 'forward_one'

The code is below:

215 # adversarial attacks and testing via foolbox 216 mean = np.array([0.485, 0.456, 0.406]).reshape((3, 1, 1)) 217 std = np.array([0.229, 0.224, 0.225]).reshape((3, 1, 1)) 218 model.eval() 219 fmodel = foolbox.models.PyTorchModel( 220 model, bounds=(0, 1), num_classes=1000, preprocessing=(mean, std)) 221
222
223 # get source image and label 224 B fimage, flabel = foolbox.utils.imagenet_example(data_format='channels_first') 225 fimage = fimage / 255. # because our model expects values in [0, 1] (Pdb) 226
227 print('label', flabel) 228 -> print('predicted class', np.argmax(fmodel.forward_one(fimage))) 229
230 # apply attack on source image 231 attack = foolbox.attacks.FGSM(fmodel) 232 adversarial = attack(fimage, flabel) 233
234 print('adversarial class', np.argmax(fmodel.forward(adversarial)))

The error occurs on the last line (234) - I'm sure there's some small typo, bad indent, etc. Has anyone run into this?

Is there a difference between the current code base and the version you get when running "pip install foolbox"?

haim-barad commented 5 years ago

I found my own mistake. Closing issue. I had installed via "pip install foolbox" and also via setup.py - this was a mistake and the versions aren't compatible.

Issue closed.

aicools commented 3 years ago

How did you solve this problem