{"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":"markdown","source":"# Importation  \n\nImport the neccessary Libraries","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport pandas as pd # To import the dataset\nimport numpy as np # For Numerical operation\nimport matplotlib.pyplot as plt # Performing basic visualization operations\nimport random # Perform things at random\nimport cv2 as cv\n\nfrom sklearn.metrics import classification_report, confusion_matrix\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:46:36.652084Z","iopub.execute_input":"2023-04-29T20:46:36.652499Z","iopub.status.idle":"2023-04-29T20:46:36.659238Z","shell.execute_reply.started":"2023-04-29T20:46:36.652465Z","shell.execute_reply":"2023-04-29T20:46:36.657771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get the data. \n\nThe dataset used in this competition was gotten from this kaggle page you can check it out and get more insights","metadata":{}},{"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 = pd.read_csv('/kaggle/input/Kannada-MNIST/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:46:36.669438Z","iopub.execute_input":"2023-04-29T20:46:36.669720Z","iopub.status.idle":"2023-04-29T20:46:40.591038Z","shell.execute_reply.started":"2023-04-29T20:46:36.669693Z","shell.execute_reply":"2023-04-29T20:46:40.589944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Exploration \n\nLets take a look at the data","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:46:40.593647Z","iopub.execute_input":"2023-04-29T20:46:40.594134Z","iopub.status.idle":"2023-04-29T20:46:40.612707Z","shell.execute_reply.started":"2023-04-29T20:46:40.594071Z","shell.execute_reply":"2023-04-29T20:46:40.611417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.label.unique()","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:46:40.614691Z","iopub.execute_input":"2023-04-29T20:46:40.615120Z","iopub.status.idle":"2023-04-29T20:46:40.626252Z","shell.execute_reply.started":"2023-04-29T20:46:40.615059Z","shell.execute_reply":"2023-04-29T20:46:40.625146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:46:40.628954Z","iopub.execute_input":"2023-04-29T20:46:40.629581Z","iopub.status.idle":"2023-04-29T20:46:40.637643Z","shell.execute_reply.started":"2023-04-29T20:46:40.629541Z","shell.execute_reply":"2023-04-29T20:46:40.636466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can see that the dataset contains about 10 unique labels which stands for  0 - 9 in the number system of the host company.The dataset looks like a comma seperated dataset where each of the columns represent a parcular data","metadata":{}},{"cell_type":"markdown","source":"## Visualize the Images","metadata":{}},{"cell_type":"markdown","source":"### Create function to visualize image","metadata":{}},{"cell_type":"code","source":"def displayimage(img, label = None, ax = None):\n    plt.imshow(img, cmap = 'gray') # Display the image\n    plt.title(label) # Set the title \n    plt.xticks([]) # Remove Xticks\n    plt.yticks([]) # Remove yticks","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:46:40.639209Z","iopub.execute_input":"2023-04-29T20:46:40.639752Z","iopub.status.idle":"2023-04-29T20:46:40.647412Z","shell.execute_reply.started":"2023-04-29T20:46:40.639713Z","shell.execute_reply":"2023-04-29T20:46:40.646108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_image(data, ytrain = False):\n    '''\n    These function convert the dataframe to 2 by 2 matrix for bette visulaization\n    \n    args\n    ---------\n    data: These is the dataset\n    ytrain: optional. This is the y part\n    \n    return\n    ------\n    X: A numpy array filled with the data\n    y: A numpy array filled with the labels\n    \n    '''\n    train_numpy = data.iloc[:, 1:].to_numpy()\n    \n    X = train_numpy.reshape((data.shape[0], 28, 28))\n    \n    if(ytrain):\n        y = data.iloc[:, 0].to_numpy()\n    else:\n        y = None\n        \n    \n    return X, y","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:46:40.648931Z","iopub.execute_input":"2023-04-29T20:46:40.649386Z","iopub.status.idle":"2023-04-29T20:46:40.658855Z","shell.execute_reply.started":"2023-04-29T20:46:40.649346Z","shell.execute_reply":"2023-04-29T20:46:40.657777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X, y = convert_image(train, True)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:46:40.660486Z","iopub.execute_input":"2023-04-29T20:46:40.660995Z","iopub.status.idle":"2023-04-29T20:46:40.668831Z","shell.execute_reply.started":"2023-04-29T20:46:40.660956Z","shell.execute_reply":"2023-04-29T20:46:40.667817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (2, 2))\ndisplayimage(X[0], f'The label is {y[0]}')","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-04-29T20:46:40.670129Z","iopub.execute_input":"2023-04-29T20:46:40.670941Z","iopub.status.idle":"2023-04-29T20:46:40.746520Z","shell.execute_reply.started":"2023-04-29T20:46:40.670897Z","shell.execute_reply":"2023-04-29T20:46:40.744796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let display all the letters \nplt.figure(figsize=(10,4))\nfor i in range(10):\n    plt.subplot(2, 5, i+1)\n    index = random.choice(np.where(y == i)[0]) # I am picking the letters randomly.\n    displayimage(X[index], f'Label {y[index]}')\n    \nplt.suptitle(\"Train Visualization\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:46:40.748203Z","iopub.execute_input":"2023-04-29T20:46:40.748932Z","iopub.status.idle":"2023-04-29T20:46:41.245272Z","shell.execute_reply.started":"2023-04-29T20:46:40.748874Z","shell.execute_reply":"2023-04-29T20:46:41.244074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Run that portion again and you would see it would generate a new image everytime. It is good practise to check lots of samples of the dataset before pushing it to production. ","metadata":{}},{"cell_type":"code","source":"test, _ = convert_image(test)\ntest.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:46:41.250053Z","iopub.execute_input":"2023-04-29T20:46:41.251361Z","iopub.status.idle":"2023-04-29T20:46:41.259325Z","shell.execute_reply.started":"2023-04-29T20:46:41.251316Z","shell.execute_reply":"2023-04-29T20:46:41.258060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let display all the letters \nplt.figure(figsize=(10,4))\nfor i in range(10):\n    plt.subplot(2, 5, i+1)\n    displayimage(test[i])\n    \nplt.suptitle(\"Test Visualiztion\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:46:41.261288Z","iopub.execute_input":"2023-04-29T20:46:41.261729Z","iopub.status.idle":"2023-04-29T20:46:41.683068Z","shell.execute_reply.started":"2023-04-29T20:46:41.261690Z","shell.execute_reply":"2023-04-29T20:46:41.682012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"It is good practise to check out both the test and the train dataset. \n\nThe dataset is in the right format hence there is no need to tranform the dataset. We might only need to scale it.","metadata":{}},{"cell_type":"markdown","source":"# Train the model","metadata":{}},{"cell_type":"markdown","source":"## Train Test Split","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nXtrain, Xtest, ytrain, ytest = train_test_split(X, y)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:46:41.685445Z","iopub.execute_input":"2023-04-29T20:46:41.686247Z","iopub.status.idle":"2023-04-29T20:46:42.220286Z","shell.execute_reply.started":"2023-04-29T20:46:41.686200Z","shell.execute_reply":"2023-04-29T20:46:42.219130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Flatten, Dense, Rescaling, Conv2D, Reshape, MaxPool2D\nfrom tensorflow.keras import Sequential","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:49:44.278679Z","iopub.execute_input":"2023-04-29T20:49:44.279484Z","iopub.status.idle":"2023-04-29T20:49:44.284986Z","shell.execute_reply.started":"2023-04-29T20:49:44.279443Z","shell.execute_reply":"2023-04-29T20:49:44.283662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.random.set_seed(42) # I added this to make the model reproducible","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:46:42.230288Z","iopub.execute_input":"2023-04-29T20:46:42.231042Z","iopub.status.idle":"2023-04-29T20:46:42.263854Z","shell.execute_reply.started":"2023-04-29T20:46:42.231000Z","shell.execute_reply":"2023-04-29T20:46:42.262864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Base Model","metadata":{}},{"cell_type":"code","source":"basemodel = Sequential([\n    Rescaling(1./225.5),\n    Flatten(input_shape = (28, 28)), # input layer\n    Dense(100, activation = 'relu'), # Hidden layer\n    Dense(10, activation = 'softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:46:42.272024Z","iopub.execute_input":"2023-04-29T20:46:42.272317Z","iopub.status.idle":"2023-04-29T20:46:42.295314Z","shell.execute_reply.started":"2023-04-29T20:46:42.272291Z","shell.execute_reply":"2023-04-29T20:46:42.294115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basemodel.compile(optimizer='Adam', \n             loss = 'SparseCategoricalCrossentropy',\n             metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:46:42.298039Z","iopub.execute_input":"2023-04-29T20:46:42.298941Z","iopub.status.idle":"2023-04-29T20:46:42.316137Z","shell.execute_reply.started":"2023-04-29T20:46:42.298900Z","shell.execute_reply":"2023-04-29T20:46:42.315153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basemodelhistory = basemodel.fit(Xtrain, ytrain, epochs = 10, validation_split = 0.2)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:46:42.317633Z","iopub.execute_input":"2023-04-29T20:46:42.318225Z","iopub.status.idle":"2023-04-29T20:47:21.691360Z","shell.execute_reply.started":"2023-04-29T20:46:42.318183Z","shell.execute_reply":"2023-04-29T20:47:21.690280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Evaluate the model","metadata":{}},{"cell_type":"code","source":"basemodel.evaluate(Xtest, ytest)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:47:21.693288Z","iopub.execute_input":"2023-04-29T20:47:21.693587Z","iopub.status.idle":"2023-04-29T20:47:23.220957Z","shell.execute_reply.started":"2023-04-29T20:47:21.693558Z","shell.execute_reply":"2023-04-29T20:47:23.219941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.squeeze(tf.argmax(basemodel.predict(Xtest), axis = 1))","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:47:23.222949Z","iopub.execute_input":"2023-04-29T20:47:23.224165Z","iopub.status.idle":"2023-04-29T20:47:24.784269Z","shell.execute_reply.started":"2023-04-29T20:47:23.224123Z","shell.execute_reply":"2023-04-29T20:47:24.783033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_confusion(ytrue, ypred):\n    cm = confusion_matrix(ytrue, ypred)\n    plt.figure(figsize = (10, 10))\n    sns.heatmap(cm, annot =True)\n    plt.axis(False)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:47:24.786275Z","iopub.execute_input":"2023-04-29T20:47:24.786662Z","iopub.status.idle":"2023-04-29T20:47:24.792998Z","shell.execute_reply.started":"2023-04-29T20:47:24.786623Z","shell.execute_reply":"2023-04-29T20:47:24.791804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basepred = tf.squeeze(tf.argmax(basemodel.predict(Xtest), axis = 1))\n\nprint(classification_report(ytest, basepred))\nprint(plot_confusion(ytest, basepred))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-04-29T20:47:24.794996Z","iopub.execute_input":"2023-04-29T20:47:24.795400Z","iopub.status.idle":"2023-04-29T20:47:26.972140Z","shell.execute_reply.started":"2023-04-29T20:47:24.795354Z","shell.execute_reply":"2023-04-29T20:47:26.971080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_loss_curves(History, ax1 = None, ax2 = None):\n    import pandas as pd\n    pdHistory = pd.DataFrame(History.history)\n    plt.figure()\n    \n    loss = pdHistory['loss']\n    val_loss = pdHistory['val_loss']\n    \n    accuracy = pdHistory['accuracy']\n    val_accuracy = pdHistory['val_accuracy']\n    \n    plt.figure()\n    loss.plot(kind = 'line', label = 'training loss')\n    val_loss.plot(kind = 'line', label = 'Valuation loss')\n    plt.title('Loss')\n    plt.legend()\n    \n    plt.figure()\n    accuracy.plot(kind = 'line', label = 'training Accuracy')\n    val_accuracy.plot(kind = 'line', label = 'Valuation Accuracy')\n    plt.title('Accuracy')\n    plt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:47:26.973769Z","iopub.execute_input":"2023-04-29T20:47:26.974468Z","iopub.status.idle":"2023-04-29T20:47:26.982015Z","shell.execute_reply.started":"2023-04-29T20:47:26.974425Z","shell.execute_reply":"2023-04-29T20:47:26.980883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_loss_curves(basemodelhistory)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:47:26.983638Z","iopub.execute_input":"2023-04-29T20:47:26.984505Z","iopub.status.idle":"2023-04-29T20:47:27.446827Z","shell.execute_reply.started":"2023-04-29T20:47:26.984442Z","shell.execute_reply":"2023-04-29T20:47:27.445564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Improve the model\n\nI tried improving the model but I realize that the model did not improve check out the next model below. It looked like it overfit the dataset. ","metadata":{}},{"cell_type":"code","source":"model1 = Sequential([\n    Rescaling(1./225.5),\n    Flatten(input_shape = (28, 28)), # input layer\n    Dense(100, activation = 'relu'), # Hidden layer\n    Dense(10, activation = 'relu'),\n    Dense(10, activation = 'softmax') # Output layer\n])","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:47:27.448782Z","iopub.execute_input":"2023-04-29T20:47:27.449188Z","iopub.status.idle":"2023-04-29T20:47:27.463373Z","shell.execute_reply.started":"2023-04-29T20:47:27.449147Z","shell.execute_reply":"2023-04-29T20:47:27.462077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.compile(optimizer='Adam', \n             loss = 'SparseCategoricalCrossentropy',\n             metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:47:27.465227Z","iopub.execute_input":"2023-04-29T20:47:27.465647Z","iopub.status.idle":"2023-04-29T20:47:27.480058Z","shell.execute_reply.started":"2023-04-29T20:47:27.465580Z","shell.execute_reply":"2023-04-29T20:47:27.478915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history1 = model1.fit(Xtrain, ytrain, epochs = 20, validation_split = 0.2)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:47:27.481667Z","iopub.execute_input":"2023-04-29T20:47:27.482026Z","iopub.status.idle":"2023-04-29T20:48:49.733998Z","shell.execute_reply.started":"2023-04-29T20:47:27.481990Z","shell.execute_reply":"2023-04-29T20:48:49.732865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:48:49.735961Z","iopub.execute_input":"2023-04-29T20:48:49.736656Z","iopub.status.idle":"2023-04-29T20:48:49.758224Z","shell.execute_reply.started":"2023-04-29T20:48:49.736608Z","shell.execute_reply":"2023-04-29T20:48:49.757388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Evaluate the model","metadata":{}},{"cell_type":"code","source":"model1.evaluate(Xtest, ytest)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:48:49.766156Z","iopub.execute_input":"2023-04-29T20:48:49.766590Z","iopub.status.idle":"2023-04-29T20:48:51.253943Z","shell.execute_reply.started":"2023-04-29T20:48:49.766552Z","shell.execute_reply":"2023-04-29T20:48:51.252940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1pred = tf.argmax(model1.predict(Xtest), axis = 1)\nmodel1pred","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:48:51.255241Z","iopub.execute_input":"2023-04-29T20:48:51.256591Z","iopub.status.idle":"2023-04-29T20:48:52.648242Z","shell.execute_reply.started":"2023-04-29T20:48:51.256549Z","shell.execute_reply":"2023-04-29T20:48:52.647158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(ytest, model1pred))\nplot_confusion(ytest, model1pred)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:48:52.649567Z","iopub.execute_input":"2023-04-29T20:48:52.650136Z","iopub.status.idle":"2023-04-29T20:48:53.324095Z","shell.execute_reply.started":"2023-04-29T20:48:52.650082Z","shell.execute_reply":"2023-04-29T20:48:53.323015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot the loss curve","metadata":{}},{"cell_type":"code","source":"plot_loss_curves(history1)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:48:53.325783Z","iopub.execute_input":"2023-04-29T20:48:53.326527Z","iopub.status.idle":"2023-04-29T20:48:53.831160Z","shell.execute_reply.started":"2023-04-29T20:48:53.326488Z","shell.execute_reply":"2023-04-29T20:48:53.830045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The loss curve is looking very funny. I don't understand it at all. It is increasing which is not good. Looks like the model is overfitting","metadata":{}},{"cell_type":"markdown","source":"#  Convolutional Neural Network ","metadata":{}},{"cell_type":"code","source":"Xtrain = Xtrain.reshape(Xtrain.shape[0], 1, 28, 28,)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:48:53.832715Z","iopub.execute_input":"2023-04-29T20:48:53.833103Z","iopub.status.idle":"2023-04-29T20:48:53.838644Z","shell.execute_reply.started":"2023-04-29T20:48:53.833051Z","shell.execute_reply":"2023-04-29T20:48:53.837303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xtrain = tf.cast(Xtrain, tf.float16)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:48:53.840349Z","iopub.execute_input":"2023-04-29T20:48:53.840778Z","iopub.status.idle":"2023-04-29T20:48:54.161523Z","shell.execute_reply.started":"2023-04-29T20:48:53.840739Z","shell.execute_reply":"2023-04-29T20:48:54.160143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xtrain.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:48:54.163037Z","iopub.execute_input":"2023-04-29T20:48:54.164050Z","iopub.status.idle":"2023-04-29T20:48:54.171785Z","shell.execute_reply.started":"2023-04-29T20:48:54.164016Z","shell.execute_reply":"2023-04-29T20:48:54.170626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ConvModel = Sequential([\n    Reshape((28, 28, 1)),\n    Rescaling(1./225.5),\n    Conv2D(25, kernel_size=(3,3), strides=(1,1), padding='valid', activation='relu', input_shape=(28,28,1)),\n    MaxPool2D(pool_size=(1,1)),\n    Flatten(),\n    Dense(100, activation = 'relu'), # Hidden layer\n    Dense(10, activation = 'softmax') # Output layer\n])","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:49:50.533707Z","iopub.execute_input":"2023-04-29T20:49:50.534773Z","iopub.status.idle":"2023-04-29T20:49:50.553476Z","shell.execute_reply.started":"2023-04-29T20:49:50.534721Z","shell.execute_reply":"2023-04-29T20:49:50.552261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ConvModel.compile(optimizer='Adam', \n             loss = 'SparseCategoricalCrossentropy',\n             metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:49:50.715020Z","iopub.execute_input":"2023-04-29T20:49:50.715545Z","iopub.status.idle":"2023-04-29T20:49:50.728033Z","shell.execute_reply.started":"2023-04-29T20:49:50.715514Z","shell.execute_reply":"2023-04-29T20:49:50.726870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Convhistory = ConvModel.fit(Xtrain, ytrain, epochs = 10, validation_split = 0.2)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:49:50.925282Z","iopub.execute_input":"2023-04-29T20:49:50.925608Z","iopub.status.idle":"2023-04-29T20:51:13.565639Z","shell.execute_reply.started":"2023-04-29T20:49:50.925578Z","shell.execute_reply":"2023-04-29T20:51:13.564188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ConvModel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:51:13.568866Z","iopub.execute_input":"2023-04-29T20:51:13.569651Z","iopub.status.idle":"2023-04-29T20:51:13.602352Z","shell.execute_reply.started":"2023-04-29T20:51:13.569600Z","shell.execute_reply":"2023-04-29T20:51:13.601492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ConvModel.evaluate(Xtest, ytest)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:51:13.603492Z","iopub.execute_input":"2023-04-29T20:51:13.603998Z","iopub.status.idle":"2023-04-29T20:51:16.693214Z","shell.execute_reply.started":"2023-04-29T20:51:13.603955Z","shell.execute_reply":"2023-04-29T20:51:16.692151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"convpred = tf.argmax(ConvModel(Xtest), axis = 1)\nconvpred","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:51:16.697765Z","iopub.execute_input":"2023-04-29T20:51:16.698151Z","iopub.status.idle":"2023-04-29T20:51:17.299134Z","shell.execute_reply.started":"2023-04-29T20:51:16.698119Z","shell.execute_reply":"2023-04-29T20:51:17.298128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_loss_curves(Convhistory)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:51:17.302511Z","iopub.execute_input":"2023-04-29T20:51:17.302810Z","iopub.status.idle":"2023-04-29T20:51:17.772874Z","shell.execute_reply.started":"2023-04-29T20:51:17.302781Z","shell.execute_reply":"2023-04-29T20:51:17.771870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(ytest, convpred))\nplot_confusion(ytest, convpred)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:51:17.775043Z","iopub.execute_input":"2023-04-29T20:51:17.775927Z","iopub.status.idle":"2023-04-29T20:51:18.784341Z","shell.execute_reply.started":"2023-04-29T20:51:17.775881Z","shell.execute_reply":"2023-04-29T20:51:18.783120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Lets compare all the models we have built","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import precision_score, accuracy_score, f1_score, recall_score\n\ndef calculateScores(y_true, y_pred):\n    return({\"Precision\": precision_score(y_true, y_pred, average = 'weighted'),\n            \"Accuracy\": accuracy_score(y_true, y_pred),\n            \"f1 Score\": f1_score(y_true, y_pred, average = 'weighted'),\n            \"Recall Score\": recall_score(y_true, y_pred, average = 'weighted')})","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:51:18.785978Z","iopub.execute_input":"2023-04-29T20:51:18.786365Z","iopub.status.idle":"2023-04-29T20:51:18.793385Z","shell.execute_reply.started":"2023-04-29T20:51:18.786324Z","shell.execute_reply":"2023-04-29T20:51:18.791943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nscores = lambda x: calculateScores(ytest, x)\n\nComparison = pd.DataFrame({\"Conv2D\": scores(convpred),\n              \"DenseLayer\": scores(model1pred),\n              \"BaseModel\": scores(basepred)})\n\nComparison = Comparison.transpose()","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:51:18.795239Z","iopub.execute_input":"2023-04-29T20:51:18.795677Z","iopub.status.idle":"2023-04-29T20:51:18.874860Z","shell.execute_reply.started":"2023-04-29T20:51:18.795630Z","shell.execute_reply":"2023-04-29T20:51:18.873823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Comparison.sort_values(\"f1 Score\", ascending = False)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:51:18.876118Z","iopub.execute_input":"2023-04-29T20:51:18.876512Z","iopub.status.idle":"2023-04-29T20:51:18.890685Z","shell.execute_reply.started":"2023-04-29T20:51:18.876469Z","shell.execute_reply":"2023-04-29T20:51:18.889317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Comparison.plot(kind = 'bar')","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:51:18.895587Z","iopub.execute_input":"2023-04-29T20:51:18.896424Z","iopub.status.idle":"2023-04-29T20:51:19.162056Z","shell.execute_reply.started":"2023-04-29T20:51:18.896370Z","shell.execute_reply":"2023-04-29T20:51:19.161103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From the plot it is obvious that the conv model is performing the best has expected","metadata":{}},{"cell_type":"markdown","source":"## Examine the wrongly predicted values","metadata":{}},{"cell_type":"code","source":"mask = convpred != ytest\nImages = Xtest[mask]\npredicted = convpred[mask]\nactual = ytest[mask]\n\nImages.shape, predicted.shape, actual.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:51:19.163745Z","iopub.execute_input":"2023-04-29T20:51:19.164124Z","iopub.status.idle":"2023-04-29T20:51:19.230160Z","shell.execute_reply.started":"2023-04-29T20:51:19.164067Z","shell.execute_reply":"2023-04-29T20:51:19.228955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let display all the letters \nplt.figure(figsize=(10,4))\nfor i in range(10):\n    plt.subplot(2, 5, i+1)\n    index = random.choice(np.where(y == i)[0]) # I am picking the letters randomly.\n    displayimage(X[index], f'Label {y[index]}')\n    \nplt.suptitle(\"Train Visualization\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:51:19.231802Z","iopub.execute_input":"2023-04-29T20:51:19.232188Z","iopub.status.idle":"2023-04-29T20:51:19.688228Z","shell.execute_reply.started":"2023-04-29T20:51:19.232150Z","shell.execute_reply":"2023-04-29T20:51:19.687120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"choice = random.choice(range(163))\ndisplayimage(Images[choice])\nplt.title(f\"Pred Label {predicted[choice]}: Actual Label {actual[choice]}\")","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:51:19.690024Z","iopub.execute_input":"2023-04-29T20:51:19.690404Z","iopub.status.idle":"2023-04-29T20:51:19.816833Z","shell.execute_reply.started":"2023-04-29T20:51:19.690356Z","shell.execute_reply":"2023-04-29T20:51:19.815129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I am satisfied with the model. Let now make predictions","metadata":{}},{"cell_type":"markdown","source":"# Lets make prediction","metadata":{}},{"cell_type":"code","source":"Testpredicton = tf.argmax(ConvModel.predict(test), axis  =1 )\nTestpredicton","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:51:19.818737Z","iopub.execute_input":"2023-04-29T20:51:19.819170Z","iopub.status.idle":"2023-04-29T20:51:20.441726Z","shell.execute_reply.started":"2023-04-29T20:51:19.819125Z","shell.execute_reply":"2023-04-29T20:51:20.440759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample['label'] = Testpredicton\n\nsample.to_csv(\"Submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T20:51:20.444128Z","iopub.execute_input":"2023-04-29T20:51:20.444874Z","iopub.status.idle":"2023-04-29T20:51:20.464295Z","shell.execute_reply.started":"2023-04-29T20:51:20.444833Z","shell.execute_reply":"2023-04-29T20:51:20.463215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}