Jovian
⭐️
Sign In

CIFAR-10 Multiple Classes

Constructing a CNN using Keras and Tensorflow for training and predicting animal from a given unclear image. Here we are using CIFAR-10 Data set. CIFAR-10 dataset which consists of 10 different image types.


The Data

CIFAR-10 is a dataset of 50,000 32x32 color training images, labeled over 10 categories, and 10,000 test images.

In [ ]:
from keras.datasets import cifar10

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
In [2]:
x_train.shape
Out[2]:
(50000, 32, 32, 3)
In [3]:
x_train[0].shape
Out[3]:
(32, 32, 3)
In [ ]:
import matplotlib.pyplot as plt
In [5]:
# FROG
plt.imshow(x_train[0])
Out[5]:
<matplotlib.image.AxesImage at 0x295e6e1cba8>
Notebook Image
In [6]:
# HORSE
plt.imshow(x_train[12])
Out[6]:
<matplotlib.image.AxesImage at 0x295e6ebb0b8>
Notebook Image

PreProcessing

In [7]:
x_train[0]
Out[7]:
array([[[ 59,  62,  63],
        [ 43,  46,  45],
        [ 50,  48,  43],
        ...,
        [158, 132, 108],
        [152, 125, 102],
        [148, 124, 103]],

       [[ 16,  20,  20],
        [  0,   0,   0],
        [ 18,   8,   0],
        ...,
        [123,  88,  55],
        [119,  83,  50],
        [122,  87,  57]],

       [[ 25,  24,  21],
        [ 16,   7,   0],
        [ 49,  27,   8],
        ...,
        [118,  84,  50],
        [120,  84,  50],
        [109,  73,  42]],

       ...,

       [[208, 170,  96],
        [201, 153,  34],
        [198, 161,  26],
        ...,
        [160, 133,  70],
        [ 56,  31,   7],
        [ 53,  34,  20]],

       [[180, 139,  96],
        [173, 123,  42],
        [186, 144,  30],
        ...,
        [184, 148,  94],
        [ 97,  62,  34],
        [ 83,  53,  34]],

       [[177, 144, 116],
        [168, 129,  94],
        [179, 142,  87],
        ...,
        [216, 184, 140],
        [151, 118,  84],
        [123,  92,  72]]], dtype=uint8)
In [8]:
x_train[0].shape
Out[8]:
(32, 32, 3)
In [9]:
x_train.max()
Out[9]:
255
In [10]:
x_train = x_train/225
In [11]:
x_test = x_test/255
In [12]:
x_train.shape
Out[12]:
(50000, 32, 32, 3)
In [13]:
x_test.shape
Out[13]:
(10000, 32, 32, 3)

Labels

In [14]:
from keras.utils import to_categorical
In [15]:
y_train.shape
Out[15]:
(50000, 1)
In [16]:
y_train[0]
Out[16]:
array([6], dtype=uint8)
In [17]:
y_cat_train = to_categorical(y_train,10)
In [18]:
y_cat_train.shape
Out[18]:
(50000, 10)
In [19]:
y_cat_train[0]
Out[19]:
array([0., 0., 0., 0., 0., 0., 1., 0., 0., 0.], dtype=float32)
In [20]:
y_cat_test = to_categorical(y_test,10)

Building the Model

In [21]:
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPool2D, Flatten
In [22]:
model = Sequential()

## FIRST SET OF LAYERS

# CONVOLUTIONAL LAYER
model.add(Conv2D(filters=32, kernel_size=(4,4),input_shape=(32, 32, 3), activation='relu',))
# POOLING LAYER
model.add(MaxPool2D(pool_size=(2, 2)))

## SECOND SET OF LAYERS

# CONVOLUTIONAL LAYER
model.add(Conv2D(filters=32, kernel_size=(4,4),input_shape=(32, 32, 3), activation='relu',))
# POOLING LAYER
model.add(MaxPool2D(pool_size=(2, 2)))

# FLATTEN IMAGES FROM 28 by 28 to 764 BEFORE FINAL LAYER
model.add(Flatten())

