{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30380,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<h1 style=\"font-family:comic sans ms; text-align:center; color:red\"> MNIST Digit Classification using CNN","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:25:45.633545Z","iopub.execute_input":"2025-01-23T10:25:45.634128Z","iopub.status.idle":"2025-01-23T10:25:45.682670Z","shell.execute_reply.started":"2025-01-23T10:25:45.633989Z","shell.execute_reply":"2025-01-23T10:25:45.680770Z"}}},{"cell_type":"markdown","source":"Introduction to CNN and MaxPooling.\n\nDataset: MNIST Digit \n\nTraining examples = 42000\n\nTest examples = 28000","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2025-01-23T10:27:33.052743Z","iopub.execute_input":"2025-01-23T10:27:33.053287Z","iopub.status.idle":"2025-01-23T10:27:42.476097Z","shell.execute_reply.started":"2025-01-23T10:27:33.053239Z","shell.execute_reply":"2025-01-23T10:27:42.474836Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nX_train.head()","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:27:42.478425Z","iopub.execute_input":"2025-01-23T10:27:42.479054Z","iopub.status.idle":"2025-01-23T10:27:46.239792Z","shell.execute_reply.started":"2025-01-23T10:27:42.479017Z","shell.execute_reply":"2025-01-23T10:27:46.238485Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Y_train = X_train[\"label\"]\nX_train = X_train.drop(labels = ['label'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:27:46.241196Z","iopub.execute_input":"2025-01-23T10:27:46.241548Z","iopub.status.idle":"2025-01-23T10:27:46.369342Z","shell.execute_reply.started":"2025-01-23T10:27:46.241508Z","shell.execute_reply":"2025-01-23T10:27:46.367603Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Converting pandas dataframe to numpy array**","metadata":{}},{"cell_type":"code","source":"X_train = np.array(X_train, dtype='float32')\nY_train = np.array(Y_train, dtype='float32')\nX_train.shape, Y_train.shape","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:27:46.371230Z","iopub.execute_input":"2025-01-23T10:27:46.371647Z","iopub.status.idle":"2025-01-23T10:27:46.556828Z","shell.execute_reply.started":"2025-01-23T10:27:46.371586Z","shell.execute_reply":"2025-01-23T10:27:46.555199Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nX_test.head()","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:27:46.559930Z","iopub.execute_input":"2025-01-23T10:27:46.560297Z","iopub.status.idle":"2025-01-23T10:27:48.551864Z","shell.execute_reply.started":"2025-01-23T10:27:46.560265Z","shell.execute_reply":"2025-01-23T10:27:48.550393Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test = np.array(X_test, dtype='float32')\nX_test.shape","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:27:48.553257Z","iopub.execute_input":"2025-01-23T10:27:48.553645Z","iopub.status.idle":"2025-01-23T10:27:48.649309Z","shell.execute_reply.started":"2025-01-23T10:27:48.553590Z","shell.execute_reply":"2025-01-23T10:27:48.647764Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Display image from training data**","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef show_image(image):\n    image1 = image.reshape((28,28))\n    plt.imshow(image1)\n    \ni = 40\nprint(f'The label of the image is {Y_train[i]}')\nshow_image(X_train[i])","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:27:48.651345Z","iopub.execute_input":"2025-01-23T10:27:48.652479Z","iopub.status.idle":"2025-01-23T10:27:48.906267Z","shell.execute_reply.started":"2025-01-23T10:27:48.652435Z","shell.execute_reply":"2025-01-23T10:27:48.904498Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Reshape data**","metadata":{}},{"cell_type":"code","source":"X_train = X_train.reshape((-1,28,28,1))\nX_test = X_test.reshape((-1,28,28,1))\nX_train.shape, X_test.shape","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:27:48.907977Z","iopub.execute_input":"2025-01-23T10:27:48.908318Z","iopub.status.idle":"2025-01-23T10:27:48.918659Z","shell.execute_reply.started":"2025-01-23T10:27:48.908287Z","shell.execute_reply":"2025-01-23T10:27:48.917060Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Normalization: It reduces the training time.**","metadata":{}},{"cell_type":"code","source":"X_train = X_train / 255.0\nX_test = X_test / 255.0","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:27:48.920364Z","iopub.execute_input":"2025-01-23T10:27:48.920910Z","iopub.status.idle":"2025-01-23T10:27:49.020108Z","shell.execute_reply.started":"2025-01-23T10:27:48.920859Z","shell.execute_reply":"2025-01-23T10:27:49.018733Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"One hot encoding of labels\n* 2 -> [0 0 1 0 0 0 0 0 0 0]\n* 5 -> [0 0 0 0 0 1 0 0 0 0]","metadata":{}},{"cell_type":"code","source":"from keras.utils.np_utils import to_categorical\nY_train = to_categorical(Y_train, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:27:49.022352Z","iopub.execute_input":"2025-01-23T10:27:49.022939Z","iopub.status.idle":"2025-01-23T10:27:49.031769Z","shell.execute_reply.started":"2025-01-23T10:27:49.022892Z","shell.execute_reply":"2025-01-23T10:27:49.029981Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Creating validation data from training data to check loss and accuracy during training.\n* Training set = 90% (37800 instances)\n* Validation set = 10% (4200 instances)","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size=0.1, random_state=2)","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:27:49.033351Z","iopub.execute_input":"2025-01-23T10:27:49.033758Z","iopub.status.idle":"2025-01-23T10:27:49.990735Z","shell.execute_reply.started":"2025-01-23T10:27:49.033713Z","shell.execute_reply":"2025-01-23T10:27:49.989269Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape, X_val.shape","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:27:49.991957Z","iopub.execute_input":"2025-01-23T10:27:49.992315Z","iopub.status.idle":"2025-01-23T10:27:50.000488Z","shell.execute_reply.started":"2025-01-23T10:27:49.992279Z","shell.execute_reply":"2025-01-23T10:27:49.999101Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Building a Model\n* Creating simple model of Convolution and Max Pooling layers.\n* Filter size will increase in each layer.\n* 'softmax' activation is used in last layer to give probability for each class","metadata":{}},{"cell_type":"code","source":"inputs = keras.Input(shape=(28,28,1))\nx = layers.Conv2D(filters=32, kernel_size=3, activation='relu')(inputs)\nx = layers.MaxPooling2D(pool_size=(2,2))(x)\nx = layers.Conv2D(filters = 64, kernel_size=3, activation='relu')(x)\nx = layers.MaxPooling2D(pool_size=(2,2))(x)\nx = layers.Conv2D(filters=128, kernel_size=3, activation='relu')(x)\nx = layers.Flatten()(x)\noutputs = layers.Dense(10, activation='softmax')(x)\nmodel = keras.Model(inputs=inputs, outputs=outputs)","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:27:50.002246Z","iopub.execute_input":"2025-01-23T10:27:50.002575Z","iopub.status.idle":"2025-01-23T10:27:50.181518Z","shell.execute_reply.started":"2025-01-23T10:27:50.002544Z","shell.execute_reply":"2025-01-23T10:27:50.180148Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:27:50.185390Z","iopub.execute_input":"2025-01-23T10:27:50.186037Z","iopub.status.idle":"2025-01-23T10:27:50.194125Z","shell.execute_reply.started":"2025-01-23T10:27:50.185984Z","shell.execute_reply":"2025-01-23T10:27:50.192495Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Using 'categorical_crossentropy' loss because labels are one-hot encoded and it is a multi-class problem.","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer = 'rmsprop', loss = 'categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:27:50.195483Z","iopub.execute_input":"2025-01-23T10:27:50.196070Z","iopub.status.idle":"2025-01-23T10:27:50.223345Z","shell.execute_reply.started":"2025-01-23T10:27:50.196024Z","shell.execute_reply":"2025-01-23T10:27:50.221770Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<h3 style=\"font-family:comic sans ms; text-align:center; color:cyan\"> Model Training","metadata":{}},{"cell_type":"code","source":"history = model.fit(X_train, Y_train, batch_size=64, epochs=10, validation_data=(X_val, Y_val))","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:27:50.225053Z","iopub.execute_input":"2025-01-23T10:27:50.225551Z","iopub.status.idle":"2025-01-23T10:30:34.711216Z","shell.execute_reply.started":"2025-01-23T10:27:50.225501Z","shell.execute_reply":"2025-01-23T10:30:34.710038Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_dict = history.history\nhistory_dict.keys()","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:30:34.713081Z","iopub.execute_input":"2025-01-23T10:30:34.713405Z","iopub.status.idle":"2025-01-23T10:30:34.720156Z","shell.execute_reply.started":"2025-01-23T10:30:34.713375Z","shell.execute_reply":"2025-01-23T10:30:34.719114Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Plot of Training loss, validation loss vs Epochs","metadata":{}},{"cell_type":"code","source":"plt.plot(range(10), history_dict['loss'], 'b-')\nplt.plot(range(10), history_dict['val_loss'], 'g--')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend(['Training loss', 'Validation loss'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:30:34.722143Z","iopub.execute_input":"2025-01-23T10:30:34.722567Z","iopub.status.idle":"2025-01-23T10:30:34.880909Z","shell.execute_reply.started":"2025-01-23T10:30:34.722517Z","shell.execute_reply":"2025-01-23T10:30:34.879833Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Plot of Training accuracy, validation accuracy vs Epochs**","metadata":{}},{"cell_type":"code","source":"plt.plot(range(10), history_dict['accuracy'], 'r-')\nplt.plot(range(10), history_dict['val_accuracy'], 'm--')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend(['Training accuracy', 'Validation accuracy'], loc='lower right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:30:34.882480Z","iopub.execute_input":"2025-01-23T10:30:34.883319Z","iopub.status.idle":"2025-01-23T10:30:35.094174Z","shell.execute_reply.started":"2025-01-23T10:30:34.883273Z","shell.execute_reply":"2025-01-23T10:30:35.092903Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = model.predict(X_test)\nresults = np.argmax(results, axis=1)\nresults = pd.Series(results, name='Label')","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:30:35.096389Z","iopub.execute_input":"2025-01-23T10:30:35.096960Z","iopub.status.idle":"2025-01-23T10:30:39.127696Z","shell.execute_reply.started":"2025-01-23T10:30:35.096916Z","shell.execute_reply":"2025-01-23T10:30:39.126399Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Creating submission file**","metadata":{}},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\nsubmission.to_csv(\"cnn_mnist.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2025-01-23T10:30:39.129067Z","iopub.execute_input":"2025-01-23T10:30:39.129402Z","iopub.status.idle":"2025-01-23T10:30:39.170376Z","shell.execute_reply.started":"2025-01-23T10:30:39.129370Z","shell.execute_reply":"2025-01-23T10:30:39.169327Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<h2 style=\"font-family:comic sans ms; background-color:lightblue; text-align:center; color:green\"> Thanks, Please Upvote","metadata":{}}]}