add images and cleanup TK's

This commit is contained in:
Daniel Bourke committed 2022-04-13 14:30:52 +10:00
1 parent 0ae3e0392c
commit cbe28e2c34
1 file changed
+83 -26
+83 -26
View File
@@ -6,18 +6,74 @@
"source": [
"# (WIP) 04. PyTorch Custom Datasets\n",
"\n",
"* Bring in custom dataset\n",
"* Try TinyVGG on custom dataset (conv layers etc) - https://poloclub.github.io/cnn-explainer/\n",
"In the last notebook, [notebook 03](https://www.learnpytorch.io/03_pytorch_computer_vision/), we looked at how to build computer vision models on an in-built dataset in PyTorch (FashionMNIST).\n",
"\n",
"## TK - What is a custom dataset?\n",
"The steps we took are similar across many different problems in machine learning.\n",
"\n",
"TK - what can a custom dataset\n",
"Find a dataset, turn the dataset into numbers, build a model (or find and existing model) to find patterns in those numbers that can be used for prediction.\n",
"\n",
"TK - PyTorch modules for custom data\n",
"PyTorch has many built-in datasets used for a wide number of machine learning benchmarks, however, you'll often want to use your own **custom dataset**."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## What is a custom dataset?\n",
"\n",
"## TK - What we're going to cover\n",
"A **custom dataset** is a collection of data relating to a specific problem you're working on.\n",
"\n",
"TK - add table of what we're going to cover"
"In essence, a **custom dataset** can be comprised of almost anything.\n",
"\n",
"For example, if we were building a food image classification app like [Nutrify](https://nutrify.app), our custom dataset might be images of food.\n",
"\n",
"Or if we were trying to build a model to classify whether or not a text-based review on a website was positive or negative, our custom dataset might be examples of existing customer reviews and their ratings.\n",
"\n",
"Or if we were trying to build a sound classification app, our custom dataset might be sound samples alongside their sample labels.\n",
"\n",
"Or if we were trying to build a recommendation system for customers purchasing things on our website, our custom dataset might be examples of products other people have bought.\n",
"\n",
"<img src=\"https://raw.githubusercontent.com/mrdbourke/pytorch-deep-learning/main/images/04-pytorch-domain-libraries.png\" alt=\"different pytorch domain libraries can be used for specific PyTorch problems\" width=1000/>\n",
"\n",
"*PyTorch includes many existing functions to load in various custom datasets in the [`TorchVision`](https://pytorch.org/vision/stable/index.html), [`TorchText`](https://pytorch.org/text/stable/index.html), [`TorchAudio`](https://pytorch.org/audio/stable/index.html) and [`TorchRec`](https://pytorch.org/torchrec/) domain libraries.*\n",
"\n",
"But sometimes these existing functions may not be enough.\n",
"\n",
"In that case, we can always subclass `torch.utils.data.Dataset` and customize it to our liking."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## What we're going to cover\n",
"\n",
"We're going to be applying the PyTorch Workflow we covered in [notebook 01](https://www.learnpytorch.io/01_pytorch_workflow/) and [notebook 02](https://www.learnpytorch.io/02_pytorch_classification/) to a computer vision problem.\n",
"\n",
"But instead of using an in-built PyTorch dataset, we're going to be using our own dataset of pizza, steak and sushi images.\n",
"\n",
"The goal will be to load these images and then build a model to train and predict on them.\n",
"\n",
"<img src=\"https://raw.githubusercontent.com/mrdbourke/pytorch-deep-learning/main/images/04-pytorch-food-vision-layout.png\" alt=\"building a pipeline to load in food images and then building a pytorch model to classify those food images\" width=800 />\n",
"\n",
"*What we're going to build. We'll use `torchvision.datasets` as well as our own custom `Dataset` class to load in images of food and then we'll build a PyTorch computer vision model to hopefully be able to classify them.*\n",
"\n",
"Specifically, we're going to cover:\n",
"\n",
"| **Topic** | **Contents** |\n",
"| ----- | ----- |\n",
"| **0. Importing PyTorch and setting up device-agnostic code** | Let's get PyTorch loaded and then follow best practice to setup our code to be device-agnostic. |\n",
"| **1. Get data** | We're going to be using our own **custom dataset** of pizza, steak and sushi images. |\n",
"| **2. Become one with the data (data preparation)** | At the beginning of any new machine learning problem, it's paramount to understand the data you're working with. Here we'll take some steps to figure out what data we have. |\n",
"| **3. Transforming data** |Often, the data you get won't be 100% ready to use with a machine learning model, here we'll look at some steps we can take to *transform* our images so they're ready to be used with a model. | \n",
"| **4. Loading data with `ImageFolder` (option 1)** | PyTorch has many in-built data loading functions for common types of data. `ImageFolder` is helpful if our images are in standard image classification format. |\n",
"| **5. Loading image data with a custom `Dataset`** | What if PyTorch didn't have an in-built function to load data with? This is where we can build our own custom subclass of `torch.utils.data.Dataset`. |\n",
"| **6. Other forms of transforms (data augmentation)** | Data augmentation is a common technique for expanding the diversity of your training data. Here we'll explore some of `torchvision`'s in-built data augmentation functions. |\n",
"| **7. Model 0: TinyVGG without data augmentation** | By this stage, we'll have our data ready, let's build a model capable of fitting it. We'll also create some training and testing functions for training and evaluating our model. |\n",
"| **8. Exploring loss curves** | Loss curves are a great way to see how your model is training/improving over time. They're also a good way to see if your model is **underfitting** or **overfitting**. |\n",
"| **9. Model 1: TinyVGG with data augmentation** | By now, we've tried a model *without*, how about we try one *with* data augmentation? |\n",
"| **10. Compare model results** | Let's compare our different model's loss curves and see which performed better and discuss some options for improving performance. |\n",
"| **11. Making a prediction on a custom image** | Our model is trained to on a dataset of pizza, steak and sushi images. In this section we'll cover how to use our trained model to predict on an image *outside* of our existing dataset. |"
]
},
{
@@ -99,7 +155,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## TK - 1. Get data\n",
"## 1. Get data\n",
"\n",
"First thing's first we need some data.\n",
"\n",
@@ -187,7 +243,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## TK - 2. Become one with the data\n",
"## 2. Become one with the data (data preparation)\n",
"\n",
"Dataset downloaded!\n",
"\n",
@@ -197,7 +253,7 @@
"\n",
"As Abraham Lossfunction said...\n",
"\n",
"![tweet by mrdbourke, if I had eight hours to build a machine learning model, I'd spend the first 6 hours preparing my dataset](https://raw.githubusercontent.com/mrdbourke/pytorch-deep-learning/main/images/04-abraham-lossfunction.png)\n",
"<img src=\"https://raw.githubusercontent.com/mrdbourke/pytorch-deep-learning/main/images/04-abraham-lossfunction.png\" alt=\"tweet by mrdbourke, if I had eight hours to build a machine learning model, I'd spend the first 6 hours preparing my dataset\" width=800/>\n",
"\n",
"*Data preparation is paramount. Before building a model, become one with the data. Ask: What am I trying to do here? Source: [@mrdbourke Twitter](https://twitter.com/mrdbourke).*\n",
"\n",
@@ -346,7 +402,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### TK - 2.1 Visualize an image\n",
"### 2.1 Visualize an image\n",
"\n",
"Okay, we've seen how our directory structure is formatted.\n",
"\n",
@@ -457,7 +513,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## TK - 3. Transforming data \n",
"## 3. Transforming data \n",
"\n",
"Now what if we wanted to load our image data into PyTorch?\n",
"\n",
@@ -639,7 +695,7 @@
"tags": []
},
"source": [
"## TK - 4. Option 1: Loading Image Data Using [`ImageFolder`](https://pytorch.org/vision/stable/generated/torchvision.datasets.ImageFolder.html#torchvision.datasets.ImageFolder)\n",
"## 4. Option 1: Loading Image Data Using [`ImageFolder`](https://pytorch.org/vision/stable/generated/torchvision.datasets.ImageFolder.html#torchvision.datasets.ImageFolder)\n",
"\n",
"Alright, time to turn our image data into a `Dataset` capable of being used with PyTorch.\n",
"\n",
@@ -684,6 +740,7 @@
],
"source": [
"# Use ImageFolder to create dataset(s)\n",
"from torchvision import datasets\n",
"train_data = datasets.ImageFolder(root=train_dir, # target folder of images\n",
" transform=data_transform, # transforms to perform on data (images)\n",
" target_transform=None) # transforms to perform on labels (if necessary)\n",
@@ -901,7 +958,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### TK - 4.1 Turn loaded images into `DataLoader`'s\n",
"### 4.1 Turn loaded images into `DataLoader`'s\n",
"\n",
"We've got our images as PyTorch `Dataset`'s but now let's turn them into `DataLoader`'s.\n",
"\n",
@@ -1005,7 +1062,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## TK - 5. Option 2: Loading Image Data with a Custom `Dataset`\n",
"## 5. Option 2: Loading Image Data with a Custom `Dataset`\n",
"\n",
"What if a pre-built `Dataset` creator like [`torchvision.datasets.ImageFolder()`](https://pytorch.org/vision/stable/datasets.html#torchvision.datasets.ImageFolder) didn't exist?\n",
"\n",
@@ -1445,7 +1502,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### TK - 5.1 Create a function to display random images\n",
"### 5.1 Create a function to display random images\n",
"\n",
"You know what time it is!\n",
"\n",
@@ -1598,7 +1655,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### TK - 5.2 Turn custom loaded images into `DataLoader`'s \n",
"### 5.2 Turn custom loaded images into `DataLoader`'s \n",
"\n",
"We've got a way to turn our raw images into `Dataset`'s (features mapped to labels or `X`'s mapped to `y`'s) through our `ImageFolderCustom` class.\n",
"\n",
@@ -1687,7 +1744,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## TK 6. Other forms of transforms (data augmentation)\n",
"## 6. Other forms of transforms (data augmentation)\n",
"\n",
"We've seen a couple of transforms on our data already but there's plenty more.\n",
"\n",
@@ -1829,7 +1886,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## TK - 7. Model 0: TinyVGG without data augmentation\n",
"## 7. Model 0: TinyVGG without data augmentation\n",
"\n",
"Alright, we've seen how to turn our data from images in folders to transformed tensors.\n",
"\n",
@@ -2217,7 +2274,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### TK - 7.4 Create train & test loop functions \n",
"### 7.4 Create train & test loop functions \n",
"\n",
"We've got data and we've got a model.\n",
"\n",
@@ -2415,7 +2472,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### TK - 7.5 Train and Evaluate Model 0\n",
"### 7.5 Train and Evaluate Model 0\n",
"\n",
"Alright, alright, alright we've got all of the ingredients we need to train and evaluate our model.\n",
"\n",
@@ -2515,7 +2572,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### TK - 7.6 Plot the loss curves of Model 0\n",
"### 7.6 Plot the loss curves of Model 0\n",
"\n",
"From the print outs of our `model_0` training, it didn't look like it did too well.\n",
"\n",
@@ -2650,7 +2707,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## TK - 8. What should an ideal loss curve look like?\n",
"## 8. What should an ideal loss curve look like?\n",
"\n",
"Looking at training and test loss curves is a great way to see if your model is **overfitting**.\n",
"\n",
@@ -2748,7 +2805,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## TK - 9. Model 1: TinyVGG with Data Augmentation\n",
"## 9. Model 1: TinyVGG with Data Augmentation\n",
"\n",
"Time to try out another model!\n",
"\n",
@@ -3076,7 +3133,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## TK - 10. Compare model results\n",
"## 10. Compare model results\n",
"\n",
"Even though our models our performing quite poorly, we can still write code to compare them.\n",
"\n",
@@ -3256,7 +3313,7 @@
"tags": []
},
"source": [
"## TK - 11. Make a prediction on a custom image\n",
"## 11. Make a prediction on a custom image\n",
"\n",
"If you've trained a model on a certain dataset, chances are you'd like to make a prediction on on your own custom data.\n",
"\n",