{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","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":31153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import tensorflow as tf\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:31:12.426280Z","iopub.execute_input":"2025-10-18T11:31:12.426715Z","iopub.status.idle":"2025-10-18T11:31:12.434048Z","shell.execute_reply.started":"2025-10-18T11:31:12.426677Z","shell.execute_reply":"2025-10-18T11:31:12.432686Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf.head","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:31:12.435766Z","iopub.execute_input":"2025-10-18T11:31:12.436895Z","iopub.status.idle":"2025-10-18T11:31:14.883766Z","shell.execute_reply.started":"2025-10-18T11:31:12.436852Z","shell.execute_reply":"2025-10-18T11:31:14.882603Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y=df.iloc[:,0].values\nX=df.iloc[:,1:].values\nX=X.reshape(-1, 28, 28, 1)\ny=to_categorical(y, num_classes=10)\nX_train,X_test,y_train,y_test=train_test_split(\n    X,y,test_size=0.25,random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:31:14.885101Z","iopub.execute_input":"2025-10-18T11:31:14.885721Z","iopub.status.idle":"2025-10-18T11:31:15.324144Z","shell.execute_reply.started":"2025-10-18T11:31:14.885694Z","shell.execute_reply":"2025-10-18T11:31:15.322933Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nX_train,X_val,y_train,y_val= train_test_split(X,y, test_size=0.25,random_state=42)\nX_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:31:15.326764Z","iopub.execute_input":"2025-10-18T11:31:15.327206Z","iopub.status.idle":"2025-10-18T11:31:15.782519Z","shell.execute_reply.started":"2025-10-18T11:31:15.327166Z","shell.execute_reply":"2025-10-18T11:31:15.781443Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def create_and_compile_model():\n    model=tf.keras.models.Sequential([\n        tf.keras.Input(shape=(28,28,1)),\n        tf.keras.layers.Rescaling(1./255),\n        tf.keras.layers.Conv2D(16, (3,3), activation='relu'),\n        tf.keras.layers.MaxPooling2D(2, 2),\n        tf.keras.layers.Conv2D(32, (3,3), activation='relu'),\n        tf.keras.layers.MaxPooling2D(2, 2),\n        tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n        tf.keras.layers.MaxPooling2D(2, 2),\n        \n        tf.keras.layers.Flatten(),\n        tf.keras.layers.Dense(512, activation='relu'),\n        tf.keras.layers.Dense(10, activation='softmax'),\n        \n    ])\n    model.compile(\n        optimizer='adam',\n        loss='categorical_crossentropy',\n        metrics=['accuracy']\n    )\n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:31:15.783530Z","iopub.execute_input":"2025-10-18T11:31:15.783867Z","iopub.status.idle":"2025-10-18T11:31:15.791112Z","shell.execute_reply.started":"2025-10-18T11:31:15.783843Z","shell.execute_reply":"2025-10-18T11:31:15.790002Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model=create_and_compile_model()\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:31:15.792361Z","iopub.execute_input":"2025-10-18T11:31:15.792746Z","iopub.status.idle":"2025-10-18T11:31:15.896169Z","shell.execute_reply.started":"2025-10-18T11:31:15.792708Z","shell.execute_reply":"2025-10-18T11:31:15.894983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history=model.fit(\n    X_train,y_train,\n    epochs=20,\n    validation_data=(X_val,y_val),\n    batch_size=64\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:31:15.896978Z","iopub.execute_input":"2025-10-18T11:31:15.897219Z","iopub.status.idle":"2025-10-18T11:34:42.060676Z","shell.execute_reply.started":"2025-10-18T11:31:15.897191Z","shell.execute_reply":"2025-10-18T11:34:42.059679Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nplt.plot(history.history['accuracy'], label='train acc')\nplt.plot(history.history['val_accuracy'], label='val acc')\nplt.legend()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:34:42.062685Z","iopub.execute_input":"2025-10-18T11:34:42.062935Z","iopub.status.idle":"2025-10-18T11:34:42.254859Z","shell.execute_reply.started":"2025-10-18T11:34:42.062916Z","shell.execute_reply":"2025-10-18T11:34:42.253549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nX_test = test_df.values.astype('float32')\nX_test = X_test.reshape(-1, 28, 28, 1)\nX_sub=np.concatenate([X_train,X_val], axis=0)\ny_sub=np.concatenate([y_train,y_val], axis=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:34:42.255738Z","iopub.execute_input":"2025-10-18T11:34:42.255985Z","iopub.status.idle":"2025-10-18T11:34:43.695529Z","shell.execute_reply.started":"2025-10-18T11:34:42.255966Z","shell.execute_reply":"2025-10-18T11:34:43.694078Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fit(\n    X_sub,y_sub,\n    batch_size=64,\n    epochs=20\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:34:43.698417Z","iopub.execute_input":"2025-10-18T11:34:43.698765Z","iopub.status.idle":"2025-10-18T11:38:47.348934Z","shell.execute_reply.started":"2025-10-18T11:34:43.698744Z","shell.execute_reply":"2025-10-18T11:38:47.347654Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_pred= model.predict(X_test)\npred_labels = np.argmax(base_pred, axis=1)\nsubmission_base = pd.DataFrame({\n    \"ImageId\": np.arange(1, len(pred_labels) + 1),\n    \"Label\": pred_labels\n})\nsubmission_base.to_csv(\"/kaggle/working/submission_base2.csv\", index=False)\nprint(\"✅ Submission file created: submission_base.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:38:47.350039Z","iopub.execute_input":"2025-10-18T11:38:47.350524Z","iopub.status.idle":"2025-10-18T11:38:51.966681Z","shell.execute_reply.started":"2025-10-18T11:38:47.350491Z","shell.execute_reply":"2025-10-18T11:38:51.965610Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def create_augmentation_model():\n    augmentation_model=tf.keras.models.Sequential([\n        tf.keras.Input(shape=(28,28,1)),\n        tf.keras.layers.RandomRotation(0.2,fill_mode='nearest'),\n        tf.keras.layers.RandomTranslation(0.2,0.2,fill_mode='nearest'),\n        tf.keras.layers.RandomZoom(0.2),\n        tf.keras.layers.RandomContrast(0.2)\n    ])\n    return augmentation_model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:38:51.968068Z","iopub.execute_input":"2025-10-18T11:38:51.968449Z","iopub.status.idle":"2025-10-18T11:38:51.976580Z","shell.execute_reply.started":"2025-10-18T11:38:51.968415Z","shell.execute_reply":"2025-10-18T11:38:51.975000Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.utils import array_to_img\nX_sample = df.iloc[0:3, 1:].values.astype('float32') / 255.0\ny_sample = df.iloc[0:3, 0].values\nX_sample = X_sample.reshape(-1, 28, 28, 1)\n\ndata_augmentation = create_augmentation_model()\nplt.figure(figsize=(12, 4))\n\nfor i in range(3):\n    plt.subplot(2, 3, i+1)\n    plt.title(f\"Original: {y_sample[i]}\")\n    plt.axis('off')\n    plt.imshow(array_to_img(X_sample[i]), cmap='gray')\n    \n    augmented_img = data_augmentation(X_sample[i:i+1], training=True)  # add batch dim\n    plt.subplot(2, 3, i+4)\n    plt.title(f\"Augmented: {y_sample[i]}\")\n    plt.axis('off')\n    plt.imshow(array_to_img(augmented_img[0]), cmap='gray')\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:38:51.977688Z","iopub.execute_input":"2025-10-18T11:38:51.977970Z","iopub.status.idle":"2025-10-18T11:38:52.470588Z","shell.execute_reply.started":"2025-10-18T11:38:51.977948Z","shell.execute_reply":"2025-10-18T11:38:52.469014Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"aug_layers=create_augmentation_model()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:38:52.472316Z","iopub.execute_input":"2025-10-18T11:38:52.473757Z","iopub.status.idle":"2025-10-18T11:38:52.498864Z","shell.execute_reply.started":"2025-10-18T11:38:52.473715Z","shell.execute_reply":"2025-10-18T11:38:52.497658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def model_with_aug():\n    model=tf.keras.models.Sequential([\n        tf.keras.Input(shape=(28,28,1)),\n        aug_layers,\n        tf.keras.layers.Rescaling(1./255),\n        tf.keras.layers.Conv2D(16, (3,3), activation='relu'),\n        tf.keras.layers.MaxPooling2D(2, 2),\n        tf.keras.layers.Conv2D(32, (3,3), activation='relu'),\n        tf.keras.layers.MaxPooling2D(2, 2),\n        tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n        tf.keras.layers.MaxPooling2D(2, 2),\n        \n        tf.keras.layers.Flatten(),\n        tf.keras.layers.Dense(512, activation='relu'),\n        tf.keras.layers.Dense(10, activation='softmax'),\n        \n    ])\n    model.compile(\n        optimizer='adam',\n        loss='categorical_crossentropy',\n        metrics=['accuracy']\n    )\n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:38:52.500079Z","iopub.execute_input":"2025-10-18T11:38:52.500592Z","iopub.status.idle":"2025-10-18T11:38:52.509648Z","shell.execute_reply.started":"2025-10-18T11:38:52.500562Z","shell.execute_reply":"2025-10-18T11:38:52.508546Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_aug=model_with_aug()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:38:52.510771Z","iopub.execute_input":"2025-10-18T11:38:52.511070Z","iopub.status.idle":"2025-10-18T11:38:52.587108Z","shell.execute_reply.started":"2025-10-18T11:38:52.511046Z","shell.execute_reply":"2025-10-18T11:38:52.585892Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"his_aug=model_aug.fit(\n    X_train,y_train,\n    epochs=20,\n    validation_data=(X_val,y_val),\n    batch_size=64\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:38:52.588566Z","iopub.execute_input":"2025-10-18T11:38:52.589007Z","iopub.status.idle":"2025-10-18T11:43:29.992691Z","shell.execute_reply.started":"2025-10-18T11:38:52.588982Z","shell.execute_reply":"2025-10-18T11:43:29.991383Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(his_aug.history['accuracy'], label='train acc')\nplt.plot(his_aug.history['val_accuracy'], label='val acc')\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:43:29.994617Z","iopub.execute_input":"2025-10-18T11:43:29.994896Z","iopub.status.idle":"2025-10-18T11:43:30.176095Z","shell.execute_reply.started":"2025-10-18T11:43:29.994876Z","shell.execute_reply":"2025-10-18T11:43:30.174932Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_aug.fit(\n    X_sub,y_sub,\n    batch_size=64,\n    epochs=20\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:43:30.177017Z","iopub.execute_input":"2025-10-18T11:43:30.177281Z","iopub.status.idle":"2025-10-18T11:48:56.967782Z","shell.execute_reply.started":"2025-10-18T11:43:30.177261Z","shell.execute_reply":"2025-10-18T11:48:56.966680Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"aug_pred= model_aug.predict(X_test)\npred_labels_aug = np.argmax(aug_pred, axis=1)\nsubmission_aug = pd.DataFrame({\n    \"ImageId\": np.arange(1, len(pred_labels_aug) + 1),\n    \"Label\": pred_labels_aug\n})\nsubmission_aug.to_csv(\"submission_aug.csv\", index=False)\nprint(\"✅ Submission file created: submission_aug.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-18T11:48:56.969030Z","iopub.execute_input":"2025-10-18T11:48:56.969394Z","iopub.status.idle":"2025-10-18T11:49:02.131648Z","shell.execute_reply.started":"2025-10-18T11:48:56.969366Z","shell.execute_reply":"2025-10-18T11:49:02.130297Z"}},"outputs":[],"execution_count":null}]}