{"cells":[{"metadata":{},"cell_type":"markdown","source":"# MNIST"},{"metadata":{},"cell_type":"markdown","source":"MNIST (\"Modified National Institute of Standards and Technology\") is the de facto “hello world” dataset of computer vision"},{"metadata":{},"cell_type":"markdown","source":"It is a large database of handwritten digits that is commonly used for training various image processing systems. The database is also widely used for training and testing in the field of machine learning"},{"metadata":{},"cell_type":"markdown","source":"# Our Challenge"},{"metadata":{},"cell_type":"markdown","source":"Our goal is to correctly identify digits from a dataset of tens of thousands of handwritten images."},{"metadata":{},"cell_type":"markdown","source":"# Importing Libraries"},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.layers import Dropout, Flatten, Dense, Conv2D, MaxPooling2D\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.models import Sequential\nfrom keras.utils.np_utils import to_categorical ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Loading dataset"},{"metadata":{},"cell_type":"markdown","source":"The data files train.csv and test.csv contain gray-scale images of hand-drawn digits, from zero to nine."},{"metadata":{},"cell_type":"markdown","source":"Each image is 28 pixels in height and 28 pixels in width, for a total of 784 pixels in total. Each pixel has a single pixel-value associated with it, indicating the lightness or darkness of that pixel, with higher numbers meaning darker. This pixel-value is an integer between 0 and 255, inclusive.\n\nThe training data set, (train.csv), has 785 columns. The first column, called \"label\", is the digit that was drawn by the user. The rest of the columns contain the pixel-values of the associated image"},{"metadata":{"trusted":true},"cell_type":"code","source":"# loading training dataset in train_data dataframe\ntrain_data=pd.read_csv('../input/digit-recognizer/train.csv')\n# loading testing dataset in test_data dataframe\ntest_data=pd.read_csv('../input/digit-recognizer/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Splitting data into features and labels"},{"metadata":{},"cell_type":"markdown","source":"###  Creating Features "},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train=train_data.drop('label',1)\n# reshaping X_train\nX_train=X_train.values.reshape(-1,28,28,1)\n# normalizing X_train\nX_train=tf.keras.utils.normalize(X_train,axis=1)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Creating Label"},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train=train_data['label']\ny_train = to_categorical(y_train, num_classes = 10)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Building the Convolutional Neural Network"},{"metadata":{},"cell_type":"markdown","source":"We will build our model using high level Keras API which uses Teensorflow on the backened. I have imported the Sequential model and added Conv2D, MaxPooling2D, Flatten, Dropout and Dense layers. Dropout will reduce some of the neurons for removing overfitting while training and Flatten layers will flatten 2D arrays to 1D arrays before building the fully connected layers."},{"metadata":{"trusted":true},"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(64, (3, 3),activation='relu',input_shape=(28,28,1)))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(32, (3, 3),activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\ntest_data=tf.keras.utils.normalize(test_data,axis=1)\n\nmodel.add(Flatten())\nmodel.add(Dense(256,activation=tf.nn.relu))\nmodel.add(Dense(128,activation=tf.nn.relu))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(10,activation=tf.nn.softmax))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Compiling and Fitting the model"},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])\nmodel.fit(X_train,y_train,epochs=10,batch_size=1000)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Testing our model"},{"metadata":{},"cell_type":"markdown","source":"We can make individual predictions for numbers to test our model is it working or not. "},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data=test_data.values.reshape(-1,28,28,1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prediction=model.predict(test_data)\nprediction=np.argmax(prediction,axis=1)\nplt.imshow(test_data[111].reshape(28,28))\nprint('The predicted number is  '+str(prediction[111]))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Submission for predicted model"},{"metadata":{"trusted":true},"cell_type":"code","source":"imageid=[]\nfor x in range(1,28001):\n    imageid.append(x)\nsubmission=pd.DataFrame({'ImageId':imageid,'Label':prediction})\nsubmission.to_csv('submission.csv',index=False)\nsubmission","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# GUI application "},{"metadata":{},"cell_type":"markdown","source":"This is the code for GUI application that i have made for predicting numbers. I have used tkinter, PIL and imageio for this. GUI is nothing but a desktop app that provides you with an interface that helps you to interact with the computers and enriches your experience of giving a command to your code. PIL and imageio are used for opening, manipulating, and saving many different image file formats.\n\nThere is a canvas on which we can draw some random numbers and our PC will predict the number by the use of deep learning."},{"metadata":{"trusted":true},"cell_type":"code","source":"# from tkinter import *\n# from PIL import ImageGrab\n# import imageio\n# import tkinter.font as font\n\n\n# class Paint(object):\n#     def __init__(self):\n#         self.root=Tk()\n#         self.root.title('Playing with numbers')\n#         self.root.wm_iconbitmap('44143.ico')\n#         self.root.configure(background='light salmon')\n#         self.c=Canvas(self.root,bg='light cyan',height=330,width=400)\n#         self.label=Label(self.root,text='Draw any numer',font=20,bg='light salmon')\n#         self.label.grid(row=0,column=3)\n#         self.c.grid(row=1,columnspan=9)\n#         self.c.create_line(0,0,400,0,width=20,fill='midnight blue')\n#         self.c.create_line(0,0,0,330,width=20,fill='midnight blue')\n#         self.c.create_line(400,0,400,330,width=20,fill='midnight blue')\n#         self.c.create_line(0,330,400,330,width=20,fill='midnight blue')\n#         self.myfont=font.Font(size=20,weight='bold')\n#         self.predicting_button=Button(self.root,text='Predict',fg='maroon',bg='steel blue',height=2,width=6,font=self.myfont,command=lambda:self.classify(self.c))\n#         self.predicting_button.grid(row=2,column=1)\n#         self.clear=Button(self.root,text='Clear',fg='blue',bg='red',height=2,width=6,font=self.myfont,command=self.clear)\n#         self.clear.grid(row=2,column=5)\n#         self.prediction_text = Text(self.root, height=5, width=5)\n#         self.prediction_text.grid(row=4, column=3)\n#         self.label=Label(self.root,text=\"Predicted Number is\",fg=\"black\",font=30,bg='light salmon')\n        \n#         self.label.grid(row=3,column=3)\n#         self.model=model\n#         self.setup()\n#         self.root.mainloop()\n#     def setup(self):\n#         self.old_x=None\n#         self.old_y=None\n#         self.color='black'\n#         self.linewidth=15\n#         self.c.bind('<B1-Motion>', self.paint)\n#         self.c.bind('<ButtonRelease-1>', self.reset)\n#     def paint(self,event):\n#         paint_color=self.color\n#         if self.old_x and self.old_y:\n#             self.c.create_line(self.old_x,self.old_y,event.x,event.y,fill=paint_color,width=self.linewidth,capstyle=ROUND,\n#                               smooth=TRUE,splinesteps=48)\n#         self.old_x=event.x\n#         self.old_y=event.y\n#     def clear(self):\n#         \"\"\"Clear drawing area\"\"\"\n#         self.c.delete(\"all\")\n\n#     def reset(self, event):\n#         \"\"\"reset old_x and old_y if the left mouse button is released\"\"\"\n#         self.old_x, self.old_y = None, None    \n#     def classify(self,widget):\n#         x=self.root.winfo_rootx()+widget.winfo_x()\n#         y=self.root.winfo_rooty()+widget.winfo_y()\n#         x1=widget.winfo_width()\n#         y1=widget.winfo_height()\n#         ImageGrab.grab().crop((x,y,x1,y1)).resize((28,28)).save('classify.png')\n#         img=imageio.imread('classify.png',as_gray=False,pilmode='P')\n#         img=np.array(img)\n#         img=np.reshape(img,(1,28,28,1))\n#         img[img==0] = 255\n#         img[img==225] = 0\n#         # Predict digit\n#         pred = self.model.predict([img])\n#         # Get index with highest probability\n#         pred = np.argmax(pred)\n#         print(pred)\n#         self.prediction_text.delete(\"1.0\", END)\n#         self.prediction_text.insert(END, pred)\n#         labelfont = ('times', 30, 'bold')\n#         self.prediction_text.config(font=labelfont)\n        \n# if __name__ == '__main__':\n#     Paint()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.4"}},"nbformat":4,"nbformat_minor":1}