{"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\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\n\nfrom subprocess import check_output\nprint( check_output(['ls', '../input']).decode('utf8') )","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-12T13:44:51.991505Z","iopub.execute_input":"2023-08-12T13:44:51.992334Z","iopub.status.idle":"2023-08-12T13:44:52.091647Z","shell.execute_reply.started":"2023-08-12T13:44:51.992293Z","shell.execute_reply":"2023-08-12T13:44:52.090642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Train & Test Data","metadata":{}},{"cell_type":"code","source":"data_dir = '../input/digit-recognizer/'\n\ndf_train = pd.read_csv(data_dir+'train.csv')\ndf_test = pd.read_csv(data_dir+'test.csv')\ndf_sample_submission = pd.read_csv(data_dir +'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:44:52.094389Z","iopub.execute_input":"2023-08-12T13:44:52.094712Z","iopub.status.idle":"2023-08-12T13:44:56.536616Z","shell.execute_reply.started":"2023-08-12T13:44:52.094660Z","shell.execute_reply":"2023-08-12T13:44:56.535715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:44:56.539323Z","iopub.execute_input":"2023-08-12T13:44:56.539834Z","iopub.status.idle":"2023-08-12T13:44:56.558535Z","shell.execute_reply.started":"2023-08-12T13:44:56.539794Z","shell.execute_reply":"2023-08-12T13:44:56.557548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df_train.shape)\nprint(df_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:44:56.560727Z","iopub.execute_input":"2023-08-12T13:44:56.561046Z","iopub.status.idle":"2023-08-12T13:44:56.572271Z","shell.execute_reply.started":"2023-08-12T13:44:56.560961Z","shell.execute_reply":"2023-08-12T13:44:56.571371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX = df_train.drop('label', axis=1)\ny = df_train['label']\n\ntrain_images, val_images, y_train, y_val = train_test_split(\n    X, y,\n    test_size=0.1,\n    random_state=42,\n    shuffle=True,\n    stratify=y,\n)","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:44:56.573630Z","iopub.execute_input":"2023-08-12T13:44:56.574046Z","iopub.status.idle":"2023-08-12T13:44:56.923676Z","shell.execute_reply.started":"2023-08-12T13:44:56.574010Z","shell.execute_reply":"2023-08-12T13:44:56.922837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## show some images","metadata":{}},{"cell_type":"code","source":"def show_imgs(imgs:list, title=None, nrows=5, ncols=5, figs=(15, 10)):\n    plt.figure(figsize=figs)\n    for i in range(len(imgs)):\n        plt.subplot(nrows, ncols, i+1)\n        \n        if title is not None:\n            if len(title) == 1:\n                plt.title(title)\n            else:\n                plt.title(title[i])\n                \n        plt.imshow(np.reshape(imgs[i], (28, 28)), cmap='gray')\n        plt.axis('off')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:44:56.926286Z","iopub.execute_input":"2023-08-12T13:44:56.926825Z","iopub.status.idle":"2023-08-12T13:44:56.934710Z","shell.execute_reply.started":"2023-08-12T13:44:56.926766Z","shell.execute_reply":"2023-08-12T13:44:56.933957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_imgs(train_images.iloc[:25].values, title=y_train.iloc[:25].values)","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:44:56.936267Z","iopub.execute_input":"2023-08-12T13:44:56.936587Z","iopub.status.idle":"2023-08-12T13:44:57.947793Z","shell.execute_reply.started":"2023-08-12T13:44:56.936544Z","shell.execute_reply":"2023-08-12T13:44:57.946924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prepare_dataset(data, label=None):\n    img = tf.cast(tf.reshape(data, (28, 28, 1)), dtype=tf.float32)\n    if label is not None:\n        return img, tf.cast(label, dtype=tf.int8)\n    return img","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:44:57.949190Z","iopub.execute_input":"2023-08-12T13:44:57.949673Z","iopub.status.idle":"2023-08-12T13:44:57.955954Z","shell.execute_reply.started":"2023-08-12T13:44:57.949630Z","shell.execute_reply":"2023-08-12T13:44:57.955162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\n\ntfds_train = tf.data.Dataset.from_tensor_slices((train_images, y_train))\n\ntfds_train = tfds_train.map(prepare_dataset)\ntfds_train = tfds_train.batch(batch_size)\n\ntfds_train","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:44:57.959882Z","iopub.execute_input":"2023-08-12T13:44:57.960645Z","iopub.status.idle":"2023-08-12T13:44:58.581830Z","shell.execute_reply.started":"2023-08-12T13:44:57.960604Z","shell.execute_reply":"2023-08-12T13:44:58.581046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for v in tfds_train.take(1).as_numpy_iterator():\n    print(v[0][0].shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:44:58.584819Z","iopub.execute_input":"2023-08-12T13:44:58.585436Z","iopub.status.idle":"2023-08-12T13:44:58.808548Z","shell.execute_reply.started":"2023-08-12T13:44:58.585402Z","shell.execute_reply":"2023-08-12T13:44:58.807724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import layers\nfrom tensorflow.keras import models","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:44:58.809954Z","iopub.execute_input":"2023-08-12T13:44:58.810685Z","iopub.status.idle":"2023-08-12T13:44:58.816467Z","shell.execute_reply.started":"2023-08-12T13:44:58.810645Z","shell.execute_reply":"2023-08-12T13:44:58.815682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_layer = layers.Input(shape=(28, 28, 1))\n\nx = layers.Conv2D(32, 3, activation='relu', padding='same')(input_layer)\nx = layers.Conv2D(64, 3, activation='relu', padding='same')(x)\nx = layers.MaxPooling2D(pool_size=(2,2), strides=(2,2))(x)\n\nx = layers.Conv2D(128, 3, activation='relu')(x)\nx = layers.Conv2D(256, 3, activation='relu')(x)\nx = layers.MaxPooling2D(pool_size=(2,2), strides=(2,2))(x)\n\nx = layers.Flatten()(x)\n\nx = layers.Dense(512, activation='relu')(x)\nx = layers.Dropout(0.4)(x)\nclassifier = layers.Dense(10, activation='softmax')(x)\n\nmodel = models.Model(inputs=[input_layer], outputs=[classifier])\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:44:58.817930Z","iopub.execute_input":"2023-08-12T13:44:58.818617Z","iopub.status.idle":"2023-08-12T13:44:58.901772Z","shell.execute_reply.started":"2023-08-12T13:44:58.818582Z","shell.execute_reply":"2023-08-12T13:44:58.901052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:44:58.903033Z","iopub.execute_input":"2023-08-12T13:44:58.903305Z","iopub.status.idle":"2023-08-12T13:44:58.914925Z","shell.execute_reply.started":"2023-08-12T13:44:58.903268Z","shell.execute_reply":"2023-08-12T13:44:58.914219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tfds_val = tf.data.Dataset.from_tensor_slices((val_images, y_val))\n\ntfds_val = tfds_val.map(prepare_dataset)\ntfds_val = tfds_val.batch(batch_size)\n\ntfds_val","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:44:58.916598Z","iopub.execute_input":"2023-08-12T13:44:58.917288Z","iopub.status.idle":"2023-08-12T13:44:58.963971Z","shell.execute_reply.started":"2023-08-12T13:44:58.917245Z","shell.execute_reply":"2023-08-12T13:44:58.962978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(tfds_train, validation_data=tfds_val, epochs=20, verbose=2)","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:44:58.965414Z","iopub.execute_input":"2023-08-12T13:44:58.965764Z","iopub.status.idle":"2023-08-12T13:48:16.233301Z","shell.execute_reply.started":"2023-08-12T13:44:58.965725Z","shell.execute_reply":"2023-08-12T13:48:16.232389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history[\"loss\"], c=\"b\", label=\"Training Loss\")\nplt.plot(history.history[\"val_loss\"], c=\"r\", label=\"Validation Loss\")\nplt.xlabel(\"no. Epoch\")\nplt.xlabel(\"Loss\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:48:16.234980Z","iopub.execute_input":"2023-08-12T13:48:16.235414Z","iopub.status.idle":"2023-08-12T13:48:16.478655Z","shell.execute_reply.started":"2023-08-12T13:48:16.235362Z","shell.execute_reply":"2023-08-12T13:48:16.477837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history[\"accuracy\"], c=\"b\", label=\"Training Accuracy\")\nplt.plot(history.history[\"val_accuracy\"], c=\"r\", label=\"Validation Accuracy\")\nplt.xlabel(\"no. Epoch\")\nplt.xlabel(\"Accuracy\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:48:16.479924Z","iopub.execute_input":"2023-08-12T13:48:16.481333Z","iopub.status.idle":"2023-08-12T13:48:16.716472Z","shell.execute_reply.started":"2023-08-12T13:48:16.481290Z","shell.execute_reply":"2023-08-12T13:48:16.715603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf_test = tf.data.Dataset.from_tensor_slices(df_test.values)\n\ntf_test = tf_test.map(prepare_dataset)\ntf_test = tf_test.batch(batch_size)\n\ntf_test","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:48:16.717622Z","iopub.execute_input":"2023-08-12T13:48:16.719310Z","iopub.status.idle":"2023-08-12T13:48:17.148821Z","shell.execute_reply.started":"2023-08-12T13:48:16.719268Z","shell.execute_reply":"2023-08-12T13:48:17.148039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"proba = model.predict(tf_test)\nclasses = tf.argmax(proba, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:48:17.150424Z","iopub.execute_input":"2023-08-12T13:48:17.150958Z","iopub.status.idle":"2023-08-12T13:48:19.265031Z","shell.execute_reply.started":"2023-08-12T13:48:17.150918Z","shell.execute_reply":"2023-08-12T13:48:19.264112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"proba","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:48:19.266940Z","iopub.execute_input":"2023-08-12T13:48:19.267282Z","iopub.status.idle":"2023-08-12T13:48:19.276305Z","shell.execute_reply.started":"2023-08-12T13:48:19.267227Z","shell.execute_reply":"2023-08-12T13:48:19.275230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes.numpy().shape","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:48:19.278231Z","iopub.execute_input":"2023-08-12T13:48:19.278624Z","iopub.status.idle":"2023-08-12T13:48:19.290241Z","shell.execute_reply.started":"2023-08-12T13:48:19.278577Z","shell.execute_reply":"2023-08-12T13:48:19.289293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_labels = classes.numpy()\ntest_images = df_test.values\n\nstart=0\nend=64\n\nfor _ in range(2):\n    show_imgs(test_images[start:end], title=test_labels[start:end], nrows=8, ncols=8, figs=(15, 15))\n    start += end\n    end += 64\n    plt.show()\n    print(\"=\"*100)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:48:19.291597Z","iopub.execute_input":"2023-08-12T13:48:19.292130Z","iopub.status.idle":"2023-08-12T13:48:24.724177Z","shell.execute_reply.started":"2023-08-12T13:48:19.292087Z","shell.execute_reply":"2023-08-12T13:48:24.723422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(tfds_val)","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:48:24.725763Z","iopub.execute_input":"2023-08-12T13:48:24.726367Z","iopub.status.idle":"2023-08-12T13:48:25.391835Z","shell.execute_reply.started":"2023-08-12T13:48:24.726321Z","shell.execute_reply":"2023-08-12T13:48:25.391030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Save our model \n","metadata":{}},{"cell_type":"code","source":"model.save(\"digit_recognizer_with_cnn.pth\")","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:48:25.395513Z","iopub.execute_input":"2023-08-12T13:48:25.395747Z","iopub.status.idle":"2023-08-12T13:48:27.082934Z","shell.execute_reply.started":"2023-08-12T13:48:25.395718Z","shell.execute_reply":"2023-08-12T13:48:27.082126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sample_submission","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:48:27.086008Z","iopub.execute_input":"2023-08-12T13:48:27.086254Z","iopub.status.idle":"2023-08-12T13:48:27.097691Z","shell.execute_reply.started":"2023-08-12T13:48:27.086218Z","shell.execute_reply":"2023-08-12T13:48:27.096883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'ImageId': df_sample_submission.ImageId, 'Label': classes.numpy()})\noutput.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:48:27.098997Z","iopub.execute_input":"2023-08-12T13:48:27.099538Z","iopub.status.idle":"2023-08-12T13:48:27.169484Z","shell.execute_reply.started":"2023-08-12T13:48:27.099488Z","shell.execute_reply":"2023-08-12T13:48:27.168643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output","metadata":{"execution":{"iopub.status.busy":"2023-08-12T13:48:27.170741Z","iopub.execute_input":"2023-08-12T13:48:27.171607Z","iopub.status.idle":"2023-08-12T13:48:27.184792Z","shell.execute_reply.started":"2023-08-12T13:48:27.171568Z","shell.execute_reply":"2023-08-12T13:48:27.183814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}