Files
mrdbourke--pytorch-deep-lea…/extras/exercises/03_pytorch_computer_vision_exercises.ipynb
wehub-resource-sync 37c25cd088
make_docs / deploy (push) Waiting to run
chore: import upstream snapshot with attribution
2026-07-13 13:27:58 +08:00

407 lines
12 KiB
Plaintext

{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "03_pytorch_computer_vision_exercises.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyMUsDcN/+FAm9Pf7Ifqs6AZ",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/mrdbourke/pytorch-deep-learning/blob/main/extras/exercises/03_pytorch_computer_vision_exercises.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# 03. PyTorch Computer Vision Exercises\n",
"\n",
"The following is a collection of exercises based on computer vision fundamentals in PyTorch.\n",
"\n",
"They're a bunch of fun.\n",
"\n",
"You're going to get to write plenty of code!\n",
"\n",
"## Resources\n",
"\n",
"1. These exercises are based on [notebook 03 of the Learn PyTorch for Deep Learning course](https://www.learnpytorch.io/03_pytorch_computer_vision/). \n",
"2. See a live [walkthrough of the solutions (errors and all) on YouTube](https://youtu.be/_PibmqpEyhA). \n",
" * **Note:** Going through these exercises took me just over 3 hours of solid coding, so you should expect around the same.\n",
"3. See [other solutions on the course GitHub](https://github.com/mrdbourke/pytorch-deep-learning/tree/main/extras/solutions)."
],
"metadata": {
"id": "Vex99np2wFVt"
}
},
{
"cell_type": "code",
"source": [
"# Check for GPU\n",
"!nvidia-smi"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "GaeYzOTLwWh2",
"outputId": "17dd5453-9639-4b01-aa18-7ddbfd5c3253"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Sat Apr 16 03:23:02 2022 \n",
"+-----------------------------------------------------------------------------+\n",
"| NVIDIA-SMI 460.32.03 Driver Version: 460.32.03 CUDA Version: 11.2 |\n",
"|-------------------------------+----------------------+----------------------+\n",
"| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\n",
"| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\n",
"| | | MIG M. |\n",
"|===============================+======================+======================|\n",
"| 0 Tesla P100-PCIE... Off | 00000000:00:04.0 Off | 0 |\n",
"| N/A 39C P0 29W / 250W | 0MiB / 16280MiB | 0% Default |\n",
"| | | N/A |\n",
"+-------------------------------+----------------------+----------------------+\n",
" \n",
"+-----------------------------------------------------------------------------+\n",
"| Processes: |\n",
"| GPU GI CI PID Type Process name GPU Memory |\n",
"| ID ID Usage |\n",
"|=============================================================================|\n",
"| No running processes found |\n",
"+-----------------------------------------------------------------------------+\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Import torch\n",
"import torch\n",
"\n",
"# Exercises require PyTorch > 1.10.0\n",
"print(torch.__version__)\n",
"\n",
"# TODO: Setup device agnostic code\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 53
},
"id": "DNwZLMbCzJLk",
"outputId": "9c150c50-a092-4f34-9d33-b45247fb080d"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1.10.0+cu111\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'cuda'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 2
}
]
},
{
"cell_type": "markdown",
"source": [
"## 1. What are 3 areas in industry where computer vision is currently being used?"
],
"metadata": {
"id": "FSFX7tc1w-en"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "VyWRkvWGbCXj"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 2. Search \"what is overfitting in machine learning\" and write down a sentence about what you find. "
],
"metadata": {
"id": "oBK-WI6YxDYa"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "d1rxD6GObCqh"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 3. Search \"ways to prevent overfitting in machine learning\", write down 3 of the things you find and a sentence about each. \n",
"> **Note:** there are lots of these, so don't worry too much about all of them, just pick 3 and start with those."
],
"metadata": {
"id": "XeYFEqw8xK26"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "ocvOdWKcbEKr"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 4. Spend 20-minutes reading and clicking through the [CNN Explainer website](https://poloclub.github.io/cnn-explainer/).\n",
"\n",
"* 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."
],
"metadata": {
"id": "DKdEEFEqxM-8"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "TqZaJIRMbFtS"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 5. Load the [`torchvision.datasets.MNIST()`](https://pytorch.org/vision/stable/generated/torchvision.datasets.MNIST.html#torchvision.datasets.MNIST) train and test datasets."
],
"metadata": {
"id": "lvf-3pODxXYI"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "SHjeuN81bHza"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 6. Visualize at least 5 different samples of the MNIST training dataset."
],
"metadata": {
"id": "qxZW-uAbxe_F"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "QVFsYi1PbItE"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 7. Turn the MNIST train and test datasets into dataloaders using `torch.utils.data.DataLoader`, set the `batch_size=32`."
],
"metadata": {
"id": "JAPDzW0wxhi3"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "ALA6MPcFbJXQ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 8. Recreate `model_2` used in notebook 03 (the same model from the [CNN Explainer website](https://poloclub.github.io/cnn-explainer/), also known as TinyVGG) capable of fitting on the MNIST dataset."
],
"metadata": {
"id": "bCCVfXk5xjYS"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "5IKNF22XbKYS"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 9. Train the model you built in exercise 8. for 5 epochs on CPU and GPU and see how long it takes on each."
],
"metadata": {
"id": "sf_3zUr7xlhy"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "jSo6vVWFbNLD"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 10. Make predictions using your trained model and visualize at least 5 of them comparing the prediciton to the target label."
],
"metadata": {
"id": "w1CsHhPpxp1w"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "_YGgZvSobNxu"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 11. Plot a confusion matrix comparing your model's predictions to the truth labels."
],
"metadata": {
"id": "qQwzqlBWxrpG"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "vSrXiT_AbQ6e"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 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?"
],
"metadata": {
"id": "lj6bDhoWxt2y"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "leCTsqtSbR5P"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## 13. Use a model similar to the trained `model_2` from notebook 03 to make predictions on the test [`torchvision.datasets.FashionMNIST`](https://pytorch.org/vision/main/generated/torchvision.datasets.FashionMNIST.html) dataset. \n",
"* Then plot some predictions where the model was wrong alongside what the label of the image should've been. \n",
"* After visualing these predictions do you think it's more of a modelling error or a data error? \n",
"* 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\")?"
],
"metadata": {
"id": "VHS20cNTxwSi"
}
},
{
"cell_type": "code",
"source": [
""
],
"metadata": {
"id": "78a8LjtdbSZj"
},
"execution_count": null,
"outputs": []
}
]
}