How to load and visualize CIFAR 10 dataset using TensorFlow Keras?

In this article, I will show you how to load and visualize the CIFAR -10 Dataset.

Please watch our video on YouTube - 

 

CIFAR -10 dataset consists of sixty thousand 32 by 32 color images.

Here the width and height of each image is 32 pixels.

And, each image has three channels RGB, red, green and, blue.

Further, these sixty thousand images are classified into 10 classes. So Each class has six thousand images.

Here 10 classes are airplane, automobile, bird …, etc.

The labels of the images are from 0 to 9. So the label of the airplane is 0, and the label of the automobile is 1, likewise, the label of the truck is 9.

Further, the images are categorized into 50,000 training images and 10,000 test images.

Now let's load the dataset and visualize some images

For this, we need to import some libraries

So let's import first "tensorflow as tf"

And "from tensorflow import keras"

For visualizing images we "import matplotlib.pyplot as plt"

So let’s load CIFAR10 dataset

For this, we use keras.datasets.cifar10.load_data().

This will return two tuples. One for training images and other for test images.

Each tuple will have the image and label.

The first tuple will have training images and their labels and the second tuple will have test images and their labels.

Run the cell and you will get the training and test images along with their labels.

Now we have loaded the dataset.

Let's check the shape of the training images

X_train.shape

It returns 50,000 by 32 by 32 by 3.

50 thousand training images each image is of height and width of 32 pixels each and each image consists of three channels rgb, red green and blue as we discussed.

Let's check the shape of the training labels

Y_train.shape

It returns 50 thousand by 1. The labels are from 0 to 9.


Let's visualize a single training image

X_train[0]

It returns ndarray of shape 32 by 32 by 3.

You can also use plt.X_train[0]

It’s a frog. And the label of the frog class is 6.

Let’s check the label of this image.

y_train[0]

It returns an array of the single value that is the label of the image. Here, the label is 6. And it is the label of the frog as we have seen here.

We can also visualize multiple images using Matplotlib.

Comments