{"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-07-04T09:08:26.887013Z","iopub.execute_input":"2021-07-04T09:08:26.887407Z","iopub.status.idle":"2021-07-04T09:08:26.899957Z","shell.execute_reply.started":"2021-07-04T09:08:26.887318Z","shell.execute_reply":"2021-07-04T09:08:26.899008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Import required libraries","metadata":{}},{"cell_type":"code","source":"# import the necessary packages\nfrom keras.models import Sequential\nfrom keras.layers.normalization import BatchNormalization\nfrom keras.layers.convolutional import Conv2D\nfrom keras.layers.convolutional import MaxPooling2D\nfrom keras.layers.core import Activation\nfrom keras.layers.core import Flatten\nfrom keras.layers.core import Dropout\nfrom keras.layers.core import Dense\nfrom keras.utils import np_utils\nimport tensorflow as tf\nimport cv2\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:08:26.901241Z","iopub.execute_input":"2021-07-04T09:08:26.901563Z","iopub.status.idle":"2021-07-04T09:08:32.677063Z","shell.execute_reply.started":"2021-07-04T09:08:26.901536Z","shell.execute_reply":"2021-07-04T09:08:32.676278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load Dataset","metadata":{}},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:08:32.678292Z","iopub.execute_input":"2021-07-04T09:08:32.678675Z","iopub.status.idle":"2021-07-04T09:08:38.349532Z","shell.execute_reply.started":"2021-07-04T09:08:32.678648Z","shell.execute_reply":"2021-07-04T09:08:38.348550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train.shape)\ndisplay(test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:08:38.351070Z","iopub.execute_input":"2021-07-04T09:08:38.351501Z","iopub.status.idle":"2021-07-04T09:08:38.361648Z","shell.execute_reply.started":"2021-07-04T09:08:38.351424Z","shell.execute_reply":"2021-07-04T09:08:38.360653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Show the image","metadata":{}},{"cell_type":"code","source":"from PIL import Image\n# create figure\nfig = plt.figure(figsize=(15, 15))\n\n# setting values to rows and column variables\nrows = 3\ncolumns = 3\n\nfor i in range(9):\n    # Adds a subplot at the 1st position\n    fig.add_subplot(rows, columns, i+1)\n    img_arr=train.iloc[i,1:].values.reshape(28,28)\n    plt.imshow(img_arr)\n    plt.axis('off')\n    plt.title(\"Label\"+' '+str(train.iloc[i,0]))","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:08:38.362830Z","iopub.execute_input":"2021-07-04T09:08:38.363097Z","iopub.status.idle":"2021-07-04T09:08:38.907390Z","shell.execute_reply.started":"2021-07-04T09:08:38.363072Z","shell.execute_reply":"2021-07-04T09:08:38.906481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Reshape all the image sizes","metadata":{}},{"cell_type":"code","source":"trainX = train.drop(\"label\", axis = 1)\ntrainY = train[\"label\"]\n\ntrainX = trainX.astype(\"float32\") / 255.0\ntrainX = trainX.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:08:47.423934Z","iopub.execute_input":"2021-07-04T09:08:47.424275Z","iopub.status.idle":"2021-07-04T09:08:47.858346Z","shell.execute_reply.started":"2021-07-04T09:08:47.424246Z","shell.execute_reply":"2021-07-04T09:08:47.857579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testX = test.astype(\"float32\") / 255.0\ntestX = testX.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:08:50.050795Z","iopub.execute_input":"2021-07-04T09:08:50.051311Z","iopub.status.idle":"2021-07-04T09:08:50.131371Z","shell.execute_reply.started":"2021-07-04T09:08:50.051268Z","shell.execute_reply":"2021-07-04T09:08:50.130658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(testX.shape)\ndisplay(trainX.shape)\ndisplay(trainY.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:08:50.458168Z","iopub.execute_input":"2021-07-04T09:08:50.458525Z","iopub.status.idle":"2021-07-04T09:08:50.467223Z","shell.execute_reply.started":"2021-07-04T09:08:50.458496Z","shell.execute_reply":"2021-07-04T09:08:50.466291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Encoding target labels","metadata":{}},{"cell_type":"code","source":"trainY.unique()## 10 classes","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:08:52.683944Z","iopub.execute_input":"2021-07-04T09:08:52.684518Z","iopub.status.idle":"2021-07-04T09:08:52.692523Z","shell.execute_reply.started":"2021-07-04T09:08:52.684471Z","shell.execute_reply":"2021-07-04T09:08:52.691538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Encoding the output class label (One-Hot Encoding)\ntrainY=np_utils.to_categorical(trainY,10)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:08:53.085396Z","iopub.execute_input":"2021-07-04T09:08:53.085897Z","iopub.status.idle":"2021-07-04T09:08:53.090637Z","shell.execute_reply.started":"2021-07-04T09:08:53.085867Z","shell.execute_reply":"2021-07-04T09:08:53.089884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainY","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:08:56.322484Z","iopub.execute_input":"2021-07-04T09:08:56.322980Z","iopub.status.idle":"2021-07-04T09:08:56.328859Z","shell.execute_reply.started":"2021-07-04T09:08:56.322939Z","shell.execute_reply":"2021-07-04T09:08:56.328101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create the Graph Model","metadata":{}},{"cell_type":"code","source":"# Note you only need to define the input data shape with the first layer. \n# The last layers is a dense layer with softmax activation that classifies the 10 categories.\n\n\nmodel = tf.keras.Sequential()\n# Must define the input shape in the first layer of the neural network\nmodel.add(tf.keras.layers.Conv2D(filters=64, kernel_size=3, padding='same', activation='relu', input_shape=(28,28,1))) \nmodel.add(tf.keras.layers.MaxPooling2D(pool_size=2 , padding='same'))\nmodel.add(tf.keras.layers.Dropout(0.2))\nmodel.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'))\nmodel.add(tf.keras.layers.MaxPooling2D(pool_size=2 , padding='valid'))\nmodel.add(tf.keras.layers.Dropout(0.3))\nmodel.add(tf.keras.layers.Flatten())\nmodel.add(tf.keras.layers.Dense(256, activation='relu'))\nmodel.add(tf.keras.layers.Dropout(0.5))\nmodel.add(tf.keras.layers.Dense(10, activation='softmax'))\n# Take a look at the model summary\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:08:58.475545Z","iopub.execute_input":"2021-07-04T09:08:58.476143Z","iopub.status.idle":"2021-07-04T09:08:58.609461Z","shell.execute_reply.started":"2021-07-04T09:08:58.476108Z","shell.execute_reply":"2021-07-04T09:08:58.608550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compile the model","metadata":{}},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n            optimizer='adam',\n            metrics=['accuracy'])\n\n\n# model.compile(loss='categorical_crossentropy',\n#              optimizer='rmsprop',\n#              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:09:01.029223Z","iopub.execute_input":"2021-07-04T09:09:01.029863Z","iopub.status.idle":"2021-07-04T09:09:01.044081Z","shell.execute_reply.started":"2021-07-04T09:09:01.029827Z","shell.execute_reply":"2021-07-04T09:09:01.043292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Fit the model","metadata":{}},{"cell_type":"code","source":"model.fit(trainX, trainY ,batch_size=64,epochs=10,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:09:03.947198Z","iopub.execute_input":"2021-07-04T09:09:03.947806Z","iopub.status.idle":"2021-07-04T09:15:04.578568Z","shell.execute_reply.started":"2021-07-04T09:09:03.947767Z","shell.execute_reply":"2021-07-04T09:15:04.577708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(trainX, trainY ,batch_size=64,epochs=20,verbose=1,initial_epoch=10)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:15:04.579989Z","iopub.execute_input":"2021-07-04T09:15:04.580265Z","iopub.status.idle":"2021-07-04T09:21:06.215942Z","shell.execute_reply.started":"2021-07-04T09:15:04.580238Z","shell.execute_reply":"2021-07-04T09:21:06.214874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Predict the model on test set","metadata":{}},{"cell_type":"code","source":"### predict \ntest_pred=model.predict(testX)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:21:51.219161Z","iopub.execute_input":"2021-07-04T09:21:51.219562Z","iopub.status.idle":"2021-07-04T09:21:57.309697Z","shell.execute_reply.started":"2021-07-04T09:21:51.219523Z","shell.execute_reply":"2021-07-04T09:21:57.308631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = np.argmax(test_pred,1)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:21:57.311128Z","iopub.execute_input":"2021-07-04T09:21:57.311407Z","iopub.status.idle":"2021-07-04T09:21:57.316220Z","shell.execute_reply.started":"2021-07-04T09:21:57.311380Z","shell.execute_reply":"2021-07-04T09:21:57.315148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:21:59.384073Z","iopub.execute_input":"2021-07-04T09:21:59.384434Z","iopub.status.idle":"2021-07-04T09:21:59.389495Z","shell.execute_reply.started":"2021-07-04T09:21:59.384405Z","shell.execute_reply":"2021-07-04T09:21:59.388526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:22:02.424388Z","iopub.execute_input":"2021-07-04T09:22:02.424760Z","iopub.status.idle":"2021-07-04T09:22:02.429919Z","shell.execute_reply.started":"2021-07-04T09:22:02.424728Z","shell.execute_reply":"2021-07-04T09:22:02.429036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['label']=test_pred","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:22:06.168771Z","iopub.execute_input":"2021-07-04T09:22:06.169392Z","iopub.status.idle":"2021-07-04T09:22:06.177401Z","shell.execute_reply.started":"2021-07-04T09:22:06.169349Z","shell.execute_reply":"2021-07-04T09:22:06.176549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### File for submission\nsubmission=pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:22:21.446804Z","iopub.execute_input":"2021-07-04T09:22:21.447297Z","iopub.status.idle":"2021-07-04T09:22:21.471367Z","shell.execute_reply.started":"2021-07-04T09:22:21.447265Z","shell.execute_reply":"2021-07-04T09:22:21.470544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['Label']=test_pred","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:22:24.150319Z","iopub.execute_input":"2021-07-04T09:22:24.150720Z","iopub.status.idle":"2021-07-04T09:22:24.155274Z","shell.execute_reply.started":"2021-07-04T09:22:24.150686Z","shell.execute_reply":"2021-07-04T09:22:24.154202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:22:26.258942Z","iopub.execute_input":"2021-07-04T09:22:26.259297Z","iopub.status.idle":"2021-07-04T09:22:26.275866Z","shell.execute_reply.started":"2021-07-04T09:22:26.259267Z","shell.execute_reply":"2021-07-04T09:22:26.274876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('DR_submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T09:22:30.717892Z","iopub.execute_input":"2021-07-04T09:22:30.718243Z","iopub.status.idle":"2021-07-04T09:22:30.776119Z","shell.execute_reply.started":"2021-07-04T09:22:30.718215Z","shell.execute_reply":"2021-07-04T09:22:30.775172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}