{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:25px;\n           background-color:Blue;\n           font-size:210%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n<p style=\"padding: 10px;\n          color:white;\n          text-align:center;\"\n          >\n       WELCOME TO MY NOTEBOOK\n</p>\n</div>\n\n# About Dataset: Digit Recognizer\nThe dataset consists of two files, \"train.csv\" and \"test.csv,\" containing grayscale images of **handwritten digits ranging from zero to nine**. Each image is a 28x28 pixel grid, totaling 784 pixels. Each pixel is represented by an integer value between 0 and 255, indicating its darkness (higher values mean darker). Our main goal is to classify the digit based on the input(that is in the form of digit image) using Maching Learning Model. We use the **Convolutional Neural Network** to classify the digits images.\n\n\n![](https://media.tenor.com/IZ2eQ1jjxdAAAAAd/numbers.gif)\n\n\n\nThank you for exploring my notebook. Please take a moment to upvote my notebook. Your support motivates me to keep improving and sharing valuable insights.😊","metadata":{}},{"cell_type":"markdown","source":"# Import all the libraries","metadata":{}},{"cell_type":"code","source":"# import all the libraries\nimport pandas as pd\nimport numpy as np\nimport tensorflow as tf\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-10-18T03:05:52.297000Z","iopub.execute_input":"2023-10-18T03:05:52.297740Z","iopub.status.idle":"2023-10-18T03:06:00.752326Z","shell.execute_reply.started":"2023-10-18T03:05:52.297711Z","shell.execute_reply":"2023-10-18T03:06:00.751168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read the Train and Test data","metadata":{}},{"cell_type":"code","source":"train= pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_data=pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-18T03:06:00.757261Z","iopub.execute_input":"2023-10-18T03:06:00.757970Z","iopub.status.idle":"2023-10-18T03:06:05.856566Z","shell.execute_reply.started":"2023-10-18T03:06:00.757936Z","shell.execute_reply":"2023-10-18T03:06:05.855752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check the Shape of Dataset","metadata":{}},{"cell_type":"code","source":"# Check the shape of the train data\ntrain.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-18T03:06:05.857617Z","iopub.execute_input":"2023-10-18T03:06:05.857972Z","iopub.status.idle":"2023-10-18T03:06:05.863348Z","shell.execute_reply.started":"2023-10-18T03:06:05.857934Z","shell.execute_reply":"2023-10-18T03:06:05.862434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Getting x_train and y_train\nx_train= train.drop(\"label\", axis=1)\ny_train=train[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2023-10-18T03:06:09.619699Z","iopub.execute_input":"2023-10-18T03:06:09.620347Z","iopub.status.idle":"2023-10-18T03:06:09.711063Z","shell.execute_reply.started":"2023-10-18T03:06:09.620315Z","shell.execute_reply":"2023-10-18T03:06:09.710000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check the shape of the data \nx_train.shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-18T03:06:10.004608Z","iopub.execute_input":"2023-10-18T03:06:10.004950Z","iopub.status.idle":"2023-10-18T03:06:10.009744Z","shell.execute_reply.started":"2023-10-18T03:06:10.004899Z","shell.execute_reply":"2023-10-18T03:06:10.008960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preprocessing","metadata":{}},{"cell_type":"code","source":"# rescale the pixels between 0 and 1\nx_train= x_train/255.0","metadata":{"execution":{"iopub.status.busy":"2023-10-18T03:06:11.057383Z","iopub.execute_input":"2023-10-18T03:06:11.058022Z","iopub.status.idle":"2023-10-18T03:06:11.139317Z","shell.execute_reply.started":"2023-10-18T03:06:11.057994Z","shell.execute_reply":"2023-10-18T03:06:11.138432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reshaping the Train Data","metadata":{}},{"cell_type":"code","source":"# reshape the training data\nx_train= x_train.values.reshape((-1, 28, 28, 1))\nx_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-18T03:06:14.571525Z","iopub.execute_input":"2023-10-18T03:06:14.571882Z","iopub.status.idle":"2023-10-18T03:06:14.577577Z","shell.execute_reply.started":"2023-10-18T03:06:14.571855Z","shell.execute_reply":"2023-10-18T03:06:14.576852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Lets visualize the training data ","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nfor i in range(20):\n    ax = plt.subplot(8, 4, i + 1)\n    plt.imshow(x_train[i])\n    plt.title(f\"Title:{y_train[i]}\")\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-10-18T03:16:07.505618Z","iopub.execute_input":"2023-10-18T03:16:07.506001Z","iopub.status.idle":"2023-10-18T03:16:08.777378Z","shell.execute_reply.started":"2023-10-18T03:16:07.505975Z","shell.execute_reply":"2023-10-18T03:16:08.776639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentation\n> Data Augmentation is a technique which is used to increase the diversity in the dataset by applying different types of transformations to the original data.The main goal of data augmentation is to create a new training examples that are the variations of the existing data without changing the underlying target or label value.This helps improve the performance and robustness of the machine learning model.","metadata":{}},{"cell_type":"code","source":"datagen = tf.keras.preprocessing.image.ImageDataGenerator(featurewise_center=False,\n                                                          samplewise_center=False,\n                                                          rotation_range=10,\n                                                          width_shift_range=0.2,\n                                                          height_shift_range=0.2,\n                                                          zoom_range=0.1\n                                                         )\ndatagen.fit(x_train)                                ","metadata":{"execution":{"iopub.status.busy":"2023-10-18T03:16:59.599435Z","iopub.execute_input":"2023-10-18T03:16:59.600067Z","iopub.status.idle":"2023-10-18T03:16:59.704729Z","shell.execute_reply.started":"2023-10-18T03:16:59.600038Z","shell.execute_reply":"2023-10-18T03:16:59.703951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Apply data augmentation to the training data\ntrain_gen= datagen.flow(x_train, y_train, batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2023-10-18T03:17:00.916994Z","iopub.execute_input":"2023-10-18T03:17:00.917313Z","iopub.status.idle":"2023-10-18T03:17:00.975180Z","shell.execute_reply.started":"2023-10-18T03:17:00.917274Z","shell.execute_reply":"2023-10-18T03:17:00.974293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Lets Create the CNN Model","metadata":{}},{"cell_type":"code","source":"# Create the model\n\ntf.random.set_seed(42)\n\ndef get_model():\n    model= tf.keras.Sequential([\n                             tf.keras.layers.Conv2D(filters=64, kernel_size=3, activation=\"relu\", input_shape=(28, 28,1), padding=\"same\"), # first layer specifies input shape (height, width, colour channels)\n                             tf.keras.layers.Conv2D(64, 3, activation=\"relu\", padding=\"same\"),\n                             tf.keras.layers.MaxPool2D(pool_size=2),\n                             tf.keras.layers.BatchNormalization(),\n\n                             tf.keras.layers.Conv2D(128, 3, activation=\"relu\", padding=\"same\"),\n                             tf.keras.layers.Conv2D(128, 3, activation=\"relu\", padding=\"same\"),\n                             tf.keras.layers.MaxPool2D(2),\n                             tf.keras.layers.BatchNormalization(),\n\n                             tf.keras.layers.Conv2D(256, 3, activation=\"relu\", padding=\"same\"),\n                             tf.keras.layers.Conv2D(256, 3, activation=\"relu\", padding=\"same\"),\n                             tf.keras.layers.MaxPool2D(2),\n                             tf.keras.layers.BatchNormalization(),\n\n                             tf.keras.layers.Flatten(),\n                             tf.keras.layers.Dense(512, activation=\"relu\"),\n                             tf.keras.layers.Dropout(0.2),\n                             tf.keras.layers.Dense(10, activation=\"softmax\") \n        \n        \n    \n    ])\n    \n    return model\n\nstrategy = tf.distribute.MirroredStrategy()\nprint(\"Number of devices: {}\".format(strategy.num_replicas_in_sync))\n\nwith strategy.scope():\n# Compile the model\n    model = get_model()\n    model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(), # different loss function for multiclass classifcation\n                  optimizer=tf.keras.optimizers.Adam(lr=0.001),\n                  metrics=[\"accuracy\"]\n                 )\n\n\n# Callbacks--> Stop training automatically once the model performance stop improving\n    learning_rate_reduction=tf.keras.callbacks.ReduceLROnPlateau(monitor='loss',patience=2,factor=0.5, min_lr = 0.00001,verbose =1)\n    Early_Stopping= tf.keras.callbacks.EarlyStopping(monitor=\"loss\", patience=500, restore_best_weights=True)\n\n    # Fit the model\n    model.fit(train_gen, epochs=100, callbacks=[learning_rate_reduction,Early_Stopping], verbose=1)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-10-14T02:55:05.812678Z","iopub.execute_input":"2023-10-14T02:55:05.813419Z","iopub.status.idle":"2023-10-14T03:28:35.754133Z","shell.execute_reply.started":"2023-10-14T02:55:05.813387Z","shell.execute_reply":"2023-10-14T03:28:35.753137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Summary","metadata":{}},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-14T03:28:35.757816Z","iopub.execute_input":"2023-10-14T03:28:35.758692Z","iopub.status.idle":"2023-10-14T03:28:35.799057Z","shell.execute_reply.started":"2023-10-14T03:28:35.758659Z","shell.execute_reply":"2023-10-14T03:28:35.798381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read the Test Data","metadata":{}},{"cell_type":"code","source":"test_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-14T03:28:35.799901Z","iopub.execute_input":"2023-10-14T03:28:35.800217Z","iopub.status.idle":"2023-10-14T03:28:35.827014Z","shell.execute_reply.started":"2023-10-14T03:28:35.800187Z","shell.execute_reply":"2023-10-14T03:28:35.826199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-14T03:28:35.828203Z","iopub.execute_input":"2023-10-14T03:28:35.829043Z","iopub.status.idle":"2023-10-14T03:28:35.834425Z","shell.execute_reply.started":"2023-10-14T03:28:35.829013Z","shell.execute_reply":"2023-10-14T03:28:35.833585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocess the Test Data and Rescale it Between 0 and 1","metadata":{}},{"cell_type":"code","source":"test_data=test_data/255.0\ntest_data= test_data.values.reshape((-1, 28, 28, 1))\ny_preds=model.predict(test_data)\ny_preds=y_preds.argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T03:28:35.835581Z","iopub.execute_input":"2023-10-14T03:28:35.836416Z","iopub.status.idle":"2023-10-14T03:28:39.138694Z","shell.execute_reply.started":"2023-10-14T03:28:35.836375Z","shell.execute_reply":"2023-10-14T03:28:39.137734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# generate the index\nindex=[]\nfor i in range(1,28001):\n    index.append(i)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T03:28:39.140097Z","iopub.execute_input":"2023-10-14T03:28:39.140715Z","iopub.status.idle":"2023-10-14T03:28:39.149539Z","shell.execute_reply.started":"2023-10-14T03:28:39.140681Z","shell.execute_reply":"2023-10-14T03:28:39.148621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# store the resluts into dataframe\ndf= pd.DataFrame(index)\ndf[\"ImageId\"]=index\ndf[\"Label\"]=y_preds\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-14T03:28:39.150759Z","iopub.execute_input":"2023-10-14T03:28:39.152332Z","iopub.status.idle":"2023-10-14T03:28:39.179921Z","shell.execute_reply.started":"2023-10-14T03:28:39.152299Z","shell.execute_reply":"2023-10-14T03:28:39.179037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop([0], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T03:28:39.181333Z","iopub.execute_input":"2023-10-14T03:28:39.181955Z","iopub.status.idle":"2023-10-14T03:28:39.195517Z","shell.execute_reply.started":"2023-10-14T03:28:39.181923Z","shell.execute_reply":"2023-10-14T03:28:39.194533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the predictions\ndf.to_csv(\"result_final.csv\", index= False)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T03:28:39.197115Z","iopub.execute_input":"2023-10-14T03:28:39.197458Z","iopub.status.idle":"2023-10-14T03:28:39.238521Z","shell.execute_reply.started":"2023-10-14T03:28:39.197425Z","shell.execute_reply":"2023-10-14T03:28:39.237727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}