{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# For tips on running notebooks in Google Colab, see\n", "# https://pytorch.org/tutorials/beginner/colab\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Writing Custom Datasets, DataLoaders and Transforms\n", "===================================================\n", "\n", "**Author**: [Sasank Chilamkurthy](https://chsasank.github.io)\n", "\n", "A lot of effort in solving any machine learning problem goes into\n", "preparing the data. PyTorch provides many tools to make data loading\n", "easy and hopefully, to make your code more readable. In this tutorial,\n", "we will see how to load and preprocess/augment data from a non trivial\n", "dataset.\n", "\n", "To run this tutorial, please make sure the following packages are\n", "installed:\n", "\n", "- `scikit-image`: For image io and transforms\n", "- `pandas`: For easier csv parsing\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os\n", "import torch\n", "import pandas as pd\n", "from skimage import io, transform\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from torch.utils.data import Dataset, DataLoader\n", "from torchvision import transforms, utils\n", "\n", "# Ignore warnings\n", "import warnings\n", "warnings.filterwarnings(\"ignore\")\n", "\n", "plt.ion() # interactive mode" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The dataset we are going to deal with is that of facial pose. This means\n", "that a face is annotated like this:\n", "\n", "![](https://pytorch.org/tutorials/_static/img/landmarked_face2.png){width=\"400px\"}\n", "\n", "Over all, 68 different landmark points are annotated for each face.\n", "\n", "
Download the dataset from hereso that the images are in a directory named 'data/faces/'.This dataset was actuallygenerated by applying excellent dlib's poseestimationon a few images from imagenet tagged as 'face'.
\n", "In the example above, uses an external library's random number generator(in this case, Numpy's ). This can result in unexpected behavior with (see here).In practice, it is safer to stick to PyTorch's random number generator, e.g. by using instead.
\n", "