# 256 NEURONS IN DENSE HIDDEN LAYER (YOU CAN CHANGE THIS NUMBER OF NEURONS)
model.add(Dense(256, activation='relu'))

# LAST LAYER IS THE CLASSIFIER, THUS 10 POSSIBLE CLASSES
model.add(Dense(10, activation='softmax'))


model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
In [23]:
model.summary()
_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_1 (Conv2D) (None, 29, 29, 32) 1568 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 14, 14, 32) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 11, 11, 32) 16416 _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 5, 5, 32) 0 _________________________________________________________________ flatten_1 (Flatten) (None, 800) 0 _________________________________________________________________ dense_1 (Dense) (None, 256) 205056 _________________________________________________________________ dense_2 (Dense) (None, 10) 2570 ================================================================= Total params: 225,610 Trainable params: 225,610 Non-trainable params: 0 _________________________________________________________________
In [25]:
model.fit(x_train,y_cat_train,verbose=1,epochs=10)
Epoch 1/10 50000/50000 [==============================] - 5s 109us/step - loss: 0.8937 - acc: 0.6919 Epoch 2/10 50000/50000 [==============================] - 5s 108us/step - loss: 0.8094 - acc: 0.7249 Epoch 3/10 50000/50000 [==============================] - 5s 109us/step - loss: 0.7420 - acc: 0.7461 Epoch 4/10 50000/50000 [==============================] - 5s 109us/step - loss: 0.6902 - acc: 0.7656 Epoch 5/10 50000/50000 [==============================] - 5s 109us/step - loss: 0.6466 - acc: 0.7780 Epoch 6/10 50000/50000 [==============================] - 5s 110us/step - loss: 0.6105 - acc: 0.7958 Epoch 7/10 50000/50000 [==============================] - 5s 110us/step - loss: 0.5891 - acc: 0.8020 Epoch 8/10 50000/50000 [==============================] - 5s 109us/step - loss: 0.5631 - acc: 0.8124 Epoch 9/10 50000/50000 [==============================] - 6s 110us/step - loss: 0.5416 - acc: 0.8203 Epoch 10/10 50000/50000 [==============================] - 5s 109us/step - loss: 0.5252 - acc: 0.8275
Out[25]:
<keras.callbacks.History at 0x295dce78dd8>
In [26]:
# Careful, don't overwrite our file!
# model.save('cifar_10epochs.h5')
In [27]:
model.metrics_names
Out[27]:
['loss', 'acc']
In [29]:
model.evaluate(x_test,y_cat_test)
10000/10000 [==============================] - 1s 56us/step
Out[29]:
[1.3332478387832642, 0.6444]
In [30]:
from sklearn.metrics import classification_report

predictions = model.predict_classes(x_test)
In [31]:
print(classification_report(y_test,predictions))
precision recall f1-score support 0 0.80 0.59 0.68 1000 1 0.83 0.76 0.80 1000 2 0.44 0.65 0.52 1000 3 0.50 0.40 0.44 1000 4 0.50 0.75 0.60 1000 5 0.52 0.57 0.54 1000 6 0.70 0.74 0.72 1000 7 0.88 0.56 0.69 1000 8 0.82 0.74 0.78 1000 9 0.81 0.68 0.74 1000 avg / total 0.68 0.64 0.65 10000

Optional: Large Model

In [33]:
model = Sequential()

## FIRST SET OF LAYERS

# CONVOLUTIONAL LAYER
model.add(Conv2D(filters=32, kernel_size=(4,4),input_shape=(32, 32, 3), activation='relu',))
# CONVOLUTIONAL LAYER
model.add(Conv2D(filters=32, kernel_size=(4,4),input_shape=(32, 32, 3), activation='relu',))

# POOLING LAYER
model.add(MaxPool2D(pool_size=(2, 2)))

## SECOND SET OF LAYERS

# CONVOLUTIONAL LAYER
model.add(Conv2D(filters=64, kernel_size=(4,4),input_shape=(32, 32, 3), activation='relu',))
# CONVOLUTIONAL LAYER
model.add(Conv2D(filters=64, kernel_size=(4,4),input_shape=(32, 32, 3), activation='relu',))

