{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30775,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization, Activation\nfrom keras.utils import to_categorical\nfrom tensorflow.keras.regularizers import l2\nfrom tensorflow.keras.optimizers import Adam\n\nfrom sklearn.model_selection import train_test_split\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau, EarlyStopping\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport numpy as np\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-25T14:30:26.676042Z","iopub.execute_input":"2024-09-25T14:30:26.676571Z","iopub.status.idle":"2024-09-25T14:30:44.698030Z","shell.execute_reply.started":"2024-09-25T14:30:26.676495Z","shell.execute_reply":"2024-09-25T14:30:44.696527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading and Preprocessing Data","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv('../input/digit-recognizer/train.csv')\ndf_test = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-09-25T14:31:10.785007Z","iopub.execute_input":"2024-09-25T14:31:10.785962Z","iopub.status.idle":"2024-09-25T14:31:18.452193Z","shell.execute_reply.started":"2024-09-25T14:31:10.785904Z","shell.execute_reply":"2024-09-25T14:31:18.450572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Separate the features and labels from the training set\nX = df_train.drop('label', axis=1).values\ny = df_train['label'].values","metadata":{"execution":{"iopub.status.busy":"2024-09-25T14:31:44.547631Z","iopub.execute_input":"2024-09-25T14:31:44.548188Z","iopub.status.idle":"2024-09-25T14:31:44.678400Z","shell.execute_reply.started":"2024-09-25T14:31:44.548131Z","shell.execute_reply":"2024-09-25T14:31:44.676861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize the images (scale from 0 to 1) by dividing the values ​​by 255\nX = X / 255.0\ndf_test = df_test / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-09-25T14:31:53.874692Z","iopub.execute_input":"2024-09-25T14:31:53.875179Z","iopub.status.idle":"2024-09-25T14:31:54.090290Z","shell.execute_reply.started":"2024-09-25T14:31:53.875133Z","shell.execute_reply":"2024-09-25T14:31:54.088877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape to 28x28 (data enlargement requires image format)\nX = X.reshape(-1, 28, 28, 1)\ndf_test = df_test.values.reshape(df_test.shape[0], 28, 28,1)","metadata":{"execution":{"iopub.status.busy":"2024-09-25T14:32:04.211719Z","iopub.execute_input":"2024-09-25T14:32:04.212157Z","iopub.status.idle":"2024-09-25T14:32:04.219344Z","shell.execute_reply.started":"2024-09-25T14:32:04.212118Z","shell.execute_reply":"2024-09-25T14:32:04.218182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert labels to one-hot encoding\ny = to_categorical(y, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2024-09-25T14:32:14.498063Z","iopub.execute_input":"2024-09-25T14:32:14.498578Z","iopub.status.idle":"2024-09-25T14:32:14.506216Z","shell.execute_reply.started":"2024-09-25T14:32:14.498530Z","shell.execute_reply":"2024-09-25T14:32:14.504731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the training set into training and validation\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)","metadata":{"execution":{"iopub.status.busy":"2024-09-25T14:32:54.744328Z","iopub.execute_input":"2024-09-25T14:32:54.744938Z","iopub.status.idle":"2024-09-25T14:32:55.932366Z","shell.execute_reply.started":"2024-09-25T14:32:54.744884Z","shell.execute_reply":"2024-09-25T14:32:55.930897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Designing CNN","metadata":{}},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(64, kernel_size=(5, 5), input_shape=(28, 28, 1)))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128, kernel_size=(3, 3)))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128, kernel_size=(3, 3)))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu', kernel_regularizer=l2(0.001)))\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(10, activation='softmax'))\n\noptimizer = Adam(learning_rate=0.0001)\nmodel.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-09-25T14:33:40.394193Z","iopub.execute_input":"2024-09-25T14:33:40.394756Z","iopub.status.idle":"2024-09-25T14:33:40.625239Z","shell.execute_reply.started":"2024-09-25T14:33:40.394699Z","shell.execute_reply":"2024-09-25T14:33:40.623606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentation","metadata":{}},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    rotation_range=10,\n    zoom_range=0.1,\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)","metadata":{"execution":{"iopub.status.busy":"2024-09-25T14:34:17.756866Z","iopub.execute_input":"2024-09-25T14:34:17.757395Z","iopub.status.idle":"2024-09-25T14:34:17.765158Z","shell.execute_reply.started":"2024-09-25T14:34:17.757350Z","shell.execute_reply":"2024-09-25T14:34:17.763596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2024-09-25T14:34:26.916942Z","iopub.execute_input":"2024-09-25T14:34:26.917570Z","iopub.status.idle":"2024-09-25T14:34:27.112873Z","shell.execute_reply.started":"2024-09-25T14:34:26.917486Z","shell.execute_reply":"2024-09-25T14:34:27.110888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\nepochs = 100","metadata":{"execution":{"iopub.status.busy":"2024-09-25T14:34:37.245063Z","iopub.execute_input":"2024-09-25T14:34:37.245652Z","iopub.status.idle":"2024-09-25T14:34:37.252617Z","shell.execute_reply.started":"2024-09-25T14:34:37.245599Z","shell.execute_reply":"2024-09-25T14:34:37.250643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',\n                                            patience=3,\n                                            verbose=1,\n                                            factor=0.5,\n                                            min_lr=0.0001)","metadata":{"execution":{"iopub.status.busy":"2024-09-25T14:34:47.450337Z","iopub.execute_input":"2024-09-25T14:34:47.450917Z","iopub.status.idle":"2024-09-25T14:34:47.457913Z","shell.execute_reply.started":"2024-09-25T14:34:47.450861Z","shell.execute_reply":"2024-09-25T14:34:47.456394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = EarlyStopping(monitor='val_loss',\n                               patience=5,\n                               verbose=1,\n                               restore_best_weights=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(datagen.flow(X_train, y_train, batch_size=batch_size),\n                    validation_data=(X_val, y_val),\n                    steps_per_epoch=len(X_train) // batch_size,\n                    epochs=epochs,\n                    callbacks=[learning_rate_reduction, early_stopping],\n                    verbose=1)","metadata":{"execution":{"iopub.status.busy":"2024-09-25T14:34:58.883599Z","iopub.execute_input":"2024-09-25T14:34:58.884041Z","iopub.status.idle":"2024-09-25T14:46:24.838819Z","shell.execute_reply.started":"2024-09-25T14:34:58.883999Z","shell.execute_reply":"2024-09-25T14:46:24.837493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training and validation loss graph\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-25T14:47:38.753153Z","iopub.execute_input":"2024-09-25T14:47:38.753676Z","iopub.status.idle":"2024-09-25T14:47:39.089523Z","shell.execute_reply.started":"2024-09-25T14:47:38.753631Z","shell.execute_reply":"2024-09-25T14:47:39.087924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Graph of training and validation accuracy\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-09-25T14:47:42.152005Z","iopub.execute_input":"2024-09-25T14:47:42.152606Z","iopub.status.idle":"2024-09-25T14:47:42.546793Z","shell.execute_reply.started":"2024-09-25T14:47:42.152548Z","shell.execute_reply":"2024-09-25T14:47:42.545253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"# Predict with test data\npredictions = model.predict(df_test, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2024-09-25T14:47:46.457935Z","iopub.execute_input":"2024-09-25T14:47:46.458540Z","iopub.status.idle":"2024-09-25T14:47:52.927128Z","shell.execute_reply.started":"2024-09-25T14:47:46.458465Z","shell.execute_reply":"2024-09-25T14:47:52.925660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert predictions to labels (the index of the class with the highest probability)\npredicted_labels = np.argmax(predictions, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-09-25T14:47:55.751165Z","iopub.execute_input":"2024-09-25T14:47:55.751713Z","iopub.status.idle":"2024-09-25T14:47:55.758275Z","shell.execute_reply.started":"2024-09-25T14:47:55.751660Z","shell.execute_reply":"2024-09-25T14:47:55.756900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Making a new dataframe called Submission\nsubmission = pd.DataFrame({\n    \"ImageId\": np.arange(1, len(predicted_labels) + 1),\n    \"Label\": predicted_labels\n})\n\n# Save the submission to a CSV file\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-09-25T14:47:58.295111Z","iopub.execute_input":"2024-09-25T14:47:58.295608Z","iopub.status.idle":"2024-09-25T14:47:58.345444Z","shell.execute_reply.started":"2024-09-25T14:47:58.295558Z","shell.execute_reply":"2024-09-25T14:47:58.344002Z"},"trusted":true},"execution_count":null,"outputs":[]}]}