{"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 using CNN","metadata":{}},{"cell_type":"markdown","source":"MNIST (\"Modified National Institute of Standards and Technology\") is the de facto “hello world” dataset of computer vision. Since its release in 1999, this classic dataset of handwritten images has served as the basis for benchmarking classification algorithms. As new machine learning techniques emerge, MNIST remains a reliable resource for researchers and learners alike.","metadata":{}},{"cell_type":"markdown","source":"## Importing necessary libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport pathlib","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:01:51.870526Z","iopub.execute_input":"2022-09-12T00:01:51.870886Z","iopub.status.idle":"2022-09-12T00:01:56.844790Z","shell.execute_reply.started":"2022-09-12T00:01:51.870853Z","shell.execute_reply":"2022-09-12T00:01:56.843771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Reading the dataset","metadata":{}},{"cell_type":"code","source":"shape = (28, 28, 1)\nbatch_size = 128","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:01:57.915804Z","iopub.execute_input":"2022-09-12T00:01:57.916436Z","iopub.status.idle":"2022-09-12T00:01:57.921489Z","shell.execute_reply.started":"2022-09-12T00:01:57.916400Z","shell.execute_reply":"2022-09-12T00:01:57.920396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtrain = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ndtest = pd.read_csv(\"../input/digit-recognizer/test.csv\")\ndtrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:01:58.125824Z","iopub.execute_input":"2022-09-12T00:01:58.126214Z","iopub.status.idle":"2022-09-12T00:02:03.267109Z","shell.execute_reply.started":"2022-09-12T00:01:58.126175Z","shell.execute_reply":"2022-09-12T00:02:03.266131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking the test data\ndtest.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:02:03.269019Z","iopub.execute_input":"2022-09-12T00:02:03.269903Z","iopub.status.idle":"2022-09-12T00:02:03.286167Z","shell.execute_reply.started":"2022-09-12T00:02:03.269865Z","shell.execute_reply":"2022-09-12T00:02:03.284894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Detecting Features and labels","metadata":{}},{"cell_type":"code","source":"labels = dtrain['label']\nfeatures = dtrain.drop(['label'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:02:03.288046Z","iopub.execute_input":"2022-09-12T00:02:03.288860Z","iopub.status.idle":"2022-09-12T00:02:03.378669Z","shell.execute_reply.started":"2022-09-12T00:02:03.288823Z","shell.execute_reply":"2022-09-12T00:02:03.377572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = np.asarray(features)\nfeatures.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:02:03.381856Z","iopub.execute_input":"2022-09-12T00:02:03.382374Z","iopub.status.idle":"2022-09-12T00:02:03.392667Z","shell.execute_reply.started":"2022-09-12T00:02:03.382333Z","shell.execute_reply":"2022-09-12T00:02:03.391720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = features.reshape((42000, 28, 28))\nfeatures.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:02:03.394612Z","iopub.execute_input":"2022-09-12T00:02:03.395277Z","iopub.status.idle":"2022-09-12T00:02:03.404182Z","shell.execute_reply.started":"2022-09-12T00:02:03.395240Z","shell.execute_reply":"2022-09-12T00:02:03.402965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = tf.keras.utils.to_categorical(labels)","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:02:03.405796Z","iopub.execute_input":"2022-09-12T00:02:03.406270Z","iopub.status.idle":"2022-09-12T00:02:04.369977Z","shell.execute_reply.started":"2022-09-12T00:02:03.406227Z","shell.execute_reply":"2022-09-12T00:02:04.368999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Ploting samples of the dataset","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (20, 4))\nfor i in range(16):\n  plt.subplot(2, 8, i + 1)\n  plt.imshow(features[i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:02:04.371577Z","iopub.execute_input":"2022-09-12T00:02:04.371950Z","iopub.status.idle":"2022-09-12T00:02:05.678819Z","shell.execute_reply.started":"2022-09-12T00:02:04.371914Z","shell.execute_reply":"2022-09-12T00:02:05.677856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Now let's create the model\ndef create_model():\n    left_input = tf.keras.layers.Input(shape = shape)\n    filter = 32\n    x = left_input\n    for i in range(3):\n        x = tf.keras.layers.Conv2D(filter,3, activation = \"relu\", padding = 'same')(x)\n        x = tf.keras.layers.Dropout(0.4)(x)\n        x = tf.keras.layers.MaxPooling2D()(x)\n    filter*=2\n    right_input = tf.keras.layers.Input(shape = shape)\n    y = right_input\n    filter = 32\n    for i  in range(3):\n        y = tf.keras.layers.Conv2D(filter,3, activation = \"relu\", padding = 'same', dilation_rate = 2)(y)\n        y = tf.keras.layers.Dropout(0.4)(y)\n        y = tf.keras.layers.MaxPooling2D()(y)\n        filter*=2\n    x = tf.keras.layers.GlobalAveragePooling2D()(x)\n    y = tf.keras.layers.GlobalAveragePooling2D()(y)\n    model = tf.keras.layers.concatenate([x, y])\n    model = tf.keras.layers.Dropout(0.4)(model)\n    output = tf.keras.layers.Dense(10, activation = \"softmax\")(model)\n    model = tf.keras.models.Model([left_input, right_input], output)\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:02:05.680481Z","iopub.execute_input":"2022-09-12T00:02:05.681122Z","iopub.status.idle":"2022-09-12T00:02:05.691470Z","shell.execute_reply.started":"2022-09-12T00:02:05.681067Z","shell.execute_reply":"2022-09-12T00:02:05.690204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:02:05.693157Z","iopub.execute_input":"2022-09-12T00:02:05.693538Z","iopub.status.idle":"2022-09-12T00:02:08.754921Z","shell.execute_reply.started":"2022-09-12T00:02:05.693502Z","shell.execute_reply":"2022-09-12T00:02:08.753875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Ploting the Model","metadata":{}},{"cell_type":"code","source":"tf.keras.utils.plot_model(model, show_shapes = True)","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:02:08.757944Z","iopub.execute_input":"2022-09-12T00:02:08.758811Z","iopub.status.idle":"2022-09-12T00:02:09.889104Z","shell.execute_reply.started":"2022-09-12T00:02:08.758771Z","shell.execute_reply":"2022-09-12T00:02:09.887843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=tf.optimizers.Adam(learning_rate=0.001), loss= \"categorical_crossentropy\", metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:02:09.890988Z","iopub.execute_input":"2022-09-12T00:02:09.891794Z","iopub.status.idle":"2022-09-12T00:02:09.915014Z","shell.execute_reply.started":"2022-09-12T00:02:09.891746Z","shell.execute_reply":"2022-09-12T00:02:09.913871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit([features, features], labels, batch_size=128, epochs=160, verbose=True, validation_split = 0.2,callbacks = [tf.keras.callbacks.EarlyStopping(patience= 20, monitor='val_loss', mode = 'min', restore_best_weights=True)])","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:02:09.917201Z","iopub.execute_input":"2022-09-12T00:02:09.918065Z","iopub.status.idle":"2022-09-12T00:11:34.069664Z","shell.execute_reply.started":"2022-09-12T00:02:09.918021Z","shell.execute_reply":"2022-09-12T00:11:34.068577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## The Results","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(figsize = (20, 5))\nplt.plot(history.history['accuracy'], label = \"accuracy\")\nplt.plot(history.history['val_accuracy'], label = \"val_accuracy\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:11:34.072980Z","iopub.execute_input":"2022-09-12T00:11:34.073415Z","iopub.status.idle":"2022-09-12T00:11:34.358448Z","shell.execute_reply.started":"2022-09-12T00:11:34.073374Z","shell.execute_reply":"2022-09-12T00:11:34.357453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20, 5))\nplt.plot(history.history['loss'], label = \"loss\")\nplt.plot(history.history['val_loss'], label= \"val_loss\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:11:34.359827Z","iopub.execute_input":"2022-09-12T00:11:34.360817Z","iopub.status.idle":"2022-09-12T00:11:34.618017Z","shell.execute_reply.started":"2022-09-12T00:11:34.360776Z","shell.execute_reply":"2022-09-12T00:11:34.617014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Evaluation","metadata":{}},{"cell_type":"code","source":"model.evaluate([features, features], labels, batch_size= 128)","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:11:34.619579Z","iopub.execute_input":"2022-09-12T00:11:34.619932Z","iopub.status.idle":"2022-09-12T00:11:37.707061Z","shell.execute_reply.started":"2022-09-12T00:11:34.619896Z","shell.execute_reply":"2022-09-12T00:11:37.706110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features_test = np.asarray(dtest)\nfeatures_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:13:58.010146Z","iopub.execute_input":"2022-09-12T00:13:58.010535Z","iopub.status.idle":"2022-09-12T00:13:58.017681Z","shell.execute_reply.started":"2022-09-12T00:13:58.010503Z","shell.execute_reply":"2022-09-12T00:13:58.016683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features_test = features_test.reshape(features_test.shape[0], 28, 28)\ny_pred = model.predict([features_test, features_test])\ny_pred","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:13:58.936343Z","iopub.execute_input":"2022-09-12T00:13:58.936700Z","iopub.status.idle":"2022-09-12T00:14:02.489762Z","shell.execute_reply.started":"2022-09-12T00:13:58.936670Z","shell.execute_reply":"2022-09-12T00:14:02.488598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = np.argmax(y_pred, axis = 1)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:14:02.491790Z","iopub.execute_input":"2022-09-12T00:14:02.492271Z","iopub.status.idle":"2022-09-12T00:14:02.501078Z","shell.execute_reply.started":"2022-09-12T00:14:02.492233Z","shell.execute_reply":"2022-09-12T00:14:02.500018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'ImageId': dtest.index + 1, 'Label': y_pred.flatten()})\noutput.to_csv('submission.csv',index=False)\nprint(\"Your submission was successfully saved!\")\noutput","metadata":{"execution":{"iopub.status.busy":"2022-09-12T00:24:14.704125Z","iopub.execute_input":"2022-09-12T00:24:14.704720Z","iopub.status.idle":"2022-09-12T00:24:14.751204Z","shell.execute_reply.started":"2022-09-12T00:24:14.704678Z","shell.execute_reply":"2022-09-12T00:24:14.750153Z"},"trusted":true},"execution_count":null,"outputs":[]}]}