{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\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":"2021-06-30T09:39:11.444045Z","iopub.execute_input":"2021-06-30T09:39:11.444465Z","iopub.status.idle":"2021-06-30T09:39:11.467232Z","shell.execute_reply.started":"2021-06-30T09:39:11.444377Z","shell.execute_reply":"2021-06-30T09:39:11.466242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport  numpy as np\nfrom keras.utils.np_utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:11.469016Z","iopub.execute_input":"2021-06-30T09:39:11.469369Z","iopub.status.idle":"2021-06-30T09:39:16.87377Z","shell.execute_reply.started":"2021-06-30T09:39:11.469333Z","shell.execute_reply":"2021-06-30T09:39:16.872904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data","metadata":{}},{"cell_type":"code","source":"train=pd.read_csv('../input/digit-recognizer/train.csv')\ntest=pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:16.87559Z","iopub.execute_input":"2021-06-30T09:39:16.875867Z","iopub.status.idle":"2021-06-30T09:39:21.539291Z","shell.execute_reply.started":"2021-06-30T09:39:16.875841Z","shell.execute_reply":"2021-06-30T09:39:21.538468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:21.540835Z","iopub.execute_input":"2021-06-30T09:39:21.541147Z","iopub.status.idle":"2021-06-30T09:39:21.570979Z","shell.execute_reply.started":"2021-06-30T09:39:21.541114Z","shell.execute_reply":"2021-06-30T09:39:21.570291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:21.573667Z","iopub.execute_input":"2021-06-30T09:39:21.573904Z","iopub.status.idle":"2021-06-30T09:39:21.579847Z","shell.execute_reply.started":"2021-06-30T09:39:21.57388Z","shell.execute_reply":"2021-06-30T09:39:21.578841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:21.58138Z","iopub.execute_input":"2021-06-30T09:39:21.581903Z","iopub.status.idle":"2021-06-30T09:39:21.588441Z","shell.execute_reply.started":"2021-06-30T09:39:21.581867Z","shell.execute_reply":"2021-06-30T09:39:21.58741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(train['label'])","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:21.590115Z","iopub.execute_input":"2021-06-30T09:39:21.590476Z","iopub.status.idle":"2021-06-30T09:39:21.756893Z","shell.execute_reply.started":"2021-06-30T09:39:21.590439Z","shell.execute_reply":"2021-06-30T09:39:21.755933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=train.drop(['label'],1)\ny_train=train['label']","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:21.759565Z","iopub.execute_input":"2021-06-30T09:39:21.759848Z","iopub.status.idle":"2021-06-30T09:39:21.839897Z","shell.execute_reply.started":"2021-06-30T09:39:21.759822Z","shell.execute_reply":"2021-06-30T09:39:21.838503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=np.array(x_train)\ntest=np.array(test)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:21.842193Z","iopub.execute_input":"2021-06-30T09:39:21.842667Z","iopub.status.idle":"2021-06-30T09:39:22.006193Z","shell.execute_reply.started":"2021-06-30T09:39:21.842625Z","shell.execute_reply":"2021-06-30T09:39:22.005342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:22.007344Z","iopub.execute_input":"2021-06-30T09:39:22.007682Z","iopub.status.idle":"2021-06-30T09:39:22.0148Z","shell.execute_reply.started":"2021-06-30T09:39:22.007647Z","shell.execute_reply":"2021-06-30T09:39:22.013524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:22.016109Z","iopub.execute_input":"2021-06-30T09:39:22.016507Z","iopub.status.idle":"2021-06-30T09:39:22.024519Z","shell.execute_reply.started":"2021-06-30T09:39:22.01644Z","shell.execute_reply":"2021-06-30T09:39:22.023557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=x_train.reshape(42000,28,28)\ntest=test.reshape(28000,28,28)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:22.02609Z","iopub.execute_input":"2021-06-30T09:39:22.026526Z","iopub.status.idle":"2021-06-30T09:39:22.032166Z","shell.execute_reply.started":"2021-06-30T09:39:22.026491Z","shell.execute_reply":"2021-06-30T09:39:22.031057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=x_train/255\ntest=test/255","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:22.033468Z","iopub.execute_input":"2021-06-30T09:39:22.033921Z","iopub.status.idle":"2021-06-30T09:39:22.206955Z","shell.execute_reply.started":"2021-06-30T09:39:22.033885Z","shell.execute_reply":"2021-06-30T09:39:22.20589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_img(i):\n    plt.imshow(x_train[i],cmap='gray')\n    plt.xlabel(y_train[i])\nshow_img(0)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:22.20832Z","iopub.execute_input":"2021-06-30T09:39:22.20869Z","iopub.status.idle":"2021-06-30T09:39:22.33022Z","shell.execute_reply.started":"2021-06-30T09:39:22.208649Z","shell.execute_reply":"2021-06-30T09:39:22.329296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target=x_train.reshape(-1,28,28,1)\ntest=test.reshape(-1,28,28,1)\ny_train=np.array(y_train)\nlabel=to_categorical(y_train)\nlabel.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:22.331564Z","iopub.execute_input":"2021-06-30T09:39:22.331919Z","iopub.status.idle":"2021-06-30T09:39:22.3402Z","shell.execute_reply.started":"2021-06-30T09:39:22.33188Z","shell.execute_reply":"2021-06-30T09:39:22.339032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model creation","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D,MaxPooling2D,Flatten,Dense,Dropout","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:22.341998Z","iopub.execute_input":"2021-06-30T09:39:22.342468Z","iopub.status.idle":"2021-06-30T09:39:22.34768Z","shell.execute_reply.started":"2021-06-30T09:39:22.342427Z","shell.execute_reply":"2021-06-30T09:39:22.346702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential([\n    Conv2D(32,(5,5) , activation='relu' , input_shape=(28,28,1)),\n    MaxPooling2D(pool_size=(2,2)),\n    \n    Conv2D(64,(5,5), activation ='relu'),\n    MaxPooling2D(pool_size=(2,2)),\n    Dropout(0.25),\n    \n    Conv2D(64,(3,3), activation ='relu'),\n    MaxPooling2D(pool_size=(2,2)),\n    Dropout(0.25),\n    \n    Flatten(),\n    Dense(64, activation='relu'),\n    Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:22.349253Z","iopub.execute_input":"2021-06-30T09:39:22.349724Z","iopub.status.idle":"2021-06-30T09:39:24.511057Z","shell.execute_reply.started":"2021-06-30T09:39:22.349687Z","shell.execute_reply":"2021-06-30T09:39:24.510255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:24.51413Z","iopub.execute_input":"2021-06-30T09:39:24.514385Z","iopub.status.idle":"2021-06-30T09:39:24.528356Z","shell.execute_reply.started":"2021-06-30T09:39:24.51436Z","shell.execute_reply":"2021-06-30T09:39:24.527464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(target,label,epochs=40,batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:39:24.529636Z","iopub.execute_input":"2021-06-30T09:39:24.529984Z","iopub.status.idle":"2021-06-30T09:41:28.17088Z","shell.execute_reply.started":"2021-06-30T09:39:24.529947Z","shell.execute_reply":"2021-06-30T09:41:28.169999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred = model.predict(test)\nY_pred_classes = np.argmax(Y_pred,axis = 1) ","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:41:28.172488Z","iopub.execute_input":"2021-06-30T09:41:28.172851Z","iopub.status.idle":"2021-06-30T09:41:29.622277Z","shell.execute_reply.started":"2021-06-30T09:41:28.172809Z","shell.execute_reply":"2021-06-30T09:41:29.621418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_data = pd.read_csv('../input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:41:29.623586Z","iopub.execute_input":"2021-06-30T09:41:29.623968Z","iopub.status.idle":"2021-06-30T09:41:29.643401Z","shell.execute_reply.started":"2021-06-30T09:41:29.623929Z","shell.execute_reply":"2021-06-30T09:41:29.642695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_data['Label']=Y_pred_classes","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:41:29.644568Z","iopub.execute_input":"2021-06-30T09:41:29.644932Z","iopub.status.idle":"2021-06-30T09:41:29.651711Z","shell.execute_reply.started":"2021-06-30T09:41:29.644896Z","shell.execute_reply":"2021-06-30T09:41:29.650915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_data.to_csv('submit.csv' ,index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:41:29.656606Z","iopub.execute_input":"2021-06-30T09:41:29.656975Z","iopub.status.idle":"2021-06-30T09:41:29.949311Z","shell.execute_reply.started":"2021-06-30T09:41:29.656946Z","shell.execute_reply":"2021-06-30T09:41:29.948542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Testing","metadata":{}},{"cell_type":"code","source":"def test_output(i):\n  plt.imshow(x_train[i],cmap='gray')\n  predicted=np.argmax(model.predict(target[i].reshape(-1,28,28,1)))\n  actual=np.argmax(label[i])\n  plt.xlabel(f'predicted= {predicted}  Actual= {actual}')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:42:02.687312Z","iopub.execute_input":"2021-06-30T09:42:02.687654Z","iopub.status.idle":"2021-06-30T09:42:02.69417Z","shell.execute_reply.started":"2021-06-30T09:42:02.687602Z","shell.execute_reply":"2021-06-30T09:42:02.691868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_output(2)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:42:03.91219Z","iopub.execute_input":"2021-06-30T09:42:03.912576Z","iopub.status.idle":"2021-06-30T09:42:04.134829Z","shell.execute_reply.started":"2021-06-30T09:42:03.912534Z","shell.execute_reply":"2021-06-30T09:42:04.134051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_output(4)","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:42:05.682991Z","iopub.execute_input":"2021-06-30T09:42:05.683317Z","iopub.status.idle":"2021-06-30T09:42:05.844329Z","shell.execute_reply.started":"2021-06-30T09:42:05.683287Z","shell.execute_reply":"2021-06-30T09:42:05.843571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model testing using real world images","metadata":{}},{"cell_type":"code","source":"from PIL import Image, ImageGrab","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:42:08.128655Z","iopub.execute_input":"2021-06-30T09:42:08.128981Z","iopub.status.idle":"2021-06-30T09:42:08.134542Z","shell.execute_reply.started":"2021-06-30T09:42:08.128954Z","shell.execute_reply":"2021-06-30T09:42:08.133592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_digit1(img):\n    img = Image.open(img)\n    plt.imshow(img)\n    img = img.convert('L', dither=Image.NONE)\n    img = img.resize((28,28))\n    img = np.array(img)\n    img=np.invert(img)\n    predicted=np.argmax(model.predict(img.reshape(-1,28,28,1)))\n    # plt.imshow(img,interpolation='nearest')\n    plt.xlabel(f'Predicted= {predicted}')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:42:09.297145Z","iopub.execute_input":"2021-06-30T09:42:09.298805Z","iopub.status.idle":"2021-06-30T09:42:09.308303Z","shell.execute_reply.started":"2021-06-30T09:42:09.298751Z","shell.execute_reply":"2021-06-30T09:42:09.307565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_digit1('../input/temporary/Images/images.jfif')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:42:11.033423Z","iopub.execute_input":"2021-06-30T09:42:11.034088Z","iopub.status.idle":"2021-06-30T09:42:11.437633Z","shell.execute_reply.started":"2021-06-30T09:42:11.034046Z","shell.execute_reply":"2021-06-30T09:42:11.436573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_digit1('../input/temporary/Images/download.png')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:42:13.537409Z","iopub.execute_input":"2021-06-30T09:42:13.537742Z","iopub.status.idle":"2021-06-30T09:42:13.694156Z","shell.execute_reply.started":"2021-06-30T09:42:13.537712Z","shell.execute_reply":"2021-06-30T09:42:13.693387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_digit1('../input/temporary/Images/531-5314816_handwritten-1-number-9-hand-written-png-transparent.png')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:42:15.675344Z","iopub.execute_input":"2021-06-30T09:42:15.675678Z","iopub.status.idle":"2021-06-30T09:42:15.953404Z","shell.execute_reply.started":"2021-06-30T09:42:15.675646Z","shell.execute_reply":"2021-06-30T09:42:15.952661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_digit1('../input/temporary/Images/1.jpg')","metadata":{"execution":{"iopub.status.busy":"2021-06-30T09:42:18.23467Z","iopub.execute_input":"2021-06-30T09:42:18.234998Z","iopub.status.idle":"2021-06-30T09:42:18.435696Z","shell.execute_reply.started":"2021-06-30T09:42:18.234969Z","shell.execute_reply":"2021-06-30T09:42:18.43492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}