{"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":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\n\nfrom sklearn.model_selection import train_test_split\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout, BatchNormalization\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.optimizers import RMSprop, Adam, Adagrad, Adamax, Adadelta\n\nfrom sklearn.metrics import classification_report, confusion_matrix","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-27T11:36:44.534821Z","iopub.execute_input":"2021-12-27T11:36:44.535283Z","iopub.status.idle":"2021-12-27T11:36:45.279811Z","shell.execute_reply.started":"2021-12-27T11:36:44.535220Z","shell.execute_reply":"2021-12-27T11:36:45.278974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:36:45.281619Z","iopub.execute_input":"2021-12-27T11:36:45.281965Z","iopub.status.idle":"2021-12-27T11:36:45.292257Z","shell.execute_reply.started":"2021-12-27T11:36:45.281928Z","shell.execute_reply":"2021-12-27T11:36:45.291417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/Kannada-MNIST/train.csv\")\ntest = pd.read_csv(\"../input/Kannada-MNIST/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:36:45.294417Z","iopub.execute_input":"2021-12-27T11:36:45.294722Z","iopub.status.idle":"2021-12-27T11:36:50.868961Z","shell.execute_reply.started":"2021-12-27T11:36:45.294694Z","shell.execute_reply":"2021-12-27T11:36:50.868213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_digit_counts =  train.label.value_counts().reset_index()\n\nplt.figure(figsize=(20,8))\nax = sns.barplot(x='index', y='label', data=df_digit_counts)\n\nfor i in ax.patches:\n    v1 = round((i.get_height()/len(train))*100, 2)\n    ax.annotate(f'{int(i.get_height())} ({v1}%)', (i.get_x()+0.4, i.get_height()),\n                    ha='center', va='bottom',\n                    color= 'black')\n\nplt.title(\"Digit Count\")\nplt.ylabel(\"Counts\")\nplt.xlabel(\"Digits\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:36:50.870293Z","iopub.execute_input":"2021-12-27T11:36:50.870652Z","iopub.status.idle":"2021-12-27T11:36:51.136153Z","shell.execute_reply.started":"2021-12-27T11:36:50.870615Z","shell.execute_reply":"2021-12-27T11:36:51.135220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X, train_y = train.drop(columns=['label']), train[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:36:51.139437Z","iopub.execute_input":"2021-12-27T11:36:51.139724Z","iopub.status.idle":"2021-12-27T11:36:51.257891Z","shell.execute_reply.started":"2021-12-27T11:36:51.139695Z","shell.execute_reply":"2021-12-27T11:36:51.256917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X = np.array(train_X)\ntrain_y = np.array(train_y)","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:36:51.260621Z","iopub.execute_input":"2021-12-27T11:36:51.261196Z","iopub.status.idle":"2021-12-27T11:36:51.400768Z","shell.execute_reply.started":"2021-12-27T11:36:51.261154Z","shell.execute_reply":"2021-12-27T11:36:51.399960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testx = test.drop(columns=['id'])","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:36:51.402338Z","iopub.execute_input":"2021-12-27T11:36:51.402783Z","iopub.status.idle":"2021-12-27T11:36:51.416042Z","shell.execute_reply.started":"2021-12-27T11:36:51.402743Z","shell.execute_reply":"2021-12-27T11:36:51.415081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testx = np.array(testx)","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:36:51.417573Z","iopub.execute_input":"2021-12-27T11:36:51.418011Z","iopub.status.idle":"2021-12-27T11:36:51.432405Z","shell.execute_reply.started":"2021-12-27T11:36:51.417970Z","shell.execute_reply":"2021-12-27T11:36:51.431711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X = train_X.reshape(-1,28,28,1)\ntestx = testx.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:36:51.433916Z","iopub.execute_input":"2021-12-27T11:36:51.434293Z","iopub.status.idle":"2021-12-27T11:36:51.439295Z","shell.execute_reply.started":"2021-12-27T11:36:51.434255Z","shell.execute_reply":"2021-12-27T11:36:51.438183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X = train_X / 255.0\ntestx = testx / 255.0","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:36:51.441236Z","iopub.execute_input":"2021-12-27T11:36:51.441819Z","iopub.status.idle":"2021-12-27T11:36:51.599819Z","shell.execute_reply.started":"2021-12-27T11:36:51.441777Z","shell.execute_reply":"2021-12-27T11:36:51.599034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y = to_categorical(train_y, num_classes = len(np.unique(train[\"label\"])))","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:36:51.601294Z","iopub.execute_input":"2021-12-27T11:36:51.601667Z","iopub.status.idle":"2021-12-27T11:36:51.609733Z","shell.execute_reply.started":"2021-12-27T11:36:51.601629Z","shell.execute_reply":"2021-12-27T11:36:51.608789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(train_X, train_y, test_size=0.25, random_state=42, stratify= train_y, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:36:51.611049Z","iopub.execute_input":"2021-12-27T11:36:51.611658Z","iopub.status.idle":"2021-12-27T11:36:52.963523Z","shell.execute_reply.started":"2021-12-27T11:36:51.611616Z","shell.execute_reply":"2021-12-27T11:36:52.962685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 20\nbatch_size = 16","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2021-12-27T11:36:52.965030Z","iopub.execute_input":"2021-12-27T11:36:52.965387Z","iopub.status.idle":"2021-12-27T11:36:52.970431Z","shell.execute_reply.started":"2021-12-27T11:36:52.965349Z","shell.execute_reply":"2021-12-27T11:36:52.969544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model():\n    model = Sequential()\n\n    model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', activation ='relu', input_shape = (28,28,1)))\n    model.add(BatchNormalization())\n    model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', activation ='relu'))\n    model.add(BatchNormalization())\n    model.add(MaxPool2D(pool_size=(2,2)))\n    model.add(Dropout(0.25))\n\n    model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', activation ='relu'))\n    model.add(BatchNormalization())\n    model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', activation ='relu'))\n    model.add(BatchNormalization())\n    model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\n    model.add(Dropout(0.25))\n\n    model.add(Conv2D(filters = 128, kernel_size = (3,3),padding = 'Same', activation ='relu'))\n    model.add(BatchNormalization())\n    model.add(Conv2D(filters = 128, kernel_size = (3,3),padding = 'Same', activation ='relu'))\n    model.add(BatchNormalization())\n    model.add(Conv2D(filters = 128, kernel_size = (3,3),padding = 'Same', activation ='relu'))\n    model.add(BatchNormalization())\n    model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\n    model.add(Dropout(0.25))\n\n    model.add(Conv2D(filters = 256, kernel_size = (3,3),padding = 'Same', activation ='relu'))\n    model.add(BatchNormalization())\n    model.add(Conv2D(filters = 256, kernel_size = (3,3),padding = 'Same', activation ='relu'))\n    model.add(BatchNormalization())\n    model.add(Conv2D(filters = 256, kernel_size = (3,3),padding = 'Same', activation ='relu'))\n    model.add(BatchNormalization())\n    model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\n    model.add(Dropout(0.25))\n\n    model.add(Flatten())\n    model.add(Dense(512, activation = \"relu\"))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.3))\n\n    model.add(Dense(10, activation = \"softmax\"))\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:36:52.972003Z","iopub.execute_input":"2021-12-27T11:36:52.972649Z","iopub.status.idle":"2021-12-27T11:36:52.993909Z","shell.execute_reply.started":"2021-12-27T11:36:52.972610Z","shell.execute_reply":"2021-12-27T11:36:52.992937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:36:52.997071Z","iopub.execute_input":"2021-12-27T11:36:52.997345Z","iopub.status.idle":"2021-12-27T11:36:55.978378Z","shell.execute_reply.started":"2021-12-27T11:36:52.997312Z","shell.execute_reply":"2021-12-27T11:36:55.977622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = Adamax()\nmodel.compile(optimizer=optimizer, loss = 'categorical_crossentropy', metrics=['accuracy'])\n\n# Set a learning rate annealer\nlr_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)\nes = EarlyStopping(monitor='val_loss',\n                              min_delta=0,\n                              patience=5,\n                              verbose=0, mode='auto')\n\n","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:36:55.980428Z","iopub.execute_input":"2021-12-27T11:36:55.980795Z","iopub.status.idle":"2021-12-27T11:36:56.002607Z","shell.execute_reply.started":"2021-12-27T11:36:55.980753Z","shell.execute_reply":"2021-12-27T11:36:56.001727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # apply ZCA whitening\n        rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.1, # Randomly zoom image \n        width_shift_range=0.12,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.12,  # randomly shift images vertically (fraction of total height)\n        horizontal_flip=False,  # randomly flip images\n        vertical_flip=False)  # randomly flip images\ndatagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:36:56.003985Z","iopub.execute_input":"2021-12-27T11:36:56.004363Z","iopub.status.idle":"2021-12-27T11:36:56.122934Z","shell.execute_reply.started":"2021-12-27T11:36:56.004320Z","shell.execute_reply":"2021-12-27T11:36:56.122161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the model\nhistory = model.fit_generator(datagen.flow(X_train,y_train, batch_size=batch_size),\n                              epochs = epochs, \n                              validation_data = (X_test,y_test), \n                              steps_per_epoch=X_train.shape[0] // batch_size,\n                              callbacks=[lr_reduction, es], \n                              shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:36:56.126320Z","iopub.execute_input":"2021-12-27T11:36:56.126662Z","iopub.status.idle":"2021-12-27T11:46:15.067349Z","shell.execute_reply.started":"2021-12-27T11:36:56.126633Z","shell.execute_reply":"2021-12-27T11:46:15.066579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(X_train, y_train), model.evaluate(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:46:15.068891Z","iopub.execute_input":"2021-12-27T11:46:15.069276Z","iopub.status.idle":"2021-12-27T11:46:21.406115Z","shell.execute_reply.started":"2021-12-27T11:46:15.069231Z","shell.execute_reply":"2021-12-27T11:46:21.405379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the loss and accuracy curves for training and validation \nfig, ax = plt.subplots(2,1)\nax[0].plot(history.history['loss'], color='b', label=\"Training loss\")\nax[0].plot(history.history['val_loss'], color='r', label=\"validation loss\",axes =ax[0])\nlegend = ax[0].legend(loc='best', shadow=True)\n\nax[1].plot(history.history['accuracy'], color='b', label=\"Training accuracy\")\nax[1].plot(history.history['val_accuracy'], color='r',label=\"Validation accuracy\")\nlegend = ax[1].legend(loc='best', shadow=True)","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:46:21.409089Z","iopub.execute_input":"2021-12-27T11:46:21.409370Z","iopub.status.idle":"2021-12-27T11:46:21.691770Z","shell.execute_reply.started":"2021-12-27T11:46:21.409342Z","shell.execute_reply":"2021-12-27T11:46:21.690890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ypred = model.predict(X_test)\nypred = np.argmax(ypred, axis=1)\nytest = np.argmax(y_test, axis=1)\n\ncf_matrix = confusion_matrix(ytest, ypred)\n\nplt.figure(figsize=(20,8))\nax = sns.heatmap(cf_matrix, annot=True, fmt='g')\nplt.show()\n\nprint(\"\\n\\n\")\nprint(classification_report(ytest, ypred))","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:46:21.693065Z","iopub.execute_input":"2021-12-27T11:46:21.693597Z","iopub.status.idle":"2021-12-27T11:46:23.229792Z","shell.execute_reply.started":"2021-12-27T11:46:21.693553Z","shell.execute_reply":"2021-12-27T11:46:23.228251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_sub():\n    results = model.predict(testx)\n    results = np.argmax(results,axis = 1)\n\n    df_sub = pd.read_csv(\"../input/Kannada-MNIST/sample_submission.csv\")\n    df_sub['label'] = results\n    df_sub.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:46:23.231221Z","iopub.execute_input":"2021-12-27T11:46:23.231597Z","iopub.status.idle":"2021-12-27T11:46:23.238677Z","shell.execute_reply.started":"2021-12-27T11:46:23.231558Z","shell.execute_reply":"2021-12-27T11:46:23.237586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_sub()","metadata":{"execution":{"iopub.status.busy":"2021-12-27T11:46:23.241449Z","iopub.execute_input":"2021-12-27T11:46:23.241848Z","iopub.status.idle":"2021-12-27T11:46:23.705714Z","shell.execute_reply.started":"2021-12-27T11:46:23.241809Z","shell.execute_reply":"2021-12-27T11:46:23.704843Z"},"trusted":true},"execution_count":null,"outputs":[]}]}