Files
pytorch-deep-learning/extras/exercises/03_pytorch_computer_vision_exercises.ipynb
T

12 KiB

Open In Colab

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

  1. These exercises are based on notebook 03 of the Learn PyTorch for Deep Learning course.
  2. 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.
  1. See other solutions on the course GitHub.
In [ ]:
# Check for GPU
!nvidia-smi
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 [ ]:
# Import torch
import torch

# Exercises require PyTorch > 1.10.0
print(torch.__version__)

# TODO: Setup device agnostic code
1.10.0+cu111
'cuda'

1. What are 3 areas in industry where computer vision is currently being used?

In [ ]:

2. Search "what is overfitting in machine learning" and write down a sentence about what you find.

In [ ]:

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 [ ]:

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 [ ]:

5. Load the torchvision.datasets.MNIST() train and test datasets.

In [ ]:

6. Visualize at least 5 different samples of the MNIST training dataset.

In [ ]:

7. Turn the MNIST train and test datasets into dataloaders using torch.utils.data.DataLoader, set the batch_size=32.

In [ ]:

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 [ ]:

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 [ ]:

10. Make predictions using your trained model and visualize at least 5 of them comparing the prediciton to the target label.

In [ ]:

11. Plot a confusion matrix comparing your model's predictions to the truth labels.

In [ ]:

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 [ ]:

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 [ ]: