{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<h1 style=\"font-size:36px\">Digit Recognizer using CNN.</h1>","metadata":{}},{"cell_type":"markdown","source":"<strong style=\"font-size:20px\">PREREQUISITES</strong>","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\nfrom tensorflow.keras import layers,models,Input,preprocessing \nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:24:08.000371Z","iopub.execute_input":"2024-05-23T11:24:08.001074Z","iopub.status.idle":"2024-05-23T11:24:08.009930Z","shell.execute_reply.started":"2024-05-23T11:24:08.001039Z","shell.execute_reply":"2024-05-23T11:24:08.008911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"<strong><em style=\"font-size:18px\">1. LOAD DATASET</em></strong>","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nprint(train.shape)\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:24:08.148319Z","iopub.execute_input":"2024-05-23T11:24:08.148690Z","iopub.status.idle":"2024-05-23T11:24:14.804314Z","shell.execute_reply.started":"2024-05-23T11:24:08.148662Z","shell.execute_reply":"2024-05-23T11:24:14.803328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = train.copy()\ndf1","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:24:14.805937Z","iopub.execute_input":"2024-05-23T11:24:14.806237Z","iopub.status.idle":"2024-05-23T11:24:14.938189Z","shell.execute_reply.started":"2024-05-23T11:24:14.806192Z","shell.execute_reply":"2024-05-23T11:24:14.937196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_df1 = df1.pop('label')","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:24:14.939415Z","iopub.execute_input":"2024-05-23T11:24:14.939715Z","iopub.status.idle":"2024-05-23T11:24:14.976455Z","shell.execute_reply.started":"2024-05-23T11:24:14.939689Z","shell.execute_reply":"2024-05-23T11:24:14.975685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<strong><em style=\"font-size:18px\">2. PREPROCESSING</em></strong>","metadata":{}},{"cell_type":"code","source":"df1 = df1.values.reshape(-1,28,28,1)/255\ny_df1 = y_df1.values","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:24:14.978471Z","iopub.execute_input":"2024-05-23T11:24:14.978799Z","iopub.status.idle":"2024-05-23T11:24:15.173747Z","shell.execute_reply.started":"2024-05-23T11:24:14.978772Z","shell.execute_reply":"2024-05-23T11:24:15.172918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:24:15.174875Z","iopub.execute_input":"2024-05-23T11:24:15.175227Z","iopub.status.idle":"2024-05-23T11:24:15.181006Z","shell.execute_reply.started":"2024-05-23T11:24:15.175178Z","shell.execute_reply":"2024-05-23T11:24:15.180085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_valid,y_train,y_valid = train_test_split(df1,y_df1)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:24:15.182143Z","iopub.execute_input":"2024-05-23T11:24:15.182453Z","iopub.status.idle":"2024-05-23T11:24:15.581783Z","shell.execute_reply.started":"2024-05-23T11:24:15.182429Z","shell.execute_reply":"2024-05-23T11:24:15.580963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<strong><em style=\"font-size:18px\">3. MODEL ARCHITECHTURE</em></strong>","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    layers.InputLayer(input_shape=[28,28,1]),\n    layers.Conv2D(filters=16,kernel_size=3,activation='relu',padding='same'),\n    layers.MaxPool2D(padding='same'),\n    layers.Conv2D(filters=32,kernel_size=3,activation='relu',padding='same'),\n    layers.MaxPool2D(padding='same'),\n    layers.Conv2D(filters=32,kernel_size=3,activation='relu', padding='same'),\n    layers.Conv2D(filters=32,kernel_size=3,activation='relu', padding='same'),\n    layers.MaxPool2D(padding='same'),\n    layers.Dropout(0.5),\n    layers.Flatten(),\n    layers.Dense(128,activation='relu'),\n    layers.Dropout(0.3),\n    layers.Dense(10,activation='softmax')   \n])","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:26:24.389763Z","iopub.execute_input":"2024-05-23T11:26:24.390818Z","iopub.status.idle":"2024-05-23T11:26:24.452855Z","shell.execute_reply.started":"2024-05-23T11:26:24.390779Z","shell.execute_reply":"2024-05-23T11:26:24.451930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:26:25.727926Z","iopub.execute_input":"2024-05-23T11:26:25.728261Z","iopub.status.idle":"2024-05-23T11:26:25.752931Z","shell.execute_reply.started":"2024-05-23T11:26:25.728235Z","shell.execute_reply":"2024-05-23T11:26:25.752080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<strong><em style=\"font-size:18px\">4. TRAINING + RESULTS</em></strong>","metadata":{}},{"cell_type":"code","source":"optimizer = tf.keras.optimizers.Adam()\nmodel.compile(\n    optimizer=optimizer,\n    loss='sparse_categorical_crossentropy',\n    metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:26:27.114171Z","iopub.execute_input":"2024-05-23T11:26:27.115153Z","iopub.status.idle":"2024-05-23T11:26:27.125783Z","shell.execute_reply.started":"2024-05-23T11:26:27.115112Z","shell.execute_reply":"2024-05-23T11:26:27.124675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train,y_train,\n          epochs=40,\n          validation_data=(X_valid,y_valid),\n          )","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:26:27.809378Z","iopub.execute_input":"2024-05-23T11:26:27.809855Z","iopub.status.idle":"2024-05-23T11:28:34.010303Z","shell.execute_reply.started":"2024-05-23T11:26:27.809826Z","shell.execute_reply":"2024-05-23T11:28:34.009474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<strong><em style=\"font-size:18px\">5. RESULTING PLOT</em></strong>","metadata":{}},{"cell_type":"code","source":"history_frame = pd.DataFrame(history.history)\nhistory_frame.loc[:,['loss','val_loss']].plot()\nhistory_frame.loc[:,['accuracy','val_accuracy']].plot()","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:28:34.012499Z","iopub.execute_input":"2024-05-23T11:28:34.012939Z","iopub.status.idle":"2024-05-23T11:28:34.629845Z","shell.execute_reply.started":"2024-05-23T11:28:34.012898Z","shell.execute_reply":"2024-05-23T11:28:34.628941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<strong><em style=\"font-size:18px\">5. TEST PREPROCESSING + Model Prediction</em></strong>","metadata":{}},{"cell_type":"code","source":"test1 = test.values.reshape(-1,28,28,1)/255","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:26:10.488615Z","iopub.execute_input":"2024-05-23T11:26:10.488993Z","iopub.status.idle":"2024-05-23T11:26:10.559559Z","shell.execute_reply.started":"2024-05-23T11:26:10.488957Z","shell.execute_reply":"2024-05-23T11:26:10.558531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_preds = model.predict(test1)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:26:10.560800Z","iopub.execute_input":"2024-05-23T11:26:10.561110Z","iopub.status.idle":"2024-05-23T11:26:12.848773Z","shell.execute_reply.started":"2024-05-23T11:26:10.561085Z","shell.execute_reply":"2024-05-23T11:26:12.847945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_labels = np.argmax(cnn_preds,axis=1)\npredicted_labels.reshape(28000,1).shape","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:26:12.850170Z","iopub.execute_input":"2024-05-23T11:26:12.850549Z","iopub.status.idle":"2024-05-23T11:26:12.858025Z","shell.execute_reply.started":"2024-05-23T11:26:12.850517Z","shell.execute_reply":"2024-05-23T11:26:12.857083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<strong><em style=\"font-size:18px\">5. Submission</em></strong>","metadata":{}},{"cell_type":"code","source":"ids = np.arange(1,28001)\nids.reshape(28000,1)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:26:12.859219Z","iopub.execute_input":"2024-05-23T11:26:12.859669Z","iopub.status.idle":"2024-05-23T11:26:12.868289Z","shell.execute_reply.started":"2024-05-23T11:26:12.859638Z","shell.execute_reply":"2024-05-23T11:26:12.867398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({\n    'ImageId':ids,\n    'Label':predicted_labels\n})","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:26:12.869433Z","iopub.execute_input":"2024-05-23T11:26:12.869711Z","iopub.status.idle":"2024-05-23T11:26:12.878105Z","shell.execute_reply.started":"2024-05-23T11:26:12.869687Z","shell.execute_reply":"2024-05-23T11:26:12.877336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:26:12.879203Z","iopub.execute_input":"2024-05-23T11:26:12.879536Z","iopub.status.idle":"2024-05-23T11:26:12.923998Z","shell.execute_reply.started":"2024-05-23T11:26:12.879506Z","shell.execute_reply":"2024-05-23T11:26:12.923318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}