Loading and exploring CIFAR-10

Let's start off by importing the required packages for this implementation:

%matplotlib inline
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import time
from datetime import timedelta
import os

# Importing a helper module for the functions of the Inception model.
import inception

Next up, we need to load another helper script that we can use to download the processing CIFAR-10 dataset:

import cifar10
#importing number of classes of CIFAR-10
from cifar10 import num_classes

If you haven't done this already, you need to set the path for CIFAR-10. This path will be used by the cifar-10.py script to persist the dataset:

cifar10.data_path = "data/CIFAR-10/"

The CIFAR-10 dataset is about 170 MB, the next line checks if the dataset is already downloaded if not it downloads the dataset and store in the previous data_path:

cifar10.maybe_download_and_extract</span>()

Output:

- Download progress: 100.0%
Download finished. Extracting files.
Done.

Let's see the categories that we have in the CIFAR-10 dataset:

#Loading the class names of CIFAR-10 dataset
class_names
= cifar10.load_class_names() class_names

Output:

Loading data: data/CIFAR-10/cifar-10-batches-py/batches.meta
['airplane',
'automobile',
'bird',
'cat',
'deer',
'dog',
'frog',
'horse',
'ship',
'truck']
Load the training-set.

This returns images, the class-numbers as integers, and the class-numbers as one-hot encoded arrays called labels:

training_images, training_cls_integers, trainig_one_hot_labels = cifar10.load_training_data()

Output:

Loading data: data/CIFAR-10/cifar-10-batches-py/data_batch_1
Loading data: data/CIFAR-10/cifar-10-batches-py/data_batch_2
Loading data: data/CIFAR-10/cifar-10-batches-py/data_batch_3
Loading data: data/CIFAR-10/cifar-10-batches-py/data_batch_4
Loading data: data/CIFAR-10/cifar-10-batches-py/data_batch_5
Load the test-set.

Now, let's do the same for the testing set by loading the images and their corresponding integer representation of the target classes with their one-hot encoding:

#Loading the test images, their class integer, and their corresponding one-hot encoding
testing_images, testing_cls_integers, testing_one_hot_labels = cifar10.load_test_data()

Output:

Loading data: data/CIFAR-10/cifar-10-batches-py/test_batch

Let's have a look at the distribution of the training and testing sets in CIFAR-10:

print("-Number of images in the training set:		{}".format(len(training_images)))
print("-Number of images in the testing set: {}".format(len(testing_images)))

Output:

-Number of images in the training set:          50000
-Number of images in the testing set:           10000

Let's define some helper functions that will enable us to explore the dataset. The following helper function plots a set of nine images in a grid:

def plot_imgs(imgs, true_class, predicted_class=None):

assert len(imgs) == len(true_class)

# Creating a placeholders for 9 subplots
fig, axes = plt.subplots(3, 3)

# Adjustting spacing.
if predicted_class is None:
hspace = 0.3
else:
hspace = 0.6
fig.subplots_adjust(hspace=hspace, wspace=0.3)


for i, ax in enumerate(axes.flat):
# There may be less than 9 images, ensure it doesn't crash.
if i < len(imgs):
# Plot image.
ax.imshow(imgs[i],
interpolation='nearest')

# Get the actual name of the true class from the class_names array
true_class_name = class_names[true_class[i]]

# Showing labels for the predicted and true classes
if predicted_class is None:
xlabel = "True: {0}".format(true_class_name)
else:
# Name of the predicted class.
predicted_class_name = class_names[predicted_class[i]]

xlabel = "True: {0} Pred: {1}".format(true_class_name, predicted_class_name)


ax.set_xlabel(xlabel)

# Remove ticks from the plot.
ax.set_xticks([])
ax.set_yticks([])

plt.show()

Let's go ahead and visualize some images from the test set along with their corresponding actual class:

# get the first 9 images in the test set
imgs = testing_images[0:9]

# Get the integer representation of the true class.
true_class = testing_cls_integers[0:9]

# Plotting the images
plot_imgs(imgs=imgs, true_class=true_class)

Output:

Figure 10.4: The first nine images of the test set
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset