{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, accuracy_score\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Dropout, Flatten, Dense, BatchNormalization, Activation\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping\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":"2024-07-07T11:18:59.452277Z","iopub.execute_input":"2024-07-07T11:18:59.452654Z","iopub.status.idle":"2024-07-07T11:19:13.549986Z","shell.execute_reply.started":"2024-07-07T11:18:59.452623Z","shell.execute_reply":"2024-07-07T11:19:13.548978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Load data\ntrain = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-07-07T11:19:29.680784Z","iopub.execute_input":"2024-07-07T11:19:29.681682Z","iopub.status.idle":"2024-07-07T11:19:35.191244Z","shell.execute_reply.started":"2024-07-07T11:19:29.681649Z","shell.execute_reply":"2024-07-07T11:19:35.190223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Check for null and missing values\nprint(train.shape)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-07T11:19:42.623981Z","iopub.execute_input":"2024-07-07T11:19:42.624420Z","iopub.status.idle":"2024-07-07T11:19:42.654441Z","shell.execute_reply.started":"2024-07-07T11:19:42.624387Z","shell.execute_reply":"2024-07-07T11:19:42.653544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Data normalization\nX_train = train.iloc[:,1:].values/255.0\nX_test = test.values/255.0","metadata":{"execution":{"iopub.status.busy":"2024-07-07T11:19:46.931571Z","iopub.execute_input":"2024-07-07T11:19:46.931931Z","iopub.status.idle":"2024-07-07T11:19:47.100114Z","shell.execute_reply.started":"2024-07-07T11:19:46.931902Z","shell.execute_reply":"2024-07-07T11:19:47.099327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reshape\nX_train = X_train.reshape(-1, 28, 28, 1)\nX_test = X_test.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2024-07-07T11:19:51.149915Z","iopub.execute_input":"2024-07-07T11:19:51.150860Z","iopub.status.idle":"2024-07-07T11:19:51.155519Z","shell.execute_reply.started":"2024-07-07T11:19:51.150824Z","shell.execute_reply":"2024-07-07T11:19:51.154539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Label encoding\nY_train = train.iloc[:, 0].values\nY_train = to_categorical(Y_train, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2024-07-07T11:19:53.581579Z","iopub.execute_input":"2024-07-07T11:19:53.581924Z","iopub.status.idle":"2024-07-07T11:19:53.587632Z","shell.execute_reply.started":"2024-07-07T11:19:53.581896Z","shell.execute_reply":"2024-07-07T11:19:53.586790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Split training and validation set\nX_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-07-07T11:19:56.190355Z","iopub.execute_input":"2024-07-07T11:19:56.191074Z","iopub.status.idle":"2024-07-07T11:19:56.582666Z","shell.execute_reply.started":"2024-07-07T11:19:56.191030Z","shell.execute_reply":"2024-07-07T11:19:56.581854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Generates any random image out of data set\nplt.figure(figsize =(9,9))\nfor i in range(20):\n    plt.subplot(5,5,1+i)\n    plt.xticks([])\n    plt.yticks([])\n    j = np.random.randint(1, 42001)\n    plt.imshow(np.array(train.drop(['label'],axis = 1).iloc[j]).reshape(28,28))\n    plt.xlabel(np.array(train['label'])[j])\n","metadata":{"execution":{"iopub.status.busy":"2024-07-07T11:20:01.230728Z","iopub.execute_input":"2024-07-07T11:20:01.231340Z","iopub.status.idle":"2024-07-07T11:20:03.734392Z","shell.execute_reply.started":"2024-07-07T11:20:01.231309Z","shell.execute_reply":"2024-07-07T11:20:03.733253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **CNN Model**","metadata":{}},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-07-07T11:20:29.850451Z","iopub.execute_input":"2024-07-07T11:20:29.851295Z","iopub.status.idle":"2024-07-07T11:20:30.813117Z","shell.execute_reply.started":"2024-07-07T11:20:29.851259Z","shell.execute_reply":"2024-07-07T11:20:30.811796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Define model\nfrom tensorflow.keras.layers import Input\n\nmodel = Sequential([\n    Input(shape=(28, 28, 1)),\n    \n    Conv2D(64, kernel_size=(3, 3), padding='same'),\n    BatchNormalization(),\n    Activation('relu'),\n    \n    Conv2D(64, kernel_size=(3, 3), padding='same'),\n    BatchNormalization(),\n    Activation('relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Dropout(0.25),\n    \n    Conv2D(128, kernel_size=(3, 3), padding='same'),\n    BatchNormalization(),\n    Activation('relu'),\n    \n    Conv2D(128, kernel_size=(3, 3), padding='same'),\n    BatchNormalization(),\n    Activation('relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Dropout(0.25),\n    \n    Conv2D(256, kernel_size=(3, 3), padding='same'),\n    BatchNormalization(),\n    Activation('relu'),\n    \n    Conv2D(256, kernel_size=(3, 3), padding='same'),\n    BatchNormalization(),\n    Activation('relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Dropout(0.25),\n    \n    Flatten(),\n    \n    Dense(512),\n    BatchNormalization(),\n    Activation('relu'),\n    Dropout(0.5),\n    \n    Dense(512),\n    BatchNormalization(),\n    Activation('relu'),\n    Dropout(0.5),\n    \n    Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2024-07-06T23:00:45.804652Z","iopub.execute_input":"2024-07-06T23:00:45.805533Z","iopub.status.idle":"2024-07-06T23:00:46.059211Z","shell.execute_reply.started":"2024-07-06T23:00:45.805468Z","shell.execute_reply":"2024-07-06T23:00:46.058160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Set the optimizer and annealer\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-07-06T23:00:46.060725Z","iopub.execute_input":"2024-07-06T23:00:46.061071Z","iopub.status.idle":"2024-07-06T23:00:46.076824Z","shell.execute_reply.started":"2024-07-06T23:00:46.061040Z","shell.execute_reply":"2024-07-06T23:00:46.075744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Data augmentation\ndatagen = ImageDataGenerator(\n    rotation_range=5,  \n    zoom_range=0.3, \n    width_shift_range=0.1, height_shift_range=0.1, channel_shift_range=0.1,\n    shear_range=0.3, \n)\n\ndatagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2024-07-06T23:00:46.078196Z","iopub.execute_input":"2024-07-06T23:00:46.078555Z","iopub.status.idle":"2024-07-06T23:00:46.178375Z","shell.execute_reply.started":"2024-07-06T23:00:46.078525Z","shell.execute_reply":"2024-07-06T23:00:46.177169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Training and validation curves\nhistory = model.fit(datagen.flow(X_train, Y_train, batch_size=32),\n                    epochs=100, validation_data=(X_val, Y_val),\n                    steps_per_epoch=X_train.shape[0] // 32,\n                    callbacks = [ReduceLROnPlateau(monitor='val_accuracy', patience=3, verbose=1, factor=0.5, min_lr=0.00000001), EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)])\n\n# Plot the training and validation accuracy and loss\nfig, ax = plt.subplots(2, 1, figsize=(12, 6))\nax[0].plot(history.history['accuracy'], label='train accuracy')\nax[0].plot(history.history['val_accuracy'], label='val accuracy')\nax[0].set_title('Model accuracy')\nax[0].set_xlabel('Epoch')\nax[0].set_ylabel('Accuracy')\nax[0].legend(loc='best')\n\nax[1].plot(history.history['loss'], label='train loss')\nax[1].plot(history.history['val_loss'], label='val loss')\nax[1].set_title('Model loss')\nax[1].set_xlabel('Epoch')\nax[1].set_ylabel('Loss')\nax[1].legend(loc='best')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-06T23:00:46.179851Z","iopub.execute_input":"2024-07-06T23:00:46.180180Z","iopub.status.idle":"2024-07-06T23:01:31.272203Z","shell.execute_reply.started":"2024-07-06T23:00:46.180142Z","shell.execute_reply":"2024-07-06T23:01:31.270283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Confusion matrix\nY_val_pred = np.argmax(model.predict(X_val), axis=-1)\nY_val_true = np.argmax(Y_val, axis=-1)\nconf_matrix = confusion_matrix(Y_val_true, Y_val_pred)\n\nplt.figure(figsize=(10, 8))\nsns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues')\nplt.xlabel('Predicted Label')\nplt.ylabel('True Label')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-06T23:01:31.273407Z","iopub.status.idle":"2024-07-06T23:01:31.273999Z","shell.execute_reply.started":"2024-07-06T23:01:31.273709Z","shell.execute_reply":"2024-07-06T23:01:31.273731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Predict and Submit results\nY_test_pred = np.argmax(model.predict(X_test), axis=-1)\nsubmission = pd.DataFrame({'ImageId': list(range(1, len(Y_test_pred) + 1)), 'Label': Y_test_pred})\nsubmission.to_csv('/kaggle/working/submission.csv', index=False)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-06T23:01:31.275920Z","iopub.status.idle":"2024-07-06T23:01:31.276456Z","shell.execute_reply.started":"2024-07-06T23:01:31.276178Z","shell.execute_reply":"2024-07-06T23:01:31.276199Z"},"trusted":true},"execution_count":null,"outputs":[]}]}