{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "checksum": "ad4e1f1c44a179ed18fcbc9e14165c26", "grade": false, "grade_id": "cell-9216a80c9a082b4f", "locked": true, "schema_version": 1, "solution": false } }, "source": [ "# Part 1: Introducing MNIST" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "checksum": "d811acddee838e5db787311da31e0b20", "grade": false, "grade_id": "cell-da3c0480859542a7", "locked": true, "schema_version": 1, "solution": false } }, "outputs": [], "source": [ "# Execute this code block to install dependencies when running on colab\n", "try:\n", " import torch\n", "except:\n", " from os.path import exists\n", " from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag\n", " platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())\n", " cuda_output = !ldconfig -p|grep cudart.so|sed -e 's/.*\\.\\([0-9]*\\)\\.\\([0-9]*\\)$/cu\\1\\2/'\n", " accelerator = cuda_output[0] if exists('/dev/nvidia0') else 'cpu'\n", "\n", " !pip install -q http://download.pytorch.org/whl/{accelerator}/torch-1.0.0-{platform}-linux_x86_64.whl torchvision\n", "\n", "try: \n", " import torchbearer\n", "except:\n", " !pip install torchbearer" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "checksum": "c7a46ba0ef59b39a9ed72a11f852d87c", "grade": false, "grade_id": "cell-df416ad322f1ef99", "locked": true, "schema_version": 1, "solution": false } }, "source": [ "## The MNIST Dataset\n", "\n", "MNIST is a dataset developed by Yann LeCun, Corinna Cortes and Christopher Burges for evaluating machine learning models on the handwritten digit classification problem.\n", "\n", "The dataset was constructed from a number of scanned document dataset available from the National Institute of Standards and Technology (NIST). This is where the name for the dataset comes from, as the Modified NIST or MNIST dataset.\n", "\n", "Images of digits were taken from a variety of scanned documents, normalized in size and centred. This makes it an excellent dataset for evaluating models, allowing the developer to focus on the machine learning with very little data cleaning or preparation required.\n", "\n", "Each image is a 28 by 28 pixel square (784 pixels total). A standard spit of the dataset is used to evaluate and compare models, where 60,000 images are used to train a model and a separate set of 10,000 images are used to test it.\n", "\n", "It is a digit recognition task. As such there are 10 digits (0 to 9) or 10 classes to predict. Results are reported using prediction error, which is nothing more than the inverted classification accuracy.\n", "\n", "Excellent results achieve a prediction error of less than 1%. State-of-the-art prediction error of approximately 0.2% can be achieved with large Convolutional Neural Networks. There is a listing of the state-of-the-art results and links to the relevant papers on the MNIST and other datasets on [Rodrigo Benenson’s webpage](http://rodrigob.github.io/are_we_there_yet/build/classification_datasets_results.html#4d4e495354)." ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "checksum": "36f9b9e50cb8bcfebb039e4dd4dbe084", "grade": false, "grade_id": "cell-c3d9156722937c07", "locked": true, "schema_version": 1, "solution": false } }, "source": [ "## Loading the MNIST dataset in PyTorch and torchvision\n", "\n", "The `torchvision` library provides a convenience method for loading the MNIST dataset. The dataset can be downloaded automatically the first time this function is called and is stored in a location of your choice. This is very handy for developing and testing deep learning models.\n", "\n", "To demonstrate how easy it is to load the MNIST dataset, we will first write a little script to download and visualize the first 4 images in the training dataset. __Read through the following code and then go-ahead and run it__." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "\n", "# Plot ad hoc mnist instances\n", "from torchvision.datasets import MNIST\n", "import matplotlib.pyplot as plt\n", "\n", "# load (download if needed) the MNIST dataset\n", "mnist_train = MNIST(\".\", train=True, download=True)\n", "\n", "# plot 4 images as gray scale\n", "plt.subplot(221)\n", "plt.imshow(mnist_train.train_data[0], cmap=plt.get_cmap('gray'))\n", "plt.subplot(222)\n", "plt.imshow(mnist_train.train_data[1], cmap=plt.get_cmap('gray'))\n", "plt.subplot(223)\n", "plt.imshow(mnist_train.train_data[2], cmap=plt.get_cmap('gray'))\n", "plt.subplot(224)\n", "plt.imshow(mnist_train.train_data[3], cmap=plt.get_cmap('gray'))\n", "\n", "# show the plot\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "checksum": "4d160453bd547c294f7136ddba25259d", "grade": false, "grade_id": "cell-3c1932790ecce2db", "locked": true, "schema_version": 1, "solution": false } }, "source": [ "You can see that downloading and loading the MNIST dataset is as easy as constructing an `MNIST` object. The `MNIST` object is a subclass of a PyTorch [`torch.utils.data.Dataset`](https://pytorch.org/docs/stable/data.html#torch.utils.data.Dataset) class, which as we'll see later can be used with a `DataLoader` object to provide streaming access to batches of data. The `MNIST` object loads all of the data in memory because it is so small; other datasets will load the data from disk (or elsewhere) on demand." ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "checksum": "1a0a5bf9735130040fc740154b3ab78e", "grade": false, "grade_id": "cell-6faff6def2827f4d", "locked": true, "schema_version": 1, "solution": false } }, "source": [ "__Use the following code block to explore the `mnist_train` object, then answer the questions that follow.__" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "checksum": "fc2246e8867cee803c56444cc6d44eef", "grade": false, "grade_id": "cell-92a830c31d4063d9", "locked": true, "schema_version": 1, "solution": false } }, "source": [ "__Answer the following questions (enter the answer in the box below each one):__\n", "\n", "__1.__ What type of object is the `mnist_train.train_data`?" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "nbgrader": { "checksum": "52b518fc5a8e2f38349f85dc9a586239", "grade": true, "grade_id": "cell-a1725ed43000ce03", "locked": false, "points": 1, "schema_version": 1, "solution": true } }, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "checksum": "6d16c4d2abd1b5b2e5a3eb4a05180306", "grade": false, "grade_id": "cell-22e7dce8272310a9", "locked": true, "schema_version": 1, "solution": false } }, "source": [ "__2.__ What is the shape of `mnist_train.train_data`?`" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "nbgrader": { "checksum": "7faeaac78ea22cdaeac895d4be526656", "grade": true, "grade_id": "cell-3380a45f5755116b", "locked": false, "points": 1, "schema_version": 1, "solution": true } }, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "checksum": "9ef2b7a5aa6847ced7f9d54728ecc3bc", "grade": false, "grade_id": "cell-1d34af95c1204db6", "locked": true, "schema_version": 1, "solution": false } }, "source": [ "__3.__ How is the label of each image stored in `mnist_train`?" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "nbgrader": { "checksum": "d6c900ecc941263d8b085929ece5194b", "grade": true, "grade_id": "cell-7df3baf74617c00c", "locked": false, "points": 2, "schema_version": 1, "solution": true } }, "source": [ "YOUR ANSWER HERE" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }