{"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":"# Digit recognizer \n> we will be using CNN (Convolutional Nueral Network)  model fto extract features and recognize digits\n\n## First of all let's import all the required libraries ","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport tensorflow as tf\nimport matplotlib.pyplot as plt \nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten\nfrom tensorflow.keras.models import Sequential\nfrom sklearn.model_selection import train_test_split\nimport cufflinks as cf\ncf.go_offline()","metadata":{"execution":{"iopub.status.busy":"2023-04-13T08:13:17.593906Z","iopub.execute_input":"2023-04-13T08:13:17.594321Z","iopub.status.idle":"2023-04-13T08:13:26.897237Z","shell.execute_reply.started":"2023-04-13T08:13:17.594240Z","shell.execute_reply":"2023-04-13T08:13:26.896230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# reading datasets using Pandas\ntrain = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-04-13T08:13:26.899133Z","iopub.execute_input":"2023-04-13T08:13:26.899815Z","iopub.status.idle":"2023-04-13T08:13:32.051677Z","shell.execute_reply.started":"2023-04-13T08:13:26.899771Z","shell.execute_reply":"2023-04-13T08:13:32.050657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# getting some idea in which manner data is given \ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-13T08:13:32.053255Z","iopub.execute_input":"2023-04-13T08:13:32.053638Z","iopub.status.idle":"2023-04-13T08:13:32.079799Z","shell.execute_reply.started":"2023-04-13T08:13:32.053595Z","shell.execute_reply":"2023-04-13T08:13:32.078713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ploting graph of digits present \ntrain['label'].iplot(kind='hist',bins = 20, xTitle= 'label of digits' , yTitle= 'number of digits', title = 'Digts Frequency Graph')","metadata":{"execution":{"iopub.status.busy":"2023-04-13T08:13:32.082547Z","iopub.execute_input":"2023-04-13T08:13:32.082934Z","iopub.status.idle":"2023-04-13T08:13:33.193166Z","shell.execute_reply.started":"2023-04-13T08:13:32.082899Z","shell.execute_reply":"2023-04-13T08:13:33.192178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> since data is given in data table format (**CSV format**) we will need to convert it into **array format**, thats because tensorflow only accept values that are in Array form  \n\nwe will also need to convert it into **28x28 matrix** since its picture's orignal format.","metadata":{}},{"cell_type":"code","source":"# storing labels into thier respected variables \nlabels = np.array(train.label)\n\n# since we moved lables into their variables, we need to remove its column from the orignal data\ntrain.drop(['label'],axis =1 , inplace=True)\n\n# resizing image into 28x28 matrix ( into orignal image)\ntrain_data = np.array(train).reshape(train.shape[0],28,28)\ntest_data = np.array(test).reshape(test.shape[0],28,28)\n\ntrain_data = np.array(train).reshape(( -1, 28, 28,1))\ntest_data = np.array(test).reshape(( -1, 28, 28,1))","metadata":{"execution":{"iopub.status.busy":"2023-04-13T08:13:33.194232Z","iopub.execute_input":"2023-04-13T08:13:33.194582Z","iopub.status.idle":"2023-04-13T08:13:33.603560Z","shell.execute_reply.started":"2023-04-13T08:13:33.194547Z","shell.execute_reply":"2023-04-13T08:13:33.602555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing images ","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15,10)) # setting figure size\nfor i in range(10): # running loop 10 times to print 10 digit at once\n    plt.subplot(5,5,i+1)\n    index = np.random.randint(0,42000) # picaing random image from our whole dataset\n    plt.imshow(train_data[index],cmap='Greys') # printing picture in black and white format using cmap\n    plt.title(labels[index]) # assigning the labeles to the pictures \n    plt.axis(False) # removing x axis and y axis bars from image ","metadata":{"execution":{"iopub.status.busy":"2023-04-13T08:13:33.604925Z","iopub.execute_input":"2023-04-13T08:13:33.605841Z","iopub.status.idle":"2023-04-13T08:13:34.008817Z","shell.execute_reply.started":"2023-04-13T08:13:33.605794Z","shell.execute_reply":"2023-04-13T08:13:34.007762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# spliting data into training and validation dataset train\ntrain_image , val_image, train_labels, val_labels = train_test_split(train_data,labels,test_size=0.2,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-04-13T08:13:34.010343Z","iopub.execute_input":"2023-04-13T08:13:34.010705Z","iopub.status.idle":"2023-04-13T08:13:34.381556Z","shell.execute_reply.started":"2023-04-13T08:13:34.010667Z","shell.execute_reply":"2023-04-13T08:13:34.380441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Builduing CNN model with TensorFlow\nsome of the parameters we need to pass in the model are given below:\n* input_shape = 28x28x1 (image shape)\n* Output_shape = 10 ( we have only 10 classes to predict)\n* Loss function = SparseCathegoricalCrosstentropy\n* Output layer function = Softmax\n\n**We will be using a combination of CNN and MacPool layers for better feature extraction.**\n> To understand the working of CNN and Pooling layer I will recommand you to visit the following website: https://poloclub.github.io/cnn-explainer/. \nFor me it was a great resource to learn from.\n","metadata":{}},{"cell_type":"code","source":"model = Sequential() # ceating the model \nmodel.add(Conv2D(filters=64, kernel_size=(5,5), activation ='relu', input_shape=(28,28,1)))\nmodel.add(Conv2D(filters = 32, kernel_size = (3,3), activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\n\n\nmodel.add(Conv2D(filters = 32, kernel_size = (3,3), activation ='relu'))\nmodel.add(Conv2D(filters = 16, kernel_size = (3,3), activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\n\nmodel.add(Flatten())\nmodel.add(Dense(64,activation = \"relu\"))\nmodel.add(Dense(10,activation='Softmax',name= \"Output_layer\"))\n\nmodel.compile(loss='sparse_categorical_crossentropy', optimizer ='adam', metrics =['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-13T08:13:34.382891Z","iopub.execute_input":"2023-04-13T08:13:34.383235Z","iopub.status.idle":"2023-04-13T08:13:37.446135Z","shell.execute_reply.started":"2023-04-13T08:13:34.383201Z","shell.execute_reply":"2023-04-13T08:13:37.444895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ploting image of our CNN model pipeline\ntf.keras.utils.plot_model(model)","metadata":{"execution":{"iopub.status.busy":"2023-04-13T08:13:37.447428Z","iopub.execute_input":"2023-04-13T08:13:37.447796Z","iopub.status.idle":"2023-04-13T08:13:38.406228Z","shell.execute_reply.started":"2023-04-13T08:13:37.447758Z","shell.execute_reply":"2023-04-13T08:13:38.405129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fitting model / Training our modell\nhistory = model.fit(train_image,train_labels, validation_data=(val_image,val_labels), epochs=12)","metadata":{"execution":{"iopub.status.busy":"2023-04-13T08:13:38.410519Z","iopub.execute_input":"2023-04-13T08:13:38.410859Z","iopub.status.idle":"2023-04-13T08:15:01.373242Z","shell.execute_reply.started":"2023-04-13T08:13:38.410828Z","shell.execute_reply":"2023-04-13T08:15:01.372185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# now we will plot graph to check the accuracy \nplt.figure(figsize = (12,5))\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Model accuracy')\nplt.xlabel('Accuracy')\nplt.ylabel('Epochs')\nplt.legend(['train','val'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-13T08:15:01.374973Z","iopub.execute_input":"2023-04-13T08:15:01.375586Z","iopub.status.idle":"2023-04-13T08:15:01.589662Z","shell.execute_reply.started":"2023-04-13T08:15:01.375545Z","shell.execute_reply":"2023-04-13T08:15:01.588688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating Submission CSV","metadata":{}},{"cell_type":"code","source":"pred = model.predict(test_data)\npred = tf.math.argmax(pred, axis = -1)\npred = pd.Series(pred, name='Label')\n\nimage_id = pd.Series(range(1,28001),name='ImageId')\nimage_id.isnull().sum()\n\npred = pd.concat([image_id,pred],axis=1)\npred.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-13T08:15:01.591348Z","iopub.execute_input":"2023-04-13T08:15:01.592064Z","iopub.status.idle":"2023-04-13T08:15:03.401542Z","shell.execute_reply.started":"2023-04-13T08:15:01.592022Z","shell.execute_reply":"2023-04-13T08:15:03.400582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# The End\n> If you liked this notebook,have a look at my other work as well!","metadata":{}}]}