Harry24k / adversarial-attacks-pytorch

PyTorch implementation of adversarial attacks [torchattacks]
https://adversarial-attacks-pytorch.readthedocs.io/en/latest/index.html
MIT License
1.86k stars 348 forks source link

[question] different code to realize same function? #85

Closed Freed-Wu closed 2 years ago

Freed-Wu commented 2 years ago

In bim.py:

            a = torch.clamp(ori_images - self.eps, min=0)
            b = (adv_images >= a).float()*adv_images \
                + (adv_images < a).float()*a
            c = (b > ori_images+self.eps).float()*(ori_images+self.eps) \
                + (b <= ori_images + self.eps).float()*b
            images = torch.clamp(c, max=1).detach()

In rfgsm.py:

            delta = torch.clamp(adv_images - images, min=-self.eps, max=self.eps)
            adv_images = torch.clamp(images + delta, min=0, max=1).detach()

Seem that there are same function:

$\mathrm{advimage} \leftarrow \max(\mathrm{advimage}, (\mathrm{oriimage} - \epsilon))$ $\mathrm{advimage} \leftarrow \min(\mathrm{advimage}, (\mathrm{oriimage} + \epsilon))$ $\mathrm{advimage} \leftarrow \max(\mathrm{advimage}, 0)$ $\mathrm{advimage} \leftarrow \min(\mathrm{advimage}, 1)$

Why use different code to realize?

Harry24k commented 2 years ago

Definitely, they are identical. However, the codes are built on the proposed algorithm in the original paper. I hope the answer helps you!