DiffAttack icon indicating copy to clipboard operation
DiffAttack copied to clipboard

代码里面关于对潜在空间的扰动

Open Scientist888-star opened this issue 9 months ago • 15 comments

您好!感谢您出彩的作品,对我的研究有很好借鉴意义,我在看您项目代码的时候有一些疑惑,我没太get到对于潜在空间扰动在代码里面如何体现的,代码其余部分我已理解,期待您的回复!

Scientist888-star avatar Jun 05 '25 13:06 Scientist888-star

哈喽 @niania-whb ,

这一部分是指对扩散模型的隐变量直接做优化哈,可以看这部分的的代码段,隐变量为优化对象.

WindVChen avatar Jun 05 '25 14:06 WindVChen

这个地方是对latent的有文本部分进行优化,然后用损失Ltransfer去逼近注意力点平均分布吗

Scientist888-star avatar Jun 05 '25 14:06 Scientist888-star

哈喽,准确来说是有三个损失函数共同作用哈(见此). 另外,这里是对整个 latent tensor 进行优化的,并不存在“latent 的文本部分”这种说法。在 classifier-free 的去噪过程中,会对 latent 进行复制,分别在带文本条件和空文本条件下进行去噪,最后将二者的结果融合 (见此).

WindVChen avatar Jun 05 '25 14:06 WindVChen

好嘞,感谢您的解答,我已理解,谢谢!

Scientist888-star avatar Jun 06 '25 00:06 Scientist888-star

发生异常: RuntimeError CUDA error: invalid argument CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect. For debugging consider passing CUDA_LAUNCH_BLOCKING=1. Compile with TORCH_USE_CUDA_DSA to enable device-side assertions. File "/root/project/DiffAttack-main/diff_latent_attack.py", line 541, in diffattack loss.backward() File "/root/project/DiffAttack-main/main.py", line 72, in run_diffusion_attack adv_image, clean_acc, adv_acc = diff_latent_attack.diffattack(diffusion_model, label, controller, File "/root/project/DiffAttack-main/main.py", line 170, in adv_image, clean_acc, adv_acc = run_diffusion_attack(tmp_image, label[ind:ind + 1], RuntimeError: CUDA error: invalid argument CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect. For debugging consider passing CUDA_LAUNCH_BLOCKING=1. Compile with TORCH_USE_CUDA_DSA to enable device-side assertions.

您好,我运行时出现这个问题,loss不会出现nan或者inf,显存情况如下,cuda都放到gpu:0上,请问这个是啥问题呢

Image

Scientist888-star avatar Jun 06 '25 12:06 Scientist888-star

这个蛮奇怪的,我这儿没有遇到过这个问题,有改动哪一部分代码嘛?是直接按照ReadMe里的步骤来跑的嘛?

WindVChen avatar Jun 06 '25 20:06 WindVChen

目前没有改动代码,我使用了我自己的数据集,总共20各类别,我先使用预训练好的resnet,fc输出已改成20,不知道是啥情况,是不是跟torch版本有关,我从网上没找到解决办法,使用cpu跑就没问题

Scientist888-star avatar Jun 07 '25 01:06 Scientist888-star

哈喽 @niania-whb ,

不好意思回复晚了,上面这个TORCH_USE_CUDA_DSA问题以及这个热力点的问题我不是很确定. 这两个问题在默认的ImageNet-Compatible数据集上会出现同样的问题吗?还是说目前只有在私有数据集上出现呢?

WindVChen avatar Jun 12 '25 15:06 WindVChen

感谢您的回复,第一个问题我已经解决,就是第二个问题,我没在imagenet上跑,我是在自己的私有数据集上跑出现的这个问题

---- 回复的原邮件 ---- | 发件人 | @.> | | 日期 | 2025年06月12日 23:57 | | 收件人 | @.> | | 抄送至 | Hongbing @.>@.> | | 主题 | Re: [WindVChen/DiffAttack] 代码里面关于对潜在空间的扰动 (Issue #49) | WindVChen left a comment (WindVChen/DiffAttack#49)

哈喽 @niania-whb ,

不好意思回复晚了,上面这个TORCH_USE_CUDA_DSA问题以及这个热力点的问题我不是很确定. 这两个问题在默认的ImageNet-Compatible数据集上会出现同样的问题吗?还是说目前只有在私有数据集上出现呢?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

Scientist888-star avatar Jun 12 '25 16:06 Scientist888-star

上面提到的"cross attention 之前 之后"是指什么呢?

热力图是说变化不明显吗?我看上下两行系列上还是有些变化

WindVChen avatar Jun 12 '25 19:06 WindVChen

您好,cross前后就是热力图的变化,感觉变化不明显,按照原文的内容和图2的cross attention变化前后的图热力图应该是平均分散的,但是我跑出来的没啥变化

---- 回复的原邮件 ---- | 发件人 | @.> | | 日期 | 2025年06月13日 03:22 | | 收件人 | @.> | | 抄送至 | Hongbing @.>@.> | | 主题 | Re: [WindVChen/DiffAttack] 代码里面关于对潜在空间的扰动 (Issue #49) | WindVChen left a comment (WindVChen/DiffAttack#49)

上面提到的"cross attention 之前 之后"是指什么呢?

热力图是说变化不明显吗?我看上下两行系列上还是有些变化

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

Scientist888-star avatar Jun 12 '25 23:06 Scientist888-star

可以尝试下增大transfer loss的权重,看看是否有更明显的变化?

https://github.com/WindVChen/DiffAttack/blob/main/main.py#L46

WindVChen avatar Jun 13 '25 06:06 WindVChen

增加了10倍还是不行,会不会是因为文字描述部分,因为文本编码器会把文字分开,分开之后语义发生了变化,比如“xxx airplane”,这个xxx是好多种修饰词,造成stable diffusion无法正确定位

---- 回复的原邮件 ---- | 发件人 | @.> | | 发送日期 | 2025年6月13日 14:52 | | 收件人 | @.> | | 抄送人 | Hongbing @.>, @.> | | 主题 | Re: [WindVChen/DiffAttack] 代码里面关于对潜在空间的扰动 (Issue #49) | WindVChen left a comment (WindVChen/DiffAttack#49)

可以尝试下增大transfer loss的权重,看看是否有更明显的变化?

https://github.com/WindVChen/DiffAttack/blob/main/main.py#L46

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

Scientist888-star avatar Jun 13 '25 06:06 Scientist888-star

我之前没有尝试过比较多的修饰词,大部分都是控制在1-3个文本之内.

那可以先尝试下 1) 减少修饰词,比如约束在1-3个词之内;2) 尝试将structure loss的权重降低,因为它对于个人数据集来说,带来的约束可能比较强.

看看这些是否带来改变.

WindVChen avatar Jun 14 '25 08:06 WindVChen

好的谢谢您,我微调一下stable diffusion试试,您说的我修改后有一定提升

Scientist888-star avatar Jun 16 '25 06:06 Scientist888-star