<jupyter_start><jupyter_text>CIFAR - 10 Basic CNN  Activate virtual environment<jupyter_code>%%bash
source ~/kerai/bin/activate<jupyter_output><empty_output><jupyter_text>Imports<jupyter_code>%matplotlib inline
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
from keras.models import Sequential
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint
from keras.models import load_model
from keras.layers import Lambda, Conv2D, MaxPooling2D, Dropout, Dense, Flatten, Activation<jupyter_output>Using TensorFlow backend.<jupyter_text>Import helper functions<jupyter_code>from helper import get_class_names, get_train_data, get_test_data, plot_images, plot_model<jupyter_output><empty_output><jupyter_text>Change matplotlib graph style<jupyter_code>matplotlib.style.use('ggplot')<jupyter_output><empty_output><jupyter_text>Constants Import class names<jupyter_code>class_names = get_class_names()
print(class_names)<jupyter_output>Decoding file: data/batches.meta
['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']<jupyter_text>Get number of classes<jupyter_code>num_classes = len(class_names)
print(num_classes)
# Hight and width of the images
IMAGE_SIZE = 32
# 3 channels, Red, Green and Blue
CHANNELS = 3<jupyter_output><empty_output><jupyter_text>Fetch and decode data Load the training dataset. Labels are integers whereas class is one-hot encoded vectors.<jupyter_code>images_train, labels_train, class_train = get_train_data()<jupyter_output>Decoding file: data/data_batch_1
Decoding file: data/data_batch_2
Decoding file: data/data_batch_3
Decoding file: data/data_batch_4
Decoding file: data/data_batch_5<jupyter_text>Normal labels<jupyter_code>print(labels_train)<jupyter_output>[6 9 9 ..., 9 1 1]<jupyter_text>One hot encoded labels<jupyter_code>print(class_train)<jupyter_output>[[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  1.]
 [ 0.  0.  0. ...,  0.  0.  1.]
 ..., 
 [ 0.  0.  0. ...,  0.  0.  1.]
 [ 0.  1.  0. ...,  0.  0.  0.]
 [ 0.  1.  0. ...,  0.  0.  0.]]<jupyter_text>Load the testing dataset.<jupyter_code>images_test, labels_test, class_test = get_test_data()
print("Training set size:\t",len(images_train))
print("Testing set size:\t",len(images_test))<jupyter_output>Training set size:	 50000
Testing set size:	 10000<jupyter_text>The CIFAR-10 dataset has been loaded and consists of a total of 60,000 images and corresponding labels.  Define the CNN model<jupyter_code>def cnn_model():
    
    model = Sequential()
    
    model.add(Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(IMAGE_SIZE,IMAGE_SIZE,CHANNELS)))    
    model.add(Conv2D(32, (3, 3), activation='relu'))    
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    
    model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))    
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))

    model.add(Flatten())
    
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.5))
    
    model.add(Dense(num_classes, activation='softmax'))
    
    model.summary()
    
    return model<jupyter_output><empty_output><jupyter_text>Build model<jupyter_code>model = cnn_model()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 32, 32, 32)        896       
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 30, 30, 32)        9248      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 15, 15, 32)        0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 15, 15, 32)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 15, 15, 64)        18496     
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 13, 13, 64)        36928     
_________________________________________________________________
max_poolin[...]<jupyter_text>Train model on the training data Save the model after every epoch<jupyter_code>checkpoint = ModelCheckpoint('best_model_simple.h5',  # model filename
                             monitor='val_loss', # quantity to monitor
                             verbose=0, # verbosity - 0 or 1
                             save_best_only= True, # The latest best model will not be overwritten
                             mode='auto') # The decision to overwrite model is made 
                                          # automatically depending on the quantity to monitor<jupyter_output><empty_output><jupyter_text>Configure the model for training<jupyter_code>model.compile(loss='categorical_crossentropy', # Better loss function for neural networks
              optimizer=Adam(lr=1.0e-4), # Adam optimizer with 1.0e-4 learning rate
              metrics = ['accuracy']) # Metrics to be evaluated by the model<jupyter_output><empty_output><jupyter_text>For more information on categorical cross entropy loss function see - https://jamesmccaffrey.wordpress.com/2013/11/05/why-you-should-use-cross-entropy-error-instead-of-classification-error-or-mean-squared-error-for-neural-network-classifier-training/ Fit the model on the data provided<jupyter_code>model_details = model.fit(images_train, class_train,
                    batch_size = 128, # number of samples per gradient update
                    epochs = 100, # number of iterations
                    validation_data= (images_test, class_test),
                    callbacks=[checkpoint],
                    verbose=1)<jupyter_output>Train on 50000 samples, validate on 10000 samples
