{"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":"#Imma import libs:\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, AveragePooling2D\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\nfrom tensorflow.keras.layers import Dropout\n\ntf.keras.backend.clear_session()\nnp.random.seed(1)\ntf.random.set_seed(1)\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-11T02:32:09.364672Z","iopub.execute_input":"2023-01-11T02:32:09.365068Z","iopub.status.idle":"2023-01-11T02:32:09.382648Z","shell.execute_reply.started":"2023-01-11T02:32:09.365038Z","shell.execute_reply":"2023-01-11T02:32:09.381590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Read CSVVVV file:\n\ntrain = pd.read_csv(\"/kaggle/input/Kannada-MNIST/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/Kannada-MNIST/Dig-MNIST.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:32:09.384521Z","iopub.execute_input":"2023-01-11T02:32:09.384888Z","iopub.status.idle":"2023-01-11T02:32:12.129285Z","shell.execute_reply.started":"2023-01-11T02:32:09.384853Z","shell.execute_reply":"2023-01-11T02:32:12.128364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Check 'train' data:\ntrain","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:32:12.130652Z","iopub.execute_input":"2023-01-11T02:32:12.130979Z","iopub.status.idle":"2023-01-11T02:32:12.151026Z","shell.execute_reply.started":"2023-01-11T02:32:12.130954Z","shell.execute_reply":"2023-01-11T02:32:12.149740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_set = train.iloc[:, 1:].to_numpy()\ny_train_set = train.iloc[:, 0].to_numpy()","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:32:12.154796Z","iopub.execute_input":"2023-01-11T02:32:12.155485Z","iopub.status.idle":"2023-01-11T02:32:12.161561Z","shell.execute_reply.started":"2023-01-11T02:32:12.155433Z","shell.execute_reply":"2023-01-11T02:32:12.160160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Check 'test' data:\ntest","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:32:12.162910Z","iopub.execute_input":"2023-01-11T02:32:12.163260Z","iopub.status.idle":"2023-01-11T02:32:12.186311Z","shell.execute_reply.started":"2023-01-11T02:32:12.163232Z","shell.execute_reply":"2023-01-11T02:32:12.185298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = test.iloc[:, 1:].to_numpy()\ny_test = test.iloc[:, 0].to_numpy()","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:32:12.187547Z","iopub.execute_input":"2023-01-11T02:32:12.187879Z","iopub.status.idle":"2023-01-11T02:32:12.194767Z","shell.execute_reply.started":"2023-01-11T02:32:12.187811Z","shell.execute_reply":"2023-01-11T02:32:12.193254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Shapes of these train and test data:\nx_train_set.shape, y_train_set.shape, x_test.shape, y_test.shape ","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:32:12.196417Z","iopub.execute_input":"2023-01-11T02:32:12.197582Z","iopub.status.idle":"2023-01-11T02:32:12.209962Z","shell.execute_reply.started":"2023-01-11T02:32:12.197547Z","shell.execute_reply":"2023-01-11T02:32:12.208587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Check a value if its fit (Maybe? I'll just take the index 15):\n\nprint('Labeled Num:',y_train_set[15])\nplt.imshow(x_train_set[15].reshape(28, 28), cmap = 'gray')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:32:12.212221Z","iopub.execute_input":"2023-01-11T02:32:12.212585Z","iopub.status.idle":"2023-01-11T02:32:12.347411Z","shell.execute_reply.started":"2023-01-11T02:32:12.212554Z","shell.execute_reply":"2023-01-11T02:32:12.345700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Split the data into train and test sets:\n\nx_train, x_valid, y_train, y_valid = train_test_split(\n x_train_set, y_train_set, random_state = 1)","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:32:12.351463Z","iopub.execute_input":"2023-01-11T02:32:12.352080Z","iopub.status.idle":"2023-01-11T02:32:12.697803Z","shell.execute_reply.started":"2023-01-11T02:32:12.352042Z","shell.execute_reply":"2023-01-11T02:32:12.696385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train/255\nx_train = x_train.astype(\"float32\")\n\nx_test = x_test/255\nx_test = x_test.astype(\"float32\")\n\nx_train = x_train.reshape(-1, 28, 28, 1)\nx_valid = x_valid.reshape(-1, 28, 28, 1)\nx_test = x_test.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:32:12.702037Z","iopub.execute_input":"2023-01-11T02:32:12.702343Z","iopub.status.idle":"2023-01-11T02:32:12.833540Z","shell.execute_reply.started":"2023-01-11T02:32:12.702318Z","shell.execute_reply":"2023-01-11T02:32:12.832709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rotation_range = 20, width_shift_range = 0.2, height_shift_range = 0.2,\n                                shear_range = 0.2, fill_mode = \"nearest\", zoom_range = 0.2,)\n\nes = EarlyStopping(monitor = 'val_loss', patience = 5, restore_best_weights = True, verbose = 0)\n\nreduce_lr = ReduceLROnPlateau(patience = 2)","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:32:12.834736Z","iopub.execute_input":"2023-01-11T02:32:12.835937Z","iopub.status.idle":"2023-01-11T02:32:12.841896Z","shell.execute_reply.started":"2023-01-11T02:32:12.835898Z","shell.execute_reply":"2023-01-11T02:32:12.841171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model's being build up:\n\nmodel = Sequential([\n    Conv2D(filters = 30, kernel_size = (4,4), strides = (1,1), padding = 'valid',\n          activation = 'relu', input_shape = (28, 28, 1)),\n    Dropout(rate = 0.25),\n    MaxPooling2D(pool_size = (2,2), padding = 'valid'),\n    Conv2D(filters = 60, kernel_size = 3, strides = 2, padding = 'same',\n          activation = 'relu'),\n    Dropout(rate = 0.25),\n    AveragePooling2D(pool_size = 2, padding = 'same'),\n    Flatten(),\n    Dropout(rate = 0.25),\n    Dense(1024, activation = 'relu'),\n    Dropout(rate = 0.25),\n    Dense(10, activation = 'softmax')\n])   ","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:32:12.843234Z","iopub.execute_input":"2023-01-11T02:32:12.844352Z","iopub.status.idle":"2023-01-11T02:32:12.916074Z","shell.execute_reply.started":"2023-01-11T02:32:12.844314Z","shell.execute_reply":"2023-01-11T02:32:12.915221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Take an overview to the model: \nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:32:12.917562Z","iopub.execute_input":"2023-01-11T02:32:12.918216Z","iopub.status.idle":"2023-01-11T02:32:12.925217Z","shell.execute_reply.started":"2023-01-11T02:32:12.918177Z","shell.execute_reply":"2023-01-11T02:32:12.924047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss = 'sparse_categorical_crossentropy', \n              optimizer = 'adam',\n              metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:32:12.926244Z","iopub.execute_input":"2023-01-11T02:32:12.927389Z","iopub.status.idle":"2023-01-11T02:32:12.946550Z","shell.execute_reply.started":"2023-01-11T02:32:12.927355Z","shell.execute_reply":"2023-01-11T02:32:12.945064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Training timeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\ntrain = model.fit(train_datagen.flow(x_train, y_train), epochs = 1000, \n                  batch_size = 32, validation_data = (x_valid, y_valid), \n                  callbacks = [es, reduce_lr])","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:32:12.947673Z","iopub.execute_input":"2023-01-11T02:32:12.947962Z","iopub.status.idle":"2023-01-11T02:42:46.367617Z","shell.execute_reply.started":"2023-01-11T02:32:12.947937Z","shell.execute_reply":"2023-01-11T02:42:46.366540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:42:46.368888Z","iopub.execute_input":"2023-01-11T02:42:46.369568Z","iopub.status.idle":"2023-01-11T02:42:48.431510Z","shell.execute_reply.started":"2023-01-11T02:42:46.369525Z","shell.execute_reply":"2023-01-11T02:42:48.429412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plotting the change in loss\nplt.figure(figsize=(8,5))\nplt.xlabel('epochs')\nplt.plot(train.history['loss'])\nplt.plot(train.history['val_loss'])","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:42:48.433805Z","iopub.execute_input":"2023-01-11T02:42:48.434272Z","iopub.status.idle":"2023-01-11T02:42:48.624298Z","shell.execute_reply.started":"2023-01-11T02:42:48.434236Z","shell.execute_reply":"2023-01-11T02:42:48.622393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plotting the change in accuracy\nplt.figure(figsize=(8,5))\nplt.xlabel('epochs')\nplt.plot(train.history['accuracy'])\nplt.plot(train.history['val_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:42:48.626800Z","iopub.execute_input":"2023-01-11T02:42:48.627327Z","iopub.status.idle":"2023-01-11T02:42:48.800404Z","shell.execute_reply.started":"2023-01-11T02:42:48.627280Z","shell.execute_reply":"2023-01-11T02:42:48.799477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#After trained the model, let's get into the real deal:\nsample_submit = pd.read_csv(\"/kaggle/input/Kannada-MNIST/sample_submission.csv\")\nreal_test = pd.read_csv(\"/kaggle/input/Kannada-MNIST/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:42:48.801645Z","iopub.execute_input":"2023-01-11T02:42:48.801991Z","iopub.status.idle":"2023-01-11T02:42:49.007165Z","shell.execute_reply.started":"2023-01-11T02:42:48.801956Z","shell.execute_reply":"2023-01-11T02:42:49.005510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"real_test = real_test.iloc[:, 1:].to_numpy()\nreal_test = real_test/255\nreal_test = real_test.astype(\"float32\")\nreal_test = real_test.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:42:49.009408Z","iopub.execute_input":"2023-01-11T02:42:49.009804Z","iopub.status.idle":"2023-01-11T02:42:49.026075Z","shell.execute_reply.started":"2023-01-11T02:42:49.009770Z","shell.execute_reply":"2023-01-11T02:42:49.023709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Prediction\nprediction = model.predict(real_test)\n\nprediction = prediction.argmax(axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:42:49.027890Z","iopub.execute_input":"2023-01-11T02:42:49.028913Z","iopub.status.idle":"2023-01-11T02:42:49.981790Z","shell.execute_reply.started":"2023-01-11T02:42:49.028842Z","shell.execute_reply":"2023-01-11T02:42:49.980391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submit[\"label\"] = prediction\nsample_submit.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:42:49.983389Z","iopub.execute_input":"2023-01-11T02:42:49.983720Z","iopub.status.idle":"2023-01-11T02:42:49.994379Z","shell.execute_reply.started":"2023-01-11T02:42:49.983689Z","shell.execute_reply":"2023-01-11T02:42:49.993575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submit.to_csv(\"submission.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2023-01-11T02:42:49.995529Z","iopub.execute_input":"2023-01-11T02:42:49.996444Z","iopub.status.idle":"2023-01-11T02:42:50.013087Z","shell.execute_reply.started":"2023-01-11T02:42:49.996410Z","shell.execute_reply":"2023-01-11T02:42:50.011172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}