<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 CIFAR - 10
Basic CNN
Activate virtual environment
Step1:  Imports
Step2:  Import helper functions
Step3:  Change matplotlib graph style
Step4:  Constants
Import class names
Step5:  Get number of classes
Step6:  Fetch and decode data
Load the training dataset. Labels are integers whereas class is one-hot encoded vectors.
Step7:  Normal labels
Step8:  One hot encoded labels
Step9:  Load the testing dataset.
Step10:  The CIFAR-10 dataset has been loaded and consists of a total of 60,000 images and corresponding labels.
Define the CNN model
Step11:  Build model
Step12:  Train model on the training data
Save the model after every epoch
Step13:  Configure the model for training
Step14:  For more information on categorical cross entropy loss function see - https
Step15:  Evaluate the model
Step16:  Model accuracy and loss plots
Step17:  Predictions
Predict class for test set images
Step18:  Get the index of the largest element in each vector
Step19:  Check which labels have been predicted correctly
Step20:  Calculate accuracy using manual calculation
Step21:  Show some mis-classifications
Get the incorrectly classified images
Step22:  Plot the first 9 mis-classified images
<ASSISTANT_TASK:>
Python Code:
%%bash
source ~/kerai/bin/activate
Explanation: CIFAR - 10
Basic CNN
Activate virtual environment
End of explanation
%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
Explanation: Imports
End of explanation
from helper import get_class_names, get_train_data, get_test_data, plot_images, plot_model
Explanation: Import helper functions
End of explanation
matplotlib.style.use('ggplot')
Explanation: Change matplotlib graph style
End of explanation
class_names = get_class_names()
print(class_names)
Explanation: Constants
Import class names
End of explanation
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
Explanation: Get number of classes
End of explanation
images_train, labels_train, class_train = get_train_data()
Explanation: Fetch and decode data
Load the training dataset. Labels are integers whereas class is one-hot encoded vectors.
End of explanation
print(labels_train)
Explanation: Normal labels
End of explanation
print(class_train)
Explanation: One hot encoded labels
End of explanation
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))
Explanation: Load the testing dataset.
End of explanation
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
Explanation: The CIFAR-10 dataset has been loaded and consists of a total of 60,000 images and corresponding labels.
Define the CNN model
End of explanation
model = cnn_model()
Explanation: Build model
End of explanation
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 
Explanation: Train model on the training data
Save the model after every epoch
End of explanation
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
Explanation: Configure the model for training
End of explanation
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)
Explanation: 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
End of explanation
scores = model.evaluate(images_test, class_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
Explanation: Evaluate the model
End of explanation
plot_model(model_details)
Explanation: Model accuracy and loss plots
End of explanation
class_pred = model.predict(images_test, batch_size=32)
print(class_pred[0])
Explanation: Predictions
Predict class for test set images
End of explanation
labels_pred = np.argmax(class_pred,axis=1)
print(labels_pred)
Explanation: Get the index of the largest element in each vector
End of explanation
correct = (labels_pred == labels_test)
print(correct)
print("Number of correct predictions: %d" % sum(correct))
Explanation: Check which labels have been predicted correctly
End of explanation
num_images = len(correct)
print("Accuracy: %.2f%%" % ((sum(correct)*100)/num_images))
Explanation: Calculate accuracy using manual calculation
End of explanation
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]
Explanation: Show some mis-classifications
Get the incorrectly classified images
End of explanation
plot_images(images=images_error[0:9],
            labels_true=labels_true[0:9],
            class_names=class_names,
            labels_pred=labels_error[0:9])
Explanation: Plot the first 9 mis-classified images
End of explanation
<END_TASK>