{"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":"nvidiaTeslaT4","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":"import cv2\nimport time\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom keras.datasets import mnist\nfrom keras.utils import to_categorical\nfrom keras.models import load_model, Sequential\nfrom keras.layers import Conv2D, Flatten, Dropout, Dense, MaxPooling2D, Input\nfrom keras.optimizers import SGD\n\nfrom sklearn.model_selection import train_test_split\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-20T09:23:13.172140Z","iopub.execute_input":"2024-06-20T09:23:13.172489Z","iopub.status.idle":"2024-06-20T09:23:26.158634Z","shell.execute_reply.started":"2024-06-20T09:23:13.172463Z","shell.execute_reply":"2024-06-20T09:23:26.157780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/input/digit-recognizer/train.csv'\ntest_path = '/kaggle/input/digit-recognizer/test.csv'\nsubmission_path = '/kaggle/input/digit-recognizer/sample_submission.csv'","metadata":{"execution":{"iopub.status.busy":"2024-06-20T09:23:30.620640Z","iopub.execute_input":"2024-06-20T09:23:30.621306Z","iopub.status.idle":"2024-06-20T09:23:30.625671Z","shell.execute_reply.started":"2024-06-20T09:23:30.621267Z","shell.execute_reply":"2024-06-20T09:23:30.624740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(X_train,y_train),(X_test,y_test) = mnist.load_data()\nX_train = np.vstack((X_train, X_test))\ny_train = np.concatenate([y_train, y_test])\nprint(X_train.shape, y_train.shape)\n\ntrain = pd.read_csv(train_path).values\ny_val = train[:,0].astype('int32')\nX_val = train[:,1:].astype('float32')\nprint(X_val.shape, y_val.shape)\n\nX_test = pd.read_csv(test_path).values.astype('float32')","metadata":{"execution":{"iopub.status.busy":"2024-06-20T09:23:32.498318Z","iopub.execute_input":"2024-06-20T09:23:32.499129Z","iopub.status.idle":"2024-06-20T09:23:39.720554Z","shell.execute_reply.started":"2024-06-20T09:23:32.499098Z","shell.execute_reply":"2024-06-20T09:23:39.719735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def deskew_an_image(image):\n    # Filter image pixel\n    rows, cols = np.where(image > 0.5)\n\n    # Normalize cell and column\n    normalized_rows = (rows - np.mean(rows)) / np.std(rows)\n    normalized_cols = (cols - np.mean(cols)) / np.std(cols)\n\n    # Find correlation coefficient\n    corr_coef = np.mean(normalized_rows * normalized_cols) * np.std(cols) / np.std(rows)\n\n    centroid_row = np.mean(rows)\n\n    # Find ic\n    ic = np.arange(image.shape[1])[np.newaxis, :]  + (centroid_row - np.arange(image.shape[0]))[:, np.newaxis] * (-corr_coef)\n\n    # Find and modify ic_floor\n    ic_floor = np.floor(ic).astype(int)\n    ic_floor[ic_floor > 27] = 27\n    ic_floor[ic_floor < 0] = 0\n\n    # Find and modify ic_ceil\n    ic_ceil = np.ceil(ic).astype(int)\n    ic_ceil[ic_ceil > 27] = 27\n    ic_ceil[ic_ceil < 0] = 0\n\n    con = ic_ceil - ic\n    deskewed_image = con * image[np.arange(image.shape[0])[:, np.newaxis], ic_floor] + (1 - con) * image[np.arange(image.shape[0])[:, np.newaxis], ic_ceil]\n\n    return deskewed_image\n\n\ndef deskew(X):\n  # YOUR CODE HERE\n  # raise NotImplementedError()\n  res = []\n  for image in X:\n      # Reshape to original shape\n      image_matrix = image.reshape(28,28)\n\n      # Deskew each image\n      deskewed_image = deskew_an_image(image_matrix)\n\n      res.append(deskewed_image)\n\n  return np.array(res)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-20T09:23:39.722230Z","iopub.execute_input":"2024-06-20T09:23:39.722905Z","iopub.status.idle":"2024-06-20T09:23:39.733456Z","shell.execute_reply.started":"2024-06-20T09:23:39.722852Z","shell.execute_reply":"2024-06-20T09:23:39.732563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nplt.figure(figsize=(10, 10))\nfor i in range(20):\n    plt.subplot(10, 10, i+1)\n    plt.imshow(X_train[i], cmap='gray')\n    plt.axis('off')\n    plt.title(f'{y_train[i]}')\nplt.suptitle(f'Before skewed')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-06-20T09:23:41.109797Z","iopub.execute_input":"2024-06-20T09:23:41.110651Z","iopub.status.idle":"2024-06-20T09:23:42.327147Z","shell.execute_reply.started":"2024-06-20T09:23:41.110615Z","shell.execute_reply":"2024-06-20T09:23:42.326226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = deskew(X_train)\nX_val = deskew(X_val)\nX_test = deskew(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T09:23:45.485926Z","iopub.execute_input":"2024-06-20T09:23:45.486886Z","iopub.status.idle":"2024-06-20T09:24:29.148606Z","shell.execute_reply.started":"2024-06-20T09:23:45.486854Z","shell.execute_reply":"2024-06-20T09:24:29.147777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\n\nfor i in range(20):\n    plt.subplot(10, 10, i+1)\n    plt.imshow(X_train[i], cmap='gray')\n    plt.axis('off')\n    plt.title(f'{y_train[i]}')\nplt.suptitle(f'After skewed')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T09:24:42.488683Z","iopub.execute_input":"2024-06-20T09:24:42.489404Z","iopub.status.idle":"2024-06-20T09:24:43.472938Z","shell.execute_reply.started":"2024-06-20T09:24:42.489373Z","shell.execute_reply":"2024-06-20T09:24:43.471941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize the pixel values\nX_train = X_train / 255.0\nX_val = X_val / 255.0\nX_test = X_test / 255.0\n\n# Reshape the data to fit the model (28x28x1)\nX_train = X_train.reshape(-1, 28, 28, 1)\nX_val = X_val.reshape(-1, 28, 28, 1)\nX_test = X_test.reshape(-1, 28, 28, 1)\n\n# One-hot encode the labels\ny_train = to_categorical(y_train, num_classes=10)\ny_val = to_categorical(y_val, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T09:24:47.062401Z","iopub.execute_input":"2024-06-20T09:24:47.063251Z","iopub.status.idle":"2024-06-20T09:24:47.323038Z","shell.execute_reply.started":"2024-06-20T09:24:47.063217Z","shell.execute_reply":"2024-06-20T09:24:47.322049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    Input(shape=(28,28,1)),\n    Conv2D(64, (3, 3), activation='relu', padding='SAME'),\n    Conv2D(64, (3, 3), activation='relu', padding='SAME'),\n    Conv2D(128, (3, 3), activation='relu', padding='SAME'),\n    MaxPooling2D((2, 2)),\n    \n    Conv2D(128, (3, 3), activation='relu',padding='SAME'),\n    Conv2D(192, (3, 3), activation='relu',padding='SAME'),\n    MaxPooling2D((2, 2)),\n#     Dropout(0.25),\n    Flatten(),\n    Dense(256, activation='relu'),\n#     Dropout(0.5),\n    Dense(10, activation='softmax')\n])\n\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T09:26:18.161206Z","iopub.execute_input":"2024-06-20T09:26:18.162293Z","iopub.status.idle":"2024-06-20T09:26:19.025298Z","shell.execute_reply.started":"2024-06-20T09:26:18.162251Z","shell.execute_reply":"2024-06-20T09:26:19.024408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint   \nfrom keras.callbacks import ReduceLROnPlateau\n\n\nreduce_lr = ReduceLROnPlateau(monitor='loss', factor=0.3, verbose=1,\n                              patience=2, min_lr=0.00000001)\nhist = model.fit(X_train, y_train, batch_size=100, epochs=25,\n          validation_data=(X_val, y_val), callbacks=[reduce_lr],\n          verbose=1, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T09:27:09.130384Z","iopub.execute_input":"2024-06-20T09:27:09.130749Z","iopub.status.idle":"2024-06-20T09:35:29.117911Z","shell.execute_reply.started":"2024-06-20T09:27:09.130722Z","shell.execute_reply":"2024-06-20T09:35:29.117086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=42)\n\n# history = model.fit(X_train, y_train, epochs=10, batch_size=128, validation_data=(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2024-06-20T09:35:38.314968Z","iopub.execute_input":"2024-06-20T09:35:38.315822Z","iopub.status.idle":"2024-06-20T09:35:38.319589Z","shell.execute_reply.started":"2024-06-20T09:35:38.315791Z","shell.execute_reply":"2024-06-20T09:35:38.318667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss,accuracy = model.evaluate(X_val,y_val,verbose=0)\n\nprint('Test loss ---> ',str(round(loss*100,2)) +str('%'))\nprint('Test accuracy ---> ',str(round(accuracy*100,2)) +str('%'))","metadata":{"execution":{"iopub.status.busy":"2024-06-20T09:35:41.409672Z","iopub.execute_input":"2024-06-20T09:35:41.410631Z","iopub.status.idle":"2024-06-20T09:35:48.493632Z","shell.execute_reply.started":"2024-06-20T09:35:41.410590Z","shell.execute_reply":"2024-06-20T09:35:48.492602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_dict = hist.history\nprint(history_dict.keys())","metadata":{"execution":{"iopub.status.busy":"2024-06-20T09:36:06.958002Z","iopub.execute_input":"2024-06-20T09:36:06.958941Z","iopub.status.idle":"2024-06-20T09:36:06.963516Z","shell.execute_reply.started":"2024-06-20T09:36:06.958904Z","shell.execute_reply":"2024-06-20T09:36:06.962548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss = history_dict['val_loss']\ntraining_loss = history_dict['loss']\ntest_accuracy = history_dict['val_accuracy']\ntraining_accuracy = history_dict['accuracy']","metadata":{"execution":{"iopub.status.busy":"2024-06-20T09:36:09.722707Z","iopub.execute_input":"2024-06-20T09:36:09.723455Z","iopub.status.idle":"2024-06-20T09:36:09.728154Z","shell.execute_reply.started":"2024-06-20T09:36:09.723420Z","shell.execute_reply":"2024-06-20T09:36:09.727087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = list(range(1,len(test_loss)+1))\n\n# Create subplots\nfig, axs = plt.subplots(1, 2, figsize=(10, 4))\n\n# Plot loss\naxs[0].plot(epochs, test_loss, marker='X', label='Test Loss')\naxs[0].plot(epochs, training_loss, marker='X', label='Training Loss')\naxs[0].set_title('Loss')\naxs[0].set_xlabel('Epochs')\naxs[0].set_ylabel('Loss')\naxs[0].legend()\n\n# Plot accuracy\naxs[1].plot(epochs, test_accuracy, marker='X', label='Test Accuracy')\naxs[1].plot(epochs, training_accuracy, marker='X', label='Training Accuracy')\naxs[1].set_title('Accuracy')\naxs[1].set_xlabel('Epochs')\naxs[1].set_ylabel('Accuracy')\naxs[1].legend()\n\n# Adjust layout\nplt.tight_layout()\n\n# Show plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T09:36:11.825999Z","iopub.execute_input":"2024-06-20T09:36:11.826869Z","iopub.status.idle":"2024-06-20T09:36:12.430732Z","shell.execute_reply.started":"2024-06-20T09:36:11.826829Z","shell.execute_reply":"2024-06-20T09:36:12.429844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test)\npredictions = np.argmax(predictions, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T09:36:17.328584Z","iopub.execute_input":"2024-06-20T09:36:17.329612Z","iopub.status.idle":"2024-06-20T09:36:20.507871Z","shell.execute_reply.started":"2024-06-20T09:36:17.329569Z","shell.execute_reply":"2024-06-20T09:36:20.507042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the submission file\nsubmission_df = pd.DataFrame({'ImageId': np.arange(1, len(predictions) + 1), 'Label': predictions})\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T09:36:22.343554Z","iopub.execute_input":"2024-06-20T09:36:22.344410Z","iopub.status.idle":"2024-06-20T09:36:22.382604Z","shell.execute_reply.started":"2024-06-20T09:36:22.344378Z","shell.execute_reply":"2024-06-20T09:36:22.381669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test[0].shape","metadata":{"execution":{"iopub.status.busy":"2024-06-19T08:32:25.591779Z","iopub.execute_input":"2024-06-19T08:32:25.592330Z","iopub.status.idle":"2024-06-19T08:32:25.599178Z","shell.execute_reply.started":"2024-06-19T08:32:25.592297Z","shell.execute_reply":"2024-06-19T08:32:25.598113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show the images\nfig, axes = plt.subplots(2, 10, figsize=(15, 3))\n\nfor i in range(20):\n\n    ax = axes[i // 10, i % 10]\n    ax.imshow(np.squeeze(X_test[i], axis=(2)), cmap='gray')\n    ax.axis('off')\n    ax.set_title(f'{i} ', fontweight = 'bold')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-19T10:12:02.640010Z","iopub.execute_input":"2024-06-19T10:12:02.640638Z","iopub.status.idle":"2024-06-19T10:12:03.902498Z","shell.execute_reply.started":"2024-06-19T10:12:02.640610Z","shell.execute_reply":"2024-06-19T10:12:03.901645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head(20)","metadata":{"execution":{"iopub.status.busy":"2024-06-19T10:12:13.028634Z","iopub.execute_input":"2024-06-19T10:12:13.029476Z","iopub.status.idle":"2024-06-19T10:12:13.047065Z","shell.execute_reply.started":"2024-06-19T10:12:13.029437Z","shell.execute_reply":"2024-06-19T10:12:13.046126Z"},"trusted":true},"execution_count":null,"outputs":[]}]}