WindVChen / DiffAttack

An unrestricted attack based on diffusion models that can achieve both good transferability and imperceptibility.
Apache License 2.0
131 stars 12 forks source link

请问我可以在我自己的多分类数据集上生成攻击样本吗?具体应该怎么做呢?我看了可以在CUB_200_2011 和 Standford数据上的实现 #23

Open woaiwojia4816294 opened 1 month ago

woaiwojia4816294 commented 1 month ago

只需要给自己数据集的每个类别添加文字描述,并制作成 label.txt,然后设置好数据集路径就可以吗?

WindVChen commented 1 month ago

哈喽 @woaiwojia4816294 ,

是的,只需要按照demo文件夹里的格式来整理数据,就能够在新数据集上生成对抗样本。

考虑到多分类任务中一幅图像会有多个标签,所以你可以尝试直接将几个标签串联在一起,或者随机选取其中一个作为text prompt,来看看效果如何。此外,对于新的数据集,你可能需要调整损失函数的权重,来避免梯度消失:

https://github.com/WindVChen/DiffAttack/blob/163f0f7f2bbfd7e57ed99c0baf6523d95167a3e4/diff_latent_attack.py#L398-L401.

woaiwojia4816294 commented 1 month ago

请问我是否应该重新训练好classifier呢,我的数据集是20类

WindVChen commented 1 month ago

是的,要生成对抗样本,首先得有训练好的分类器.

woaiwojia4816294 commented 1 month ago

请问为什么训练的时候 imagenet_compatible数据集的 pred 需要除以10,另外两个数据集不需要呢?是否在计算 attack_loss 前使用 torch.nn.Softmax()(pred) 就能够避免不同的数据集的 pred 是否要放大缩小的问题了 image

WindVChen commented 1 month ago

哈喽,Softmax已经包含在torch.nn.CrossEntropyLoss()函数中了,关于这个系数的含义,可以搜索关键词softmax temperature来了解下~

woaiwojia4816294 commented 1 month ago

非常感谢您的耐心回复,请问我是不是应该增加iteration,让 variance_cross_attn_loss和self_attn_loss继续下降呢 image image image

WindVChen commented 1 month ago

结果看着有些奇怪,第一个iteration的时候attack loss值就有28,可以再验证下所用的分类器,看看它是否能输出正确的分类结果

woaiwojia4816294 commented 1 month ago

分类器是正常的,也可以成功攻击,差不多1000个样本,800个可以攻击成功。就是用其他指标评价攻击样本时,效果比较差,就是生成的攻击样本和原样本相比差别稍微有点大

WindVChen commented 1 month ago

哈喽,可以尝试通过增大structure loss的权重、减少迭代轮次、减少DDIM Inversion步数等来提升内容一致性

不过还是觉得上面图片里的loss日志有点奇怪,比如attack loss每一个iteration基本都不怎么下降🤔