{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.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":16017,"databundleVersionId":708189,"sourceType":"competition"}],"dockerImageVersionId":30635,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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\nimport keras \nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, Dense, MaxPool2D, Dropout, Flatten\nfrom keras.optimizers import Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport seaborn as sns\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-01-21T16:29:31.996211Z","iopub.execute_input":"2024-01-21T16:29:31.996586Z","iopub.status.idle":"2024-01-21T16:29:46.431028Z","shell.execute_reply.started":"2024-01-21T16:29:31.996556Z","shell.execute_reply":"2024-01-21T16:29:46.430020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\nX_train = df_train.iloc[:, 1:]\nY_train = df_train.iloc[:, 0]","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:29:49.972538Z","iopub.execute_input":"2024-01-21T16:29:49.973446Z","iopub.status.idle":"2024-01-21T16:29:56.063393Z","shell.execute_reply.started":"2024-01-21T16:29:49.973393Z","shell.execute_reply":"2024-01-21T16:29:56.062508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:30:10.704884Z","iopub.execute_input":"2024-01-21T16:30:10.705941Z","iopub.status.idle":"2024-01-21T16:30:10.739460Z","shell.execute_reply.started":"2024-01-21T16:30:10.705902Z","shell.execute_reply":"2024-01-21T16:30:10.738327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:30:18.340107Z","iopub.execute_input":"2024-01-21T16:30:18.340580Z","iopub.status.idle":"2024-01-21T16:30:18.349109Z","shell.execute_reply.started":"2024-01-21T16:30:18.340543Z","shell.execute_reply":"2024-01-21T16:30:18.347930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.array(X_train)\nY_train = np.array(Y_train)","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:30:20.468528Z","iopub.execute_input":"2024-01-21T16:30:20.468915Z","iopub.status.idle":"2024-01-21T16:30:20.654258Z","shell.execute_reply.started":"2024-01-21T16:30:20.468885Z","shell.execute_reply":"2024-01-21T16:30:20.652892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize inputs\nX_train = X_train / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:30:23.396091Z","iopub.execute_input":"2024-01-21T16:30:23.397220Z","iopub.status.idle":"2024-01-21T16:30:23.613929Z","shell.execute_reply.started":"2024-01-21T16:30:23.397145Z","shell.execute_reply":"2024-01-21T16:30:23.612695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_digits(X, Y):\n    for i in range(20):\n        plt.subplot(5, 4, i+1)\n        plt.tight_layout()\n        plt.imshow(X[i].reshape(28, 28), cmap='gray')\n        plt.title('Digit:{}'.format(Y[i]))\n        plt.xticks([])\n        plt.yticks([])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:30:27.772380Z","iopub.execute_input":"2024-01-21T16:30:27.772776Z","iopub.status.idle":"2024-01-21T16:30:27.781046Z","shell.execute_reply.started":"2024-01-21T16:30:27.772744Z","shell.execute_reply":"2024-01-21T16:30:27.779852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_digits(X_train, Y_train)","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:30:31.724454Z","iopub.execute_input":"2024-01-21T16:30:31.724852Z","iopub.status.idle":"2024-01-21T16:30:34.462041Z","shell.execute_reply.started":"2024-01-21T16:30:31.724821Z","shell.execute_reply":"2024-01-21T16:30:34.460978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Train-Test Split\nX_dev, X_val, Y_dev, Y_val = train_test_split(X_train, Y_train, test_size=0.03, shuffle=True, random_state=2019)\nT_dev = pd.get_dummies(Y_dev).values\nT_val = pd.get_dummies(Y_val).values","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:30:37.620432Z","iopub.execute_input":"2024-01-21T16:30:37.621920Z","iopub.status.idle":"2024-01-21T16:30:38.214495Z","shell.execute_reply.started":"2024-01-21T16:30:37.621863Z","shell.execute_reply":"2024-01-21T16:30:38.213272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reshape the input \nX_dev = X_dev.reshape(X_dev.shape[0], 28, 28, 1)\nX_val = X_val.reshape(X_val.shape[0], 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:30:41.900296Z","iopub.execute_input":"2024-01-21T16:30:41.900709Z","iopub.status.idle":"2024-01-21T16:30:41.906484Z","shell.execute_reply.started":"2024-01-21T16:30:41.900669Z","shell.execute_reply":"2024-01-21T16:30:41.905280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters=32, kernel_size=(5,5), padding='same', activation='relu', input_shape=(28, 28, 1)))\nmodel.add(MaxPool2D(strides=2))\nmodel.add(Conv2D(filters=48, kernel_size=(5,5), padding='valid', activation='relu'))\nmodel.add(MaxPool2D(strides=2))\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dense(84, activation='relu'))\nmodel.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:30:45.156159Z","iopub.execute_input":"2024-01-21T16:30:45.156608Z","iopub.status.idle":"2024-01-21T16:30:45.395936Z","shell.execute_reply.started":"2024-01-21T16:30:45.156573Z","shell.execute_reply":"2024-01-21T16:30:45.394803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.build()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:30:48.732475Z","iopub.execute_input":"2024-01-21T16:30:48.732906Z","iopub.status.idle":"2024-01-21T16:30:48.766274Z","shell.execute_reply.started":"2024-01-21T16:30:48.732870Z","shell.execute_reply":"2024-01-21T16:30:48.765129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"adam = Adam(lr=5e-4)\nmodel.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer=adam)","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:30:55.391947Z","iopub.execute_input":"2024-01-21T16:30:55.392317Z","iopub.status.idle":"2024-01-21T16:30:55.412389Z","shell.execute_reply.started":"2024-01-21T16:30:55.392289Z","shell.execute_reply":"2024-01-21T16:30:55.411507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set a learning rate annealer\nreduce_lr = ReduceLROnPlateau(monitor='val_acc', \n                                patience=3, \n                                verbose=1, \n                                factor=0.2, \n                                min_lr=1e-6)","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:30:59.640111Z","iopub.execute_input":"2024-01-21T16:30:59.640529Z","iopub.status.idle":"2024-01-21T16:30:59.646153Z","shell.execute_reply.started":"2024-01-21T16:30:59.640495Z","shell.execute_reply":"2024-01-21T16:30:59.645034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Augmentation\ndatagen = ImageDataGenerator(\n            rotation_range=10, \n            width_shift_range=0.1, \n            height_shift_range=0.1, \n            zoom_range=0.1)\ndatagen.fit(X_dev)","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:31:02.984137Z","iopub.execute_input":"2024-01-21T16:31:02.984547Z","iopub.status.idle":"2024-01-21T16:31:03.185746Z","shell.execute_reply.started":"2024-01-21T16:31:02.984513Z","shell.execute_reply":"2024-01-21T16:31:03.184448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit_generator(datagen.flow(X_dev, T_dev, batch_size=100), steps_per_epoch=len(X_dev)/100, \n                    epochs=15, validation_data=(X_val, T_val), callbacks=[reduce_lr])","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:31:06.988587Z","iopub.execute_input":"2024-01-21T16:31:06.989058Z","iopub.status.idle":"2024-01-21T16:43:33.491002Z","shell.execute_reply.started":"2024-01-21T16:31:06.989014Z","shell.execute_reply":"2024-01-21T16:43:33.489860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(X_val, T_val, batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:43:33.492999Z","iopub.execute_input":"2024-01-21T16:43:33.493344Z","iopub.status.idle":"2024-01-21T16:43:34.194055Z","shell.execute_reply.started":"2024-01-21T16:43:33.493314Z","shell.execute_reply":"2024-01-21T16:43:34.192904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:43:46.652801Z","iopub.execute_input":"2024-01-21T16:43:46.653182Z","iopub.status.idle":"2024-01-21T16:43:46.659597Z","shell.execute_reply.started":"2024-01-21T16:43:46.653151Z","shell.execute_reply":"2024-01-21T16:43:46.658586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\nX_test = np.array(df_test.iloc[:, 1:])\nX_test = X_test/255.0\n","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:47:37.316197Z","iopub.execute_input":"2024-01-21T16:47:37.316609Z","iopub.status.idle":"2024-01-21T16:47:37.698288Z","shell.execute_reply.started":"2024-01-21T16:47:37.316578Z","shell.execute_reply":"2024-01-21T16:47:37.696843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_test = X_test.reshape((X_test.shape[0]*, 28, 28, 1))\n\nX_test_ = X_test.reshape(X_test.shape[0], 28, 28, 1)\nY_test_ = model.predict(X_test_)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:47:53.591613Z","iopub.execute_input":"2024-01-21T16:47:53.592053Z","iopub.status.idle":"2024-01-21T16:47:56.333396Z","shell.execute_reply.started":"2024-01-21T16:47:53.592018Z","shell.execute_reply":"2024-01-21T16:47:56.332365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_test = np.argmax(Y_test_, axis=1)\nY_test[:5]","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:48:15.431765Z","iopub.execute_input":"2024-01-21T16:48:15.432223Z","iopub.status.idle":"2024-01-21T16:48:15.440805Z","shell.execute_reply.started":"2024-01-21T16:48:15.432182Z","shell.execute_reply":"2024-01-21T16:48:15.439798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(data={\n    \"id\": np.array (df_test.iloc[:, 0].index),\n    \"label\": Y_test\n})\nsubmission.to_csv(\"submission.csv\", index=None)","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:53:06.943860Z","iopub.execute_input":"2024-01-21T16:53:06.944325Z","iopub.status.idle":"2024-01-21T16:53:06.966649Z","shell.execute_reply.started":"2024-01-21T16:53:06.944289Z","shell.execute_reply":"2024-01-21T16:53:06.965479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_out = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')\n# df_out['Label'] = Y_test\n# df_out.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:48:47.779433Z","iopub.execute_input":"2024-01-21T16:48:47.779833Z","iopub.status.idle":"2024-01-21T16:48:47.800133Z","shell.execute_reply.started":"2024-01-21T16:48:47.779800Z","shell.execute_reply":"2024-01-21T16:48:47.799327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-21T16:53:37.787465Z","iopub.execute_input":"2024-01-21T16:53:37.788567Z","iopub.status.idle":"2024-01-21T16:53:37.798573Z","shell.execute_reply.started":"2024-01-21T16:53:37.788529Z","shell.execute_reply":"2024-01-21T16:53:37.797331Z"},"trusted":true},"execution_count":null,"outputs":[]}]}