{"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":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#Importing libraries \nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras import layers\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix,classification_report\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-16T16:37:09.912027Z","iopub.execute_input":"2024-08-16T16:37:09.912573Z","iopub.status.idle":"2024-08-16T16:37:09.920794Z","shell.execute_reply.started":"2024-08-16T16:37:09.912517Z","shell.execute_reply":"2024-08-16T16:37:09.918993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loading the data\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntrain_d = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-08-16T16:37:09.922694Z","iopub.execute_input":"2024-08-16T16:37:09.923067Z","iopub.status.idle":"2024-08-16T16:37:15.019895Z","shell.execute_reply.started":"2024-08-16T16:37:09.923038Z","shell.execute_reply":"2024-08-16T16:37:15.019013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_sample(l_data, index):\n    data = l_data.drop('label', axis = 1)\n    image = data.iloc[index].to_numpy()\n    image = image.reshape((28,28))\n    plt.figure(figsize=(3,3))\n    plt.imshow(image,cmap='gray')\n    plt.title(l_data.iloc[index, 0])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-16T16:37:15.021123Z","iopub.execute_input":"2024-08-16T16:37:15.021454Z","iopub.status.idle":"2024-08-16T16:37:15.027999Z","shell.execute_reply.started":"2024-08-16T16:37:15.021425Z","shell.execute_reply":"2024-08-16T16:37:15.026826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(10):    \n    image_sample(train_d,i)","metadata":{"execution":{"iopub.status.busy":"2024-08-16T16:37:15.030418Z","iopub.execute_input":"2024-08-16T16:37:15.030758Z","iopub.status.idle":"2024-08-16T16:37:18.391997Z","shell.execute_reply.started":"2024-08-16T16:37:15.030729Z","shell.execute_reply":"2024-08-16T16:37:18.390893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Image normalization\ny_train = train_d['label']\nx_train = train_d.drop('label', axis = 1).values / 255.0\ntest = test.values / 255.0\n\nnp.shape(test)","metadata":{"execution":{"iopub.status.busy":"2024-08-16T16:37:18.393657Z","iopub.execute_input":"2024-08-16T16:37:18.394438Z","iopub.status.idle":"2024-08-16T16:37:18.672327Z","shell.execute_reply.started":"2024-08-16T16:37:18.394398Z","shell.execute_reply":"2024-08-16T16:37:18.671127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train test split\nX_train, X_test, Y_train, Y_test = train_test_split(x_train, y_train, test_size = 0.2, random_state = 42)\n\nX_train = X_train.reshape(-1, 28, 28)\nX_test = X_test.reshape(-1, 28, 28)\ntest = test.reshape(-1, 28, 28)\nnp.shape(test)","metadata":{"execution":{"iopub.status.busy":"2024-08-16T16:37:18.673819Z","iopub.execute_input":"2024-08-16T16:37:18.674248Z","iopub.status.idle":"2024-08-16T16:37:19.103015Z","shell.execute_reply.started":"2024-08-16T16:37:18.674211Z","shell.execute_reply":"2024-08-16T16:37:19.101858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#This function will exponentially slow down the learning rating after\n#10 epochs to prevent overfitting in the latter stages\n\ndef scheduler(epoch, lr):\n    if epoch < 10:\n        return lr\n    else:\n        return lr * np.exp(-0.1)","metadata":{"execution":{"iopub.status.busy":"2024-08-16T16:37:19.104401Z","iopub.execute_input":"2024-08-16T16:37:19.104734Z","iopub.status.idle":"2024-08-16T16:37:19.110179Z","shell.execute_reply.started":"2024-08-16T16:37:19.104706Z","shell.execute_reply":"2024-08-16T16:37:19.109145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building a model \nmodel = tf.keras.Sequential()\n# Input layer\nmodel.add(tf.keras.Input(shape=(28, 28, 1)))\nmodel.add(layers.RandomRotation(0.25))\nmodel.add(layers.Conv2D(filters = 16,\n                kernel_size = (5, 5),\n                activation = \"relu\"))\nmodel.add(layers.Dropout(rate=0.2))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(filters = 32,\n                kernel_size = (2, 2),\n                activation = \"relu\"))\nmodel.add(layers.Dropout(rate=0.2))\nmodel.add(layers.Conv2D(filters = 32,\n                kernel_size = (2, 2),\n                activation = \"relu\"))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(128, activation = 'relu'))\n#Output layer\nmodel.add(layers.Dense(10, activation = \"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2024-08-16T16:37:19.111783Z","iopub.execute_input":"2024-08-16T16:37:19.112121Z","iopub.status.idle":"2024-08-16T16:37:19.278836Z","shell.execute_reply.started":"2024-08-16T16:37:19.112093Z","shell.execute_reply":"2024-08-16T16:37:19.277735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback = keras.callbacks.LearningRateScheduler(scheduler)\nmodel.summary()\nmodel.compile(optimizer = \"Adam\",\n             loss = \"sparse_categorical_crossentropy\",\n             metrics = [\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2024-08-16T16:37:19.280340Z","iopub.execute_input":"2024-08-16T16:37:19.280688Z","iopub.status.idle":"2024-08-16T16:37:19.315676Z","shell.execute_reply.started":"2024-08-16T16:37:19.280659Z","shell.execute_reply":"2024-08-16T16:37:19.314779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, Y_train, epochs = 30, verbose = 1, validation_data = (X_test, Y_test), validation_split=0.1, callbacks=[callback])","metadata":{"execution":{"iopub.status.busy":"2024-08-16T16:37:19.319361Z","iopub.execute_input":"2024-08-16T16:37:19.320240Z","iopub.status.idle":"2024-08-16T16:40:26.329047Z","shell.execute_reply.started":"2024-08-16T16:37:19.320201Z","shell.execute_reply":"2024-08-16T16:40:26.328106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure()\nplt1 = fig.add_subplot(2, 1, 1)\nplt1.plot(history.history['accuracy'])\nplt1.plot(history.history['val_accuracy'])\nplt1.set_title('model accuracy')\nplt1.set_ylabel('accuracy')\nplt1.set_xlabel('epoch')\nplt1.legend(['train', 'validation'], loc='upper left')\nplt.subplots_adjust(hspace=0.5)\n\nplt2 = fig.add_subplot(2, 1, 2)\nplt2.plot(history.history['loss'])\nplt2.plot(history.history['val_loss'])\nplt2.set_title('model loss')\nplt2.set_ylabel('loss')\nplt2.set_xlabel('epoch')\nplt2.legend(['train', 'validation'], loc='upper left')","metadata":{"execution":{"iopub.status.busy":"2024-08-16T16:40:26.330566Z","iopub.execute_input":"2024-08-16T16:40:26.330872Z","iopub.status.idle":"2024-08-16T16:40:26.925441Z","shell.execute_reply.started":"2024-08-16T16:40:26.330845Z","shell.execute_reply":"2024-08-16T16:40:26.924430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(test)\nprediction = np.argmax(prediction, axis = 1)\nprint(prediction)","metadata":{"execution":{"iopub.status.busy":"2024-08-16T16:40:26.926791Z","iopub.execute_input":"2024-08-16T16:40:26.927332Z","iopub.status.idle":"2024-08-16T16:40:29.215111Z","shell.execute_reply.started":"2024-08-16T16:40:26.927295Z","shell.execute_reply":"2024-08-16T16:40:29.214025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'ImageId': range(1, len(prediction) + 1),\n    'Label': prediction\n})\n\nsubmission.to_csv('submission.csv', index=False)\nprint(f\"File saved as: {os.path.abspath('submission.csv')}\")\nprint(submission.head())","metadata":{"execution":{"iopub.status.busy":"2024-08-16T16:40:29.216434Z","iopub.execute_input":"2024-08-16T16:40:29.216775Z","iopub.status.idle":"2024-08-16T16:40:29.269031Z","shell.execute_reply.started":"2024-08-16T16:40:29.216746Z","shell.execute_reply":"2024-08-16T16:40:29.267917Z"},"trusted":true},"execution_count":null,"outputs":[]}]}