{"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":"train_ds_path = '/kaggle/input/digit-recognizer/train.csv'\ntest_ds_path = '/kaggle/input/digit-recognizer/test.csv'","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:13:53.207327Z","iopub.execute_input":"2023-03-03T19:13:53.207674Z","iopub.status.idle":"2023-03-03T19:13:53.212849Z","shell.execute_reply.started":"2023-03-03T19:13:53.207642Z","shell.execute_reply":"2023-03-03T19:13:53.211515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\ntrain_df = pd.read_csv(train_ds_path)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:13:53.522346Z","iopub.execute_input":"2023-03-03T19:13:53.522667Z","iopub.status.idle":"2023-03-03T19:13:55.913290Z","shell.execute_reply.started":"2023-03-03T19:13:53.522625Z","shell.execute_reply":"2023-03-03T19:13:55.912415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.np_utils import to_categorical\n\nX = train_df.drop(columns=['label']).to_numpy()\ny = to_categorical(train_df['label'], num_classes=10)\n\nX.shape, y.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:13:55.917632Z","iopub.execute_input":"2023-03-03T19:13:55.921852Z","iopub.status.idle":"2023-03-03T19:13:56.088467Z","shell.execute_reply.started":"2023-03-03T19:13:55.921814Z","shell.execute_reply":"2023-03-03T19:13:56.087582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X.reshape(-1, 28, 28, 1)\n\nX.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:13:56.092681Z","iopub.execute_input":"2023-03-03T19:13:56.093472Z","iopub.status.idle":"2023-03-03T19:13:56.103409Z","shell.execute_reply.started":"2023-03-03T19:13:56.093435Z","shell.execute_reply":"2023-03-03T19:13:56.102354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.min(), X.max()","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:13:56.105498Z","iopub.execute_input":"2023-03-03T19:13:56.106011Z","iopub.status.idle":"2023-03-03T19:13:56.191708Z","shell.execute_reply.started":"2023-03-03T19:13:56.105979Z","shell.execute_reply":"2023-03-03T19:13:56.190720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X / 255\n\nX.min(), X.max()","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:13:56.193456Z","iopub.execute_input":"2023-03-03T19:13:56.193809Z","iopub.status.idle":"2023-03-03T19:13:56.349499Z","shell.execute_reply.started":"2023-03-03T19:13:56.193758Z","shell.execute_reply":"2023-03-03T19:13:56.348367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(\n    X,\n    y,\n    shuffle=True,\n    test_size=0.2,\n    random_state=1,\n    stratify=y)\n\nX_train, X_val, y_train, y_val = train_test_split(\n    X_train,\n    y_train,\n    shuffle=True,\n    test_size=0.1,\n    random_state=1,\n    stratify=y_train)\n\nprint(\"Train: X_shape=\" + str(X_train.shape) + \", y_shape=\" + str(y_train.shape))\nprint(\"Val: X_shape=\" + str(X_val.shape) + \", y_shape=\" + str(y_val.shape))\nprint(\"Test: X_shape=\" + str(X_test.shape) + \", y_shape=\" + str(y_test.shape))","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:13:57.200855Z","iopub.execute_input":"2023-03-03T19:13:57.201532Z","iopub.status.idle":"2023-03-03T19:13:58.256999Z","shell.execute_reply.started":"2023-03-03T19:13:57.201488Z","shell.execute_reply":"2023-03-03T19:13:58.256039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Example of train images","metadata":{}},{"cell_type":"code","source":"from matplotlib import pyplot as plt\nfrom matplotlib import image as mpimg\n\ncont = 1\n\nplt.figure(figsize=(6,6))\n\nfor i in range(25):\n    plt.subplot(5, 5, cont)\n    plt.imshow(X_train[i], cmap='gray')\n    plt.title('Class {}'.format(y_train[i].argmax()))\n    plt.axis('off')\n\n    cont += 1","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:13:59.275375Z","iopub.execute_input":"2023-03-03T19:13:59.276065Z","iopub.status.idle":"2023-03-03T19:14:00.010812Z","shell.execute_reply.started":"2023-03-03T19:13:59.276023Z","shell.execute_reply":"2023-03-03T19:14:00.009833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Example of validation images","metadata":{}},{"cell_type":"code","source":"cont = 1\n\nplt.figure(figsize=(6,6))\n\nfor i in range(25):\n    plt.subplot(5, 5, cont)\n    plt.imshow(X_val[i], cmap='gray')\n    plt.title('Class {}'.format(y_val[i].argmax()))\n    plt.axis('off')\n\n    cont += 1","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:14:00.752508Z","iopub.execute_input":"2023-03-03T19:14:00.752897Z","iopub.status.idle":"2023-03-03T19:14:01.479621Z","shell.execute_reply.started":"2023-03-03T19:14:00.752863Z","shell.execute_reply":"2023-03-03T19:14:01.478718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## CNN","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\n\ncnn_model = keras.Sequential(\n    [\n        keras.Input((28, 28, 1)),\n        layers.Conv2D(filters = 32, kernel_size = (3,3), activation ='relu'),\n        layers.MaxPool2D(2),\n        layers.Dropout(0.2),\n        layers.Conv2D(filters = 64, kernel_size = (3,3), activation ='relu'),\n        layers.MaxPool2D(2),\n        layers.Dropout(0.2),\n        layers.Conv2D(filters = 128, kernel_size = (3,3), activation ='relu'),\n        layers.Dropout(0.2),\n        layers.Flatten(),\n        layers.Dense(25, activation=\"relu\"),\n        layers.Dropout(0.2),\n        layers.Dense(10, activation=\"softmax\"),\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:22:31.582736Z","iopub.execute_input":"2023-03-03T19:22:31.583451Z","iopub.status.idle":"2023-03-03T19:22:31.677518Z","shell.execute_reply.started":"2023-03-03T19:22:31.583415Z","shell.execute_reply":"2023-03-03T19:22:31.676573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:22:32.189159Z","iopub.execute_input":"2023-03-03T19:22:32.189527Z","iopub.status.idle":"2023-03-03T19:22:32.248578Z","shell.execute_reply.started":"2023-03-03T19:22:32.189495Z","shell.execute_reply":"2023-03-03T19:22:32.246401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model.compile(\n    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),\n    loss='categorical_crossentropy',\n    metrics='accuracy')","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:22:32.752303Z","iopub.execute_input":"2023-03-03T19:22:32.752668Z","iopub.status.idle":"2023-03-03T19:22:32.763026Z","shell.execute_reply.started":"2023-03-03T19:22:32.752638Z","shell.execute_reply":"2023-03-03T19:22:32.762016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monitor_val_loss = tf.keras.callbacks.EarlyStopping(\n    monitor='val_loss',\n    patience=10\n)\n\nbest_model_path = '/kaggle/working/best_model'\n\nsave_best = tf.keras.callbacks.ModelCheckpoint(\n    best_model_path,\n    monitor='val_loss',\n    mode='min',\n    save_weights_only=True,\n    save_best_only=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:22:33.937200Z","iopub.execute_input":"2023-03-03T19:22:33.937555Z","iopub.status.idle":"2023-03-03T19:22:33.943344Z","shell.execute_reply.started":"2023-03-03T19:22:33.937525Z","shell.execute_reply":"2023-03-03T19:22:33.942272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 100","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:22:34.397529Z","iopub.execute_input":"2023-03-03T19:22:34.397911Z","iopub.status.idle":"2023-03-03T19:22:34.402627Z","shell.execute_reply.started":"2023-03-03T19:22:34.397878Z","shell.execute_reply":"2023-03-03T19:22:34.401621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = cnn_model.fit(\n    X_train,\n    y_train,\n    batch_size=64,\n    epochs=epochs,\n    validation_data=(X_val, y_val),\n    callbacks=[monitor_val_loss, save_best])","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:22:34.929143Z","iopub.execute_input":"2023-03-03T19:22:34.929811Z","iopub.status.idle":"2023-03-03T19:23:22.008540Z","shell.execute_reply.started":"2023-03-03T19:22:34.929753Z","shell.execute_reply":"2023-03-03T19:23:22.007373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'], label=\"Train\")\nplt.plot(history.history['val_loss'], label=\"Validation\")\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:23:22.011057Z","iopub.execute_input":"2023-03-03T19:23:22.011489Z","iopub.status.idle":"2023-03-03T19:23:22.250623Z","shell.execute_reply.started":"2023-03-03T19:23:22.011452Z","shell.execute_reply":"2023-03-03T19:23:22.249598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], label=\"Train\")\nplt.plot(history.history['val_accuracy'], label=\"Validation\")\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:23:22.253026Z","iopub.execute_input":"2023-03-03T19:23:22.254167Z","iopub.status.idle":"2023-03-03T19:23:22.480654Z","shell.execute_reply.started":"2023-03-03T19:23:22.254125Z","shell.execute_reply":"2023-03-03T19:23:22.479723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model.load_weights(best_model_path)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:23:52.984419Z","iopub.execute_input":"2023-03-03T19:23:52.984788Z","iopub.status.idle":"2023-03-03T19:23:53.017760Z","shell.execute_reply.started":"2023-03-03T19:23:52.984743Z","shell.execute_reply":"2023-03-03T19:23:53.016843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_model.evaluate(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T19:23:53.547177Z","iopub.execute_input":"2023-03-03T19:23:53.547958Z","iopub.status.idle":"2023-03-03T19:23:54.345671Z","shell.execute_reply.started":"2023-03-03T19:23:53.547913Z","shell.execute_reply":"2023-03-03T19:23:54.344725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(test_ds_path).to_numpy()\n\ntest.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-03T18:51:45.240708Z","iopub.execute_input":"2023-03-03T18:51:45.241424Z","iopub.status.idle":"2023-03-03T18:51:46.335414Z","shell.execute_reply.started":"2023-03-03T18:51:45.241387Z","shell.execute_reply":"2023-03-03T18:51:46.334465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.reshape(-1, 28, 28, 1)\n\ntest.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-03T18:51:47.627419Z","iopub.execute_input":"2023-03-03T18:51:47.628104Z","iopub.status.idle":"2023-03-03T18:51:47.635547Z","shell.execute_reply.started":"2023-03-03T18:51:47.628066Z","shell.execute_reply":"2023-03-03T18:51:47.634455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = cnn_model.predict(test)\n\npredictions.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-03T18:51:49.986402Z","iopub.execute_input":"2023-03-03T18:51:49.987048Z","iopub.status.idle":"2023-03-03T18:51:53.031205Z","shell.execute_reply.started":"2023-03-03T18:51:49.987008Z","shell.execute_reply":"2023-03-03T18:51:53.030193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pandas import DataFrame\n\nsubmission = DataFrame(\n    {\n        'ImageId': np.arange(1, predictions.shape[0] + 1),\n        'Label': predictions.argmax(axis=1)\n    })\n\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-03T18:51:56.062489Z","iopub.execute_input":"2023-03-03T18:51:56.062874Z","iopub.status.idle":"2023-03-03T18:51:56.076602Z","shell.execute_reply.started":"2023-03-03T18:51:56.062840Z","shell.execute_reply":"2023-03-03T18:51:56.075618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-03T18:51:56.420102Z","iopub.execute_input":"2023-03-03T18:51:56.420919Z","iopub.status.idle":"2023-03-03T18:51:56.453058Z","shell.execute_reply.started":"2023-03-03T18:51:56.420869Z","shell.execute_reply":"2023-03-03T18:51:56.452117Z"},"trusted":true},"execution_count":null,"outputs":[]}]}