{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"},{"sourceId":16017,"databundleVersionId":708189,"sourceType":"competition"}],"dockerImageVersionId":30665,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"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)\n\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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-12T09:45:39.239669Z","iopub.execute_input":"2024-07-12T09:45:39.239976Z","iopub.status.idle":"2024-07-12T09:45:40.365252Z","shell.execute_reply.started":"2024-07-12T09:45:39.239904Z","shell.execute_reply":"2024-07-12T09:45:40.364301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:45:40.367173Z","iopub.execute_input":"2024-07-12T09:45:40.367671Z","iopub.status.idle":"2024-07-12T09:45:53.283897Z","shell.execute_reply.started":"2024-07-12T09:45:40.367635Z","shell.execute_reply":"2024-07-12T09:45:53.283116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntest = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:51:33.753609Z","iopub.execute_input":"2024-07-12T09:51:33.754038Z","iopub.status.idle":"2024-07-12T09:51:37.229555Z","shell.execute_reply.started":"2024-07-12T09:51:33.754006Z","shell.execute_reply":"2024-07-12T09:51:37.228667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Getting Know the Data\n\nWe need to know about the shape of the data and how the data can be visualized etc.\nIt would give us better insights.","metadata":{}},{"cell_type":"code","source":"print(train.shape)\n#print(train.max(axis=1))\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:49:34.622660Z","iopub.execute_input":"2024-07-12T09:49:34.623497Z","iopub.status.idle":"2024-07-12T09:49:34.627932Z","shell.execute_reply.started":"2024-07-12T09:49:34.623466Z","shell.execute_reply":"2024-07-12T09:49:34.627042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's drop the labels\ny = train['label']\ntrain.drop(\"label\", inplace=True, axis=\"columns\")\ntest.drop(\"id\", inplace=True, axis=\"columns\")","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:51:38.690412Z","iopub.execute_input":"2024-07-12T09:51:38.690771Z","iopub.status.idle":"2024-07-12T09:51:38.812949Z","shell.execute_reply.started":"2024-07-12T09:51:38.690742Z","shell.execute_reply":"2024-07-12T09:51:38.812127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample1 = np.reshape(train.iloc[2, :].values, (28, 28))\nplt.imshow(sample1)\nplt.show()\nsample2 = np.reshape(train.iloc[3, :].values, (28, 28))\nplt.imshow(sample2)","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:45:58.507756Z","iopub.execute_input":"2024-07-12T09:45:58.508058Z","iopub.status.idle":"2024-07-12T09:45:59.170184Z","shell.execute_reply.started":"2024-07-12T09:45:58.508034Z","shell.execute_reply":"2024-07-12T09:45:59.169332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preprocessing the data\n\nWe have several steps for preprocessing the data:\n1. We know that the images are _28x28_ but they are given as a **1D arrays**. \n   We have to convert them into **2D matrices**.\n2. From the output of the pixel data (maximum value of the pixels), we can see that the maximum value is **255**. \n   For better results, we will normalize the data by dividing all values by the maximum (255).\n3. We will use a channel dimension, therefore we will expand the dimensions by one (shape-channel order).\n4. We will split the training data for cross-validation.","metadata":{}},{"cell_type":"code","source":"# Images are 28x28\nX = train.values.reshape(-1,28,28,1)\nX_test = test.values.reshape(-1,28,28,1)\n\nX = X / 255.0\nX_test = X_test / 255.0\n\nX = np.expand_dims(X, axis=-1)\nX_test = np.expand_dims(X_test, axis=-1)\n\nX_train, X_val, y_train, y_val = train_test_split(X, y, train_size=0.8, random_state=42)\n\nprint(f\"No of data train: {X_train.shape[0]}\")\nprint(f\"No of data val: {X_val.shape[0]}\")\nprint(f\"No of data test: {X_test.shape[0]}\")","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:51:42.501538Z","iopub.execute_input":"2024-07-12T09:51:42.501908Z","iopub.status.idle":"2024-07-12T09:51:43.208903Z","shell.execute_reply.started":"2024-07-12T09:51:42.501877Z","shell.execute_reply":"2024-07-12T09:51:43.207944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Building the model\n\nThe model is in the below, using convolutional and dense layers. ","metadata":{}},{"cell_type":"code","source":"def build_model(input_shape=(28, 28, 1)):\n    model = tf.keras.Sequential(\n            layers=[tf.keras.layers.Input(shape=input_shape),\n                    tf.keras.layers.Conv2D(32, 3, activation='relu', data_format=\"channels_last\"),\n                    tf.keras.layers.Conv2D(32, 3, activation='relu', data_format=\"channels_last\"),\n                    tf.keras.layers.MaxPooling2D(),\n                    tf.keras.layers.Dropout(0.25),\n                    tf.keras.layers.Conv2D(32, 3, activation='relu', data_format=\"channels_last\"),\n                    tf.keras.layers.Conv2D(32, 3, activation='relu', data_format=\"channels_last\"),\n                    tf.keras.layers.MaxPooling2D(),\n                    tf.keras.layers.Dropout(0.25),\n                    tf.keras.layers.Flatten(),\n                    tf.keras.layers.Dense(32, activation='relu'),\n                    tf.keras.layers.Dense(64, activation='relu'),\n                    tf.keras.layers.Dropout(0.25),\n                    tf.keras.layers.Dense(128, activation='relu'),\n                    tf.keras.layers.Dense(10, activation='softmax'),\n                   ]\n            )\n    \n    model.build()\n    model.summary()\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:51:45.730518Z","iopub.execute_input":"2024-07-12T09:51:45.730895Z","iopub.status.idle":"2024-07-12T09:51:45.739953Z","shell.execute_reply.started":"2024-07-12T09:51:45.730864Z","shell.execute_reply":"2024-07-12T09:51:45.738966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = build_model()\n\nmodel.compile(optimizer=tf.keras.optimizers.Adam(), loss=\"sparse_categorical_crossentropy\", metrics=[\"acc\"])\n\ncallbacks = tf.keras.callbacks.ReduceLROnPlateau(monitor=\"val_acc\", factor=0.5, patience=5, min_lr=1e-3)\n\nhist = model.fit(X_train, y_train, validation_data = (X_val, y_val), epochs=20, callbacks=[callbacks])","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:51:47.877764Z","iopub.execute_input":"2024-07-12T09:51:47.879033Z","iopub.status.idle":"2024-07-12T09:53:29.832449Z","shell.execute_reply.started":"2024-07-12T09:51:47.878978Z","shell.execute_reply":"2024-07-12T09:53:29.831660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(X_test)\nres = np.argmax(pred, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:53:29.834147Z","iopub.execute_input":"2024-07-12T09:53:29.834433Z","iopub.status.idle":"2024-07-12T09:53:31.556070Z","shell.execute_reply.started":"2024-07-12T09:53:29.834407Z","shell.execute_reply":"2024-07-12T09:53:31.555293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = pd.Series(res, name=\"Label\")\nsubmission = pd.concat([pd.Series(range(1, X_test.shape[0] + 1), name=\"id\"), results], axis=1)\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:53:44.534839Z","iopub.execute_input":"2024-07-12T09:53:44.535740Z","iopub.status.idle":"2024-07-12T09:53:44.548257Z","shell.execute_reply.started":"2024-07-12T09:53:44.535698Z","shell.execute_reply":"2024-07-12T09:53:44.547376Z"},"trusted":true},"execution_count":null,"outputs":[]}]}