{"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-08-14T04:06:56.757610Z","iopub.execute_input":"2021-08-14T04:06:56.757932Z","iopub.status.idle":"2021-08-14T04:06:56.770848Z","shell.execute_reply.started":"2021-08-14T04:06:56.757845Z","shell.execute_reply":"2021-08-14T04:06:56.769839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import all the requirements Files\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:07:00.900519Z","iopub.execute_input":"2021-08-14T04:07:00.900875Z","iopub.status.idle":"2021-08-14T04:07:05.729645Z","shell.execute_reply.started":"2021-08-14T04:07:00.900838Z","shell.execute_reply":"2021-08-14T04:07:05.728825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Testing the Train CSV file \ndf_train = pd.read_csv('../input/digit-recognizer/train.csv')\ndf_train.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:07:08.943575Z","iopub.execute_input":"2021-08-14T04:07:08.943919Z","iopub.status.idle":"2021-08-14T04:07:12.180807Z","shell.execute_reply.started":"2021-08-14T04:07:08.943884Z","shell.execute_reply":"2021-08-14T04:07:12.179987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.info","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:07:15.561008Z","iopub.execute_input":"2021-08-14T04:07:15.561442Z","iopub.status.idle":"2021-08-14T04:07:15.577022Z","shell.execute_reply.started":"2021-08-14T04:07:15.561408Z","shell.execute_reply":"2021-08-14T04:07:15.575891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:07:18.938841Z","iopub.execute_input":"2021-08-14T04:07:18.939179Z","iopub.status.idle":"2021-08-14T04:07:18.944485Z","shell.execute_reply.started":"2021-08-14T04:07:18.939145Z","shell.execute_reply":"2021-08-14T04:07:18.943605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:07:21.162631Z","iopub.execute_input":"2021-08-14T04:07:21.162952Z","iopub.status.idle":"2021-08-14T04:07:21.209148Z","shell.execute_reply.started":"2021-08-14T04:07:21.162920Z","shell.execute_reply":"2021-08-14T04:07:21.208194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_orig = df_train.drop('label', axis=1).to_numpy()\nX_train_orig","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:07:23.990219Z","iopub.execute_input":"2021-08-14T04:07:23.990550Z","iopub.status.idle":"2021-08-14T04:07:24.075091Z","shell.execute_reply.started":"2021-08-14T04:07:23.990521Z","shell.execute_reply":"2021-08-14T04:07:24.074154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_orig = df_train.label.to_numpy()\ny_train_orig","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:07:26.293333Z","iopub.execute_input":"2021-08-14T04:07:26.293659Z","iopub.status.idle":"2021-08-14T04:07:26.298924Z","shell.execute_reply.started":"2021-08-14T04:07:26.293629Z","shell.execute_reply":"2021-08-14T04:07:26.298134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Testing the Test CSV files \ndf_test = pd.read_csv('../input/digit-recognizer/test.csv')\ndf_test.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:07:28.483050Z","iopub.execute_input":"2021-08-14T04:07:28.483387Z","iopub.status.idle":"2021-08-14T04:07:30.365711Z","shell.execute_reply.started":"2021-08-14T04:07:28.483354Z","shell.execute_reply":"2021-08-14T04:07:30.364954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_orig = df_test.to_numpy()\nX_test_orig","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:07:33.663576Z","iopub.execute_input":"2021-08-14T04:07:33.663886Z","iopub.status.idle":"2021-08-14T04:07:33.673388Z","shell.execute_reply.started":"2021-08-14T04:07:33.663856Z","shell.execute_reply":"2021-08-14T04:07:33.672623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train_orig.astype('float32')/255\ny_train = y_train_orig.astype('float32')\nX_test = X_test_orig.astype('float32')/255","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:07:36.015378Z","iopub.execute_input":"2021-08-14T04:07:36.015710Z","iopub.status.idle":"2021-08-14T04:07:36.137158Z","shell.execute_reply.started":"2021-08-14T04:07:36.015681Z","shell.execute_reply":"2021-08-14T04:07:36.136274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape(-1,28,28,1)\nX_test = X_test.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:07:38.481451Z","iopub.execute_input":"2021-08-14T04:07:38.481766Z","iopub.status.idle":"2021-08-14T04:07:38.486217Z","shell.execute_reply.started":"2021-08-14T04:07:38.481737Z","shell.execute_reply":"2021-08-14T04:07:38.485011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = tf.one_hot(y_train, 10)\ny_train","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:07:40.102659Z","iopub.execute_input":"2021-08-14T04:07:40.102983Z","iopub.status.idle":"2021-08-14T04:07:42.023028Z","shell.execute_reply.started":"2021-08-14T04:07:40.102954Z","shell.execute_reply":"2021-08-14T04:07:42.022134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('X_train shape: ',X_train.shape)\nprint('X_test shape: ',X_test.shape)\nprint('y_train shape: ',y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:07:45.065624Z","iopub.execute_input":"2021-08-14T04:07:45.065975Z","iopub.status.idle":"2021-08-14T04:07:45.075791Z","shell.execute_reply.started":"2021-08-14T04:07:45.065930Z","shell.execute_reply":"2021-08-14T04:07:45.074983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = (28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:07:50.301653Z","iopub.execute_input":"2021-08-14T04:07:50.301995Z","iopub.status.idle":"2021-08-14T04:07:50.306294Z","shell.execute_reply.started":"2021-08-14T04:07:50.301963Z","shell.execute_reply":"2021-08-14T04:07:50.305049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CNN = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(filters=32, kernel_size=(5, 5), strides=1, padding='same',input_shape=input_shape),\n    tf.keras.layers.ReLU(),\n    tf.keras.layers.Conv2D(filters=32, kernel_size=(5, 5), strides=1, padding='same'),\n    tf.keras.layers.BatchNormalization(axis=3),\n    tf.keras.layers.ReLU(),\n    tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=2),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Conv2D(filters=64, kernel_size=(3, 3), padding='same'),\n    tf.keras.layers.BatchNormalization(axis=3),\n    tf.keras.layers.ReLU(),\n    tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=2),\n    tf.keras.layers.Conv2D(filters=64, kernel_size=(3, 3), padding='same'),\n    tf.keras.layers.BatchNormalization(axis=3),\n    tf.keras.layers.ReLU(),\n    tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=2, padding='same'),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(units=256, activation='relu'),\n    tf.keras.layers.Dense(units=10, activation='softmax'),    \n])","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:07:54.530311Z","iopub.execute_input":"2021-08-14T04:07:54.530703Z","iopub.status.idle":"2021-08-14T04:07:54.691966Z","shell.execute_reply.started":"2021-08-14T04:07:54.530660Z","shell.execute_reply":"2021-08-14T04:07:54.691195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CNN.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:07:57.573452Z","iopub.execute_input":"2021-08-14T04:07:57.573764Z","iopub.status.idle":"2021-08-14T04:07:57.590377Z","shell.execute_reply.started":"2021-08-14T04:07:57.573736Z","shell.execute_reply":"2021-08-14T04:07:57.589460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CNN.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:08:02.682716Z","iopub.execute_input":"2021-08-14T04:08:02.683053Z","iopub.status.idle":"2021-08-14T04:08:02.701491Z","shell.execute_reply.started":"2021-08-14T04:08:02.683022Z","shell.execute_reply":"2021-08-14T04:08:02.700665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#checking the GPU\n!nvidia-smi","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:08:42.493662Z","iopub.execute_input":"2021-08-14T04:08:42.493997Z","iopub.status.idle":"2021-08-14T04:08:43.303690Z","shell.execute_reply.started":"2021-08-14T04:08:42.493950Z","shell.execute_reply":"2021-08-14T04:08:43.302806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CNN_history = CNN.fit(X_train, y_train, epochs=25)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:09:29.562167Z","iopub.execute_input":"2021-08-14T04:09:29.562507Z","iopub.status.idle":"2021-08-14T04:11:53.706293Z","shell.execute_reply.started":"2021-08-14T04:09:29.562480Z","shell.execute_reply":"2021-08-14T04:11:53.705424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_loss_acc = pd.DataFrame(CNN_history.history)\ndf_acc = df_loss_acc[['accuracy']]\ndf_acc.plot(title='CNN Accuracy', figsize=(12, 8)).set(xlabel='Epoch', ylabel='Accuracy')","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:12:07.263668Z","iopub.execute_input":"2021-08-14T04:12:07.264010Z","iopub.status.idle":"2021-08-14T04:12:07.477958Z","shell.execute_reply.started":"2021-08-14T04:12:07.263976Z","shell.execute_reply":"2021-08-14T04:12:07.477195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_loss = df_loss_acc[['loss']]","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:12:13.223340Z","iopub.execute_input":"2021-08-14T04:12:13.223677Z","iopub.status.idle":"2021-08-14T04:12:13.229586Z","shell.execute_reply.started":"2021-08-14T04:12:13.223648Z","shell.execute_reply":"2021-08-14T04:12:13.228058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_loss.plot(title='CNN Loss', figsize=(12, 8)).set(xlabel='Epoch', ylabel='Loss')","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:12:17.056792Z","iopub.execute_input":"2021-08-14T04:12:17.057181Z","iopub.status.idle":"2021-08-14T04:12:17.438698Z","shell.execute_reply.started":"2021-08-14T04:12:17.057138Z","shell.execute_reply":"2021-08-14T04:12:17.437552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = CNN.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:12:33.833751Z","iopub.execute_input":"2021-08-14T04:12:33.834103Z","iopub.status.idle":"2021-08-14T04:12:35.320870Z","shell.execute_reply.started":"2021-08-14T04:12:33.834056Z","shell.execute_reply":"2021-08-14T04:12:35.319608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = np.argmax(predictions,axis=1)\nresults = pd.Series(results, name=\"Label\")\nlist=[]\n[list.append(i) for i in range(1,28001)]\nImageID = pd.Series(list, name=\"ImageID\").astype(\"int32\")\nsubmission = pd.concat([ImageID,results],axis = 1)\n\nsubmission.to_csv(\"BasedCNN Digit-Recoginer.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:12:38.963199Z","iopub.execute_input":"2021-08-14T04:12:38.963514Z","iopub.status.idle":"2021-08-14T04:12:39.033498Z","shell.execute_reply.started":"2021-08-14T04:12:38.963485Z","shell.execute_reply":"2021-08-14T04:12:39.032658Z"},"trusted":true},"execution_count":null,"outputs":[]}]}