{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":false},"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 in \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 \"../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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.losses import categorical_crossentropy\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Activation,BatchNormalization,Conv2D,Dense,Dropout,Flatten,MaxPooling2D\nfrom tensorflow.keras.callbacks import LearningRateScheduler,ModelCheckpoint,EarlyStopping\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, accuracy_score, classification_report","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Let's load the data."},{"metadata":{"trusted":false},"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntest = pd.read_csv(\"/kaggle/input/Kannada-MNIST/test.csv\")\nDig_MNIST = pd.read_csv(\"/kaggle/input/Kannada-MNIST/Dig-MNIST.csv\")\nsample_sub = pd.read_csv(\"/kaggle/input/Kannada-MNIST/sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"print(f\"Train shape: {train.shape}\")\nprint(f\"Test shape: {test.shape}\")\nprint(f\"Dif shape: {Dig_MNIST.shape}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We slice the dataframes to define the features and the labels"},{"metadata":{"trusted":false},"cell_type":"code","source":"X=train.iloc[:,1:].values\nY=train.iloc[:,0].values\nY[:10]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now we must reshape the date to make it TensorFlow friendly."},{"metadata":{"trusted":false},"cell_type":"code","source":"X = X.reshape(X.shape[0], 28, 28,1) \nprint(f\"X data shape: {X.shape}\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now we convert the labels to categorical."},{"metadata":{"trusted":false},"cell_type":"code","source":"Y = tf.keras.utils.to_categorical(Y, num_classes=10) \nprint(f\"Y data shape: {Y.shape}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"x_test=test.drop('id', axis=1).iloc[:,:].values\nx_test = x_test.reshape(x_test.shape[0], 28, 28,1)\nprint(f\"x_test shape: {x_test.shape}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"Dig_MNIST.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"x_dig=Dig_MNIST.drop('label',axis=1).iloc[:,:].values\nx_dig = x_dig.reshape(x_dig.shape[0], 28, 28,1)\nprint(f\"x_dig shape: {x_dig.shape}\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"y_dig=Dig_MNIST.label\nprint(f\"y_dig shape: {y_dig.shape}\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We split the data into training and validation set."},{"metadata":{"trusted":false},"cell_type":"code","source":"X_train, X_valid, Y_train, Y_valid = train_test_split(X, Y, test_size = 0.2, random_state=42) ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Visualizing the digits data\n\nFunction to plot one random digit along with its label"},{"metadata":{"trusted":false},"cell_type":"code","source":"def plot_random_digit():\n    random_index = np.random.randint(0,X_train.shape[0])\n    plt.imshow(X_train[random_index][:,:,0], cmap='gray')\n    index = tf.argmax(Y_train[random_index], axis=0)\n    plt.title(index.numpy())\n    plt.axis(\"Off\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"plt.figure(figsize=[2,2])\nplot_random_digit()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Execute the cell multiple times to see random examples.\n\nLooking at 50 samples at one go"},{"metadata":{"trusted":false},"cell_type":"code","source":"plt.figure(figsize=[10,6])\nfor i in range(50):\n    plt.subplot(5, 10, i+1)\n    plt.imshow(X_train[i][:,:,0], cmap='gray')\n    plt.axis('Off')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We use TensorFlow ImageDataGenerator to artificially increase our training set"},{"metadata":{"trusted":false},"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale = 1./255,\n                                   rotation_range = 10,\n                                   width_shift_range = 0.25,\n                                   height_shift_range = 0.25,\n                                   shear_range = 0.1,\n                                   zoom_range = 0.25,\n                                   horizontal_flip=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"valid_datagen = ImageDataGenerator(rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"model = Sequential([\n    Conv2D(64, 3, padding='same', input_shape=(28, 28, 1)),\n    BatchNormalization(scale=False, center=True),\n    Activation('relu'),\n    Conv2D(64, 3, padding='same'),\n    BatchNormalization(scale=False, center=True),\n    Activation('relu'),\n    MaxPooling2D(2,2),\n    Dropout(0.2),\n\n    Conv2D(128, 3, padding='same'),\n    BatchNormalization(scale=False, center=True),\n    Activation('relu'),\n    Conv2D(128, 3, padding='same'),\n    BatchNormalization(scale=False, center=True),\n    Activation('relu'),\n    MaxPooling2D(2,2),\n    Dropout(0.2),\n    \n    Conv2D(256, 3, padding='same'),\n    BatchNormalization(scale=False, center=True),\n    Activation('relu'),\n    Conv2D(256, 3, padding='same'),\n    BatchNormalization(scale=False, center=True),\n    Activation('relu'),\n    MaxPooling2D(2,2),\n    Dropout(0.2),\n    \n    Flatten(),\n    Dense(256),\n    BatchNormalization(scale=False, center=True),\n    Activation('relu'),\n    Dense(10, activation='softmax')\n])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"tf.keras.utils.plot_model(model, to_file=\"model.png\", show_shapes=True)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Specify the training configuration (optimizer, loss, metrics)"},{"metadata":{"trusted":false},"cell_type":"code","source":"learning_rate=0.001\nbatch_size = 1024\nepochs = 50\nsteps_per_epoch = 100\nvalidation_steps = 50","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"model.compile(optimizer=Adam(lr=learning_rate),\n              loss=categorical_crossentropy,\n              metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The next function reduces the learning rate as the training advances."},{"metadata":{"trusted":false},"cell_type":"code","source":"def scheduler(epoch):\n    return learning_rate * 0.99 ** epoch","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"lr_scheduler = LearningRateScheduler(scheduler)\n\nmodel_checkpoint  = ModelCheckpoint('model_best_checkpoint.h5', save_best_only=True,\n                                    save_weights_only=True, monitor='val_loss', verbose=1)\n\nearly_stopping = EarlyStopping(monitor='val_loss', patience=10, verbose=1)\n\ncallbacks_list = [lr_scheduler, model_checkpoint, early_stopping]","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"history = model.fit_generator(\n      train_datagen.flow(X_train,Y_train,batch_size=batch_size),\n      steps_per_epoch=steps_per_epoch,\n      epochs=epochs,\n      validation_data=valid_datagen.flow(X_valid,Y_valid),\n      validation_steps=validation_steps,\n      verbose=1,\n      callbacks=callbacks_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"accuracy = history.history['accuracy']\nval_accuracy = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(len(accuracy))\n\nplt.figure(figsize=(16, 4))\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, accuracy, label='Training Accuracy')\nplt.plot(epochs_range, val_accuracy, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"model.load_weights('model_best_checkpoint.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"results = model.evaluate(X_valid, Y_valid, batch_size=batch_size, verbose=0)\n\nprint(f\"\\nLoss: {results[0]}\")\nprint(f\"Accuracy: {results[1]}\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Assessing performance on the train set.\nThe predictions of the model for the training data."},{"metadata":{"trusted":false},"cell_type":"code","source":"preds_dig = model.predict_classes(x_dig/255)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Plotting the confusion matrix"},{"metadata":{"trusted":false},"cell_type":"code","source":"cm = confusion_matrix(y_dig, preds_dig)\n\nplt.figure(figsize=[7,6])\nsns.heatmap(cm, cmap=\"Reds\", annot=True, fmt='.0f')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"print(f\"Accuracy: {accuracy_score(y_dig, preds_dig)}\")\nprint(classification_report(y_dig, preds_dig))","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"predictions = model.predict_classes(x_test/255)\npredictions[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"submission = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"submission['label'] = predictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"submission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"submission.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}