[英]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:请记住,我只能访问以下文件:
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.它们可以是
int
或long
张量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.