{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Loading the data"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntest_data = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\ny_train = train_data['label']\nX = train_data.drop(['label'],axis=1)\ndel train_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Id = test_data['id']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data = test_data.drop(['id'],axis=1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Checking for null values"},{"metadata":{"trusted":true},"cell_type":"code","source":"X.isnull().all().unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train.isnull().any()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data.isnull().all().unique()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Visualizations"},{"metadata":{"trusted":true},"cell_type":"code","source":"label_val = y_train.value_counts()\nplt.figure(figsize=(12,6))\nsns.barplot(x=label_val.index,y=label_val.values)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Right now our data is flattened out, therefore we have to reshape it"},{"metadata":{"trusted":true},"cell_type":"code","source":"X_temp = X.values.reshape(X.shape[0], 28, 28)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']\nnum_classes = len(classes)\nsamples_per_class = 6\nplt.figure(0,figsize=(16,10))\nfor y, cls in enumerate(classes):\n    idxs = np.flatnonzero(y_train == y)\n    idxs = np.random.choice(idxs, samples_per_class, replace=False)\n    for i, idx in enumerate(idxs):\n        plt_idx = i * num_classes + y + 1\n        plt.subplot(samples_per_class, num_classes, plt_idx)\n        plt.imshow(X_temp[idx])\n        plt.axis('off')\n        if i == 0:\n            plt.title(cls)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Keras needs an additional dimension as it also take info about no. of color channel\nsince here we only have greyscale images , so we only need to add third dimension as 1"},{"metadata":{"trusted":true},"cell_type":"code","source":"X = X.values.reshape(X.shape[0], 28, 28,1)\ntest_data = test_data.values.reshape(test_data.shape[0], 28, 28,1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Adding data Augmentation"},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen = ImageDataGenerator(\n        rotation_range= 8,  \n        zoom_range = 0.12,  \n        width_shift_range=0.1, \n        height_shift_range=0.1)\ndatagen.fit(X)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, Dense, Dropout, BatchNormalization, Flatten, MaxPool2D\nfrom keras.utils.np_utils import to_categorical","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train = to_categorical(y_train,num_classes=10)\n\nfrom keras.callbacks import ModelCheckpoint\ncheckpoint = ModelCheckpoint('BWeight.md5',monitor='val_loss',\n                            save_best_only=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Designing the model"},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(32,kernel_size=3,activation='relu',input_shape=(28,28,1)))\nmodel.add(Conv2D(32,kernel_size=3,activation='relu'))\nmodel.add(MaxPool2D())\nmodel.add(Conv2D(32,kernel_size=5,strides=2,padding='same',activation='relu'))\nmodel.add(Dropout(0.4))\n\nmodel.add(Conv2D(64,kernel_size=3,activation='relu'))\nmodel.add(Conv2D(64,kernel_size=3,activation='relu'))\nmodel.add(MaxPool2D())\nmodel.add(Conv2D(64,kernel_size=5,strides=2,padding='same',activation='relu'))\nmodel.add(Dropout(0.4))\n\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dropout(0.4))\nmodel.add(Dense(10, activation='softmax'))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Compiling the model"},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer=\"adam\", loss=\"categorical_crossentropy\", metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Splitting data"},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_val1, y_train, y_val1 = train_test_split(\n    X, y_train, test_size=0.05, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Training"},{"metadata":{"trusted":true},"cell_type":"code","source":"size_batch = 64","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit_generator(datagen.flow(X_train,y_train, batch_size=size_batch),\n                              epochs = 60,\n                              validation_data = (X_val1,y_val1),\n                              verbose = 2,\n                              steps_per_epoch = X_train.shape[0] // size_batch,\n                              callbacks=[checkpoint])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, 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['acc'], color='b', label=\"Training accuracy\")\nax[1].plot(history.history['val_acc'], color='r',label=\"Validation accuracy\")\nlegend = ax[1].legend(loc='best', shadow=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Extra validation"},{"metadata":{},"cell_type":"markdown","source":"Loading best weights"},{"metadata":{"trusted":true},"cell_type":"code","source":"model.load_weights('BWeight.md5')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Testing on extra validation data"},{"metadata":{"trusted":true},"cell_type":"code","source":"extra_validation = pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_extra_validate = extra_validation['label']\nX_extra_validate = extra_validation.drop(['label'],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_extra_validate = X_extra_validate.values.reshape(X_extra_validate.shape[0], 28, 28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import classification_report","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(classification_report(y_extra_validate,model.predict_classes(X_extra_validate)))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Model's performance is good\nTherefore, amking final predictions from it"},{"metadata":{},"cell_type":"markdown","source":"# Final Predictions"},{"metadata":{"trusted":true},"cell_type":"code","source":"FINAL_PREDS = model.predict_classes(test_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame({ 'id': Id,\n                            'label': FINAL_PREDS })\nsubmission.to_csv(path_or_buf =\"Kannada_MNIST_KERAS.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}