Getting Started with Datasets in Keras | by Sadrach Pierre, Ph.D. | Towards Data Science

Keras is an open-source python library for neural networks. It enables easy implementation and experimentation of a variety of neural network architectures. In addition to providing many of the building blocks for neural networks, Keras also has many built-in datasets.
In this post, we will take a look at some of the benchmark data from the Keras library. These data sets are often used to demonstrate improved performance, over state of the art methods, of novel machine learning algorithms.
Let’s get started!
The first data set we will look at is the ‘CIFAR10’ small image classification data set. The ‘CIFAR10’ data set contains images of airplanes, automobiles, birds, cats, deer, dogs, frogs, horses, ships and trucks.
Let’s import the ‘CIFAR10’ data:
from keras.datasets import cifar10  (x_train, y_train), (x_test, y_test) = cifar10.load_data()
Next, let’s look at the list of images and labels in data. This can be found in the documentation for the ‘CIFAR10’ data.
If we print the first value of the training input data we get:
print(X_train[0])
The label corresponding to this record is:
print("Label: ", y_train[0])
Since the labels are from 0–9, this corresponds to the 7th label, which is a frog.
Let’s import ‘matplotlib’ and use the ‘imshow’ method to display a few of the samples:
import matplotlib.pyplot as pltplt.imshow(X_train[0])plt.show()
We can print a few other values for inpsection:
import matplotlib.pyplot as pltplt.imshow(X_train[1])plt.show()
The label corresponding to this record is:
print("Label: ", y_train[0])
This corresponds to the 10th label which is a truck.
Let’s try one more:
import matplotlib.pyplot as pltplt.imshow(X_train[2])plt.show()
The label corresponding to this record is:
print("Label: ", y_train[0])
This also corresponds to an image of a truck.
The ‘IMDB’ data set contains 25K movie reviews. It is typically used to build baseline natural language processing models or for simple text analytics. The data set contains binary scores (1 & 0) for each review which indicates positive or negative sentiment respectively. The text has been preprocessed where each word has been encoded with indexes. Each word is indexed by the overall frequency of appearance in the dataset.
Let’s load the ‘IMDB’ data:
from keras.datasets import imdb(x_train, y_train), (x_test, y_test) = imdb.load_data(path="imdb.npz", num_words=None,skip_top=0, maxlen=None, seed=113, start_char=1, oov_char=2,index_from=3)
Let’s print the first training record:
print(X_train[0])
The corresponding label:
print("Label: ", y_train[0])
Given that the text is encoded, there isn’t too much to see here. If you are interested in analyzing the original data, you can find it here. Let’s move on to the next data set.
The next data set we’ll look at is the ‘MNIST’ data set. The data set contain 60K 28x28 gray-scale handwritten digits from (0–9). It is also used as a benchmark dataset for validating novel image classification methods.
Let’s load the data:
from keras.datasets import mnist(X_train, y_train), (X_test, y_test) = mnist.load_data()
Let’s print the first record:
print(X_train[0])
The label corresponding to this record is:
print("Label: ", y_train[0])
Now let’s use ‘imshow’ to display the image:
plt.imshow(X_train[0], cmap='gray')plt.show()
Let’s print the next record:
plt.imshow(X_train[1], cmap='gray')plt.show()
Let’s try one more:
plt.imshow(X_train[2], cmap='gray')plt.show()
Now let’s look at the ‘Fashion MNIST’ data set.
The ‘Fashion-MNIST’ data set contains 60K gray-scaled images of labeled fashion categories from Zalando articles. It is also used to benchmark machine learning algorithms for image classification.
The label codes are as follows:
Let’s import the data:
from keras.datasets import fashion_mnist(X_train, y_train), (X_test, y_test) = fashion_mnist.load_data()
Let’s display the first image and its label:
plt.imshow(X_train[0], cmap='gray')plt.show()print("Label: ", y_train[0])
Label ‘9’ corresponds to an ankle boot.
Let’s try another:
plt.imshow(X_train[1], cmap='gray')plt.show()print("Label: ", y_train[1])
And finally, one more:
plt.imshow(X_train[2], cmap='gray')plt.show()print("Label: ", y_train[2])
I’ll stop here but feel free to explore each of these data sets further. Given that these data set come structured and labeled they are a great resource for budding data scientists interested in strengthening their model building skills.
To summarize, in this post we went over how to pull data from the Keras library. We looked at ‘CIFAR’, ‘MNIST’ and ‘Fashion-MNIST’ data, all of which are common benchmark data sets for image classification. We also looked at the ‘IMDB’ data set which contains encoded reviews with accompanying labels corresponding to positive and negative sentiment. This is another benchmark data set often used for sentiment analysis. I hope you found this post interesting. The code from this post will be available on GitHub. Thank you for reading!