Chapter 27
03. PyTorch Computer Vision Exercises
NotebookPython 330 cells
03. PyTorch Computer Vision Exercises
The following is a collection of exercises based on computer vision fundamentals in PyTorch.
They're a bunch of fun.
You're going to get to write plenty of code!
Resources
- These exercises are based on notebook 03 of the Learn PyTorch for Deep Learning course.
- See a live walkthrough of the solutions (errors and all) on YouTube.
- Note: Going through these exercises took me just over 3 hours of solid coding, so you should expect around the same.
In [ ]python · cell 3
python
# Check for GPU
!nvidia-smiOutput
Sat Apr 16 03:23:02 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 460.32.03 Driver Version: 460.32.03 CUDA Version: 11.2 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Tesla P100-PCIE... Off | 00000000:00:04.0 Off | 0 |
| N/A 39C P0 29W / 250W | 0MiB / 16280MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+
In [ ]python · cell 4
python
# Import torch
import torch
# Exercises require PyTorch > 1.10.0
print(torch.__version__)
# TODO: Setup device agnostic codeOutput
1.10.0+cu111
'cuda'
1. What are 3 areas in industry where computer vision is currently being used?
In [ ]python · cell 6
python
2. Search "what is overfitting in machine learning" and write down a sentence about what you find.
In [ ]python · cell 8
python
3. Search "ways to prevent overfitting in machine learning", write down 3 of the things you find and a sentence about each.
Note: there are lots of these, so don't worry too much about all of them, just pick 3 and start with those.
In [ ]python · cell 10
python
4. Spend 20-minutes reading and clicking through the CNN Explainer website.
- Upload your own example image using the "upload" button on the website and see what happens in each layer of a CNN as your image passes through it.
In [ ]python · cell 12
python
5. Load the torchvision.datasets.MNIST() train and test datasets.
In [ ]python · cell 14
python
6. Visualize at least 5 different samples of the MNIST training dataset.
In [ ]python · cell 16
python
7. Turn the MNIST train and test datasets into dataloaders using torch.utils.data.DataLoader, set the batch_size=32.
In [ ]python · cell 18
python
8. Recreate model_2 used in notebook 03 (the same model from the CNN Explainer website, also known as TinyVGG) capable of fitting on the MNIST dataset.
In [ ]python · cell 20
python
9. Train the model you built in exercise 8. for 5 epochs on CPU and GPU and see how long it takes on each.
In [ ]python · cell 22
python
10. Make predictions using your trained model and visualize at least 5 of them comparing the prediciton to the target label.
In [ ]python · cell 24
python
11. Plot a confusion matrix comparing your model's predictions to the truth labels.
In [ ]python · cell 26
python
12. Create a random tensor of shape [1, 3, 64, 64] and pass it through a nn.Conv2d() layer with various hyperparameter settings (these can be any settings you choose), what do you notice if the kernel_size parameter goes up and down?
In [ ]python · cell 28
python
13. Use a model similar to the trained model_2 from notebook 03 to make predictions on the test torchvision.datasets.FashionMNIST dataset.
- Then plot some predictions where the model was wrong alongside what the label of the image should've been.
- After visualing these predictions do you think it's more of a modelling error or a data error?
- As in, could the model do better or are the labels of the data too close to each other (e.g. a "Shirt" label is too close to "T-shirt/top")?
In [ ]python · cell 30
python
