{"id":1982,"date":"2019-04-07T19:00:27","date_gmt":"2019-04-07T19:00:27","guid":{"rendered":"https:\/\/www.aiproblog.com\/index.php\/2019\/04\/07\/how-to-load-and-visualize-standard-computer-vision-datasets-with-keras\/"},"modified":"2019-04-07T19:00:27","modified_gmt":"2019-04-07T19:00:27","slug":"how-to-load-and-visualize-standard-computer-vision-datasets-with-keras","status":"publish","type":"post","link":"https:\/\/www.aiproblog.com\/index.php\/2019\/04\/07\/how-to-load-and-visualize-standard-computer-vision-datasets-with-keras\/","title":{"rendered":"How to Load and Visualize Standard Computer Vision Datasets With Keras"},"content":{"rendered":"<p>Author: Jason Brownlee<\/p>\n<div>\n<p>It can be convenient to use a standard computer vision dataset when getting started with deep learning methods for computer vision.<\/p>\n<p>Standard datasets are often well understood, small, and easy to load. They can provide the basis for testing techniques and reproducing results in order to build confidence with libraries and methods.<\/p>\n<p>In this tutorial, you will discover the standard computer vision datasets provided with the Keras deep learning library.<\/p>\n<p>After completing this tutorial, you will know:<\/p>\n<ul>\n<li>The API and idioms for downloading standard computer vision datasets using Keras.<\/li>\n<li>The structure, nature, and top results for the MNIST, Fashion-MNIST, CIFAR-10, and CIFAR-100 computer vision datasets.<\/li>\n<li>How to load and visualize standard computer vision datasets using the Keras API.<\/li>\n<\/ul>\n<p>Let\u2019s get started.<\/p>\n<div id=\"attachment_7413\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7413\" class=\"size-full wp-image-7413\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2019\/04\/How-to-Load-and-Visualize-Standard-Computer-Vision-Datasets-With-Keras.jpg\" alt=\"How to Load and Visualize Standard Computer Vision Datasets With Keras\" width=\"640\" height=\"426\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/04\/How-to-Load-and-Visualize-Standard-Computer-Vision-Datasets-With-Keras.jpg 640w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/04\/How-to-Load-and-Visualize-Standard-Computer-Vision-Datasets-With-Keras-300x200.jpg 300w\" sizes=\"(max-width: 640px) 100vw, 640px\"><\/p>\n<p id=\"caption-attachment-7413\" class=\"wp-caption-text\">How to Load and Visualize Standard Computer Vision Datasets With Keras<br \/>Photo by <a href=\"https:\/\/www.flickr.com\/photos\/marinadelcastell\/14032345481\/\">Marina del Castell<\/a>, some rights reserved.<\/p>\n<\/div>\n<h2>Tutorial Overview<\/h2>\n<p>This tutorial is divided into five parts; they are:<\/p>\n<ol>\n<li>Keras Computer Vision Datasets<\/li>\n<li>MNIST Dataset<\/li>\n<li>Fashion-MNIST Dataset<\/li>\n<li>CIFAR-10 Dataset<\/li>\n<li>CIFAR-100 Dataset<\/li>\n<\/ol>\n<h2>Keras Computer Vision Datasets<\/h2>\n<p>The Keras deep learning library provides access to four standard computer vision datasets.<\/p>\n<p>This is particularly helpful as it allows you to rapidly start testing model architectures and configurations for computer vision.<\/p>\n<p>Four specific multi-class image classification dataset are provided; they are:<\/p>\n<ul>\n<li><strong>MNIST<\/strong>: Classify photos of handwritten digits (10 classes).<\/li>\n<li><strong>Fashion-MNIST<\/strong>: Classify photos of items of clothing (10 classes).<\/li>\n<li><strong>CIFAR-10<\/strong>: Classify small photos of objects (10 classes).<\/li>\n<li><strong>CIFAR-100<\/strong>: Classify small photos of common objects (100 classes).<\/li>\n<\/ul>\n<p>The datasets are available under the <em>keras.datasets<\/em> module via dataset-specific load functions.<\/p>\n<p>After a call to the load function, the dataset is downloaded to your workstation and stored in the <em>~\/.keras<\/em> directory under a \u201c<em>datasets<\/em>\u201d subdirectory. The datasets are stored in a compressed format, but may also include additional metadata.<\/p>\n<p>After the first call to a dataset-specific load function and the dataset is downloaded, the dataset does not need to be downloaded again. Subsequent calls will load the dataset immediately from disk.<\/p>\n<p>The load functions return two tuples, the first containing the input and output elements for samples in the training dataset, and the second containing the input and output elements for samples in the test dataset. The splits between train and test datasets often follow a standard split, used when benchmarking algorithms on the dataset.<\/p>\n<p>The standard idiom for loading the datasets is as follows:<\/p>\n<pre class=\"crayon-plain-tag\">...\r\n# load dataset\r\n(trainX, trainy), (testX, testy) = load_data()<\/pre>\n<p>Each of the train and test <em>X<\/em> and <em>y<\/em> elements are NumPy arrays of pixel or class values respectively.<\/p>\n<p>Two of the datasets contain grayscale images and two contain color images. The shape of the grayscale images must be converted from two-dimensional to three-dimensional arrays to match the preferred channel ordering of Keras. For example:<\/p>\n<pre class=\"crayon-plain-tag\"># reshape grayscale images to have a single channel\r\nwidth, height, channels = trainX.shape[1], trainX.shape[2], 1\r\ntrainX = trainX.reshape((trainX.shape[0], width, height, channels))\r\ntestX = testX.reshape((testX.shape[0], width, height, channels))<\/pre>\n<p>Both grayscale and color image pixel data are stored as unsigned integer values with values between 0 and 255.<\/p>\n<p>Before modeling, the image data will need to be rescaled, e.g. such as normalization to the range 0-1 and perhaps further standardized. For example:<\/p>\n<pre class=\"crayon-plain-tag\"># normalize pixel values\r\ntrainX = trainX.astype('float32') \/ 255\r\ntestX = testX.astype('float32') \/ 255<\/pre>\n<p>The output elements of each sample (<em>y<\/em>) are stored as class integer values. Each problem is a multi-class classification problem (more than two classes); as such, it is common practice to one hot encode the class values prior to modeling. This can be achieved using the <em>to_categorical()<\/em> function provided by Keras; for example:<\/p>\n<pre class=\"crayon-plain-tag\">...\r\n# one hot encode target values\r\ntrainy = to_categorical(trainy)\r\ntesty = to_categorical(testy)<\/pre>\n<p>Now that we are familiar with the idioms for working with the standard computer vision datasets provided by Keras, let\u2019s take a closer look at each dataset in turn.<\/p>\n<p>Note, the examples in this tutorial assume that you have internet access and may download the datasets the first time each example is run on your system. The download speed will depend on the speed of your internet connection and you are recommended to run the examples from the command line.<\/p>\n<div class=\"woo-sc-hr\"><\/div>\n<p><center><\/p>\n<h3>Want Results with Deep Learning for Computer Vision?<\/h3>\n<p>Take my free 7-day email crash course now (with sample code).<\/p>\n<p>Click to sign-up and also get a free PDF Ebook version of the course.<\/p>\n<p><a href=\"https:\/\/machinelearningmastery.lpages.co\/leadbox\/1458ca1e0972a2%3A164f8be4f346dc\/4715926590455808\/\" target=\"_blank\" style=\"background: rgb(255, 206, 10); color: rgb(255, 255, 255); text-decoration: none; font-family: Helvetica, Arial, sans-serif; font-weight: bold; font-size: 16px; line-height: 20px; padding: 10px; display: inline-block; max-width: 300px; border-radius: 5px; text-shadow: rgba(0, 0, 0, 0.25) 0px -1px 1px; box-shadow: rgba(255, 255, 255, 0.5) 0px 1px 3px inset, rgba(0, 0, 0, 0.5) 0px 1px 3px;\" rel=\"noopener noreferrer\">Download Your FREE Mini-Course<\/a><script data-leadbox=\"1458ca1e0972a2:164f8be4f346dc\" data-url=\"https:\/\/machinelearningmastery.lpages.co\/leadbox\/1458ca1e0972a2%3A164f8be4f346dc\/4715926590455808\/\" data-config=\"%7B%7D\" type=\"text\/javascript\" src=\"https:\/\/machinelearningmastery.lpages.co\/leadbox-1553357564.js\"><\/script><\/p>\n<p><\/center><\/p>\n<div class=\"woo-sc-hr\"><\/div>\n<h2>MNIST Dataset<\/h2>\n<p>The <a href=\"https:\/\/en.wikipedia.org\/wiki\/MNIST_database\">MNIST dataset<\/a> is an acronym that stands for the Modified National Institute of Standards and Technology dataset.<\/p>\n<p>It is a dataset of 60,000 small square 28\u00d728 pixel grayscale images of handwritten single digits between 0 and 9.<\/p>\n<p>The task is to classify a given image of a handwritten digit into one of 10 classes representing integer values from 0 to 9, inclusively.<\/p>\n<p>It is a widely used and deeply understood dataset, and for the most part, is \u201c<em>solved<\/em>.\u201d Top-performing models are deep learning convolutional neural networks that achieve a classification accuracy of above 99%, with an error rate between 0.4 %and 0.2% on the holdout test dataset.<\/p>\n<p>The example below loads the MNIST dataset using the Keras API and creates a plot of the first 9 images in the training dataset.<\/p>\n<pre class=\"crayon-plain-tag\"># example of loading the mnist dataset\r\nfrom keras.datasets import mnist\r\nfrom matplotlib import pyplot\r\n# load dataset\r\n(trainX, trainy), (testX, testy) = mnist.load_data()\r\n# summarize loaded dataset\r\nprint('Train: X=%s, y=%s' % (trainX.shape, trainy.shape))\r\nprint('Test: X=%s, y=%s' % (testX.shape, testy.shape))\r\n# plot first few images\r\nfor i in range(9):\r\n\t# define subplot\r\n\tpyplot.subplot(330 + 1 + i)\r\n\t# plot raw pixel data\r\n\tpyplot.imshow(trainX[i], cmap=pyplot.get_cmap('gray'))\r\n# show the figure\r\npyplot.show()<\/pre>\n<p>Running the example loads the MNIST train and test dataset and prints their shape.<\/p>\n<p>We can see that there are 60,000 examples in the training dataset and 10,000 in the test dataset and that images are indeed square with 28\u00d728 pixels.<\/p>\n<pre class=\"crayon-plain-tag\">Train: X=(60000, 28, 28), y=(60000,)\r\nTest: X=(10000, 28, 28), y=(10000,)<\/pre>\n<p>A plot of the first nine images in the dataset is also created showing the natural handwritten nature of the images to be classified.<\/p>\n<div id=\"attachment_7409\" style=\"width: 1290px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7409\" class=\"size-full wp-image-7409\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-MNIST-Dataset.png\" alt=\"Plot of a Subset of Images From the MNIST Dataset\" width=\"1280\" height=\"960\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-MNIST-Dataset.png 1280w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-MNIST-Dataset-300x225.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-MNIST-Dataset-768x576.png 768w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-MNIST-Dataset-1024x768.png 1024w\" sizes=\"(max-width: 1280px) 100vw, 1280px\"><\/p>\n<p id=\"caption-attachment-7409\" class=\"wp-caption-text\">Plot of a Subset of Images From the MNIST Dataset<\/p>\n<\/div>\n<h2>Fashion-MNIST Dataset<\/h2>\n<p>The <a href=\"https:\/\/github.com\/zalandoresearch\/fashion-mnist\">Fashion-MNIST<\/a> is proposed as a more challenging replacement dataset for the MNIST dataset.<\/p>\n<p>It is a dataset comprised of 60,000 small square 28\u00d728 pixel grayscale images of items of 10 types of clothing, such as shoes, t-shirts, dresses, and more.<\/p>\n<p>It is a more challenging classification problem than MNIST and top results are achieved by deep learning convolutional networks with a classification accuracy of about 95% to 96% on the holdout test dataset.<\/p>\n<p>The example below loads the Fashion-MNIST dataset using the Keras API and creates a plot of the first nine images in the training dataset.<\/p>\n<pre class=\"crayon-plain-tag\"># example of loading the fashion mnist dataset\r\nfrom matplotlib import pyplot\r\nfrom keras.datasets import fashion_mnist\r\n# load dataset\r\n(trainX, trainy), (testX, testy) = fashion_mnist.load_data()\r\n# summarize loaded dataset\r\nprint('Train: X=%s, y=%s' % (trainX.shape, trainy.shape))\r\nprint('Test: X=%s, y=%s' % (testX.shape, testy.shape))\r\n# plot first few images\r\nfor i in range(9):\r\n\t# define subplot\r\n\tpyplot.subplot(330 + 1 + i)\r\n\t# plot raw pixel data\r\n\tpyplot.imshow(trainX[i], cmap=pyplot.get_cmap('gray'))\r\n# show the figure\r\npyplot.show()<\/pre>\n<p>Running the example loads the Fashion-MNIST train and test dataset and prints their shape.<\/p>\n<p>We can see that there are 60,000 examples in the training dataset and 10,000 in the test dataset and that images are indeed square with 28\u00d728 pixels.<\/p>\n<pre class=\"crayon-plain-tag\">Train: X=(60000, 28, 28), y=(60000,)\r\nTest: X=(10000, 28, 28), y=(10000,)<\/pre>\n<p>A plot of the first nine images in the dataset is also created, showing that indeed the images are grayscale photographs of items of clothing.<\/p>\n<div id=\"attachment_7410\" style=\"width: 1290px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7410\" class=\"size-full wp-image-7410\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-Fashion-MNIST-Dataset.png\" alt=\"Plot of a Subset of Images From the Fashion-MNIST Dataset\" width=\"1280\" height=\"960\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-Fashion-MNIST-Dataset.png 1280w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-Fashion-MNIST-Dataset-300x225.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-Fashion-MNIST-Dataset-768x576.png 768w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-Fashion-MNIST-Dataset-1024x768.png 1024w\" sizes=\"(max-width: 1280px) 100vw, 1280px\"><\/p>\n<p id=\"caption-attachment-7410\" class=\"wp-caption-text\">Plot of a Subset of Images From the Fashion-MNIST Dataset<\/p>\n<\/div>\n<h2>CIFAR-10 Dataset<\/h2>\n<p>CIFAR is an acronym that stands for the Canadian Institute For Advanced Research and the <a href=\"https:\/\/en.wikipedia.org\/wiki\/CIFAR-10\">CIFAR-10 dataset<\/a> was developed along with the CIFAR-100 dataset (covered in the next section) by <a href=\"https:\/\/www.cs.toronto.edu\/~kriz\/cifar.html\">researchers at the CIFAR institute<\/a>.<\/p>\n<p>The dataset is comprised of 60,000 32\u00d732 pixel color photographs of objects from 10 classes, such as frogs, birds, cats, ships, etc.<\/p>\n<p>These are very small images, much smaller than a typical photograph, and the dataset is intended for computer vision research.<\/p>\n<p>CIFAR-10 is a dataset and was widely used for benchmarking computer vision algorithms in the field of machine learning. The problem is \u201c<em>solved<\/em>.\u201d Top performance on the problem is achieved by deep learning convolutional neural networks with a classification accuracy above 96% or 97% on the test dataset.<\/p>\n<p>The example below loads the CIFAR-10 dataset using the Keras API and creates a plot of the first nine images in the training dataset.<\/p>\n<pre class=\"crayon-plain-tag\"># example of loading the cifar10 dataset\r\nfrom matplotlib import pyplot\r\nfrom keras.datasets import cifar10\r\n# load dataset\r\n(trainX, trainy), (testX, testy) = cifar10.load_data()\r\n# summarize loaded dataset\r\nprint('Train: X=%s, y=%s' % (trainX.shape, trainy.shape))\r\nprint('Test: X=%s, y=%s' % (testX.shape, testy.shape))\r\n# plot first few images\r\nfor i in range(9):\r\n\t# define subplot\r\n\tpyplot.subplot(330 + 1 + i)\r\n\t# plot raw pixel data\r\n\tpyplot.imshow(trainX[i])\r\n# show the figure\r\npyplot.show()<\/pre>\n<p>Running the example loads the CIFAR-10 train and test dataset and prints their shape.<\/p>\n<p>We can see that there are 50,000 examples in the training dataset and 10,000 in the test dataset and that images are indeed square with 32\u00d732 pixels and color, with three channels.<\/p>\n<pre class=\"crayon-plain-tag\">Train: X=(50000, 32, 32, 3), y=(50000, 1)\r\nTest: X=(10000, 32, 32, 3), y=(10000, 1)<\/pre>\n<p>A plot of the first nine images in the dataset is also created. It is clear that the images are indeed very small compared to modern photographs; it can be challenging to see what exactly is represented in some of the images given the extremely low resolution.<\/p>\n<p>This low resolution is likely the cause of the limited performance that top-of-the-line algorithms are able to achieve on the dataset.<\/p>\n<div id=\"attachment_7411\" style=\"width: 1290px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7411\" class=\"size-full wp-image-7411\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-CIFAR-10-Dataset.png\" alt=\"Plot of a Subset of Images From the CIFAR-10 Dataset\" width=\"1280\" height=\"960\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-CIFAR-10-Dataset.png 1280w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-CIFAR-10-Dataset-300x225.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-CIFAR-10-Dataset-768x576.png 768w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-CIFAR-10-Dataset-1024x768.png 1024w\" sizes=\"(max-width: 1280px) 100vw, 1280px\"><\/p>\n<p id=\"caption-attachment-7411\" class=\"wp-caption-text\">Plot of a Subset of Images From the CIFAR-10 Dataset<\/p>\n<\/div>\n<h2>CIFAR-100 Dataset<\/h2>\n<p>The CIFAR-100 dataset was prepared along with the CIFAR-10 dataset by academics at the Canadian Institute For Advanced Research (CIFAR).<\/p>\n<p>The dataset is comprised of 60,000 32\u00d732 pixel color photographs of objects from 100 classes, such as fish, flowers, insects, and much more.<\/p>\n<p>Like CIFAR-10, the images are intentionally small and unrealistic photographs and the dataset is intended for computer vision research.<\/p>\n<p>The example below loads the CIFAR-100 dataset using the Keras API and creates a plot of the first nine images in the training dataset.<\/p>\n<pre class=\"crayon-plain-tag\"># example of loading the cifar100 dataset\r\nfrom matplotlib import pyplot\r\nfrom keras.datasets import cifar100\r\n# load dataset\r\n(trainX, trainy), (testX, testy) = cifar100.load_data()\r\n# summarize loaded dataset\r\nprint('Train: X=%s, y=%s' % (trainX.shape, trainy.shape))\r\nprint('Test: X=%s, y=%s' % (testX.shape, testy.shape))\r\n# plot first few images\r\nfor i in range(9):\r\n\t# define subplot\r\n\tpyplot.subplot(330 + 1 + i)\r\n\t# plot raw pixel data\r\n\tpyplot.imshow(trainX[i])\r\n# show the figure\r\npyplot.show()<\/pre>\n<p>Running the example loads the CIFAR-100 train and test dataset and prints their shape.<\/p>\n<p>We can see that there are 50,000 examples in the training dataset and 10,000 in the test dataset and that images are indeed square with 32\u00d732 pixels and color, with three channels.<\/p>\n<pre class=\"crayon-plain-tag\">Train: X=(50000, 32, 32, 3), y=(50000, 1)\r\nTest: X=(10000, 32, 32, 3), y=(10000, 1)<\/pre>\n<p>A plot of the first nine images in the dataset is also created, and like CIFAR-10, the low resolution of the images can make it challenging to clearly see what is present in some photos.<\/p>\n<div id=\"attachment_7412\" style=\"width: 1290px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7412\" class=\"size-full wp-image-7412\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-CIFAR-100-Dataset.png\" alt=\"Plot of a Subset of Images From the CIFAR-100 Dataset\" width=\"1280\" height=\"960\" srcset=\"http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-CIFAR-100-Dataset.png 1280w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-CIFAR-100-Dataset-300x225.png 300w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-CIFAR-100-Dataset-768x576.png 768w, http:\/\/3qeqpr26caki16dnhd19sv6by6v.wpengine.netdna-cdn.com\/wp-content\/uploads\/2019\/01\/Plot-of-a-Subset-of-Images-from-the-CIFAR-100-Dataset-1024x768.png 1024w\" sizes=\"(max-width: 1280px) 100vw, 1280px\"><\/p>\n<p id=\"caption-attachment-7412\" class=\"wp-caption-text\">Plot of a Subset of Images From the CIFAR-100 Dataset<\/p>\n<\/div>\n<p>Although there are images organized into 100 classes, the 100 classes are organized into 20 super-classes, e.g. groups of common classes.<\/p>\n<p>Keras will return labels for 100 classes by default, although labels can be retrieved by setting the \u201c<em>label_mode<\/em>\u201d argument to \u201c<em>coarse<\/em>\u201d (instead of the default \u201c<em>fine<\/em>\u201c) when calling the <em>load_data()\u00a0<\/em>function. For example:<\/p>\n<pre class=\"crayon-plain-tag\"># load coarse labels\r\n(trainX, trainy), (testX, testy) = cifar100.load_data(label_mode='coarse')<\/pre>\n<p>The difference is made clear when the labels are one hot encoded using the <em>to_categorical()<\/em> function, where instead of each output vector having 100 dimensions, it will only have 20. The example below demonstrates this by loading the dataset with course labels and encoding the class labels.<\/p>\n<pre class=\"crayon-plain-tag\"># example of loading the cifar100 dataset with coarse labels\r\nfrom keras.datasets import cifar100\r\nfrom keras.utils import to_categorical\r\n# load coarse labels\r\n(trainX, trainy), (testX, testy) = cifar100.load_data(label_mode='coarse')\r\n# one hot encode target values\r\ntrainy = to_categorical(trainy)\r\ntesty = to_categorical(testy)\r\n# summarize loaded dataset\r\nprint('Train: X=%s, y=%s' % (trainX.shape, trainy.shape))\r\nprint('Test: X=%s, y=%s' % (testX.shape, testy.shape))<\/pre>\n<p>Running the example loads the CIFAR-100 dataset as before, but images are now classified as belonging to one of the twenty super-classes.<\/p>\n<p>The class labels are one hot encoded and we can see that each label is represented by a twenty element vector instead of a 100 element vector we would expect for the fine class labels.<\/p>\n<pre class=\"crayon-plain-tag\">Train: X=(50000, 32, 32, 3), y=(50000, 20)\r\nTest: X=(10000, 32, 32, 3), y=(10000, 20)<\/pre>\n<\/p>\n<h2>Further Reading<\/h2>\n<p>This section provides more resources on the topic if you are looking to go deeper.<\/p>\n<h3>APIs<\/h3>\n<ul>\n<li><a href=\"https:\/\/keras.io\/datasets\/\">Keras Datasets API<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/keras-team\/keras\/tree\/master\/keras\/datasets\">Keras Datasets Code<\/a><\/li>\n<\/ul>\n<h3>Articles<\/h3>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/MNIST_database\">MNIST database, Wikipedia.<\/a><\/li>\n<li><a href=\"http:\/\/rodrigob.github.io\/are_we_there_yet\/build\/classification_datasets_results.html\">Classification datasets results, What is the class of this image?<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/zalandoresearch\/fashion-mnist\">Fashion-MNIST GitHub Repository<\/a><\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/CIFAR-10\">CIFAR-10, Wikipedia.<\/a><\/li>\n<li><a href=\"https:\/\/www.cs.toronto.edu\/~kriz\/cifar.html\">The CIFAR-10 dataset and CIFAR-100 datasets<\/a>.<\/li>\n<\/ul>\n<h2>Summary<\/h2>\n<p>In this tutorial, you discovered the standard computer vision datasets provided with the Keras deep learning library.<\/p>\n<p>Specifically, you learned:<\/p>\n<ul>\n<li>The API and idioms for downloading standard computer vision datasets using Keras.<\/li>\n<li>The structure, nature, and top results for the MNIST, Fashion-MNIST, CIFAR-10 and CIFAR-100 computer vision datasets.<\/li>\n<li>How to load and visualize standard computer vision datasets using the Keras API.<\/li>\n<\/ul>\n<p>Do you have any questions?<br \/>\nAsk your questions in the comments below and I will do my best to answer.<\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/machinelearningmastery.com\/how-to-load-and-visualize-standard-computer-vision-datasets-with-keras\/\">How to Load and Visualize Standard Computer Vision Datasets With Keras<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/machinelearningmastery.com\/\">Machine Learning Mastery<\/a>.<\/p>\n<\/div>\n<p><a href=\"https:\/\/machinelearningmastery.com\/how-to-load-and-visualize-standard-computer-vision-datasets-with-keras\/\">Go to Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Author: Jason Brownlee It can be convenient to use a standard computer vision dataset when getting started with deep learning methods for computer vision. Standard [&hellip;] <span class=\"read-more-link\"><a class=\"read-more\" href=\"https:\/\/www.aiproblog.com\/index.php\/2019\/04\/07\/how-to-load-and-visualize-standard-computer-vision-datasets-with-keras\/\">Read More<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":1983,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts\/1982"}],"collection":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/comments?post=1982"}],"version-history":[{"count":0,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts\/1982\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media\/1983"}],"wp:attachment":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=1982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=1982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=1982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}