MadryLab / robustness

A library for experimenting with, training and evaluating neural networks, with a focus on adversarial robustness.
MIT License
903 stars 181 forks source link

Non contiguous arrays? #93

Closed giuliolovisotto closed 3 years ago

giuliolovisotto commented 3 years ago

Hi, I found a problem reproducing the basic example in https://github.com/microsoft/robust-models-transfer (I downloaded resnet18_l2_eps3.ckpt).

Problem is that I get NaN values for accuracy. If I print the exception caught at Line 470 of robustness/train.py it says the following UserWarning: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.

Screenshot 2021-03-05 at 10 58 07

I investigated a bit and I can fix this by adding .contiguous() in the accuracy computation in robustness/tools/helpers.py: correct_k = correct[:k].contiguous().view(-1).float(), I found a similar issue here https://github.com/cezannec/capsule_net_pytorch/issues/4. The problem was when computing top-5 accuracy specifically (I assume it would happen with everything higher than top-1). Changing that line made everything work again.

I haven't opened a pull request because I'm not sure why this happens in my setup, I am running everything inside this docker container and on an Nvidia Titan RTX.

andrewilyas commented 3 years ago

Thanks for making an issue! Does this still happen with the latest version of the library? We became aware of this issue (it actually only happens with PyTorch 1.7) a few months ago and so we changed that line to use .reshape() instead:

https://github.com/MadryLab/robustness/blob/79d371fd799885ea5fe5553c2b749f41de1a2c4e/robustness/tools/helpers.py#L75

The updated version should be available on PyPI; let us know if the problem persists after a pip install --upgrade robustness!

giuliolovisotto commented 3 years ago

Great thanks, updating to the newest version fixed it! Don't know if you want to change the robustness version in the requirements.txt file for the other project.