kkew3 / pytorch-cw2

A rich-documented PyTorch implementation of Carlini-Wagner's L2 attack.
MIT License
59 stars 17 forks source link

min-max of normalized values #3

Closed ryang-25 closed 2 months ago

ryang-25 commented 2 months ago

Hello,

I am writing a cw attack and noticed these few lines and I wanted to say that this is only true when mean and std are both 0.5. If the normalizing transform uses other values (such as in the case of imagenet, you end up with values < -1 and >1 at the extremes.

https://github.com/kkew3/pytorch-cw2/blob/44993391ac9444b9941596d7fec6627fe6673910/cw.py#L174-L179

Thanks for the well documented code otherwise

kkew3 commented 2 months ago

Hello. Thanks for your comment!

You're right that the wording is not (strictly) correct -- probably should be corrected as:

The normalized input image will now be roughly in range [-1, 1].

However, that the box defaults to (-1, 1) doesn't mean it has to be that range; you may adapt it to your normalization. That said, it's been a long time since I wrote this code, and I may not be able to give precise guideline on how to set the box range.

ryang-25 commented 2 months ago

However, that the box defaults to (-1, 1) doesn't mean it has to be that range; you may adapt it to your normalization.

I understand, thanks.

One approach that should work if you have the mean and std values handy though:

mean = torch.tensor(...)
std = torch.tensor(...)
# then min and max from normalization definition
min = -mean/std
max = std.reciprocal() + min
kkew3 commented 2 months ago

I'm glad it helps! So I'll close the issue. Please, feel free to ask if you'd like to reopen.