Day 51 of 100DaysofML
CIFAR-10 Photo Classification. Since this is a bigger project, I shall be spreading it out over a couple of blogs so that it is easier for y’all to continue as well. Alright, let's get right into it.
So, what exactly is this CIFAR-10 dataset?
CIFAR is an acronym that stands for the Canadian Institute for Advanced Research and the CIFAR-10 dataset was developed along with the CIFAR-100 dataset by researchers at the CIFAR institute.
The dataset is comprised of 60,000 32×32 pixel color photographs of objects from 10 classes, such as frogs, birds, cats, ships, etc. The class labels and their standard associated integer values are listed below:
0: airplane
- 1: automobile
- 2: bird
- 3: cat
- 4: deer
- 5: dog
- 6: frog
- 7: horse
- 8: ship
- 9: truck
So basically, the neural network will be able to predict from a random image whether the given picture belongs to any of the classes or not.
Let’s start with the essential and important step of importing all the libraries first.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import keras
Once the libraries have been imported, let us import the CIFAR-10 data which comes loaded along with the Keras wrapper package.
from keras.datasets import cifar10
(trainX,trainY), (testX,testY) = cifar10.load_data()
You can print and check the size of the training and testing data.
print("Shape of training data")
print('Training data: X={} Y={}'.format(trainX.shape,trainY.shape))
print('Testing data: X={} Y={}'.format(testX.shape,testY.shape))
You can print the first few values of the dataset to get a rough idea of it.
from matplotlib import pyplot as plt
for i in range(9):
plt.subplot(330 + 1 + i)
plt.imshow(trainX[i])
The following pictures would be present along with target variables represented by trainY in our dataset.
We now use One Hot Encoding in order to convert the integer into a 10 element binary vector with a 1 for the index of the class value. We can achieve this using the to_categorical() utility function which is to be imported from the keras.utils module.
We can have a look at the categorical values:
from keras.utils import to_categorical
trainY = to_categorical(trainY)
testY = to_categorical(testY)
trainX
We can see how One Hot Encoding has converted the label into a 9 dimension vector which shall be fed to the machine learning model.
We do not know the best way to scale the pixel values for modeling, but we know that some scaling will be required. A good starting point is to normalize the pixel values, e.g. rescale them to the range [0,1]. This involves first converting the data type from unsigned integers to floats, then dividing the pixel values by the maximum value.
train_norm = trainX.astype('float32')
test_norm = testX.astype('float32')
train_norm = train_norm / 255.0
test_norm = test_norm / 255.0
I shall be continuing in tomorrow’s blog about the implementation of the model and creation of the neural network.
Thanks for reading. Keep Learning.
Cheers.