{"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":"2021-06-22T20:16:53.036241Z","iopub.execute_input":"2021-06-22T20:16:53.036598Z","iopub.status.idle":"2021-06-22T20:16:53.047585Z","shell.execute_reply.started":"2021-06-22T20:16:53.036527Z","shell.execute_reply":"2021-06-22T20:16:53.046759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Imports****","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:16:53.049337Z","iopub.execute_input":"2021-06-22T20:16:53.049925Z","iopub.status.idle":"2021-06-22T20:16:58.319595Z","shell.execute_reply.started":"2021-06-22T20:16:53.049875Z","shell.execute_reply":"2021-06-22T20:16:58.318782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data preparation**","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(\"../input/Kannada-MNIST/train.csv\")\ntest_data = pd.read_csv(\"../input/Kannada-MNIST/test.csv\")\nprint(train_data.shape,test_data.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:16:58.323585Z","iopub.execute_input":"2021-06-22T20:16:58.323829Z","iopub.status.idle":"2021-06-22T20:17:02.634844Z","shell.execute_reply.started":"2021-06-22T20:16:58.323805Z","shell.execute_reply":"2021-06-22T20:17:02.633984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_data.drop([\"label\"],axis = 1).values\nY = train_data[\"label\"].values","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:17:02.63642Z","iopub.execute_input":"2021-06-22T20:17:02.636672Z","iopub.status.idle":"2021-06-22T20:17:02.754922Z","shell.execute_reply.started":"2021-06-22T20:17:02.636646Z","shell.execute_reply":"2021-06-22T20:17:02.753879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X.shape,Y.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:17:02.756756Z","iopub.execute_input":"2021-06-22T20:17:02.757168Z","iopub.status.idle":"2021-06-22T20:17:02.763348Z","shell.execute_reply.started":"2021-06-22T20:17:02.757129Z","shell.execute_reply":"2021-06-22T20:17:02.762161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X.reshape([60000,28,28,1])\nY = Y.reshape([60000,1])","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:17:02.764948Z","iopub.execute_input":"2021-06-22T20:17:02.765476Z","iopub.status.idle":"2021-06-22T20:17:02.771795Z","shell.execute_reply.started":"2021-06-22T20:17:02.76544Z","shell.execute_reply":"2021-06-22T20:17:02.770857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert to one-hot-encoding\nfrom keras.utils.np_utils import to_categorical \nY = to_categorical(Y, num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:17:02.773429Z","iopub.execute_input":"2021-06-22T20:17:02.77396Z","iopub.status.idle":"2021-06-22T20:17:02.826415Z","shell.execute_reply.started":"2021-06-22T20:17:02.773924Z","shell.execute_reply":"2021-06-22T20:17:02.825688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split(X, Y, test_size = 0.1, random_state = 14)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:17:02.827496Z","iopub.execute_input":"2021-06-22T20:17:02.827817Z","iopub.status.idle":"2021-06-22T20:17:03.513101Z","shell.execute_reply.started":"2021-06-22T20:17:02.827784Z","shell.execute_reply":"2021-06-22T20:17:03.512005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train.shape)\nprint(y_train.shape)\nprint(x_test.shape)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:17:03.516045Z","iopub.execute_input":"2021-06-22T20:17:03.516395Z","iopub.status.idle":"2021-06-22T20:17:03.523375Z","shell.execute_reply.started":"2021-06-22T20:17:03.516359Z","shell.execute_reply":"2021-06-22T20:17:03.52058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train/255\nx_test = x_test/255","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:17:03.525257Z","iopub.execute_input":"2021-06-22T20:17:03.525714Z","iopub.status.idle":"2021-06-22T20:17:03.671859Z","shell.execute_reply.started":"2021-06-22T20:17:03.525678Z","shell.execute_reply":"2021-06-22T20:17:03.671071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_visualization = x_train.reshape(x_train.shape[0], 28, 28)\n\nfig, axis = plt.subplots(1, 4, figsize=(20, 10))\nfor i, ax in enumerate(axis.flat):\n    ax.imshow(X_visualization[i], cmap='binary')\n    digit = y_train[i].argmax()\n    ax.set(title = f\"Real Number is {digit}\");\n# we see how our data look like.","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:17:03.673313Z","iopub.execute_input":"2021-06-22T20:17:03.673658Z","iopub.status.idle":"2021-06-22T20:17:04.233106Z","shell.execute_reply.started":"2021-06-22T20:17:03.67362Z","shell.execute_reply":"2021-06-22T20:17:04.232342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(64, (3,3), padding = 'same', activation='relu', input_shape=(28, 28, 1)),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Conv2D(64, (3,3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Conv2D(128, (3,3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Conv2D(128, (3,3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(256, activation='relu'),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:17:04.234365Z","iopub.execute_input":"2021-06-22T20:17:04.234728Z","iopub.status.idle":"2021-06-22T20:17:06.155883Z","shell.execute_reply.started":"2021-06-22T20:17:04.234693Z","shell.execute_reply":"2021-06-22T20:17:06.155055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.optimizers import Adam\noptimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)\nmodel.compile(optimizer = optimizer,\n              loss = 'categorical_crossentropy',\n              metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:17:06.157155Z","iopub.execute_input":"2021-06-22T20:17:06.157494Z","iopub.status.idle":"2021-06-22T20:17:06.171789Z","shell.execute_reply.started":"2021-06-22T20:17:06.15746Z","shell.execute_reply":"2021-06-22T20:17:06.170965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ReduceLROnPlateau\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.6, \n                                            min_lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:17:06.17304Z","iopub.execute_input":"2021-06-22T20:17:06.173485Z","iopub.status.idle":"2021-06-22T20:17:06.180102Z","shell.execute_reply.started":"2021-06-22T20:17:06.173418Z","shell.execute_reply":"2021-06-22T20:17:06.179208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 128\nepochs = 50","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:17:06.181455Z","iopub.execute_input":"2021-06-22T20:17:06.181921Z","iopub.status.idle":"2021-06-22T20:17:06.19015Z","shell.execute_reply.started":"2021-06-22T20:17:06.181843Z","shell.execute_reply":"2021-06-22T20:17:06.189384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator( \n        rotation_range=10,  \n        zoom_range = 0.1, \n        width_shift_range=0.1,  \n        height_shift_range=0.1,\n        shear_range = 0.1,\n        horizontal_flip=False,  \n        vertical_flip=False\n        )\ntrain_datagen.fit(x_train)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:17:06.192623Z","iopub.execute_input":"2021-06-22T20:17:06.193296Z","iopub.status.idle":"2021-06-22T20:17:06.328374Z","shell.execute_reply.started":"2021-06-22T20:17:06.193259Z","shell.execute_reply":"2021-06-22T20:17:06.327424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n            train_datagen.flow(x_train,y_train,batch_size = batch_size),\n            validation_data = (x_test,y_test),\n            batch_size = batch_size,\n            steps_per_epoch = x_train.shape[0]//batch_size,\n            epochs = epochs,\n            verbose = 1,\n            callbacks=[learning_rate_reduction]\n            )","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:17:06.330843Z","iopub.execute_input":"2021-06-22T20:17:06.331394Z","iopub.status.idle":"2021-06-22T20:26:47.01659Z","shell.execute_reply.started":"2021-06-22T20:17:06.331354Z","shell.execute_reply":"2021-06-22T20:26:47.015723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:26:47.018175Z","iopub.execute_input":"2021-06-22T20:26:47.018534Z","iopub.status.idle":"2021-06-22T20:26:48.119028Z","shell.execute_reply.started":"2021-06-22T20:26:47.018497Z","shell.execute_reply":"2021-06-22T20:26:48.118264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(x_test)\nX_new = x_test.reshape(x_test.shape[0], 28, 28)\n\nfig, axis = plt.subplots(4, 4, figsize=(12, 14))\nfor i, ax in enumerate(axis.flat):\n    ax.imshow(X_new[i], cmap='binary')\n    ax.set(title = f\"Real Number is {y_test[i].argmax()}\\nPredict Number is {y_pred[i].argmax()}\");","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:26:48.120326Z","iopub.execute_input":"2021-06-22T20:26:48.120657Z","iopub.status.idle":"2021-06-22T20:26:50.204768Z","shell.execute_reply.started":"2021-06-22T20:26:48.120621Z","shell.execute_reply":"2021-06-22T20:26:50.203997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nplt.figure()\nplt.plot(acc,color = 'green',label = 'Training Acuracy')\nplt.plot(val_acc,color = 'red',label = 'Validation Accuracy')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:26:50.20609Z","iopub.execute_input":"2021-06-22T20:26:50.206442Z","iopub.status.idle":"2021-06-22T20:26:50.350641Z","shell.execute_reply.started":"2021-06-22T20:26:50.206405Z","shell.execute_reply":"2021-06-22T20:26:50.349713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure()\nplt.plot(loss,color = 'green',label = 'Training Loss')\nplt.plot(val_loss,color = 'red',label = 'Validation Loss')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:26:50.3522Z","iopub.execute_input":"2021-06-22T20:26:50.352556Z","iopub.status.idle":"2021-06-22T20:26:50.493248Z","shell.execute_reply.started":"2021-06-22T20:26:50.352521Z","shell.execute_reply":"2021-06-22T20:26:50.492335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"raw_test = test_data\nsample = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:38:17.894732Z","iopub.execute_input":"2021-06-22T20:38:17.895112Z","iopub.status.idle":"2021-06-22T20:38:17.913908Z","shell.execute_reply.started":"2021-06-22T20:38:17.895079Z","shell.execute_reply":"2021-06-22T20:38:17.912786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub=sample\nraw_test_id=raw_test.id\nraw_test=raw_test.drop(\"id\",axis=\"columns\")\nraw_test=raw_test / 255\ntest=raw_test.values.reshape(-1,28,28,1)\ntest.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:38:30.785566Z","iopub.execute_input":"2021-06-22T20:38:30.785874Z","iopub.status.idle":"2021-06-22T20:38:30.836044Z","shell.execute_reply.started":"2021-06-22T20:38:30.785847Z","shell.execute_reply":"2021-06-22T20:38:30.835243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub=model.predict(test)     ##making prediction\nsub=np.argmax(sub,axis=1) ##changing the prediction intro labels\n\nsample_sub['label']=sub\nsample_sub.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:39:57.528299Z","iopub.execute_input":"2021-06-22T20:39:57.528627Z","iopub.status.idle":"2021-06-22T20:39:58.435032Z","shell.execute_reply.started":"2021-06-22T20:39:57.528595Z","shell.execute_reply":"2021-06-22T20:39:58.434218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:26:50.520233Z","iopub.status.idle":"2021-06-22T20:26:50.521253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:26:50.522596Z","iopub.status.idle":"2021-06-22T20:26:50.523157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:26:50.524386Z","iopub.status.idle":"2021-06-22T20:26:50.524965Z"},"trusted":true},"execution_count":null,"outputs":[]}]}