{"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":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow as tf \nimport os \nimport seaborn as sns \nimport matplotlib.image as mpimg","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":6.966097,"end_time":"2020-12-21T05:16:36.646543","exception":false,"start_time":"2020-12-21T05:16:29.680446","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T09:28:20.885996Z","iopub.execute_input":"2021-11-23T09:28:20.886293Z","iopub.status.idle":"2021-11-23T09:28:24.635745Z","shell.execute_reply.started":"2021-11-23T09:28:20.886257Z","shell.execute_reply":"2021-11-23T09:28:24.635132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_input = \"../input/digit-recognizer/train.csv\"\ntest_input = \"../input/digit-recognizer/test.csv\"\n\ntrain_dataset = pd.read_csv(train_input)\ntest_dataset = pd.read_csv(test_input)","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","papermill":{"duration":6.30201,"end_time":"2020-12-21T05:16:42.996295","exception":false,"start_time":"2020-12-21T05:16:36.694285","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T09:28:24.637198Z","iopub.execute_input":"2021-11-23T09:28:24.637683Z","iopub.status.idle":"2021-11-23T09:28:30.292779Z","shell.execute_reply.started":"2021-11-23T09:28:24.637641Z","shell.execute_reply":"2021-11-23T09:28:30.291943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.head()","metadata":{"papermill":{"duration":0.052086,"end_time":"2020-12-21T05:16:43.065675","exception":false,"start_time":"2020-12-21T05:16:43.013589","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T09:28:30.294292Z","iopub.execute_input":"2021-11-23T09:28:30.294589Z","iopub.status.idle":"2021-11-23T09:28:30.322956Z","shell.execute_reply.started":"2021-11-23T09:28:30.294550Z","shell.execute_reply":"2021-11-23T09:28:30.322306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset.head()","metadata":{"papermill":{"duration":0.040378,"end_time":"2020-12-21T05:16:43.123368","exception":false,"start_time":"2020-12-21T05:16:43.08299","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T09:28:30.324491Z","iopub.execute_input":"2021-11-23T09:28:30.325101Z","iopub.status.idle":"2021-11-23T09:28:30.342178Z","shell.execute_reply.started":"2021-11-23T09:28:30.325067Z","shell.execute_reply":"2021-11-23T09:28:30.341633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.bar(range(10), train_dataset.label.value_counts(), color=\"#FF6F00\", tick_label=range(10))\nplt.xlabel(\"Numbers\")\nplt.ylabel(\"Counts\")\nplt.show()","metadata":{"papermill":{"duration":0.241305,"end_time":"2020-12-21T05:16:43.382799","exception":false,"start_time":"2020-12-21T05:16:43.141494","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T09:28:30.343095Z","iopub.execute_input":"2021-11-23T09:28:30.343663Z","iopub.status.idle":"2021-11-23T09:28:30.581155Z","shell.execute_reply.started":"2021-11-23T09:28:30.343628Z","shell.execute_reply":"2021-11-23T09:28:30.580611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = tf.keras.utils.to_categorical(train_dataset.pop(\"label\"))","metadata":{"papermill":{"duration":0.11069,"end_time":"2020-12-21T05:16:43.514464","exception":false,"start_time":"2020-12-21T05:16:43.403774","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T09:28:30.582282Z","iopub.execute_input":"2021-11-23T09:28:30.583054Z","iopub.status.idle":"2021-11-23T09:28:31.786563Z","shell.execute_reply.started":"2021-11-23T09:28:30.583009Z","shell.execute_reply":"2021-11-23T09:28:31.785707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = np.array(train_dataset.values.reshape(-1, 28, 28, 1))\ntest_dataset = np.array(test_dataset.values.reshape(-1, 28, 28, 1))","metadata":{"papermill":{"duration":0.213248,"end_time":"2020-12-21T05:16:43.747205","exception":false,"start_time":"2020-12-21T05:16:43.533957","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T09:28:31.787903Z","iopub.execute_input":"2021-11-23T09:28:31.788207Z","iopub.status.idle":"2021-11-23T09:28:32.184376Z","shell.execute_reply.started":"2021-11-23T09:28:31.788167Z","shell.execute_reply":"2021-11-23T09:28:32.183583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = train_dataset/255.0\ntest_dataset = test_dataset/255.0","metadata":{"papermill":{"duration":0.26224,"end_time":"2020-12-21T05:16:44.042223","exception":false,"start_time":"2020-12-21T05:16:43.779983","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T09:28:32.186190Z","iopub.execute_input":"2021-11-23T09:28:32.186509Z","iopub.status.idle":"2021-11-23T09:28:32.384932Z","shell.execute_reply.started":"2021-11-23T09:28:32.186467Z","shell.execute_reply":"2021-11-23T09:28:32.384218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_path = \"logs/checkpoints/\"","metadata":{"papermill":{"duration":0.037192,"end_time":"2020-12-21T05:16:44.10756","exception":false,"start_time":"2020-12-21T05:16:44.070368","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T09:28:32.386556Z","iopub.execute_input":"2021-11-23T09:28:32.386848Z","iopub.status.idle":"2021-11-23T09:28:32.391520Z","shell.execute_reply.started":"2021-11-23T09:28:32.386809Z","shell.execute_reply":"2021-11-23T09:28:32.390544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(64, (3, 3), input_shape=(28, 28, 1), activation=tf.nn.relu, padding=\"SAME\"),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Conv2D(64, (3, 3), activation=tf.nn.relu, padding=\"SAME\"),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Conv2D(128, (3, 3), activation=tf.nn.relu, padding=\"SAME\"),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(128, (3, 3), activation=tf.nn.relu, padding=\"SAME\"),\n    tf.keras.layers.MaxPooling2D(),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(128, activation=tf.nn.relu),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(10, activation=\"softmax\")\n])\n\nepochs=10\n\ncallbacks = [\n    tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,\n                                        monitor=\"accuracy\",\n                                        save_best_only=True,\n                                        save_weights_only=True),\n    tf.keras.callbacks.EarlyStopping(monitor=\"accuracy\", patience=15)\n]\n\nmodel.compile(optimizer=\"adam\", loss=\"categorical_crossentropy\", metrics=[\"accuracy\"])\nhistory = model.fit(train_dataset, train_labels, epochs=epochs, callbacks=callbacks, batch_size=64)","metadata":{"papermill":{"duration":156.225854,"end_time":"2020-12-21T05:19:20.413167","exception":false,"start_time":"2020-12-21T05:16:44.187313","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T09:28:32.394416Z","iopub.execute_input":"2021-11-23T09:28:32.394694Z","iopub.status.idle":"2021-11-23T09:35:55.674520Z","shell.execute_reply.started":"2021-11-23T09:28:32.394626Z","shell.execute_reply":"2021-11-23T09:35:55.673289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_metric(metric):\n    plt.plot(range(epochs), history.history[metric], color=\"#FF6F00\")\n    plt.title(metric)\n    plt.show()\n    \nplot_metric(\"accuracy\")\nplot_metric(\"loss\")","metadata":{"papermill":{"duration":1.670458,"end_time":"2020-12-21T05:19:23.147473","exception":false,"start_time":"2020-12-21T05:19:21.477015","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T09:35:55.676441Z","iopub.execute_input":"2021-11-23T09:35:55.676765Z","iopub.status.idle":"2021-11-23T09:35:56.068186Z","shell.execute_reply.started":"2021-11-23T09:35:55.676715Z","shell.execute_reply":"2021-11-23T09:35:56.067380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights(checkpoint_path)","metadata":{"papermill":{"duration":1.560804,"end_time":"2020-12-21T05:19:25.918388","exception":false,"start_time":"2020-12-21T05:19:24.357584","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T09:35:56.069513Z","iopub.execute_input":"2021-11-23T09:35:56.069732Z","iopub.status.idle":"2021-11-23T09:35:56.135022Z","shell.execute_reply.started":"2021-11-23T09:35:56.069706Z","shell.execute_reply":"2021-11-23T09:35:56.134155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = [np.argmax(predict) for predict in model.predict(test_dataset)]\ndf = pd.DataFrame({\n    \"ImageId\": list(range(1, len(test_dataset)+1)),\n    \"Label\": labels,\n})","metadata":{"papermill":{"duration":2.709839,"end_time":"2020-12-21T05:19:31.960624","exception":false,"start_time":"2020-12-21T05:19:29.250785","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T09:35:56.136090Z","iopub.execute_input":"2021-11-23T09:35:56.136296Z","iopub.status.idle":"2021-11-23T09:36:05.423941Z","shell.execute_reply.started":"2021-11-23T09:35:56.136271Z","shell.execute_reply":"2021-11-23T09:36:05.423033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"submission.csv\", index=False)","metadata":{"papermill":{"duration":1.45916,"end_time":"2020-12-21T05:19:34.456241","exception":false,"start_time":"2020-12-21T05:19:32.997081","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T09:36:05.425126Z","iopub.execute_input":"2021-11-23T09:36:05.425369Z","iopub.status.idle":"2021-11-23T09:36:05.488381Z","shell.execute_reply.started":"2021-11-23T09:36:05.425339Z","shell.execute_reply":"2021-11-23T09:36:05.487744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"model.h5\")","metadata":{"papermill":{"duration":1.17937,"end_time":"2020-12-21T05:19:36.674532","exception":false,"start_time":"2020-12-21T05:19:35.495162","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-11-23T09:36:05.489682Z","iopub.execute_input":"2021-11-23T09:36:05.490164Z","iopub.status.idle":"2021-11-23T09:36:05.549486Z","shell.execute_reply.started":"2021-11-23T09:36:05.490122Z","shell.execute_reply":"2021-11-23T09:36:05.548655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":1.041806,"end_time":"2020-12-21T05:19:38.755768","exception":false,"start_time":"2020-12-21T05:19:37.713962","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":1.186697,"end_time":"2020-12-21T05:19:41.090793","exception":false,"start_time":"2020-12-21T05:19:39.904096","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]}]}