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

Using Saved model for generating adversarial samples. #189

Open chamanbanolia opened 1 week ago

chamanbanolia commented 1 week ago

I am not able to create an attack instance when I am using loaded saved model (.pth file) for generating adversarial samples, can you please help find out, what can be the potential issue in this.

Below is the error I am getting

RuntimeError Traceback (most recent call last) Input In [25], in 1 from torchattacks import FGSM, PGD ----> 3 atk = FGSM(saved_model) 4 print(atk)

File /opt/conda/lib/python3.8/site-packages/torchattacks/attacks/fgsm.py:30, in FGSM.init(self, model, eps) 29 def init(self, model, eps=8 / 255): ---> 30 super().init("FGSM", model) 31 self.eps = eps 32 self.supported_mode = ["default", "targeted"]

File /opt/conda/lib/python3.8/site-packages/torchattacks/attack.py:40, in Attack.init(self, name, model) 37 self.attack = name 38 self._attacks = OrderedDict() ---> 40 self.set_model(model) 41 try: 42 self.device = next(model.parameters()).device

File /opt/conda/lib/python3.8/site-packages/torchattacks/attack.py:10, in wrapper_method..wrapper_func(self, *args, kwargs) 9 def wrapper_func(self, *args, *kwargs): ---> 10 result = func(self, args, kwargs) 11 for atk in self.dict.get("_attacks").values(): 12 eval("atk." + func.name + "(*args, **kwargs)")

File /opt/conda/lib/python3.8/site-packages/torchattacks/attack.py:73, in Attack.set_model(self, model) 71 @wrapper_method 72 def set_model(self, model): ---> 73 self.model = model 74 self.model_name = model.class.name

File /opt/conda/lib/python3.8/site-packages/torchattacks/attack.py:563, in Attack.setattr(self, name, value) 560 if isinstance(items, Attack): 561 yield items --> 563 for num, value in enumerate(get_all_values(value)): 564 attacks[name + "." + str(num)] = value 565 for subname, subvalue in value.dict.get("_attacks").items():

File /opt/conda/lib/python3.8/site-packages/torchattacks/attack.py:555, in Attack.setattr..get_all_values(items, stack) 553 items = list(items.keys()) + list(items.values()) 554 for item in items: --> 555 yield from get_all_values(item, stack) 556 else: 557 if isinstance(items, Attack):

File /opt/conda/lib/python3.8/site-packages/torchattacks/attack.py:549, in Attack.setattr..get_all_values(items, stack) 548 def get_all_values(items, stack=[]): --> 549 if items not in stack: 550 stack.append(items) 551 if isinstance(items, list) or isinstance(items, dict):

RuntimeError: Boolean value of Tensor with more than one value is ambiguous

rikonaka commented 1 week ago

Hi @chamanbanolia , can you provide your full code?