{"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":"**Hello , in this small guide we'll be using a simple CNN to predict handwritten digits , i will be using a simple CNN architecture so hope you enjoy it :D .**\n\n\n\n **upvote if you liked this work :)**\n\n**well let's get started**","metadata":{}},{"cell_type":"markdown","source":"# 1. Importing all packages","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport os\nimport tensorflow as tf\n\n\nfrom tensorflow.keras.layers import Dense, Flatten , Conv2D , MaxPooling2D , Dropout\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2022-03-14T07:09:40.169073Z","iopub.execute_input":"2022-03-14T07:09:40.169373Z","iopub.status.idle":"2022-03-14T07:09:45.654877Z","shell.execute_reply.started":"2022-03-14T07:09:40.169322Z","shell.execute_reply":"2022-03-14T07:09:45.654168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"now that we have imported all of our necessary packages let's start with the next step.","metadata":{}},{"cell_type":"markdown","source":"# 2. visualising our data","metadata":{}},{"cell_type":"code","source":"path = '../input/digit-recognizer/train.csv'\ndf = pd.read_csv(path)\nval = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-14T07:09:45.660157Z","iopub.execute_input":"2022-03-14T07:09:45.660605Z","iopub.status.idle":"2022-03-14T07:09:50.797203Z","shell.execute_reply.started":"2022-03-14T07:09:45.660568Z","shell.execute_reply":"2022-03-14T07:09:50.79642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-14T07:09:50.800519Z","iopub.execute_input":"2022-03-14T07:09:50.800726Z","iopub.status.idle":"2022-03-14T07:09:50.828176Z","shell.execute_reply.started":"2022-03-14T07:09:50.800701Z","shell.execute_reply":"2022-03-14T07:09:50.827554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-14T07:09:50.830478Z","iopub.execute_input":"2022-03-14T07:09:50.830706Z","iopub.status.idle":"2022-03-14T07:09:50.883238Z","shell.execute_reply.started":"2022-03-14T07:09:50.830675Z","shell.execute_reply":"2022-03-14T07:09:50.882542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-14T07:09:50.884417Z","iopub.execute_input":"2022-03-14T07:09:50.88469Z","iopub.status.idle":"2022-03-14T07:09:50.892325Z","shell.execute_reply.started":"2022-03-14T07:09:50.884655Z","shell.execute_reply":"2022-03-14T07:09:50.891615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we will display the number of entries in each categorie","metadata":{}},{"cell_type":"code","source":"data_counts = pd.DataFrame({'labels':[0,1,2,3,4,5,6,7,8,9], 'values':[4132,4684,4177,4351,4072,3795,4137,4401,4063,4188]})","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-03-14T07:09:50.89357Z","iopub.execute_input":"2022-03-14T07:09:50.894302Z","iopub.status.idle":"2022-03-14T07:09:50.900919Z","shell.execute_reply.started":"2022-03-14T07:09:50.894263Z","shell.execute_reply":"2022-03-14T07:09:50.900179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"this is a simple plot to visualise the number of entires in each catogries using barplot","metadata":{}},{"cell_type":"code","source":"sns.set_theme(style=\"whitegrid\")\nsns.barplot(x='labels',y='values',data=data_counts)","metadata":{"execution":{"iopub.status.busy":"2022-03-14T07:09:50.902093Z","iopub.execute_input":"2022-03-14T07:09:50.902343Z","iopub.status.idle":"2022-03-14T07:09:51.180507Z","shell.execute_reply.started":"2022-03-14T07:09:50.902306Z","shell.execute_reply":"2022-03-14T07:09:51.179776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we define a target for our dataset","metadata":{}},{"cell_type":"code","source":"y = df['label']\nx = df.drop('label',axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-03-14T07:09:51.181906Z","iopub.execute_input":"2022-03-14T07:09:51.1822Z","iopub.status.idle":"2022-03-14T07:09:51.265762Z","shell.execute_reply.started":"2022-03-14T07:09:51.182166Z","shell.execute_reply":"2022-03-14T07:09:51.264808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scalling it to a readabel\nx_train = x/255.0\nval = val/255.0\nx_train = x_train.values.reshape(-1,28,28,1)\nval = val.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2022-03-14T07:09:51.26947Z","iopub.execute_input":"2022-03-14T07:09:51.269679Z","iopub.status.idle":"2022-03-14T07:09:51.435529Z","shell.execute_reply.started":"2022-03-14T07:09:51.269652Z","shell.execute_reply":"2022-03-14T07:09:51.434756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(25,25))\nfor i in range(18):\n    plt.subplot(9,6,i+1)\n    plt.axis('off')\n    plt.imshow(x_train[i],cmap='gray')\n    plt.title(y[i])\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-14T07:09:51.436869Z","iopub.execute_input":"2022-03-14T07:09:51.437196Z","iopub.status.idle":"2022-03-14T07:09:52.728058Z","shell.execute_reply.started":"2022-03-14T07:09:51.437156Z","shell.execute_reply":"2022-03-14T07:09:52.727263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"now that we have visualised and prepared ou dataset time to build our model\n\n# 3. Building the model","metadata":{}},{"cell_type":"markdown","source":"i made a 3 layer convolutional network , with first layer being a 32 filters along with 1x1 padding ,, and one 64 filter layer with 3x3 padding and two 128 filters with 5x5 padding , i also added some dropout layers of 10%  , lastely a simple dense layer with 128 neurons","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, (1, 1), activation='relu', padding = 'Same', input_shape=(28,28,1)))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Dropout(0.1))\nmodel.add(Conv2D(64, (3, 3), activation='relu', padding = 'Same'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Dropout(0.1))\nmodel.add(Conv2D(128, (5, 5), activation='relu', padding = 'Same'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(128, (5, 5), activation='relu', padding = 'Same'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Dropout(0.1))\nmodel.add(Flatten())\nmodel.add(Dense(128 , activation = 'relu'))\nmodel.add(Dense(10, activation = 'softmax'))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-14T07:09:52.729303Z","iopub.execute_input":"2022-03-14T07:09:52.730212Z","iopub.status.idle":"2022-03-14T07:09:55.004367Z","shell.execute_reply.started":"2022-03-14T07:09:52.730164Z","shell.execute_reply":"2022-03-14T07:09:55.003592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. training the model","metadata":{}},{"cell_type":"code","source":"model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(),optimizer='adam' , metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-03-14T07:09:55.005673Z","iopub.execute_input":"2022-03-14T07:09:55.006086Z","iopub.status.idle":"2022-03-14T07:09:55.019059Z","shell.execute_reply.started":"2022-03-14T07:09:55.006042Z","shell.execute_reply":"2022-03-14T07:09:55.018273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set callback functions to early stop training \nmycallbacks = [EarlyStopping(monitor='val_loss', patience=5)]\nhist = model.fit(x_train,y, epochs=50 , callbacks=mycallbacks )","metadata":{"execution":{"iopub.status.busy":"2022-03-14T07:09:55.021754Z","iopub.execute_input":"2022-03-14T07:09:55.022621Z","iopub.status.idle":"2022-03-14T07:14:26.432606Z","shell.execute_reply.started":"2022-03-14T07:09:55.022572Z","shell.execute_reply":"2022-03-14T07:14:26.431893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. evaluating the model","metadata":{}},{"cell_type":"code","source":"plt.plot(hist.history['loss'])\nplt.plot(hist.history['accuracy'])\nplt.legend(['loss','accuracy'],loc='upper right')","metadata":{"execution":{"iopub.status.busy":"2022-03-14T07:14:26.434129Z","iopub.execute_input":"2022-03-14T07:14:26.434503Z","iopub.status.idle":"2022-03-14T07:14:26.67627Z","shell.execute_reply.started":"2022-03-14T07:14:26.434465Z","shell.execute_reply":"2022-03-14T07:14:26.6756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(25,25))\nfor i in range(18):\n    plt.subplot(9,6,i+1)\n    plt.axis('off')\n    plt.imshow(val[i],cmap='Greys')\n    plt.title(model.predict(val[i].reshape(1,28,28,1)).argmax())\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-14T07:14:26.677425Z","iopub.execute_input":"2022-03-14T07:14:26.677828Z","iopub.status.idle":"2022-03-14T07:14:28.464455Z","shell.execute_reply.started":"2022-03-14T07:14:26.67779Z","shell.execute_reply":"2022-03-14T07:14:28.463747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"our model has marked an impressive score for accuracy , but it can of course be improved , thank you for following along this guide.","metadata":{}},{"cell_type":"markdown","source":"# 6. saving results","metadata":{}},{"cell_type":"code","source":"prediction = []\nfor i in range(len(val)):\n    prediction.append(model.predict(val[i].reshape(1,28,28,1)).argmax())\nsubmissions=pd.DataFrame({\"ImageId\": list(range(1,len(prediction)+1)),\"Label\": prediction})\nsubmissions.to_csv(\"submission.csv\", index=False, header=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-14T07:14:28.465966Z","iopub.execute_input":"2022-03-14T07:14:28.466448Z","iopub.status.idle":"2022-03-14T07:30:25.17292Z","shell.execute_reply.started":"2022-03-14T07:14:28.466399Z","shell.execute_reply":"2022-03-14T07:30:25.172193Z"},"trusted":true},"execution_count":null,"outputs":[]}]}