{"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":" \n<h1 align=\"center\"> CNN FOR BEGINNERS </h1>\n\n\n\n### Hello, I'm Prathish and an absolute beginner at CNN. My aim of this project is to learn to use CNN properly and to serve this notebook as a guide to my fellow beginners. Let's Start.","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:47:20.570378Z","iopub.execute_input":"2022-07-12T13:47:20.571073Z","iopub.status.idle":"2022-07-12T13:47:20.578910Z","shell.execute_reply.started":"2022-07-12T13:47:20.571032Z","shell.execute_reply":"2022-07-12T13:47:20.577828Z"}}},{"cell_type":"code","source":"# Importing essential libraries\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\n# Input data files are available in the read-only \"../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# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-13T15:35:06.331251Z","iopub.execute_input":"2022-07-13T15:35:06.332220Z","iopub.status.idle":"2022-07-13T15:35:06.340048Z","shell.execute_reply.started":"2022-07-13T15:35:06.332180Z","shell.execute_reply":"2022-07-13T15:35:06.338857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reading the train and test datasets from Kaggle\nmnist_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nmnist_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nmnist_output =  pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:35:06.830723Z","iopub.execute_input":"2022-07-13T15:35:06.831554Z","iopub.status.idle":"2022-07-13T15:35:11.169486Z","shell.execute_reply.started":"2022-07-13T15:35:06.831505Z","shell.execute_reply":"2022-07-13T15:35:11.168311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Shape of the train and test dataframes\nprint('train shape: ', mnist_train.shape)\nprint('test shape: ', mnist_test.shape)\nmnist_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:35:11.171377Z","iopub.execute_input":"2022-07-13T15:35:11.171790Z","iopub.status.idle":"2022-07-13T15:35:11.189676Z","shell.execute_reply.started":"2022-07-13T15:35:11.171759Z","shell.execute_reply":"2022-07-13T15:35:11.188971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#This program will require Keras, so to check if you have installed Keras or the version of it, run this snippet\n\nimport keras\nkeras.__version__\n\n#If not, you need install Keras ('pip install keras')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:39:52.845474Z","iopub.execute_input":"2022-07-13T15:39:52.845897Z","iopub.status.idle":"2022-07-13T15:39:52.854614Z","shell.execute_reply.started":"2022-07-13T15:39:52.845847Z","shell.execute_reply":"2022-07-13T15:39:52.852968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Spliting into X and y\n\nX = mnist_train.drop('label' , axis = 1)\ny = np.array(mnist_train['label'])","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:35:20.363168Z","iopub.execute_input":"2022-07-13T15:35:20.364086Z","iopub.status.idle":"2022-07-13T15:35:20.490072Z","shell.execute_reply.started":"2022-07-13T15:35:20.364053Z","shell.execute_reply":"2022-07-13T15:35:20.489072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Perform train test split so that we can validate the model\nfrom sklearn.model_selection import train_test_split\nX_train,X_test,y_train,y_test = train_test_split(X, y, test_size=0.2, random_state=42)\nX_train","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:35:20.491581Z","iopub.execute_input":"2022-07-13T15:35:20.491923Z","iopub.status.idle":"2022-07-13T15:35:20.827693Z","shell.execute_reply.started":"2022-07-13T15:35:20.491884Z","shell.execute_reply":"2022-07-13T15:35:20.826443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#FLatten or normalize the values from 0-255, so that computation will be easier\nX_train_flattened = X_train/255\nX_test_flattened = X_test/255","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:38:02.597122Z","iopub.execute_input":"2022-07-13T15:38:02.597568Z","iopub.status.idle":"2022-07-13T15:38:02.699480Z","shell.execute_reply.started":"2022-07-13T15:38:02.597534Z","shell.execute_reply":"2022-07-13T15:38:02.698120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reshaping the dataframe from 1x784 to 28x28\nX_train_flattened = np.array(X_train_flattened).reshape(-1,28,28,1)\nX_test_flattened = np.array(X_test_flattened).reshape(-1,28,28,1)\n\n#To know why -1 in reshape, check this out https://stackoverflow.com/questions/18691084/what-does-1-mean-in-numpy-reshape\n\n# And 1 because it has single layer. If it is RGB then replace with 3","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:35:20.916712Z","iopub.execute_input":"2022-07-13T15:35:20.917051Z","iopub.status.idle":"2022-07-13T15:35:21.053254Z","shell.execute_reply.started":"2022-07-13T15:35:20.917021Z","shell.execute_reply":"2022-07-13T15:35:21.052260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nplt.imshow(X_train_flattened[2][:])","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:35:21.055220Z","iopub.execute_input":"2022-07-13T15:35:21.055638Z","iopub.status.idle":"2022-07-13T15:35:21.418368Z","shell.execute_reply.started":"2022-07-13T15:35:21.055607Z","shell.execute_reply":"2022-07-13T15:35:21.417336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Showing the grayscale image\nplt.imshow(X_train_flattened[147][:], cmap='gray_r')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:35:26.230148Z","iopub.execute_input":"2022-07-13T15:35:26.230849Z","iopub.status.idle":"2022-07-13T15:35:26.361355Z","shell.execute_reply.started":"2022-07-13T15:35:26.230806Z","shell.execute_reply":"2022-07-13T15:35:26.360555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Buliding ","metadata":{}},{"cell_type":"code","source":"#Import necessary modules\nimport tensorflow as tf\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.layers import Dense, Flatten, Conv2D, BatchNormalization, MaxPool2D, Dropout","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:35:29.042803Z","iopub.execute_input":"2022-07-13T15:35:29.043754Z","iopub.status.idle":"2022-07-13T15:35:29.475013Z","shell.execute_reply.started":"2022-07-13T15:35:29.043719Z","shell.execute_reply":"2022-07-13T15:35:29.473811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#We will use sequential model, which is feed forward\n\nmodel = tf.keras.models.Sequential()\n\n#We will use built-in flatten which will convert the 28x28 into a single one dimentional array\n\nmodel.add(tf.keras.layers.Flatten())\nmodel.add(tf.keras.layers.Dense(512, activation = tf.nn.relu))\nmodel.add(keras.layers.BatchNormalization())\nmodel.add(tf.keras.layers.Dense(256, activation = tf.nn.relu))\nmodel.add(tf.keras.layers.Dense(256, activation = tf.nn.relu))\nmodel.add(tf.keras.layers.Dense(10, activation = tf.nn.softmax))\n\n#We will now compile the model specifing the loss, optimizer and metric\nmodel.compile(optimizer = 'adam',\n             loss = 'sparse_categorical_crossentropy',\n             metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:35:31.627262Z","iopub.execute_input":"2022-07-13T15:35:31.628149Z","iopub.status.idle":"2022-07-13T15:35:31.736492Z","shell.execute_reply.started":"2022-07-13T15:35:31.628099Z","shell.execute_reply":"2022-07-13T15:35:31.735413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Now we will fit the model\n\nmodel.fit(X_train_flattened, y_train, epochs = 10)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:35:36.258684Z","iopub.execute_input":"2022-07-13T15:35:36.259338Z","iopub.status.idle":"2022-07-13T15:36:59.284280Z","shell.execute_reply.started":"2022-07-13T15:35:36.259297Z","shell.execute_reply":"2022-07-13T15:36:59.282692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#The 98% looks attractive, but the neural nets are great at fitting, the question is do they overfit?\n#To answer that question, let us check the validation set\n\nval_loss, val_acc = model.evaluate(X_test_flattened, y_test)\nprint(val_acc, val_loss)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:36:59.287851Z","iopub.execute_input":"2022-07-13T15:36:59.289253Z","iopub.status.idle":"2022-07-13T15:37:00.694137Z","shell.execute_reply.started":"2022-07-13T15:36:59.289147Z","shell.execute_reply":"2022-07-13T15:37:00.692578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Seems like the model is doing well as the val_acc is close to the trained accuracy\n# Summary of the model.\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:37:00.695700Z","iopub.execute_input":"2022-07-13T15:37:00.696390Z","iopub.status.idle":"2022-07-13T15:37:00.703598Z","shell.execute_reply.started":"2022-07-13T15:37:00.696352Z","shell.execute_reply":"2022-07-13T15:37:00.702066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Now let us predict the test csv which we need to submit\n\nX_sub_flattened = mnist_test/255\nX_sub_flattened = np.array(X_sub_flattened).reshape(-1,28,28,1)\n\nmnist_sub = model.predict(X_sub_flattened)\npredicted = mnist_sub.argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:42:13.118617Z","iopub.execute_input":"2022-07-13T15:42:13.119077Z","iopub.status.idle":"2022-07-13T15:42:15.847609Z","shell.execute_reply.started":"2022-07-13T15:42:13.119044Z","shell.execute_reply":"2022-07-13T15:42:15.846226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking the type of the prediction\nprint(type(predicted))\nprint(predicted.shape)\npredicted[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:42:33.743834Z","iopub.execute_input":"2022-07-13T15:42:33.744264Z","iopub.status.idle":"2022-07-13T15:42:33.753370Z","shell.execute_reply.started":"2022-07-13T15:42:33.744232Z","shell.execute_reply":"2022-07-13T15:42:33.752085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# For the submission format we need to have a df with 2 columns (ImageId and Label), so we create ImageId and join with the label\n\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),pd.Series(predicted, name ='Label')],axis = 1)\nsubmission.info()\n\n#As you can see the 'Label' column is in float64 but in submission, we need it be be in int","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:43:18.384780Z","iopub.execute_input":"2022-07-13T15:43:18.385212Z","iopub.status.idle":"2022-07-13T15:43:18.407209Z","shell.execute_reply.started":"2022-07-13T15:43:18.385179Z","shell.execute_reply":"2022-07-13T15:43:18.406181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Export it as .csv\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T15:45:38.704064Z","iopub.execute_input":"2022-07-13T15:45:38.704494Z","iopub.status.idle":"2022-07-13T15:45:38.761330Z","shell.execute_reply.started":"2022-07-13T15:45:38.704460Z","shell.execute_reply":"2022-07-13T15:45:38.760397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Now just submit the submission.csv. I hope the readers understood the notebook. If you have any doubts please comment and I will try to explain.\n\n## Do not forget to upvote.\n\n### By Prathish Murugan","metadata":{}}]}