{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import keras\nfrom keras import layers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix\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))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Load data and normalize it\nroot_dir = \"/kaggle/input\"\ntrain_data = pd.read_csv(root_dir + \"/Kannada-MNIST/train.csv\")\ntrain_labels = train_data['label'].to_numpy()\ntrain_pics = train_data.iloc[:,1:].to_numpy().reshape(-1,28,28,1)/255\n\nval_data = pd.read_csv(root_dir + \"/Kannada-MNIST/Dig-MNIST.csv\")\nval_labels = val_data['label'].to_numpy()\nval_pics = val_data.iloc[:,1:].to_numpy().reshape(-1,28,28,1)/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(train_pics.shape, val_pics.shape)\n\nplt.figure(1)\nplt.hist(train_labels, bins = 10)\nplt.title(\"Histogram of the training data\")\nplt.xlabel(\"Labels\")\nplt.show()\n\nplt.figure(2)\nplt.imshow(train_pics[0].reshape(28,28), cmap = \"gray\")\nplt.title(f\"Example input labeled as {train_labels[0]}\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Data augmentation while training\ndatagen = ImageDataGenerator(\n    rotation_range=10,\n    zoom_range=0.1,\n    width_shift_range=0.1,\n    height_shift_range=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Construct the model\nmodel = keras.Sequential([\n    layers.Input(shape=(28,28,1)),\n    layers.Conv2D(32, 5, padding='same', activation='relu', name='conv1'),\n    layers.Conv2D(32, 5, padding='same', activation='relu', name='conv2'),\n    layers.BatchNormalization(),\n    layers.MaxPooling2D(),\n    layers.Dropout(0.4),\n    \n    layers.Conv2D(32, 3, padding='same', activation='relu', name='conv3'),\n    layers.Conv2D(32, 3, padding='same', activation='relu', name='conv4'),\n    layers.BatchNormalization(),\n    layers.MaxPooling2D(),\n    layers.Dropout(0.4),\n    \n    layers.Conv2D(64, 3, padding='same', activation='relu', name='conv5'),\n    layers.Conv2D(64, 3, padding='same', activation='relu', name='conv6'),\n    layers.BatchNormalization(),\n    layers.MaxPooling2D(),\n    layers.Dropout(0.4),\n    \n    layers.Flatten(),\n    layers.Dense(128, activation='relu'),\n    layers.Dropout(0.4),\n    layers.Dense(64, activation='relu'),\n    layers.Dropout(0.4),\n    layers.Dense(10, activation='softmax')\n])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer=keras.optimizers.Adam(1e-3), \n              loss = keras.losses.SparseCategoricalCrossentropy(),\n              metrics = ['accuracy'])\nbatch_size = 64\nepochs = 20\n\n#Validate model performance\nlog = model.fit(datagen.flow(train_pics,train_labels, batch_size=batch_size), \n                steps_per_epoch=len(train_data)/batch_size,\n                epochs=epochs,verbose=1, validation_data=(val_pics,val_labels))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Plot learning curve\nplt.figure(3)\nplt.plot(log.history['accuracy'],label = 'train_accuracy')\nplt.plot(log.history['val_accuracy'],label = 'val_accuracy')\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Accuracy\")\nplt.legend()\nplt.show();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Plot confusion matrix\ny_valid = model.predict(val_pics)\ny_valid = np.argmax(y_valid, axis=1)\nconf_matrix = confusion_matrix(val_labels, y_valid)\nax = plt.subplot()\nsns.heatmap(conf_matrix, annot=True, ax=ax, cmap='Blues', fmt='3d')\n#labels, title\nax.set_xlabel('Predicted labels')\nax.set_ylabel('True labels')\nax.set_title('Confusion Matrix')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Merge training and validation data for the final training\nmerged_pics = np.vstack((train_pics,val_pics))\nmerged_label =  np.hstack((train_labels,val_labels))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Train with all the available data\nmodel.compile(optimizer=keras.optimizers.Adam(1e-3), \n              loss = keras.losses.SparseCategoricalCrossentropy(),\n              metrics = ['accuracy'])\nlog = model.fit(datagen.flow(merged_pics,merged_label, batch_size=batch_size), \n                steps_per_epoch=len(train_data)/batch_size,\n                epochs=epochs,verbose=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Load test data\ntest_data = pd.read_csv(root_dir + \"/Kannada-MNIST/test.csv\")\nprint(test_data.shape)\ntest_id = np.array(test_data.index)\ntest_pics = test_data.iloc[:,1:].to_numpy().reshape(-1,28,28,1)/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Get predictions for the test data\ntest_prediction = model.predict(test_pics)\ntest_prediction = np.argmax(test_prediction, axis = 1)\nres = pd.DataFrame(np.concatenate([test_id[:, np.newaxis], test_prediction[:,np.newaxis]], axis=1),\n                   columns=[\"id\", \"label\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"res","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"res.to_csv('./submission.csv', index = False)","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":4}