# POOLING LAYER
model.add(MaxPool2D(pool_size=(2, 2)))

# FLATTEN IMAGES FROM 28 by 28 to 764 BEFORE FINAL LAYER
model.add(Flatten())

# 512 NEURONS IN DENSE HIDDEN LAYER (YOU CAN CHANGE THIS NUMBER OF NEURONS)
model.add(Dense(512, activation='relu'))

# LAST LAYER IS THE CLASSIFIER, THUS 10 POSSIBLE CLASSES
model.add(Dense(10, activation='softmax'))


model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
In [34]:
model.fit(x_train,y_cat_train,verbose=1,epochs=20)
Epoch 1/20 50000/50000 [==============================] - 7s 148us/step - loss: 1.6025 - acc: 0.4197 Epoch 2/20 50000/50000 [==============================] - 8s 159us/step - loss: 1.1793 - acc: 0.5849 Epoch 3/20 50000/50000 [==============================] - 8s 155us/step - loss: 1.0083 - acc: 0.6508 Epoch 4/20 50000/50000 [==============================] - 8s 156us/step - loss: 0.9479 - acc: 0.6782 Epoch 5/20 50000/50000 [==============================] - 8s 154us/step - loss: 0.9145 - acc: 0.6952 Epoch 6/20 50000/50000 [==============================] - 8s 151us/step - loss: 0.9005 - acc: 0.7015 Epoch 7/20 50000/50000 [==============================] - 8s 151us/step - loss: 0.8867 - acc: 0.7061 Epoch 8/20 50000/50000 [==============================] - 8s 152us/step - loss: 0.8796 - acc: 0.7121 Epoch 9/20 50000/50000 [==============================] - 8s 151us/step - loss: 0.8693 - acc: 0.7158 Epoch 10/20 50000/50000 [==============================] - 8s 151us/step - loss: 0.8591 - acc: 0.7194 Epoch 11/20 50000/50000 [==============================] - 7s 145us/step - loss: 0.8586 - acc: 0.7235 Epoch 12/20 50000/50000 [==============================] - 8s 151us/step - loss: 0.8497 - acc: 0.7248 Epoch 13/20 50000/50000 [==============================] - 7s 148us/step - loss: 0.8462 - acc: 0.7296 Epoch 14/20 50000/50000 [==============================] - 7s 139us/step - loss: 0.8422 - acc: 0.7263 Epoch 15/20 50000/50000 [==============================] - 7s 147us/step - loss: 0.8321 - acc: 0.7330 Epoch 16/20 50000/50000 [==============================] - 7s 143us/step - loss: 0.8154 - acc: 0.7363 Epoch 17/20 50000/50000 [==============================] - 7s 147us/step - loss: 0.8241 - acc: 0.7353 Epoch 18/20 50000/50000 [==============================] - 7s 138us/step - loss: 0.8061 - acc: 0.7411 Epoch 19/20 50000/50000 [==============================] - 7s 140us/step - loss: 0.8120 - acc: 0.7409 Epoch 20/20 50000/50000 [==============================] - 7s 142us/step - loss: 0.8097 - acc: 0.7437
Out[34]:
<keras.callbacks.History at 0x295e6eb3e80>
In [35]:
model.evaluate(x_test,y_cat_test)
10000/10000 [==============================] - 1s 75us/step
Out[35]:
[0.9843294318199157, 0.6902]
In [36]:
from sklearn.metrics import classification_report

predictions = model.predict_classes(x_test)
In [37]:
print(classification_report(y_test,predictions))
precision recall f1-score support 0 0.82 0.61 0.70 1000 1 0.77 0.88 0.82 1000 2 0.64 0.57 0.60 1000 3 0.66 0.30 0.41 1000 4 0.66 0.67 0.66 1000 5 0.45 0.81 0.58 1000 6 0.80 0.75 0.77 1000 7 0.76 0.69 0.72 1000 8 0.78 0.81 0.80 1000 9 0.75 0.82 0.79 1000 avg / total 0.71 0.69 0.69 10000
In [38]:
model.save('larger_CIFAR10_model.h5')