{"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":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#Import libraries\nimport pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Input, ReLU, BatchNormalization\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:31:37.257680Z","iopub.execute_input":"2024-12-07T22:31:37.258587Z","iopub.status.idle":"2024-12-07T22:31:37.263884Z","shell.execute_reply.started":"2024-12-07T22:31:37.258538Z","shell.execute_reply":"2024-12-07T22:31:37.262895Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 1. Load Data from CSV\ntrain_df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-12-07T22:30:14.189731Z","iopub.execute_input":"2024-12-07T22:30:14.190430Z","iopub.status.idle":"2024-12-07T22:30:16.993583Z","shell.execute_reply.started":"2024-12-07T22:30:14.190397Z","shell.execute_reply":"2024-12-07T22:30:16.992838Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 2. Preprocess the Data\nX = train_df.iloc[:, 1:].values  # Features (all columns except the first one)\ny = train_df.iloc[:, 0].values   # Target (first column)\n\nX = X.reshape(-1, 28, 28, 1)\nX = X/255.0\n","metadata":{"execution":{"iopub.status.busy":"2024-12-07T22:30:18.895070Z","iopub.execute_input":"2024-12-07T22:30:18.895897Z","iopub.status.idle":"2024-12-07T22:30:18.998532Z","shell.execute_reply.started":"2024-12-07T22:30:18.895854Z","shell.execute_reply":"2024-12-07T22:30:18.997789Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 3. Data Augmentation\ndatagen = ImageDataGenerator(\n    rotation_range=30,              # Randomly rotate images by up to 30 degrees\n    width_shift_range=0.15,         # Randomly shift images horizontally by 20%\n    height_shift_range=0.15,        # Randomly shift images vertically by 20%\n    zoom_range=0.2,                 # Randomly zoom in or out\n    shear_range=0.2,                # Shear the images by 20%\n    fill_mode='nearest' ,           # How to fill in new pixels after transformations\n    validation_split=0.2 \n)\n\ndatagen.fit(X)\n# Apply augmentation to the training data and validation split\ntrain_generator = datagen.flow(X, y, batch_size=32, subset='training')\nval_generator = datagen.flow(X, y, batch_size=32, subset='validation')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T22:30:21.299845Z","iopub.execute_input":"2024-12-07T22:30:21.300532Z","iopub.status.idle":"2024-12-07T22:30:21.427085Z","shell.execute_reply.started":"2024-12-07T22:30:21.300501Z","shell.execute_reply":"2024-12-07T22:30:21.426211Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 4. Define the Convolutionary Neural Network Model\nmodel = Sequential([\n    Input(shape=(28, 28, 1)),\n\n    # First Convolutional Layer\n    Conv2D(32, (3, 3), padding='same'),\n    ReLU(),  # Apply ReLU after Conv2D\n    \n    # Second Convolutional Layer\n    Conv2D(64, (3, 3), padding='same'),\n    ReLU(),  # Apply ReLU after Conv2D\n    \n    # Third Convolutional Layer\n    Conv2D(128, (5, 5), padding='same'),\n    ReLU(),  # Apply ReLU after Conv2D\n    \n    # Fourth Convolutional Layer\n    Conv2D(128, (7, 7), padding='same'),\n    BatchNormalization(), #Batch normalization\n    ReLU(),  # Apply ReLU after Conv2D\n    \n    MaxPooling2D(pool_size=(2, 2)),  # Max pooling to reduce the spatial dimensions\n\n    # Flatten the 3D outputs to 1D\n    Flatten(),\n    \n    # Dense Layers\n    Dense(256),                          # First dense layer\n    BatchNormalization(),                # Batch Normalization after the first dense layer\n    ReLU(),                              # Activation function after BatchNorm\n    \n    Dense(128),                          # Second dense layer (256 nodes added)\n    ReLU(),                              # Activation function after Dense\n    \n    Dense(64),                           # Third dense layer\n    ReLU(),                              # Activation function after Dense\n\n    Dense(10, activation='softmax')      # Output layer (classification)\n])\n\n\n#6: Compile the model\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',  # For integer labels\n              metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2024-12-07T22:31:41.572880Z","iopub.execute_input":"2024-12-07T22:31:41.573518Z","iopub.status.idle":"2024-12-07T22:31:42.381081Z","shell.execute_reply.started":"2024-12-07T22:31:41.573483Z","shell.execute_reply":"2024-12-07T22:31:42.380376Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 5: Train the model\nmodel.fit(train_generator, epochs=20, validation_data=val_generator)\n","metadata":{"execution":{"iopub.status.busy":"2024-12-07T21:54:49.614381Z","iopub.execute_input":"2024-12-07T21:54:49.614993Z","iopub.status.idle":"2024-12-07T21:58:51.537055Z","shell.execute_reply.started":"2024-12-07T21:54:49.614959Z","shell.execute_reply":"2024-12-07T21:58:51.536306Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 6. Output the submission file\ntest_df= pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")#load the test data\n\ntest= test_df.to_numpy()\ntest= test.reshape(-1, 28 , 28,1)\ntest = test/255.0\n\npredictions = model.predict(test)\npredicted_labels = np.argmax(predictions, axis=1)\n\nsubmissions= pd.DataFrame({'Imageid': range(1, 28001)})\nsubmissions['Label']= predicted_labels\nsubmissions.to_csv('submission.csv', index=None)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-07T21:58:58.756950Z","iopub.execute_input":"2024-12-07T21:58:58.757577Z","iopub.status.idle":"2024-12-07T21:59:03.974004Z","shell.execute_reply.started":"2024-12-07T21:58:58.757544Z","shell.execute_reply":"2024-12-07T21:59:03.973008Z"}},"outputs":[],"execution_count":null}]}