rwightman / pytorch-nips2017-attack-example

A PyTorch baseline attack example for the NIPS 2017 adversarial competition
https://www.kaggle.com/c/nips-2017-targeted-adversarial-attack
Apache License 2.0
84 stars 27 forks source link

the generated adv example as the same original images #5

Open xubo00 opened 5 years ago

xubo00 commented 5 years ago

I have tried the attack, but it seems that there is no difference between the adv example and the original image. Maybe the attack failed. the file of run_attack_cwl2.py can not achieve the attack function.

ParagonLight commented 5 years ago

I found the same problem. My solution is to send the copy of the input (input.clone()) when calling the run function.

xubo0321 commented 5 years ago

I found the same problem. My solution is to send the copy of the input (input.clone()) when calling the run function. 也许只是眼睛看起来不太明显,但是利用函数求原始图片以及对抗样本的distortion,还是能够看出区别,很明显针对两幅原图比较,结果为0,原始图片与对抗图片肯定不能为0。

ParagonLight commented 5 years ago

I found the same problem. My solution is to send the copy of the input (input.clone()) when calling the run function. 也许只是眼睛看起来不太明显,但是利用函数求原始图片以及对抗样本的distortion,还是能够看出区别,很明显针对两幅原图比较,结果为0,原始图片与对抗图片肯定不能为0。

其实很明显,你只要在optimizer里不停打印input_adv和input_var就能看出每次更新,input_var也会被更新。我还没细究原因,目前看,可能是直接用input_var的话, pytorch内部机制导致一些关于grad的地方没有真正被关掉。我尝试在使用input_var的地方先clone一份出来,然后发现,input就不变了。

xubo0321 commented 5 years ago

I found the same problem. My solution is to send the copy of the input (input.clone()) when calling the run function. 也许只是眼睛看起来不太明显,但是利用函数求原始图片以及对抗样本的distortion,还是能够看出区别,很明显针对两幅原图比较,结果为0,原始图片与对抗图片肯定不能为0。

其实很明显,你只要在optimizer里不停打印input_adv和input_var就能看出每次更新,input_var也会被更新。我还没细究原因,目前看,可能是直接用input_var的话, pytorch内部机制导致一些关于grad的地方没有真正被关掉。我尝试在使用input_var的地方先clone一份出来,然后发现,input就不变了。

input中的原始图片也会更新??

ParagonLight commented 5 years ago

I found the same problem. My solution is to send the copy of the input (input.clone()) when calling the run function. 也许只是眼睛看起来不太明显,但是利用函数求原始图片以及对抗样本的distortion,还是能够看出区别,很明显针对两幅原图比较,结果为0,原始图片与对抗图片肯定不能为0。

其实很明显,你只要在optimizer里不停打印input_adv和input_var就能看出每次更新,input_var也会被更新。我还没细究原因,目前看,可能是直接用input_var的话, pytorch内部机制导致一些关于grad的地方没有真正被关掉。我尝试在使用input_var的地方先clone一份出来,然后发现,input就不变了。

input中的原始图片也会更新??

对,最终input_var和input_adv变成数值上一样的tensor,那dist自然是0,loss2就是0。然后input_adv肯定能让model分类错误,那loss1也是0。这样下来,最终就全是0了。

你想想,什么情况会导致loss是0,一步步推,打印输出,就能发现问题。

xubo0321 commented 5 years ago

I found the same problem. My solution is to send the copy of the input (input.clone()) when calling the run function. 也许只是眼睛看起来不太明显,但是利用函数求原始图片以及对抗样本的distortion,还是能够看出区别,很明显针对两幅原图比较,结果为0,原始图片与对抗图片肯定不能为0。 其实很明显,你只要在optimizer里不停打印input_adv和input_var就能看出每次更新,input_var也会被更新。我还没细究原因,目前看,可能是直接用input_var的话, pytorch内部机制导致一些关于grad的地方没有真正被关掉。我尝试在使用input_var的地方先clone一份出来,然后发现,input就不变了。 input中的原始图片也会更新??

对,最终input_var和input_adv变成数值上一样的tensor,那dist自然是0,loss2就是0。然后input_adv肯定能让model分类错误,那loss1也是0。这样下来,最终就全是0了。

你想想,什么情况会导致loss是0,一步步推,打印输出,就能发现问题。

提供的这个方法是不是就没有成功的进行攻击。

ParagonLight commented 5 years ago

I found the same problem. My solution is to send the copy of the input (input.clone()) when calling the run function. 也许只是眼睛看起来不太明显,但是利用函数求原始图片以及对抗样本的distortion,还是能够看出区别,很明显针对两幅原图比较,结果为0,原始图片与对抗图片肯定不能为0。 其实很明显,你只要在optimizer里不停打印input_adv和input_var就能看出每次更新,input_var也会被更新。我还没细究原因,目前看,可能是直接用input_var的话, pytorch内部机制导致一些关于grad的地方没有真正被关掉。我尝试在使用input_var的地方先clone一份出来,然后发现,input就不变了。 input中的原始图片也会更新??

对,最终input_var和input_adv变成数值上一样的tensor,那dist自然是0,loss2就是0。然后input_adv肯定能让model分类错误,那loss1也是0。这样下来,最终就全是0了。 你想想,什么情况会导致loss是0,一步步推,打印输出,就能发现问题。

提供的这个方法是不是就没有成功的进行攻击。

Although the issue exists, CW attack in this repo still works.

JonathanCMitchell commented 4 years ago

It might be because in https://github.com/rwightman/pytorch-nips2017-attack-example/blob/master/attacks/attack_iterative.py#L78 total_adv = torch.clamp(total_adv, -eps, eps) This means that it is clipping total_adv to only contain values between -eps and eps, where eps~0.031 or something like that. Therefore you can only contain pixels within values [-0.031, 0.031].