From c7d6e5f2599d828a6a1f2b554887feb5abcae500 Mon Sep 17 00:00:00 2001 From: Daniel Bourke Date: Mon, 14 Mar 2022 14:00:56 +1000 Subject: [PATCH] update computer vision and custom datasets notebooks --- 03_pytorch_computer_vision.ipynb | 281 +++++++++++++++++++------------ 04_pytorch_custom_datasets.ipynb | 2 +- 2 files changed, 177 insertions(+), 106 deletions(-) diff --git a/03_pytorch_computer_vision.ipynb b/03_pytorch_computer_vision.ipynb index 14b7ee1..639210b 100644 --- a/03_pytorch_computer_vision.ipynb +++ b/03_pytorch_computer_vision.ipynb @@ -4,14 +4,55 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# 03. Computer Vision with PyTorch\n", + "# (WIP) 03. PyTorch computer vision\n", "\n", - "What we're going to cover:\n", - "* Basic dataset\n", - " * PyTorch FashionMNIST - https://pytorch.org/vision/stable/datasets.html\n", - "* Custom dataset \n", - " * (made from scratch)\n", - "* Visualizing models\n", + "[Computer vision](https://en.wikipedia.org/wiki/Computer_vision) is the art of teaching a computer to see.\n", + "\n", + "For example, it could involve decided whether a photo is of a cat or a dog ([binary classification](https://developers.google.com/machine-learning/glossary#binary-classification)).\n", + "\n", + "Or whether a photo is of a cat, dog or chicken ([multi-class classification](https://developers.google.com/machine-learning/glossary#multi-class-classification)).\n", + "\n", + "Or where a car appears in a video frame ([object detection](https://en.wikipedia.org/wiki/Object_detection)).\n", + "\n", + "Or where different objects in an image can be separated ([panoptic segmentation](https://arxiv.org/abs/1801.00868)).\n", + "\n", + "## Where does computer vision get used?\n", + "\n", + "If you use a smartphone, you've already used computer vision.\n", + "\n", + "Camera and photo apps use computer vision to enhance and sort images - TK Link: https://machinelearning.apple.com/research/panoptic-segmentation.\n", + "\n", + "Modern cars use computer vision to avoid other cars and stay within lane lines.\n", + "\n", + "Manufacturers use computer vision to identify defects in various products.\n", + "\n", + "Security cameras use computer vision to detect potential intruders.\n", + "\n", + "In essence, anything that can described in a visual sense can be a potential computer vision problem.\n", + "\n", + "TK image - put above into computer vision use cases\n", + "\n", + "## What we're going to cover\n", + "\n", + "TK image: pytorch workflow + classes? (could link to this image in a previous notebook?)\n", + "\n", + "We're going to apply the PyTorch Workflow we've been learning in the past couple of sections to computer vision.\n", + "\n", + "Specifically, we're going to cover:\n", + "\n", + "| **Topic** | **Contents** |\n", + "| ----- | ----- |\n", + "| **0. Computer vision libraries in PyTorch** | PyTorch has a bunch of built-in helpful computer vision libraries, let's check them out. |\n", + "| **1. Load data** | To practice computer vision, we'll start with some images of different pieces of clothing from [FashionMNIST](https://github.com/zalandoresearch/fashion-mnist). |\n", + "| **2. Prepare data** | We've got some images, let's load them in with a [PyTorch `DataLoader`](https://pytorch.org/docs/stable/data.html) so we can use them with our training loop. |\n", + "| **3. Model 0: Building a baseline model** | Here we'll create a multi-class classification model to learn patterns in the data, we'll also choose a **loss function**, **optimizer** and build a **training loop**. | \n", + "| **4. Making predictions and evaluting model 0** | Let's make some predictions with our baseline model and evaluate them. |\n", + "| **5. Setup device agnostic code for future models** | It's best practice to write device-agnostic code, so let's set it up. |\n", + "| **6. Model 1: Adding non-linearity** | Experimenting is a large part of machine learning, let's try and improve upon our baseline model by adding non-linear layers. |\n", + "| **7. Model 2: Convolutional Neural Network (CNN)** | Time to get computer vision specific and introduce the powerful convolutional neural network architecture. |\n", + "| **8. Evaluating our best model** | Let's make some predictons on random images and evaluate our best model. |\n", + "| **9. Making a confusion matrix** | A confusion matrix is a great way to evaluate a classification model, let's see how we can make one. |\n", + "| **10. Saving and loading the best performing model** | Since we might want to use our model for later, let's save it and make sure it loads back in correctly. |\n", "\n", "Workflow:\n", "\n", @@ -27,36 +68,70 @@ ] }, { - "cell_type": "code", - "execution_count": 21, + "cell_type": "markdown", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'cuda'" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], "source": [ - "import torch\n", - "from torch import nn" + "## Where can can you get help?\n", + "\n", + "All of the materials for this course [live on GitHub](https://github.com/mrdbourke/pytorch-deep-learning).\n", + "\n", + "If you run into trouble, you can ask a question on the course [GitHub Discussions page](https://github.com/mrdbourke/pytorch-deep-learning/discussions) there too.\n", + "\n", + "And of course, there's the [PyTorch documentation](https://pytorch.org/docs/stable/index.html) and [PyTorch developer forums](https://discuss.pytorch.org/), a very helpful place for all things PyTorch. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## 0. Computer vision libraries in PyTorch\n", + "## TK - 0. Computer vision libraries in PyTorch\n", "\n", - "* `torchvision`\n", - "* `torchvision.datasets`\n", - "* `torchvision.transforms`\n", - "* Dataset/DataLoader" + "Before we get started writing code, let's talk about some PyTorch computer vision libraries you should be aware of.\n", + "\n", + "| PyTorch module | What does it do? |\n", + "| ----- | ----- |\n", + "| [`torchvision`](https://pytorch.org/vision/stable/index.html) | Contains datasets, model architectures and image transformations often used for computer vision problems. |\n", + "| [`torchvision.datasets`](https://pytorch.org/vision/stable/datasets.html) | Here you'll find many example computer vision datasets for a range of problems from image classification, object detection, image captioning, video classification and more. It also contains [a series of base classes for making custom datasets](https://pytorch.org/vision/stable/datasets.html#base-classes-for-custom-datasets). |\n", + "| [`torchvision.models`](https://pytorch.org/vision/stable/models.html) | This module contains well-performing and commonly used computer vision model architectures implemented in PyTorch, you can use these with your own problems. | \n", + "| [`torchvision.transforms`](https://pytorch.org/vision/stable/transforms.html) | Often images need to be transformed (turned into numbers/processed/augmented) before being used with a model, common image transformations are found here. | \n", + "| [`torch.utils.data.Dataset`](https://pytorch.org/docs/stable/data.html#torch.utils.data.Dataset) | Base dataset class for PyTorch, all custom dataset classes should inherit from this and override `__getitem()__` and `__len()__`. | \n", + "| [`torch.utils.data.DataLoader`](https://pytorch.org/docs/stable/data.html#module-torch.utils.data) | Creates a Python iteralbe over a dataset (created with `torch.utils.data.Dataset`). |\n", + "\n", + "> **Note:** The `torch.utils.data.Dataset` and `torch.utils.data.DataLoader` classes aren't only for computer vision in PyTorch, they are capable of dealing with many different types of data.\n", + "\n", + "Now we've covered some of the most important PyTorch computer vision libraries, let's import the relevant dependencies.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "PyTorch version: 1.10.0\n", + "torchvision version: 0.11.1\n" + ] + } + ], + "source": [ + "# Import PyTorch\n", + "import torch\n", + "from torch import nn\n", + "\n", + "# Import torchvision \n", + "import torchvision\n", + "from torchvision import datasets\n", + "from torchvision.transforms import ToTensor\n", + "\n", + "# Import matplotlib for visualization\n", + "import matplotlib.pyplot as plt\n", + "\n", + "# Check versions\n", + "# Note: your PyTorch version shouldn't be lower than 1.10.0 and torchvision version shouldn't be lower than 0.11)\n", + "print(f\"PyTorch version: {torch.__version__}\\ntorchvision version: {torchvision.__version__}\")" ] }, { @@ -65,22 +140,15 @@ "source": [ "## TK - 1. Data\n", "\n", - "TK - what do these do?\n", - "* torchvision - https://pytorch.org/vision/stable/index.html\n", - "* torchvision.datasets - https://pytorch.org/vision/stable/datasets.html\n", - "* torchvision.transforms" + "UPTOHERE: explaining what downloading images is doing, what everything going on below is doing" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 120, "metadata": {}, "outputs": [], "source": [ - "from torchvision import datasets\n", - "from torchvision.transforms import ToTensor\n", - "import matplotlib.pyplot as plt\n", - "\n", "train_data = datasets.FashionMNIST(\n", " root=\"data\", # where to download data to?\n", " train=True, # get training data\n", @@ -99,7 +167,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 95, "metadata": {}, "outputs": [ { @@ -108,7 +176,7 @@ "tensor([9, 0, 0, ..., 3, 0, 5])" ] }, - "execution_count": 6, + "execution_count": 95, "metadata": {}, "output_type": "execute_result" } @@ -120,7 +188,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 96, "metadata": {}, "outputs": [ { @@ -138,7 +206,7 @@ " 'Ankle boot']" ] }, - "execution_count": 8, + "execution_count": 96, "metadata": {}, "output_type": "execute_result" } @@ -151,7 +219,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 97, "metadata": {}, "outputs": [ { @@ -160,7 +228,7 @@ "(torch.Size([1, 28, 28]), 9)" ] }, - "execution_count": 3, + "execution_count": 97, "metadata": {}, "output_type": "execute_result" } @@ -173,7 +241,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 98, "metadata": {}, "outputs": [ { @@ -198,7 +266,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 99, "metadata": {}, "outputs": [ { @@ -222,7 +290,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 100, "metadata": {}, "outputs": [ { @@ -256,7 +324,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "TK - Can these be modelled with straight lines? Let's try." + "TK question - Can these be modelled with straight lines? Let's try." ] }, { @@ -270,19 +338,19 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "(,\n", - " ,\n", + "(,\n", + " ,\n", " 1875,\n", " 313)" ] }, - "execution_count": 14, + "execution_count": 101, "metadata": {}, "output_type": "execute_result" } @@ -307,7 +375,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 102, "metadata": {}, "outputs": [ { @@ -316,7 +384,7 @@ "(torch.Size([32, 1, 28, 28]), torch.Size([32]))" ] }, - "execution_count": 15, + "execution_count": 102, "metadata": {}, "output_type": "execute_result" } @@ -330,7 +398,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 103, "metadata": {}, "outputs": [ { @@ -380,7 +448,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 104, "metadata": {}, "outputs": [ { @@ -389,7 +457,7 @@ "(torch.Size([1, 28, 28]), torch.Size([1, 784]))" ] }, - "execution_count": 17, + "execution_count": 104, "metadata": {}, "output_type": "execute_result" } @@ -403,7 +471,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 105, "metadata": {}, "outputs": [], "source": [ @@ -423,7 +491,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 106, "metadata": {}, "outputs": [ { @@ -438,7 +506,7 @@ ")" ] }, - "execution_count": 30, + "execution_count": 106, "metadata": {}, "output_type": "execute_result" } @@ -463,7 +531,7 @@ }, { "cell_type": "code", - "execution_count": 87, + "execution_count": 107, "metadata": {}, "outputs": [ { @@ -491,7 +559,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 108, "metadata": {}, "outputs": [], "source": [ @@ -503,7 +571,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 109, "metadata": {}, "outputs": [], "source": [ @@ -526,7 +594,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 110, "metadata": {}, "outputs": [ { @@ -614,7 +682,7 @@ "Test loss: 0.46712 | Test accuracy: 83.57%\n", "\n", "\n", - "Train time on cpu: 26.490 seconds\n" + "Train time on cpu: 27.108 seconds\n" ] } ], @@ -687,7 +755,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 111, "metadata": {}, "outputs": [ { @@ -698,7 +766,7 @@ " 'model_acc': 83.55630990415335}" ] }, - "execution_count": 34, + "execution_count": 111, "metadata": {}, "output_type": "execute_result" } @@ -729,7 +797,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 112, "metadata": {}, "outputs": [ { @@ -738,7 +806,7 @@ "'FashionMNISTModelV0'" ] }, - "execution_count": 35, + "execution_count": 112, "metadata": {}, "output_type": "execute_result" } @@ -761,7 +829,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 113, "metadata": {}, "outputs": [ { @@ -770,7 +838,7 @@ "'cuda'" ] }, - "execution_count": 36, + "execution_count": 113, "metadata": {}, "output_type": "execute_result" } @@ -792,7 +860,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 114, "metadata": {}, "outputs": [], "source": [ @@ -813,7 +881,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 115, "metadata": {}, "outputs": [ { @@ -822,7 +890,7 @@ "device(type='cuda', index=0)" ] }, - "execution_count": 40, + "execution_count": 115, "metadata": {}, "output_type": "execute_result" } @@ -845,7 +913,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 116, "metadata": {}, "outputs": [], "source": [ @@ -867,7 +935,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 117, "metadata": {}, "outputs": [], "source": [ @@ -923,7 +991,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 118, "metadata": {}, "outputs": [ { @@ -1006,7 +1074,7 @@ "Test loss: 0.37307 | Test accuracy: 86.46%\n", "\n", "\n", - "Train time on cuda: 28.010 seconds\n" + "Train time on cuda: 27.904 seconds\n" ] } ], @@ -1038,7 +1106,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 119, "metadata": {}, "outputs": [ { @@ -1075,7 +1143,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1126,7 +1194,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1137,7 +1205,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1223,7 +1291,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1244,7 +1312,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1255,7 +1323,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1371,7 +1439,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1399,7 +1467,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1427,7 +1495,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1499,7 +1567,7 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1528,7 +1596,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1548,7 +1616,7 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1568,7 +1636,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1583,7 +1651,7 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1607,7 +1675,7 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1616,7 +1684,7 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1636,7 +1704,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1665,7 +1733,7 @@ }, { "cell_type": "code", - "execution_count": 74, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1711,7 +1779,7 @@ }, { "cell_type": "code", - "execution_count": 91, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1725,7 +1793,7 @@ }, { "cell_type": "code", - "execution_count": 89, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1743,7 +1811,7 @@ }, { "cell_type": "code", - "execution_count": 75, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1752,7 +1820,7 @@ }, { "cell_type": "code", - "execution_count": 77, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1768,7 +1836,7 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1799,7 +1867,7 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1821,7 +1889,7 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1846,10 +1914,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## TK - Exercises and Extra-curriculum\n", + "## TK - Exercises\n", "\n", "* TK - exercises\n", - "* TK - extra-curriculum" + "\n", + "## TK - Extra-curriculum\n", + "* Spend 10-minutes clicking thorugh the different options of the PyTorch vision library, what different modules are available? - https://pytorch.org/vision/stable/index.html \n", + "* Timm library for computer vision" ] }, { diff --git a/04_pytorch_custom_datasets.ipynb b/04_pytorch_custom_datasets.ipynb index 3da5457..2985b27 100644 --- a/04_pytorch_custom_datasets.ipynb +++ b/04_pytorch_custom_datasets.ipynb @@ -4,7 +4,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# 04. Custom Datasets with PyTorch \n", + "# (WIP) 04. Custom Datasets with PyTorch \n", "\n", "* Bring in custom dataset\n", "* Try TinyVGG on custom dataset (conv layers etc) - https://poloclub.github.io/cnn-explainer/\n",