Epoch 1/100
50000/50000 [==============================] - 85s - loss: 1.9323 - acc: 0.2891 - val_loss: 1.6324 - val_acc: 0.4150
Epoch 2/100
50000/50000 [==============================] - 82s - loss: 1.5806 - acc: 0.4253 - val_loss: 1.4275 - val_acc: 0.4862
Epoch 3/100
50000/50000 [==============================] - 82s - loss: 1.4481 - acc: 0.4753 - val_loss: 1.3389 - val_acc: 0.5209
Epoch 4/100
50000/50000 [==============================] - 82s - loss: 1.3712 - acc: 0.5065 - val_loss: 1.2795 - val_acc: 0.5449
Epoch 5/100
50000/50000 [==============================] - 82s - loss: 1.3134 - acc: 0.5290 - val_loss: 1.2220 - val_acc: 0.5619
Epoch 6/100
50000/50000 [==============================] - 82s - loss: 1.2571 - acc: 0.5529 - val_loss: 1.1620 - val_acc: 0.5902
Epoch 7/100
50000/50000 [==============================] - 82s - loss: 1.2140 - acc: 0.5695 - val_loss: 1.1246 - val_acc: 0.6024
Epoch 8/100
50000/50000 [======================[...]<jupyter_text>Evaluate the model<jupyter_code>scores = model.evaluate(images_test, class_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))<jupyter_output>Accuracy: 78.83%<jupyter_text>Model accuracy and loss plots<jupyter_code>plot_model(model_details)<jupyter_output><empty_output><jupyter_text>Predictions Predict class for test set images<jupyter_code>class_pred = model.predict(images_test, batch_size=32)
print(class_pred[0])<jupyter_output>[  2.34778727e-05   3.61718005e-03   7.07750034e-04   6.86623812e-01
   2.35363492e-04   2.97410578e-01   9.98710049e-04   1.62866409e-03
   8.46819486e-03   2.86295195e-04]<jupyter_text>Get the index of the largest element in each vector<jupyter_code>labels_pred = np.argmax(class_pred,axis=1)
print(labels_pred)<jupyter_output>[3 8 8 ..., 5 1 7]<jupyter_text>Check which labels have been predicted correctly<jupyter_code>correct = (labels_pred == labels_test)
print(correct)
print("Number of correct predictions: %d" % sum(correct))<jupyter_output>[ True  True  True ...,  True  True  True]
Number of correct predictions: 7883<jupyter_text>Calculate accuracy using manual calculation<jupyter_code>num_images = len(correct)
print("Accuracy: %.2f%%" % ((sum(correct)*100)/num_images))<jupyter_output>Accuracy: 78.83%<jupyter_text>Show some mis-classifications Get the incorrectly classified images<jupyter_code>incorrect = (correct == False)

# Images of the test-set that have been incorrectly classified.
images_error = images_test[incorrect]

# Get predicted classes for those images
labels_error = labels_pred[incorrect]

# Get true classes for those images
labels_true = labels_test[incorrect]<jupyter_output><empty_output><jupyter_text>Plot the first 9 mis-classified images<jupyter_code>plot_images(images=images_error[0:9],
            labels_true=labels_true[0:9],
            class_names=class_names,
            labels_pred=labels_error[0:9])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1404893449092344, 'fraction_numerical': 0.07103393843725335, 'mean_word_length': 4.273483947681331, 'pattern_counts': {'":': 0, '<': 80, '<?xml version=': 0, '>': 80, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '23', 'dir': 'jupyter-structured-clean-dedup', 'id': '191084', 'license': 'MIT', 'n_tokens_mistral': 3314, 'n_tokens_neox': 2869, 'n_words': 637, 'path': 'cifar10-simple-cnn.ipynb', 'repo_name': 'shubham14p3/-CIFAR-10-object-recognition-task', 'size': '8869'}