简体   繁体   English

无法使用经过训练的 NSGA-Net PyTorch 模型生成对抗样本

[英]Failed to generate adversarial examples using trained NSGA-Net PyTorch models

I have used NSGA-Net neural architecture search to generate and train several architectures.我使用 NSGA-Net 神经架构搜索来生成和训练几种架构。 I am trying to generate PGD adversarial examples using my trained PyTorch models.我正在尝试使用经过训练的 PyTorch 模型生成 PGD 对抗性示例。 I tried using both Adversarial Robustness Toolbox 1.3 (ART) and torchattacks 2.4 but I get the same error.我尝试同时使用 Adversarial Robustness Toolbox 1.3 (ART) 和 torchattacks 2.4,但我得到了同样的错误。

These few lines of code describe the main functionality of my code and what I am trying to achieve here:这几行代码描述了我的代码的主要功能以及我在这里想要实现的目标:

# net is my trained NSGA-Net PyTorch model

# Defining PGA attack

pgd_attack = PGD(net, eps=4 / 255, alpha=2 / 255, steps=3)

# Creating adversarial examples using validation data and the defined PGD attack

for images, labels in valid_data:
    images = pgd_attack(images, labels).cuda()
    outputs = net(images)


So here is what the error generally looks like:因此,错误通常如下所示:

Traceback (most recent call last):
  File "torch-attacks.py", line 296, in <module>
    main()
  File "torch-attacks.py", line 254, in main
    images = pgd_attack(images, labels).cuda()
  File "\Anaconda3\envs\GPU\lib\site-packages\torchattacks\attack.py", line 114, in __call__
    images = self.forward(*input, **kwargs)
  File "\Anaconda3\envs\GPU\lib\site-packages\torchattacks\attacks\pgd.py", line 57, in forward
    outputs = self.model(adv_images)
  File "\envs\GPU\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "\codes\NSGA\nsga-net\models\macro_models.py", line 79, in forward
    x = self.gap(self.model(x))
  File "\Anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "\Anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\container.py", line 100, in forward
    input = module(input)
  File "\Anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "\codes\NSGA\nsga-net\models\macro_decoder.py", line 978, in forward
    x = self.first_conv(x)
  File "\Anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "\Anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\conv.py", line 345, in forward
    return self.conv2d_forward(input, self.weight)
  File "\Anaconda3\envs\GPU\lib\site-packages\torch\nn\modules\conv.py", line 342, in conv2d_forward
    self.padding, self.dilation, self.groups)
RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #2 'weight' in call to _thnn_conv2d_forward

I have used the same the code with a simple PyTorch model and it worked but I am using NSGA-Net so I haven't designed the model myself.我已经使用了与简单的 PyTorch model 相同的代码并且它工作但我使用的是 NSGA-Net 所以我没有自己设计 model。 I also tried using .float() on both the model and inputs and still got the same error.我还尝试在 model 和输入上使用.float() ,但仍然遇到相同的错误。

Keep in mind that I only have access to the following files:请记住,我只能访问以下文件:

  • torch-attacks.py火炬攻击.py
  • macro_models.py宏模型.py
  • macro_decoder.py宏解码器.py

You should convert images to the desired type ( images.float() in your case).您应该将images转换为所需的类型(在您的情况下为images.float() )。 Labels must not be converted to any floating type.标签不得转换为任何浮动类型。 They are allowed to be either int or long tensors.它们可以是intlong张量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在pytorch中实现对抗性示例? - How does one implement adversarial examples in pytorch? 接收断言失败虽然通过任何方法生成对抗样本 - Receiving Assertion failed While generate adversarial samples by any methods 在 PyTorch 中加载 Torch7 训练模型 (.t7) - Loading Torch7 trained models (.t7) in PyTorch 如何使用我自己的图片使用 FGSM 生成对抗样本? - How to use my own picture to generate adversarial example using FGSM? 如何使用预训练模型从 RGB 图像中提取所需特征的张量? - How to generate a tensor of desired features, extracted from RGB images using pre-trained models? PyTorch前传使用Theano训练的举重 - PyTorch forward pass using weights trained by Theano 在GANEstimator中使用对抗性损失 - Using adversarial loss in GANEstimator 如何使用经过训练的 Image to Emotion Pytorch 模型进行预测 - How do I predict using a trained Image to Emotion Pytorch model 在 pytorch、model 中使用相同的脚本和 model 随机训练 - In pytorch, model randomly trained when using same script and model 有什么方法可以将PyTorch中可用的预训练模型下载到特定路径吗? - Is there any way I can download the pre-trained models available in PyTorch to a specific path?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM