{"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":"2022-03-28T16:36:06.897765Z","iopub.execute_input":"2022-03-28T16:36:06.898189Z","iopub.status.idle":"2022-03-28T16:36:06.928164Z","shell.execute_reply.started":"2022-03-28T16:36:06.898075Z","shell.execute_reply":"2022-03-28T16:36:06.925756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Importing requirements","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' \n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport random\n\nfrom IPython.display import YouTubeVideo\n\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom keras.models import Model\nfrom keras.callbacks import ReduceLROnPlateau\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-03-28T16:55:21.915992Z","iopub.execute_input":"2022-03-28T16:55:21.916260Z","iopub.status.idle":"2022-03-28T16:55:22.102611Z","shell.execute_reply.started":"2022-03-28T16:55:21.916231Z","shell.execute_reply":"2022-03-28T16:55:22.101806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Getting dataset","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest_df = pd.read_csv(\"../input/digit-recognizer/test.csv\")\ntrain_df.shape, test_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-28T16:54:26.075924Z","iopub.execute_input":"2022-03-28T16:54:26.076378Z","iopub.status.idle":"2022-03-28T16:54:29.830324Z","shell.execute_reply.started":"2022-03-28T16:54:26.076342Z","shell.execute_reply":"2022-03-28T16:54:29.829565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-28T16:54:31.389100Z","iopub.execute_input":"2022-03-28T16:54:31.389352Z","iopub.status.idle":"2022-03-28T16:54:31.406373Z","shell.execute_reply.started":"2022-03-28T16:54:31.389324Z","shell.execute_reply":"2022-03-28T16:54:31.405562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reshaping Data and spliting","metadata":{}},{"cell_type":"code","source":"X = train_df.drop(columns = ['label']).values.astype('float32')\ny = train_df['label'].values\nprint(X)\nprint(y)","metadata":{"execution":{"iopub.status.busy":"2022-03-28T16:54:34.513841Z","iopub.execute_input":"2022-03-28T16:54:34.514320Z","iopub.status.idle":"2022-03-28T16:54:34.659720Z","shell.execute_reply.started":"2022-03-28T16:54:34.514284Z","shell.execute_reply":"2022-03-28T16:54:34.658851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1, stratify = y, random_state = 0)","metadata":{"execution":{"iopub.status.busy":"2022-03-28T16:55:26.498897Z","iopub.execute_input":"2022-03-28T16:55:26.499185Z","iopub.status.idle":"2022-03-28T16:55:26.837904Z","shell.execute_reply.started":"2022-03-28T16:55:26.499155Z","shell.execute_reply":"2022-03-28T16:55:26.836803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-28T16:55:47.433186Z","iopub.execute_input":"2022-03-28T16:55:47.433449Z","iopub.status.idle":"2022-03-28T16:55:47.438195Z","shell.execute_reply.started":"2022-03-28T16:55:47.433418Z","shell.execute_reply":"2022-03-28T16:55:47.437505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape(X_train.shape[0], 28, 28)\nX_test = X_test.reshape(X_test.shape[0], 28, 28)","metadata":{"execution":{"iopub.status.busy":"2022-03-28T16:55:59.321279Z","iopub.execute_input":"2022-03-28T16:55:59.321567Z","iopub.status.idle":"2022-03-28T16:55:59.328436Z","shell.execute_reply.started":"2022-03-28T16:55:59.321538Z","shell.execute_reply":"2022-03-28T16:55:59.327715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape\n","metadata":{"execution":{"iopub.status.busy":"2022-03-28T16:56:11.259440Z","iopub.execute_input":"2022-03-28T16:56:11.259801Z","iopub.status.idle":"2022-03-28T16:56:11.265917Z","shell.execute_reply.started":"2022-03-28T16:56:11.259765Z","shell.execute_reply":"2022-03-28T16:56:11.264937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test","metadata":{"execution":{"iopub.status.busy":"2022-03-28T16:56:33.889601Z","iopub.execute_input":"2022-03-28T16:56:33.889912Z","iopub.status.idle":"2022-03-28T16:56:33.899636Z","shell.execute_reply.started":"2022-03-28T16:56:33.889883Z","shell.execute_reply":"2022-03-28T16:56:33.898534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualizing the Images","metadata":{}},{"cell_type":"code","source":"def images_and_pixels(image, ax):\n    \n    ax.imshow(image, cmap = 'PuBu')\n    width, height = image.shape\n    threshold = image.max() / 2.5\n    \n    for x in range(width):\n        for y in range(height):\n            ax.annotate(str(round(image[x][y],2)), \n                        xy = (y,x),\n                        horizontalalignment = 'center',\n                        verticalalignment = 'center',\n                        color = 'white' if image[x][y] < threshold else 'black')\n\nfig = plt.figure(figsize = (10,10)) \nax = fig.add_subplot(111)\nrandom_number = random.randint(0, 37800)\n\nimages_and_pixels(X_train[1], ax)\n\nplt.title('Label = ' + str(y_train[random_number]), fontsize = 20)\nplt.axis('off')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-03-28T17:08:13.145027Z","iopub.execute_input":"2022-03-28T17:08:13.145638Z","iopub.status.idle":"2022-03-28T17:08:13.416426Z","shell.execute_reply.started":"2022-03-28T17:08:13.145599Z","shell.execute_reply":"2022-03-28T17:08:13.415326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)\nX_test = X_test.reshape(X_test.shape[0], 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2022-03-28T16:58:16.991579Z","iopub.execute_input":"2022-03-28T16:58:16.992295Z","iopub.status.idle":"2022-03-28T16:58:16.997361Z","shell.execute_reply.started":"2022-03-28T16:58:16.992259Z","shell.execute_reply":"2022-03-28T16:58:16.996198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Augmentation","metadata":{}},{"cell_type":"code","source":"model = keras.Sequential([\n            keras.Input(shape = (28, 28, 1)),\n            layers.Conv2D(32, 3, padding = 'same', activation = 'relu'),\n            layers.Conv2D(32, 3, padding = 'same', activation = 'relu'),\n            layers.Conv2D(32, 3, padding = 'same', activation = 'relu'),\n            layers.MaxPooling2D(),\n            layers.Conv2D(64, 3, padding = 'same', activation = 'relu'),\n            layers.Conv2D(64, 3, padding = 'same', activation = 'relu'),\n            layers.Conv2D(64, 3, padding = 'same', activation = 'relu'),\n            layers.MaxPooling2D(),\n            layers.Conv2D(64, 3, padding = 'same', activation = 'relu'),\n            layers.Conv2D(128, 3, padding = 'same', activation = 'relu'),\n            layers.Flatten(),\n            layers.Dense(128, activation = 'relu'),\n            layers.Dense(10, activation = 'softmax'),\n])\n\nmodel.compile(\n        loss = keras.losses.SparseCategoricalCrossentropy(from_logits = True),\n        optimizer = keras.optimizers.Adam(lr = 1e-4),\n        metrics = ['acc'],\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-28T16:59:06.176902Z","iopub.execute_input":"2022-03-28T16:59:06.177196Z","iopub.status.idle":"2022-03-28T16:59:08.380591Z","shell.execute_reply.started":"2022-03-28T16:59:06.177164Z","shell.execute_reply":"2022-03-28T16:59:08.379855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-28T16:59:27.920718Z","iopub.execute_input":"2022-03-28T16:59:27.921361Z","iopub.status.idle":"2022-03-28T16:59:27.932573Z","shell.execute_reply.started":"2022-03-28T16:59:27.921322Z","shell.execute_reply":"2022-03-28T16:59:27.931887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Using Learning Rate Reduction","metadata":{}},{"cell_type":"code","source":"reduce_lr = ReduceLROnPlateau(monitor = 'val_acc', patience = 3, verbose = 1, factor = 0.5, min_lr = 0.00001)","metadata":{"execution":{"iopub.status.busy":"2022-03-28T17:00:00.398836Z","iopub.execute_input":"2022-03-28T17:00:00.399123Z","iopub.status.idle":"2022-03-28T17:00:00.403285Z","shell.execute_reply.started":"2022-03-28T17:00:00.399092Z","shell.execute_reply":"2022-03-28T17:00:00.402482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier = model.fit(X_train, y_train, batch_size = 64, \n                                 epochs = 50, \n                                 validation_data = (X_test, y_test), \n                                 verbose = 1,\n                                 steps_per_epoch = X_train.shape[0] // 64,\n                                 callbacks = [reduce_lr])\n","metadata":{"execution":{"iopub.status.busy":"2022-03-28T17:00:20.973607Z","iopub.execute_input":"2022-03-28T17:00:20.974623Z","iopub.status.idle":"2022-03-28T17:03:37.450383Z","shell.execute_reply.started":"2022-03-28T17:00:20.974571Z","shell.execute_reply":"2022-03-28T17:03:37.449718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model performance","metadata":{}},{"cell_type":"code","source":"def model_performance_graphs():\n    \n    fig, axes = plt.subplots(1, 2, figsize = (15, 8))\n\n    axes[0].plot(classifier.epoch, classifier.history['acc'], label = 'acc')\n    axes[0].plot(classifier.epoch, classifier.history['val_acc'], label = 'val_acc')\n    axes[0].set_title('Accuracy vs Epochs', fontsize = 20)\n    axes[0].set_xlabel('Epochs', fontsize = 15)\n    axes[0].set_ylabel('Accuracy', fontsize = 15)\n    axes[0].legend()\n\n    axes[1].plot(classifier.epoch, classifier.history['loss'], label = 'loss')\n    axes[1].plot(classifier.epoch, classifier.history['val_loss'], label=\"val_loss\")\n    axes[1].set_title(\"Loss Curve\",fontsize=18)\n    axes[1].set_xlabel(\"Epochs\",fontsize=15)\n    axes[1].set_ylabel(\"Loss\",fontsize=15)\n    axes[1].legend()\n\n    plt.show()\n    \nmodel_performance_graphs()\n","metadata":{"execution":{"iopub.status.busy":"2022-03-28T17:04:21.014109Z","iopub.execute_input":"2022-03-28T17:04:21.014793Z","iopub.status.idle":"2022-03-28T17:04:21.512328Z","shell.execute_reply.started":"2022-03-28T17:04:21.014745Z","shell.execute_reply":"2022-03-28T17:04:21.511593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model evaluation","metadata":{}},{"cell_type":"code","source":"model.evaluate(X_train, y_train, batch_size = 64, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-03-28T17:04:56.976216Z","iopub.execute_input":"2022-03-28T17:04:56.976492Z","iopub.status.idle":"2022-03-28T17:04:59.772313Z","shell.execute_reply.started":"2022-03-28T17:04:56.976462Z","shell.execute_reply":"2022-03-28T17:04:59.771654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(X_test, y_test, batch_size = 64, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-03-28T17:05:07.616025Z","iopub.execute_input":"2022-03-28T17:05:07.616720Z","iopub.status.idle":"2022-03-28T17:05:07.980186Z","shell.execute_reply.started":"2022-03-28T17:05:07.616656Z","shell.execute_reply":"2022-03-28T17:05:07.979393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_labels = model.predict(((test_df.values).reshape(-1, 28, 28, 1) / 255.0))\nfinal_labels = np.argmax(final_labels, axis = 1)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-28T17:06:04.879962Z","iopub.execute_input":"2022-03-28T17:06:04.880732Z","iopub.status.idle":"2022-03-28T17:06:06.709065Z","shell.execute_reply.started":"2022-03-28T17:06:04.880669Z","shell.execute_reply":"2022-03-28T17:06:06.708232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating the Submission File","metadata":{}},{"cell_type":"code","source":"submission_df = pd.DataFrame()\nimage_id = [i for i in range(1, 28001)]\nsubmission_df['ImageId'] = image_id\nsubmission_df['Label'] = final_labels\nsubmission_df.to_csv('submission.csv', index = False)\nsubmission_df.head(10)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-28T17:06:29.994144Z","iopub.execute_input":"2022-03-28T17:06:29.994820Z","iopub.status.idle":"2022-03-28T17:06:30.081900Z","shell.execute_reply.started":"2022-03-28T17:06:29.994780Z","shell.execute_reply":"2022-03-28T17:06:30.081254Z"},"trusted":true},"execution_count":null,"outputs":[]}]}