{"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 numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, AveragePooling2D, Dense, Dropout, Flatten\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\nfrom sklearn.preprocessing import OneHotEncoder","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:23:28.193968Z","iopub.execute_input":"2023-06-29T09:23:28.194887Z","iopub.status.idle":"2023-06-29T09:23:36.784155Z","shell.execute_reply.started":"2023-06-29T09:23:28.194830Z","shell.execute_reply":"2023-06-29T09:23:36.782083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntrain_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:23:36.789761Z","iopub.execute_input":"2023-06-29T09:23:36.793219Z","iopub.status.idle":"2023-06-29T09:23:43.942879Z","shell.execute_reply.started":"2023-06-29T09:23:36.793177Z","shell.execute_reply":"2023-06-29T09:23:43.941885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:23:43.944330Z","iopub.execute_input":"2023-06-29T09:23:43.944765Z","iopub.status.idle":"2023-06-29T09:23:43.971814Z","shell.execute_reply.started":"2023-06-29T09:23:43.944727Z","shell.execute_reply":"2023-06-29T09:23:43.970703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train_df[\"label\"]\nx_train = train_df.drop(\"label\", axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:23:43.974186Z","iopub.execute_input":"2023-06-29T09:23:43.974556Z","iopub.status.idle":"2023-06-29T09:23:44.060033Z","shell.execute_reply.started":"2023-06-29T09:23:43.974523Z","shell.execute_reply":"2023-06-29T09:23:44.058565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.values.reshape(-1, 28, 28, 1).astype(\"float32\") / 255.0\nx_test = test_df.values.reshape(-1, 28, 28, 1).astype(\"float32\") / 255.0\ny_train = np.array(y_train)","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:23:44.061937Z","iopub.execute_input":"2023-06-29T09:23:44.062378Z","iopub.status.idle":"2023-06-29T09:23:44.192893Z","shell.execute_reply.started":"2023-06-29T09:23:44.062337Z","shell.execute_reply":"2023-06-29T09:23:44.191865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_, ax = plt.subplots(4, 4, figsize=(10, 5))\ni = 0\nfor image in x_train[:16]:\n    label = y_train[i]\n    ax[i // 4, i % 4].imshow(image[:, :, 0], cmap=\"Blues\")\n    ax[i // 4, i % 4].set_title(label)\n    ax[i // 4, i % 4].axis(\"off\")\n    i += 1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:23:44.194510Z","iopub.execute_input":"2023-06-29T09:23:44.194902Z","iopub.status.idle":"2023-06-29T09:23:45.143520Z","shell.execute_reply.started":"2023-06-29T09:23:44.194865Z","shell.execute_reply":"2023-06-29T09:23:45.142615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"enc = OneHotEncoder()\ny_train=enc.fit_transform(y_train.reshape(y_train.shape[0],1)).toarray().astype(int)\n\ny_train[0]","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:23:45.144465Z","iopub.execute_input":"2023-06-29T09:23:45.144772Z","iopub.status.idle":"2023-06-29T09:23:45.162875Z","shell.execute_reply.started":"2023-06-29T09:23:45.144742Z","shell.execute_reply":"2023-06-29T09:23:45.162020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:23:45.163982Z","iopub.execute_input":"2023-06-29T09:23:45.164374Z","iopub.status.idle":"2023-06-29T09:23:45.171577Z","shell.execute_reply.started":"2023-06-29T09:23:45.164341Z","shell.execute_reply":"2023-06-29T09:23:45.170460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Build_Model() -> Sequential:\n    model = Sequential()\n    \n    model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1), padding='same'))\n    model.add(AveragePooling2D((2, 2), padding='same'))\n    \n    model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))\n    model.add(AveragePooling2D((2, 2), padding='same'))\n    \n    model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))\n    model.add(AveragePooling2D((2, 2), padding='same'))\n    \n    model.add(Flatten())\n    \n    model.add(Dense(256, activation='relu'))\n    model.add(Dropout(0.25))\n    \n    model.add(Dense(256, activation='relu'))\n    model.add(Dropout(0.25))\n    \n    model.add(Dense(10, activation='softmax'))\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:23:56.492894Z","iopub.execute_input":"2023-06-29T09:23:56.493261Z","iopub.status.idle":"2023-06-29T09:23:56.503362Z","shell.execute_reply.started":"2023-06-29T09:23:56.493230Z","shell.execute_reply":"2023-06-29T09:23:56.502129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Build_Model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:23:57.022823Z","iopub.execute_input":"2023-06-29T09:23:57.023197Z","iopub.status.idle":"2023-06-29T09:23:59.729159Z","shell.execute_reply.started":"2023-06-29T09:23:57.023168Z","shell.execute_reply":"2023-06-29T09:23:59.728432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy', 'AUC'])\n\ncheckpoint = ModelCheckpoint(\"digit_weights.h5\", monitor='loss', verbose=1, save_best_only=True, mode='min', save_weights_only = True)\n\nreduceLROnPlat = ReduceLROnPlateau(monitor='loss', factor=0.8, patience=2, verbose=1, mode='auto', min_delta=0.0001, cooldown=5)\n\nearly_stop = EarlyStopping(monitor='loss', mode='min', patience=20, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:23:59.730710Z","iopub.execute_input":"2023-06-29T09:23:59.731098Z","iopub.status.idle":"2023-06-29T09:23:59.762150Z","shell.execute_reply.started":"2023-06-29T09:23:59.731045Z","shell.execute_reply":"2023-06-29T09:23:59.761139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    x_train, y_train,\n    epochs=1000,\n    batch_size=32,\n    callbacks=[checkpoint, early_stop, reduceLROnPlat]\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:23:59.763596Z","iopub.execute_input":"2023-06-29T09:23:59.763999Z","iopub.status.idle":"2023-06-29T09:41:27.873838Z","shell.execute_reply.started":"2023-06-29T09:23:59.763966Z","shell.execute_reply":"2023-06-29T09:41:27.873000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nloss = history.history['loss']\n\nplt.figure(figsize=(10, 16))\nplt.rcParams['figure.figsize'] = [16, 9]\nplt.rcParams['font.size'] = 14\nplt.rcParams['axes.grid'] = True\nplt.rcParams['figure.facecolor'] = 'white'\nplt.subplot(2, 1, 1)\nplt.plot(acc, label='Accuracy')\nplt.legend(loc='lower right')\nplt.ylabel('Accuracy')\nplt.title(f'Accuracy: {str(acc[-1])}')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:43:48.563557Z","iopub.execute_input":"2023-06-29T09:43:48.563934Z","iopub.status.idle":"2023-06-29T09:43:49.070897Z","shell.execute_reply.started":"2023-06-29T09:43:48.563903Z","shell.execute_reply":"2023-06-29T09:43:49.069972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplot(2, 1, 2)\nplt.plot(loss, label='Loss')\nplt.legend(loc='upper right')\nplt.ylabel('Cross Entropy')\nplt.title(f'Loss: {str(loss[-1])}')\nplt.xlabel('epoch')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:44:06.533346Z","iopub.execute_input":"2023-06-29T09:44:06.533707Z","iopub.status.idle":"2023-06-29T09:44:06.947792Z","shell.execute_reply.started":"2023-06-29T09:44:06.533677Z","shell.execute_reply":"2023-06-29T09:44:06.946939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights('/kaggle/working/digit_weights.h5')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(x_test)\npred = np.argmax(pred,axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:44:23.834657Z","iopub.execute_input":"2023-06-29T09:44:23.835006Z","iopub.status.idle":"2023-06-29T09:44:26.741890Z","shell.execute_reply.started":"2023-06-29T09:44:23.834978Z","shell.execute_reply":"2023-06-29T09:44:26.740811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_, ax = plt.subplots(4, 4, figsize=(10, 5))\ni = 0\nfor image in x_test[:16]:\n    label = pred[i]\n    ax[i // 4, i % 4].imshow(image[:, :, 0], cmap=\"Blues\")\n    ax[i // 4, i % 4].set_title(label)\n    ax[i // 4, i % 4].axis(\"off\")\n    i += 1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:45:10.014271Z","iopub.execute_input":"2023-06-29T09:45:10.014629Z","iopub.status.idle":"2023-06-29T09:45:10.893985Z","shell.execute_reply.started":"2023-06-29T09:45:10.014600Z","shell.execute_reply":"2023-06-29T09:45:10.893025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n         \"ImageId\": range(1, len(x_test) + 1),\n         \"Label\": pred\n     })\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-29T09:45:13.100562Z","iopub.execute_input":"2023-06-29T09:45:13.101138Z","iopub.status.idle":"2023-06-29T09:45:13.161790Z","shell.execute_reply.started":"2023-06-29T09:45:13.101084Z","shell.execute_reply":"2023-06-29T09:45:13.160850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}