Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inconsistencies in fgsm_tutorial #2419

Merged
merged 14 commits into from
Jun 12, 2023
Merged

Conversation

QasimKhan5x
Copy link
Contributor

Fixes #1032

Description

  1. The architecture of the model is changed. The tutorial says that it uses the same MNIST model from pytorch/examples/mnist. However, the dimensions of the convolutional layers were different. Therefore, the exact model is copy-pasted as is.
  2. The model in the MNIST example was trained using a transform in the data loader that normalized the data using mean=0.1307 and std=0.3081. However, no normalization was being applied in this example. Thus, the same normalization is applied to the test_loader in this example.
  3. The state_dict of the MNIST was being loaded on the CPU even though use_cuda=True and device is being dynamically defined. Therefore, map_location=device instead of cpu
  4. A denormalization function is added to unnormalize the data before applying FGSM on it, since FGSM takes unnormalized input
  5. The test function is modified. After computing the gradient of the normalized data, the data is unnormalized. FGSM is then applied on unnormalized data. Finally, the perturbed data is again normalized before applying the model on it.

I also checked the code without step 5, i.e., not performing normalization and denormalization inside the test function. The observation was that the accuracy stayed the same between 98-99%, although it was supposed to fall. Applying step 5 rectified this issue.

Checklist

  • The issue that is being fixed is referred in the description (see above "Fixes #ISSUE_NUMBER")
  • Only one issue is addressed in this pull request
  • Labels from the issue that this PR is fixing are added to this pull request
  • No unnecessary issues are included into this pull request.

Model architecture was not the same as that of the one in Basic MNIST Example, so it has been changed to be the exact same
The model is trained on normalized data, so it is unfair to use unnormalized data in this example.
The MNIST model is trained with normalized data but no normalization was applied in this tutorial. Thus, a denorm function is created, which is called to denorm the data before performing FGSM. The perturbed data is again normalized before feeding it to the model.
@github-actions github-actions bot added Adversarial Training Issues relating to the adversarial example generation tutorial docathon-h1-2023 A label for the docathon in H1 2023 medium and removed cla signed labels Jun 3, 2023
@netlify
Copy link

netlify bot commented Jun 3, 2023

Deploy Preview for pytorch-tutorials-preview ready!

Name Link
🔨 Latest commit eb1338d
🔍 Latest deploy log https://app.netlify.com/sites/pytorch-tutorials-preview/deploys/648782d9aa44ac0008bd8563
😎 Deploy Preview https://deploy-preview-2419--pytorch-tutorials-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@QasimKhan5x
Copy link
Contributor Author

QasimKhan5x commented Jun 3, 2023

the CI is failing because the tutorial relies on "data/lenet_mnist_model.pth", which contains the weights of the old model. I don't have access to this file, otherwise I would change it.

@malfet
Copy link
Contributor

malfet commented Jun 5, 2023

@QasimKhan5x I can update the file, but weights referenced from the tutorial are indeed for small model:

>>> import torch
>>> for k, v in torch.load("lenet_mnist_model.pth", weights_only=True, map_location='cpu').items():
...   print(k, v.shape)
conv1.weight torch.Size([10, 1, 5, 5])
conv1.bias torch.Size([10])
conv2.weight torch.Size([20, 10, 5, 5])
conv2.bias torch.Size([20])
fc1.weight torch.Size([50, 320])
fc1.bias torch.Size([50])
fc2.weight torch.Size([10, 50])
fc2.bias torch.Size([10])

I.e. your PR should include new location to download the model in both tutorial itself as well as here:

wget -nv -N https://s3.amazonaws.com/pytorch-tutorial-assets/lenet_mnist_model.pth -P $(DATADIR)

@QasimKhan5x
Copy link
Contributor Author

@malfet I trained a model using the pytorch/examples/mnist code. Should I upload it to my own publicly accessible S3 bucket and provide its link?

@malfet
Copy link
Contributor

malfet commented Jun 8, 2023

@QasimKhan5x any public location will do, though google drive is preferable (as tutorials are most frequently run on colab, and one can connect collab to g-drive, can't they?)

@pytorch-bot
Copy link

pytorch-bot bot commented Jun 9, 2023

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/tutorials/2419

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit eb1338d:
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@QasimKhan5x
Copy link
Contributor Author

@malfet completed the changes

@svekars svekars merged commit 9a44540 into pytorch:main Jun 12, 2023
@QasimKhan5x QasimKhan5x deleted the fgsm-fix branch June 13, 2023 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Adversarial Training Issues relating to the adversarial example generation tutorial cla signed docathon-h1-2023 A label for the docathon in H1 2023 medium
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adversarial example generation by FGSM: different normalization of training vs test images?
4 participants