Open bluefier opened 12 months ago
你好,在运行main_test.py时,我遇到如下报错
通过查看代码,发现FGSM和PGD类定义时,并没有set_training_mode属性,具体定义代码如下:
class FGSM(Attack): def __init__(self, model, forward_function=None, eps=0.007, T=None, **kwargs): super().__init__("FGSM", model) self.eps = eps self._supported_mode = ['default', 'targeted'] self.forward_function = forward_function self.T = T def forward(self, images, labels): r""" Overridden. """ # images.unsqueeze_(1) # images = images.repeat(8, 1, 1, 1, 1) images = images.clone().detach().to(self.device) labels = labels.clone().detach().to(self.device) if self._targeted: target_labels = self._get_target_label(images, labels) loss = nn.CrossEntropyLoss() images.requires_grad = True if self.forward_function is not None: outputs = self.forward_function(self.model, images, self.T) else: outputs = self.model(images) # Calculate loss if self._targeted: cost = -loss(outputs, target_labels) else: cost = loss(outputs, labels) # Update adversarial images grad = torch.autograd.grad(cost, images, retain_graph=False, create_graph=False)[0] adv_images = images + self.eps*grad.sign() adv_images = torch.clamp(adv_images, min=0, max=1).detach() return adv_images class PGD(Attack): def __init__(self, model, forward_function=None, eps=0.3, alpha=2/255, steps=40, random_start=True, T=None, **kwargs): super().__init__("PGD", model) self.eps = eps self.alpha = alpha self.steps = steps self.random_start = random_start self._supported_mode = ['default', 'targeted'] self.forward_function = forward_function self.T = T def forward(self, images, labels): r""" Overridden. """ images = images.clone().detach().to(self.device) labels = labels.clone().detach().to(self.device) if self._targeted: target_labels = self._get_target_label(images, labels) loss = nn.CrossEntropyLoss() adv_images = images.clone().detach() if self.random_start: # Starting at a uniformly random point adv_images = adv_images + torch.empty_like(adv_images).uniform_(-self.eps, self.eps) adv_images = torch.clamp(adv_images, min=0, max=1).detach() for _ in range(self.steps): adv_images.requires_grad = True if self.forward_function is not None: outputs = self.forward_function(self.model, adv_images, self.T) else: outputs = self.model(adv_images) # Calculate loss if self._targeted: cost = -loss(outputs, target_labels) else: cost = loss(outputs, labels) # Update adversarial images grad = torch.autograd.grad(cost, adv_images, retain_graph=False, create_graph=False)[0] adv_images = adv_images.detach() + self.alpha*grad.sign() delta = torch.clamp(adv_images - images, min=-self.eps, max=self.eps) adv_images = torch.clamp(images + delta, min=0, max=1).detach() return adv_images
请问是否是由于部分代码遗失导致的呢?请问该如何解决呢?
torchattacks版本导致
你好,在运行main_test.py时,我遇到如下报错
通过查看代码,发现FGSM和PGD类定义时,并没有set_training_mode属性,具体定义代码如下:
请问是否是由于部分代码遗失导致的呢?请问该如何解决呢?