{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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":"2022-11-20T19:47:03.092638Z","iopub.execute_input":"2022-11-20T19:47:03.093462Z","iopub.status.idle":"2022-11-20T19:47:03.145687Z","shell.execute_reply.started":"2022-11-20T19:47:03.093345Z","shell.execute_reply":"2022-11-20T19:47:03.144679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow.keras as keras\nfrom tensorflow.keras import layers\nkeras.backend.set_image_data_format('channels_last')\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Input, Add, BatchNormalization, Dropout\nfrom tensorflow.keras.models import Model\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.optimizers import SGD","metadata":{"execution":{"iopub.status.busy":"2022-11-20T19:47:03.147714Z","iopub.execute_input":"2022-11-20T19:47:03.148447Z","iopub.status.idle":"2022-11-20T19:47:14.297927Z","shell.execute_reply.started":"2022-11-20T19:47:03.148411Z","shell.execute_reply":"2022-11-20T19:47:14.296848Z"},"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/Dig-MNIST.csv\")\nX_train = train.drop(\"label\", axis = 1).copy()\nX_test= test.drop(\"label\", axis = 1).copy()\nY_train = train[\"label\"].copy()\nY_test = test[\"label\"].copy()\nX_train = X_train.values.reshape(-1,28, 28,1)\nX_test = X_test.values.reshape(-1,28, 28,1)\nX_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.2)\nY_train = keras.utils.to_categorical(Y_train, num_classes = 10)\nY_val = keras.utils.to_categorical(Y_val, num_classes = 10)\nY_test = keras.utils.to_categorical(Y_test, num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2022-11-20T19:47:14.299373Z","iopub.execute_input":"2022-11-20T19:47:14.300066Z","iopub.status.idle":"2022-11-20T19:47:20.309844Z","shell.execute_reply.started":"2022-11-20T19:47:14.300024Z","shell.execute_reply":"2022-11-20T19:47:20.308564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def class_model():\n    model = Sequential()\n    model.add(Conv2D(filters = 32, kernel_size = (4,4), input_shape = (28,28,1), activation = \"relu\"))\n    model.add(MaxPool2D(pool_size = (2,2)))\n    model.add(Dropout(0.2))\n    model.add(BatchNormalization())\n    \n    model.add(Conv2D(filters = 64, kernel_size = (3,3),  activation = \"relu\"))\n    model.add(MaxPool2D(pool_size = (2,2)))\n    model.add(Dropout(0.2))\n    model.add(BatchNormalization())\n\n    model.add(Conv2D(filters = 128, kernel_size = (2,2),  activation = \"relu\"))\n    model.add(MaxPool2D(pool_size = (2,2)))\n    model.add(Dropout(0.2))\n    model.add(BatchNormalization())\n\n    model.add(Flatten())\n\n    model.add(Dense(512, activation='relu'))\n    model.add(Dropout(0.2))\n    model.add(Dense(128, activation='relu'))\n    model.add(Dropout(0.2))\n    model.add(Dense(10, activation='softmax'))\n\n    return model\n\nmodel = class_model()\n#model.compile(optimizer = \"Adam\", loss = \"CategoricalCrossentropy\", metrics = \"accuracy\")\nopt = SGD(learning_rate=0.01, momentum=0.9)\nmodel.compile(optimizer = opt, loss = \"CategoricalCrossentropy\", metrics = \"accuracy\")","metadata":{"execution":{"iopub.status.busy":"2022-11-20T19:47:20.312514Z","iopub.execute_input":"2022-11-20T19:47:20.313009Z","iopub.status.idle":"2022-11-20T19:47:25.682591Z","shell.execute_reply.started":"2022-11-20T19:47:20.312969Z","shell.execute_reply":"2022-11-20T19:47:25.681647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-20T19:47:25.684670Z","iopub.execute_input":"2022-11-20T19:47:25.685270Z","iopub.status.idle":"2022-11-20T19:47:25.694729Z","shell.execute_reply.started":"2022-11-20T19:47:25.685233Z","shell.execute_reply":"2022-11-20T19:47:25.693308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\nearly_stop = EarlyStopping(monitor = \"val_loss\", patience = 5)\nhistory = model.fit(X_train, Y_train, validation_data = (X_val, Y_val), batch_size = 64, epochs = 50, verbose = 1,shuffle = True, callbacks = [early_stop] )","metadata":{"execution":{"iopub.status.busy":"2022-11-20T19:47:25.697334Z","iopub.execute_input":"2022-11-20T19:47:25.697884Z","iopub.status.idle":"2022-11-20T19:49:25.108962Z","shell.execute_reply.started":"2022-11-20T19:47:25.697847Z","shell.execute_reply":"2022-11-20T19:49:25.107934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(X_test, Y_test)","metadata":{"execution":{"iopub.status.busy":"2022-11-20T19:49:25.110813Z","iopub.execute_input":"2022-11-20T19:49:25.111212Z","iopub.status.idle":"2022-11-20T19:49:50.221909Z","shell.execute_reply.started":"2022-11-20T19:49:25.111173Z","shell.execute_reply":"2022-11-20T19:49:50.220851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test4=pd.read_csv('../input/Kannada-MNIST/test.csv')\ntest5 = test4.drop( \"id\", axis = 1).copy()\ntest5 = test5.values.reshape(-1,28, 28,1)\ny_hat = model.predict(test5).argmax(axis=-1)\nsubmission = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\nsubmission['label']=y_hat\nsubmission.to_csv('submission.csv', index=False)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-20T19:51:12.855661Z","iopub.execute_input":"2022-11-20T19:51:12.856035Z","iopub.status.idle":"2022-11-20T19:51:13.843099Z","shell.execute_reply.started":"2022-11-20T19:51:12.856003Z","shell.execute_reply":"2022-11-20T19:51:13.841961Z"},"trusted":true},"execution_count":null,"outputs":[]}]}