{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Deep Learning Models -- A collection of various deep learning architectures, models, and tips for TensorFlow and PyTorch in Jupyter Notebooks.\n", "- Author: Sebastian Raschka\n", "- GitHub Repository: https://github.com/rasbt/deeplearning-models" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sebastian Raschka \n", "\n", "CPython 3.7.1\n", "IPython 7.2.0\n", "\n", "torch 1.0.0\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -a 'Sebastian Raschka' -v -p torch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Model Zoo -- Using PyTorch Dataset Loading Utilities for Custom Datasets (Images from Quickdraw)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook provides an example for how to load an image dataset, stored as individual PNG files, using PyTorch's data loading utilities. For a more in-depth discussion, please see the official\n", "\n", "- [Data Loading and Processing Tutorial](http://pytorch.org/tutorials/beginner/data_loading_tutorial.html)\n", "- [torch.utils.data](http://pytorch.org/docs/master/data.html) API documentation\n", "\n", "In this example, we are using the Quickdraw dataset consisting of handdrawn objects, which is available at https://quickdraw.withgoogle.com. \n", "\n", "To execute the following examples, you need to download the \".npy\" (bitmap files in NumPy). You don't need to download all of the 345 categories but only a subset you are interested in. The groups/subsets can be individually downloaded from https://console.cloud.google.com/storage/browser/quickdraw_dataset/full/numpy_bitmap\n", "\n", "Unfortunately, the Google cloud storage currently does not support selecting and downloading multiple groups at once. Thus, in order to download all groups most coneniently, we need to use their `gsutil` (https://cloud.google.com/storage/docs/gsutil_install) tool. If you want to install that, you can then use \n", "\n", " mkdir quickdraw-npy\n", " gsutil -m cp gs://quickdraw_dataset/full/numpy_bitmap/*.npy quickdraw-npy\n", "\n", "Note that if you download the whole dataset, this will take up 37 Gb of storage space.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import os\n", "\n", "import torch\n", "from torch.utils.data import Dataset\n", "from torch.utils.data import DataLoader\n", "from torchvision import transforms\n", "\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "from PIL import Image" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dataset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After downloading the dataset to a local directory, `quickdraw-npy`, the next step is to select certain groups we are interested in analyzing. Let's say we are interested in the following groups defined in the `label_dict` in the next code cell:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "label_dict = {\n", " \"lollipop\": 0,\n", " \"binoculars\": 1,\n", " \"mouse\": 2,\n", " \"basket\": 3,\n", " \"penguin\": 4,\n", " \"washing machine\": 5,\n", " \"canoe\": 6,\n", " \"eyeglasses\": 7,\n", " \"beach\": 8,\n", " \"screwdriver\": 9,\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The dictionary values shall represent class labels that we could use for a classification task, for example." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Conversion to PNG files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next we are going to convert the groups we are interested in (specified in the dictionary above) to individual PNG files using a helper function (note that this might take a while):" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# load utilities from ../helper.py\n", "import sys\n", "sys.path.insert(0, '..') \n", "from helper import quickdraw_npy_to_imagefile\n", "\n", " \n", "quickdraw_npy_to_imagefile(inpath='quickdraw-npy',\n", " outpath='quickdraw-png_set1',\n", " subset=label_dict.keys())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Preprocessing into train/valid/test subsets and creating a label files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For convenience, let's create a CSV file mapping file names to class labels. First, let's collect the files and labels." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Num paths: 1515745\n", "Num labels: 1515745\n" ] } ], "source": [ "paths, labels = [], []\n", "\n", "main_dir = 'quickdraw-png_set1/'\n", "\n", "for d in os.listdir(main_dir):\n", " subdir = os.path.join(main_dir, d)\n", " if not os.path.isdir(subdir):\n", " continue\n", " for f in os.listdir(subdir):\n", " path = os.path.join(d, f)\n", " paths.append(path)\n", " labels.append(label_dict[d])\n", " \n", "print('Num paths:', len(paths))\n", "print('Num labels:', len(labels))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we shuffle the dataset and assign 70% of the dataset for training, 10% for validation, and 20% for testing." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from mlxtend.preprocessing import shuffle_arrays_unison\n", "\n", "\n", "paths2, labels2 = shuffle_arrays_unison(arrays=[np.array(paths), np.array(labels)], random_seed=3)\n", "\n", "\n", "cut1 = int(len(paths)*0.7)\n", "cut2 = int(len(paths)*0.8)\n", "\n", "paths_train, labels_train = paths2[:cut1], labels2[:cut1]\n", "paths_valid, labels_valid = paths2[cut1:cut2], labels2[cut1:cut2]\n", "paths_test, labels_test = paths2[cut2:], labels2[cut2:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, let us create a CSV file that maps the file paths to the class labels (here only shown for the training set for simplicity):" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | Label | \n", "
|---|---|
| Path | \n", "\n", " |
| penguin/penguin_182463.png | \n", "4 | \n", "
| mouse/mouse_139942.png | \n", "2 | \n", "
| screwdriver/screwdriver_066105.png | \n", "9 | \n", "
| beach/beach_026711.png | \n", "8 | \n", "
| eyeglasses/eyeglasses_035833.png | \n", "7 | \n", "