{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline \n\nimport tensorflow as tf \nfrom tensorflow.keras.datasets import mnist\nfrom keras.models import Sequential\nfrom keras.layers import Dense, BatchNormalization, MaxPooling2D, Conv2D, Flatten, Activation\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:46:49.900517Z","iopub.execute_input":"2023-05-15T10:46:49.900926Z","iopub.status.idle":"2023-05-15T10:46:49.926854Z","shell.execute_reply.started":"2023-05-15T10:46:49.900891Z","shell.execute_reply":"2023-05-15T10:46:49.925812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntrain.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:46:49.929481Z","iopub.execute_input":"2023-05-15T10:46:49.929879Z","iopub.status.idle":"2023-05-15T10:46:53.540816Z","shell.execute_reply.started":"2023-05-15T10:46:49.929842Z","shell.execute_reply":"2023-05-15T10:46:53.539813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use the data built in the Keras dataset as it contains 60k samples and 2 datasets are the same but different at the ratio between training and validation set\n# I will use them for train set and val set \n(X_train, y_train), (X_val, y_val) = mnist.load_data()","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:46:53.545043Z","iopub.execute_input":"2023-05-15T10:46:53.54536Z","iopub.status.idle":"2023-05-15T10:46:53.810965Z","shell.execute_reply.started":"2023-05-15T10:46:53.545331Z","shell.execute_reply":"2023-05-15T10:46:53.809983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape, y_train.shape, X_val.shape, y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:46:53.813843Z","iopub.execute_input":"2023-05-15T10:46:53.814315Z","iopub.status.idle":"2023-05-15T10:46:53.821821Z","shell.execute_reply.started":"2023-05-15T10:46:53.814277Z","shell.execute_reply":"2023-05-15T10:46:53.819484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape the target data to the same size\n\ny_train.reshape(-1,)\ny_val.reshape(-1,)\ny_train","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:46:53.823562Z","iopub.execute_input":"2023-05-15T10:46:53.824415Z","iopub.status.idle":"2023-05-15T10:46:53.832974Z","shell.execute_reply.started":"2023-05-15T10:46:53.824378Z","shell.execute_reply":"2023-05-15T10:46:53.831817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot a sample\n\ni = np.random.randint(0, 1000)\nplt.imshow(X_train[i], cmap = 'gray')\nplt.title(y_train[i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:46:53.834558Z","iopub.execute_input":"2023-05-15T10:46:53.835657Z","iopub.status.idle":"2023-05-15T10:46:54.040683Z","shell.execute_reply.started":"2023-05-15T10:46:53.835612Z","shell.execute_reply":"2023-05-15T10:46:54.039719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the distributions of the training dataset and validation dataset\nsns.countplot(y_train).set_title('Dist of the train set')\nplt.xlabel('Labels')\nplt.ylabel('Number of sample')\nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:46:54.042253Z","iopub.execute_input":"2023-05-15T10:46:54.043374Z","iopub.status.idle":"2023-05-15T10:46:54.310307Z","shell.execute_reply.started":"2023-05-15T10:46:54.043334Z","shell.execute_reply":"2023-05-15T10:46:54.309181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# For validation set\nsns.countplot(y_val).set_title('Dist of the train set')\nplt.xlabel('Labels')\nplt.ylabel('Number of sample')\nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:46:54.311858Z","iopub.execute_input":"2023-05-15T10:46:54.313381Z","iopub.status.idle":"2023-05-15T10:46:54.578836Z","shell.execute_reply.started":"2023-05-15T10:46:54.313338Z","shell.execute_reply":"2023-05-15T10:46:54.577748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CNN model\n","metadata":{}},{"cell_type":"code","source":"X_train = X_train.reshape(-1,28,28,1)\nX_val = X_val.reshape(-1,28,28,1)\nprint(X_train.shape, X_val.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:46:54.580439Z","iopub.execute_input":"2023-05-15T10:46:54.583208Z","iopub.status.idle":"2023-05-15T10:46:54.589698Z","shell.execute_reply.started":"2023-05-15T10:46:54.583162Z","shell.execute_reply":"2023-05-15T10:46:54.588107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Change the form of the data in y_test into one-hot encoding\ny_train =  tf.keras.utils.to_categorical(y_train)\ny_val = tf.keras.utils.to_categorical(y_val)\ny_train","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:46:54.595108Z","iopub.execute_input":"2023-05-15T10:46:54.595591Z","iopub.status.idle":"2023-05-15T10:46:54.606874Z","shell.execute_reply.started":"2023-05-15T10:46:54.595553Z","shell.execute_reply":"2023-05-15T10:46:54.605816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 20\ncnn = Sequential()\ncnn.add(Conv2D(filters = 32, kernel_size = (3,3), padding = 'same', activation = 'relu', kernel_initializer = 'he_normal' , input_shape = (28, 28,1)))   \ncnn.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'same',kernel_initializer = 'he_normal', activation = 'relu'))\ncnn.add(MaxPooling2D())\ncnn.add(BatchNormalization())\n\ncnn.add(Conv2D(filters = 128, kernel_size = (3,3), padding = 'same', activation = 'relu', kernel_initializer = 'he_normal'))\ncnn.add(MaxPooling2D())\ncnn.add(BatchNormalization())\n\ncnn.add(Flatten())\n\n#layers.GlobalAveragePooling2D(),\ncnn.add(Dense(128, activation = 'relu'))\ncnn.add(Dense(64, activation = 'relu'))\ncnn.add(Dense(32, activation = 'relu'))\ncnn.add(Dense(10, activation = 'softmax'))\n\n\ncnn.compile(optimizer = 'adam', loss= 'categorical_crossentropy', metrics = ['accuracy'])\n\ncnn.fit(X_train, y_train, validation_data=(X_val, y_val), batch_size = 32, epochs = epochs, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:46:54.608525Z","iopub.execute_input":"2023-05-15T10:46:54.610048Z","iopub.status.idle":"2023-05-15T10:50:05.7179Z","shell.execute_reply.started":"2023-05-15T10:46:54.610012Z","shell.execute_reply":"2023-05-15T10:50:05.716708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:05.71975Z","iopub.execute_input":"2023-05-15T10:50:05.720496Z","iopub.status.idle":"2023-05-15T10:50:05.729188Z","shell.execute_reply.started":"2023-05-15T10:50:05.720457Z","shell.execute_reply":"2023-05-15T10:50:05.728199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loss of training and validation dataset\nplt.figure(figsize = (10, 12))\nnumOfepochs = np.arange(0, epochs)\nplt.plot(numOfepochs, cnn.history.history['accuracy'], label = 'Training accuracy')\nplt.plot(numOfepochs, cnn.history.history['val_accuracy'], label = 'Validation accuracy')\n\nplt.title('Accuracy')\nplt.xlabel('Epoch')\nplt.legend()\nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:05.730803Z","iopub.execute_input":"2023-05-15T10:50:05.731502Z","iopub.status.idle":"2023-05-15T10:50:06.035149Z","shell.execute_reply.started":"2023-05-15T10:50:05.731464Z","shell.execute_reply":"2023-05-15T10:50:06.034162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loss of training and validation dataset\nplt.figure(figsize = (10, 12))\nnumOfepochs = np.arange(0, epochs)\nplt.plot(numOfepochs, cnn.history.history['loss'], label = 'Training loss')\nplt.plot(numOfepochs, cnn.history.history['val_loss'], label = 'Validation loss')\n\nplt.title('Accuracy')\nplt.xlabel('Epoch')\nplt.legend()\nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:06.036792Z","iopub.execute_input":"2023-05-15T10:50:06.037488Z","iopub.status.idle":"2023-05-15T10:50:06.333701Z","shell.execute_reply.started":"2023-05-15T10:50:06.037442Z","shell.execute_reply":"2023-05-15T10:50:06.332699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.to_numpy()\ntype(test)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:06.335177Z","iopub.execute_input":"2023-05-15T10:50:06.336204Z","iopub.status.idle":"2023-05-15T10:50:06.344036Z","shell.execute_reply.started":"2023-05-15T10:50:06.336163Z","shell.execute_reply":"2023-05-15T10:50:06.342696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = np.reshape(test, (test.shape[0], 28, 28, 1))\ntest.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:06.345548Z","iopub.execute_input":"2023-05-15T10:50:06.346032Z","iopub.status.idle":"2023-05-15T10:50:06.356117Z","shell.execute_reply.started":"2023-05-15T10:50:06.34599Z","shell.execute_reply":"2023-05-15T10:50:06.354833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = cnn.predict(X_test)\ny_pred = np.argmax(y_pred, axis = 1)\ny_val = np.argmax(y_val, axis = 1)\naccuracy_score(y_test, y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:06.358126Z","iopub.execute_input":"2023-05-15T10:50:06.358537Z","iopub.status.idle":"2023-05-15T10:50:06.941803Z","shell.execute_reply.started":"2023-05-15T10:50:06.358478Z","shell.execute_reply":"2023-05-15T10:50:06.940797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot some random samples of ground truth vs predictions \n\nfig, axes = plt.subplots(ncols = 5, nrows = 4, sharex = True, sharey = True, figsize = (16,14))\nidx = 0\nfor i in np.random.choice(range(1000), 20):\n    idx += 1\n    plt.subplot(4,5,idx)\n    plt.imshow(X_test[i], cmap = 'gray')\n    plt.title(f'{y_test[i]} vs {y_pred[i]}')\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:06.943499Z","iopub.execute_input":"2023-05-15T10:50:06.943954Z","iopub.status.idle":"2023-05-15T10:50:08.942919Z","shell.execute_reply.started":"2023-05-15T10:50:06.943901Z","shell.execute_reply":"2023-05-15T10:50:08.941703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Total Number of wrong predictions\nwrong_num = (1 - accuracy_score(y_pred, y_test)) * 10000\nwrong_num","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:08.944485Z","iopub.execute_input":"2023-05-15T10:50:08.945526Z","iopub.status.idle":"2023-05-15T10:50:08.955296Z","shell.execute_reply.started":"2023-05-15T10:50:08.94547Z","shell.execute_reply":"2023-05-15T10:50:08.954132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Submissions","metadata":{}},{"cell_type":"code","source":"y_pred_test = cnn.predict(test)\ny_pred_test = np.argmax(y_pred_test, axis =1 )\ny_pred_test","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:08.957292Z","iopub.execute_input":"2023-05-15T10:50:08.958182Z","iopub.status.idle":"2023-05-15T10:50:12.066956Z","shell.execute_reply.started":"2023-05-15T10:50:08.958136Z","shell.execute_reply":"2023-05-15T10:50:12.06583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\ndf = pd.DataFrame({\n        'ImageId': sample['ImageId'],\n        'Label'  : y_pred_test\n})\ndf.to_csv('submission.csv')\ndf.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:12.068874Z","iopub.execute_input":"2023-05-15T10:50:12.069258Z","iopub.status.idle":"2023-05-15T10:50:12.148375Z","shell.execute_reply.started":"2023-05-15T10:50:12.06922Z","shell.execute_reply":"2023-05-15T10:50:12.147099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = pd.read_csv('/kaggle/working/submission.csv', index_col=[0])\npredict","metadata":{"execution":{"iopub.status.busy":"2023-05-15T10:50:12.150035Z","iopub.execute_input":"2023-05-15T10:50:12.150413Z","iopub.status.idle":"2023-05-15T10:50:12.176671Z","shell.execute_reply.started":"2023-05-15T10:50:12.150374Z","shell.execute_reply":"2023-05-15T10:50:12.175749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}