-
Describe the bug While the document of To Reproduce import torch
import torch.nn as nn
from art.defences.preprocessor.preprocessor import PreprocessorPyTorch
from art.estimators.classification import PyTorchClassifier
from torchvision.models import resnet18
class Defense(PreprocessorPyTorch):
def forward(self, x, y=None):
return x.detach(), y
def estimate_forward(self, x, y=None):
return x, y
if __name__ == '__main__':
classifier = PyTorchClassifier(
model=resnet18(),
loss=nn.CrossEntropyLoss(),
input_shape=(3, 224, 224),
nb_classes=1000,
preprocessing_defences=Defense(),
device_type='cpu'
)
x = torch.rand(1, 3, 224, 224)
y = torch.eye(1000)[[0]]
grad = classifier.loss_gradient(x, y) The above code will raise the following error: Traceback (most recent call last):
File "/xxx/test.py", line 20, in <module>
classifier.loss_gradient(x, y)
File "/xxx/envs/armory/lib/python3.7/site-packages/art/estimators/classification/pytorch.py", line 810, in loss_gradient
assert grads.shape == x.shape
AttributeError: 'NoneType' object has no attribute 'shape' Expected behavior Screenshots System information (please complete the following information):
More information: adversarial-robustness-toolbox/art/defences/preprocessor/preprocessor.py Lines 168 to 179 in bc49671 After tracking down the function adversarial-robustness-toolbox/art/estimators/classification/pytorch.py Lines 807 to 808 in bc49671 I am not sure how to make from art.defences.preprocessor.preprocessor import Preprocessor
class DummyDefense(Preprocessor):
def __call__(self, x, y=None):
return x, y
def forward(self, x, y=None):
return x, y
if __name__ == '__main__':
classifier = PyTorchClassifier(
model=resnet18(),
loss=nn.CrossEntropyLoss(),
input_shape=(3, 224, 224),
nb_classes=1000,
preprocessing_defences=[DummyDefense(), Defense()],
device_type='cpu'
)
x = torch.rand(1, 3, 224, 224)
y = torch.eye(1000)[[0]]
grad = classifier.loss_gradient(x, y) But got the following error: Traceback (most recent call last):
File "/xxx/envs/armory/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/xxx/envs/armory/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/xxx/dummy.py", line 36, in <module>
grad = classifier.loss_gradient(x, y)
File "/xxx/envs/armory/lib/python3.7/site-packages/art/estimators/classification/pytorch.py", line 775, in loss_gradient
raise NotImplementedError("Combination of inputs and preprocessing not supported.")
NotImplementedError: Combination of inputs and preprocessing not supported. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @beat-buesser, sorry to bother but I was wondering why this is not a bug. You can verify so by searching "estimate_forward" in the entire codebase: If I understand it correctly, the current Thanks! |
Beta Was this translation helpful? Give feedback.
Hi @beat-buesser, sorry to bother but I was wondering why this is not a bug.
The
estimate_forward
method inPreprocessorPyTorch
is documented but not invoked anywhere.You can verify so by searching "estimate_forward" in the entire codebase:
https://github.com/Trusted-AI/adversarial-robustness-toolbox/search?q=estimate_forward
If I understand it correctly, the current
PreprocessorPyTorch
does not support BPDA. It seems thatPreprocessorPyTorch
is assumed to have a differentiableforward
, and theestimate_forward
is only put there for documentation.Thanks!