<jupyter_start><jupyter_text>Setting up the Environment<jupyter_code>import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Conv2D, Dropout, Activation, Dense, BatchNormalization, MaxPooling2D, Flatten
tf.__version__
tf.test.is_gpu_available()<jupyter_output><empty_output><jupyter_text>Getting the Data<jupyter_code>data = pd.read_csv("train.csv")
data.head()
data.shape
X = data.iloc[:,1:]
y = data[['label']]<jupyter_output><empty_output><jupyter_text>Splitting the Data into Train and Validation<jupyter_code>X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size = 0.2, random_state = 0, shuffle = True)
print("X_train : ", X_train.shape)
print("y_train : ", y_train.shape)
print("X_valid : ", X_valid.shape)
print("y_valid : ", y_valid.shape)<jupyter_output>X_train :  (33600, 784)
y_train :  (33600, 1)
X_valid :  (8400, 784)
y_valid :  (8400, 1)<jupyter_text>Reshaping the Train and Validation Data **The Images have 784 pixels, that is, 28x28 pixels and 1 channel(greyscale).**<jupyter_code>X_train = X_train.to_numpy().reshape(-1, 28, 28, 1)  
y_train = y_train.to_numpy()
X_valid = X_valid.to_numpy().reshape(-1, 28, 28, 1)
y_valid = y_valid.to_numpy()
print("X_train : ", X_train.shape)
print("y_train : ", y_train.shape)
print("X_valid : ", X_valid.shape)
print("y_valid : ", y_valid.shape)
np.unique(y_train)
np.unique(y_valid)<jupyter_output><empty_output><jupyter_text>Displaying 10 random examples<jupyter_code>fig = plt.figure(figsize = (20,10))
for i in range(10):
    plt.subplot(2,5,i+1)
    plt.imshow(X_train[i].reshape(28, 28), cmap = 'Greys')    
plt.show()<jupyter_output><empty_output><jupyter_text>Creating the Convolutional Neural Network<jupyter_code>model = Sequential()

model.add(Conv2D(16, kernel_size = 3, padding = 'same', input_shape = (28, 28, 1)))
#model.add(BatchNormalization())
model.add(Activation('relu'))
          
model.add(Conv2D(64, kernel_size = 3, padding = 'same'))
#model.add(BatchNormalization())
model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size = (2,2), strides = (2,2)))

model.add(Conv2D(128, kernel_size = 3, padding = 'same'))
#model.add(BatchNormalization())
model.add(Activation('relu'))

model.add(Conv2D(256, kernel_size = 3, padding = 'same'))
#model.add(BatchNormalization())
model.add(Activation('relu'))

model.add(Conv2D(64, kernel_size = 3, padding = 'same'))
#model.add(BatchNormalization())
model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size = (2,2), strides = (2,2)))
          
model.add(Flatten())

model.add(Dense(256, activation = 'relu'))
#model.add(Dropout(0.5))
model.add(Dense(128, activation = 'relu'))
#model.add(Dropout(0.5))

model.add(Dense(10, activation = 'softmax'))<jupyter_output><empty_output><jupyter_text>**I experimented with BatchNormalization and Dropout, but they seemed to reduce both the Training and Validation accuracy. So, I removed them.**<jupyter_code>model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 28, 28, 16)        160       
_________________________________________________________________
activation (Activation)      (None, 28, 28, 16)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 28, 28, 64)        9280      
_________________________________________________________________
activation_1 (Activation)    (None, 28, 28, 64)        0         
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 14, 14, 64)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 14, 14, 128)       73856     
________________________________________________________[...]<jupyter_text>Creating the Train and Test Image Generators<jupyter_code>train_datagen = ImageDataGenerator(rotation_range = 15,
                                   width_shift_range = 0.3,
                                   height_shift_range = 0.3,
                                   shear_range = 0.2,
                                   zoom_range = 0.15,
                                   rescale = (1./255)) 

valid_datagen = ImageDataGenerator(rescale = 1./255)
train_generator = train_datagen.flow(X_train, y_train, batch_size = 2048, shuffle = True)
validation_generator = valid_datagen.flow(X_valid, y_valid, batch_size = 16, shuffle = True)<jupyter_output><empty_output><jupyter_text>Compiling and Fitting the Model<jupyter_code>model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
hist = model.fit_generator(train_generator,
                           steps_per_epoch = len(train_generator),
                           epochs = 250,
                           validation_data = validation_generator,
                           verbose = 2,
                           validation_steps = len(validation_generator),
                           validation_freq = 1)<jupyter_output>Epoch 1/250<jupyter_text>Plotting the Losses<jupyter_code>def plot_loss(history):
    acc = history.history['accuracy']
    val_acc = history.history['val_accuracy']
    loss = history.history['loss']
    val_loss = history.history['val_loss']

    epochs = range(len(acc))

    plt.figure(figsize=(12,5))

    plt.subplot(1,2,1)
    plt.plot(epochs, acc, 'b', label='Training acc')
    plt.plot(epochs, val_acc, 'r', label='Validation acc')
    plt.title('Training and validation accuracy')
    plt.legend()

    plt.subplot(1,2,2)
    plt.plot(epochs, loss, 'b', label='Training loss')
    plt.plot(epochs, val_loss, 'r', label='Validation loss')
    plt.title('Training and validation loss')
    plt.legend()

    plt.show()


plot_loss(hist)<jupyter_output><empty_output><jupyter_text>Saving the Model<jupyter_code>model.save('model_Keras.h5')<jupyter_output><empty_output><jupyter_text>Evaluating the Model<jupyter_code>train_loss, train_acc = model.evaluate_generator(train_generator, steps = len(train_generator))
print("Train Loss: ", train_loss)
print("Train Acc: ", train_acc)
validation_loss, validation_acc = model.evaluate_generator(validation_generator, steps = len(validation_generator))
print("Validation Loss: ", validation_loss)
print("Validation Acc: ", validation_acc)<jupyter_output>Validation Loss:  0.01838196912108829
Validation Acc:  0.99535716<jupyter_text>Getting the Test Data<jupyter_code>test_data = pd.read_csv("test.csv")
test_data.head()<jupyter_output><empty_output><jupyter_text>Reshaping the Test Data<jupyter_code>X_test = test_data.to_numpy().reshape(-1, 28, 28, 1)
print(X_test.shape)<jupyter_output>(28000, 28, 28, 1)<jupyter_text>Rescaling the Test Input<jupyter_code>X_test = X_test / 255<jupyter_output><empty_output><jupyter_text>Predicting the Labels of the Test Data<jupyter_code>pred_probs = model.predict(X_test, batch_size = 2048)
print(pred_probs.shape)
pred_labels = pred_probs.argmax(axis=1)
pred_labels.shape<jupyter_output><empty_output><jupyter_text>Creating the Output CSV File<jupyter_code>imageid = np.arange(pred_labels.shape[0]) + 1
imageid
df = pd.DataFrame({'ImageId': imageid, 'Label': pred_labels})
df.head()
df.to_csv('output_Keras.csv', index = False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1300430865648257, 'fraction_numerical': 0.036036036036036036, 'mean_word_length': 4.785498489425982, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '398088', 'license': 'MIT', 'n_tokens_mistral': 2732, 'n_tokens_neox': 2510, 'n_words': 463, 'path': 'Keras_Implementation.ipynb', 'repo_name': 'rdutta1999/MNIST', 'size': '7659'}