<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Artificial Intelligence Nanodegree
Convolutional Neural Networks
In this notebook, we train a CNN on augmented images from the CIFAR-10 database.
1. Load CIFAR-10 Database
Step1:  2. Visualize the First 24 Training Images
Step2:  3. Rescale the Images by Dividing Every Pixel in Every Image by 255
Step3:  4.  Break Dataset into Training, Testing, and Validation Sets
Step4:  5. Create and Configure Augmented Image Generator
Step5:  6. Visualize Original and Augmented Images
Step6:  7. Define the Model Architecture
Step7:  8. Compile the Model
Step8:  9. Train the Model
Step9:  10. Load the Model with the Best Validation Accuracy
Step10:  11. Calculate Classification Accuracy on Test Set
<ASSISTANT_TASK:>
Python Code:
import keras
from keras.datasets import cifar10
# load the pre-shuffled train and test data
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
Explanation: Artificial Intelligence Nanodegree
Convolutional Neural Networks
In this notebook, we train a CNN on augmented images from the CIFAR-10 database.
1. Load CIFAR-10 Database
End of explanation
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
fig = plt.figure(figsize=(20,5))
for i in range(36):
    ax = fig.add_subplot(3, 12, i + 1, xticks=[], yticks=[])
    ax.imshow(np.squeeze(x_train[i]))
Explanation: 2. Visualize the First 24 Training Images
End of explanation
# rescale [0,255] --> [0,1]
x_train = x_train.astype('float32')/255
x_test = x_test.astype('float32')/255 
Explanation: 3. Rescale the Images by Dividing Every Pixel in Every Image by 255
End of explanation
from keras.utils import np_utils
# break training set into training and validation sets
(x_train, x_valid) = x_train[5000:], x_train[:5000]
(y_train, y_valid) = y_train[5000:], y_train[:5000]
# one-hot encode the labels
num_classes = len(np.unique(y_train))
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
y_valid = keras.utils.to_categorical(y_valid, num_classes)
# print shape of training set
print('x_train shape:', x_train.shape)
# print number of training, validation, and test images
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
print(x_valid.shape[0], 'validation samples')
Explanation: 4.  Break Dataset into Training, Testing, and Validation Sets
End of explanation
from keras.preprocessing.image import ImageDataGenerator
# create and configure augmented image generator
datagen_train = ImageDataGenerator(
    width_shift_range=0.1,  # randomly shift images horizontally (10% of total width)
    height_shift_range=0.1,  # randomly shift images vertically (10% of total height)
    horizontal_flip=True) # randomly flip images horizontally
# create and configure augmented image generator
datagen_valid = ImageDataGenerator(
    width_shift_range=0.1,  # randomly shift images horizontally (10% of total width)
    height_shift_range=0.1,  # randomly shift images vertically (10% of total height)
    horizontal_flip=True) # randomly flip images horizontally
# fit augmented image generator on data
datagen_train.fit(x_train)
datagen_valid.fit(x_valid)
Explanation: 5. Create and Configure Augmented Image Generator
End of explanation
import matplotlib.pyplot as plt
# take subset of training data
x_train_subset = x_train[:12]
# visualize subset of training data
fig = plt.figure(figsize=(20,2))
for i in range(0, len(x_train_subset)):
    ax = fig.add_subplot(1, 12, i+1)
    ax.imshow(x_train_subset[i])
fig.suptitle('Subset of Original Training Images', fontsize=20)
plt.show()
# visualize augmented images
fig = plt.figure(figsize=(20,2))
for x_batch in datagen.flow(x_train_subset, batch_size=12):
    for i in range(0, 12):
        ax = fig.add_subplot(1, 12, i+1)
        ax.imshow(x_batch[i])
    fig.suptitle('Augmented Images', fontsize=20)
    plt.show()
    break;
Explanation: 6. Visualize Original and Augmented Images
End of explanation
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
model = Sequential()
model.add(Conv2D(filters=16, kernel_size=2, padding='same', activation='relu', 
                        input_shape=(32, 32, 3)))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(filters=32, kernel_size=2, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(filters=64, kernel_size=2, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.3))
model.add(Flatten())
model.add(Dense(500, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
model.summary()
Explanation: 7. Define the Model Architecture
End of explanation
# compile the model
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', 
                  metrics=['accuracy'])
Explanation: 8. Compile the Model
End of explanation
from keras.callbacks import ModelCheckpoint   
batch_size = 32
epochs = 100
# train the model
checkpointer = ModelCheckpoint(filepath='aug_model.weights.best.hdf5', verbose=1, 
                               save_best_only=True)
model.fit_generator(datagen_train.flow(x_train, y_train, batch_size=batch_size),
                    steps_per_epoch=x_train.shape[0] // batch_size,
                    epochs=epochs, verbose=2, callbacks=[checkpointer],
                    validation_data=datagen_valid.flow(x_valid, y_valid, batch_size=batch_size),
                    validation_steps=x_valid.shape[0] // batch_size)
Explanation: 9. Train the Model
End of explanation
# load the weights that yielded the best validation accuracy
model.load_weights('aug_model.weights.best.hdf5')
Explanation: 10. Load the Model with the Best Validation Accuracy
End of explanation
# evaluate and print test accuracy
score = model.evaluate(x_test, y_test, verbose=0)
print('\n', 'Test accuracy:', score[1])
Explanation: 11. Calculate Classification Accuracy on Test Set
End of explanation
<END_TASK>