xaddwell / Adversarial-Attack-on-Machinery-Fault-Diagnosis

The code for http://arxiv.org/abs/2110.02498
19 stars 2 forks source link

AttributeError: 'PGD' object has no attribute '_supported_mode' #3

Open zy899 opened 1 year ago

zy899 commented 1 year ago

当我运行main时候,在utils.py中的254行。 if 'target' not in atk._supported_mode:处报错如标题所示,请问大家有遇到相同错误的嘛?

xaddwell commented 1 year ago

i add "MyAttackModel" dir, which may solve this problem.

zy899 commented 1 year ago

Dear author, I have added this directory and for the “models” under the models folder I have generated “.pth” files with pytorch.

The file generated here is the folder you mentioned .But there is an error in the attack. In main.py,line 80: trainer.attack()

Then there is a error at utils.py line 254: if 'target' not in atk._supported_mode:

The content of the error is as shown in the title

zy899 commented 1 year ago

Thank you very much for your generous upload, and I wish you success in your scientific research work!

xaddwell commented 1 year ago

我刚刚更新了一下attackMethod.py,你再看看行不行

zy899 commented 1 year ago

跑了一下似乎还是不行,不过我在询问torchattack的作者。我再尝试调试一下

xaddwell commented 1 year ago

import torch import torch.nn as nn

from ..attack import Attack

class PGD(Attack): r""" PGD in the paper 'Towards Deep Learning Models Resistant to Adversarial Attacks' [https://arxiv.org/abs/1706.06083]

Distance Measure : Linf

Arguments:
    model (nn.Module): model to attack.
    eps (float): maximum perturbation. (Default: 0.3)
    alpha (float): step size. (Default: 2/255)
    steps (int): number of steps. (Default: 40)
    random_start (bool): using random initialization of delta. (Default: True)

Shape:
    - images: :math:`(N, C, H, W)` where `N = number of batches`, `C = number of channels`,        `H = height` and `W = width`. It must have a range [0, 1].
    - labels: :math:`(N)` where each value :math:`y_i` is :math:`0 \leq y_i \leq` `number of labels`.
    - output: :math:`(N, C, H, W)`.

Examples::
    >>> attack = torchattacks.PGD(model, eps=8/255, alpha=1/255, steps=40, random_start=True)
    >>> adv_images = attack(images, labels)

"""
def __init__(self, model, eps=0.3,
             alpha=2/255, steps=40, random_start=True,minBound=0,maxBound=1):
    super().__init__("PGD", model)
    self.min = minBound
    self.max = maxBound
    self.eps = eps*(maxBound-minBound)
    self.alpha = alpha
    self.steps = steps
    self.random_start = random_start
    self._supported_mode = ['default', 'targeted']
xaddwell commented 1 year ago

这是torchattack的代码pgd.py,我可能有点改动

xaddwell commented 1 year ago

主要是看最后这一行

zy899 commented 1 year ago

这里又出现问题了 可能您的PGD确实改动了一些 1678368989872

xaddwell commented 1 year ago

我把torchattacks上传了,你可以直接import本地的文件

zy899 commented 1 year ago

解决了!太感谢您了!谢谢您慷慨耐心的一起帮忙解决问题,祝您科研顺利,身体健康!