{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-23T17:56:28.573285Z","iopub.execute_input":"2024-10-23T17:56:28.573999Z","iopub.status.idle":"2024-10-23T17:56:28.583791Z","shell.execute_reply.started":"2024-10-23T17:56:28.573941Z","shell.execute_reply":"2024-10-23T17:56:28.582576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-23T17:56:31.037578Z","iopub.execute_input":"2024-10-23T17:56:31.038108Z","iopub.status.idle":"2024-10-23T17:56:35.382933Z","shell.execute_reply.started":"2024-10-23T17:56:31.038056Z","shell.execute_reply":"2024-10-23T17:56:35.381765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data.drop(columns=['label']).values\ny = data['label'].values","metadata":{"execution":{"iopub.status.busy":"2024-10-23T17:56:37.705659Z","iopub.execute_input":"2024-10-23T17:56:37.706280Z","iopub.status.idle":"2024-10-23T17:56:37.796061Z","shell.execute_reply.started":"2024-10-23T17:56:37.706220Z","shell.execute_reply":"2024-10-23T17:56:37.794895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X.reshape(-1, 28, 28, 1) \nmean = X.mean()\nstd = X.std()\nX = (X - mean) / std","metadata":{"execution":{"iopub.status.busy":"2024-10-23T17:56:42.073099Z","iopub.execute_input":"2024-10-23T17:56:42.074089Z","iopub.status.idle":"2024-10-23T17:56:42.464046Z","shell.execute_reply.started":"2024-10-23T17:56:42.074045Z","shell.execute_reply":"2024-10-23T17:56:42.463160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#importing libraries\nimport tensorflow as tf\nimport numpy as np\nfrom tensorflow.keras.layers import Input,Conv2D,MaxPooling2D,Dense,Flatten,Dropout,Activation,BatchNormalization\nfrom tensorflow.keras.models import Model,Sequential\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.regularizers import l2\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport math","metadata":{"execution":{"iopub.status.busy":"2024-10-23T17:56:44.889996Z","iopub.execute_input":"2024-10-23T17:56:44.890763Z","iopub.status.idle":"2024-10-23T17:56:59.643416Z","shell.execute_reply.started":"2024-10-23T17:56:44.890720Z","shell.execute_reply":"2024-10-23T17:56:59.642437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = to_categorical(y, num_classes=10)\nX_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2, random_state=42)\nprint(\"Training shape:\", X_train.shape, y_train.shape)\nprint(\"Validation shape:\", X_valid.shape, y_valid.shape)","metadata":{"execution":{"iopub.status.busy":"2024-10-23T17:56:59.645343Z","iopub.execute_input":"2024-10-23T17:56:59.645954Z","iopub.status.idle":"2024-10-23T17:57:00.085441Z","shell.execute_reply.started":"2024-10-23T17:56:59.645915Z","shell.execute_reply":"2024-10-23T17:57:00.084395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def LeNet5(input_shape = (28, 28, 1), classes = 10):\n \n    model = Sequential([\n        \n    # Layer 1\n    Conv2D(filters = 32, kernel_size = 5, strides = 1, activation = 'relu', input_shape = (28,28,1), kernel_regularizer=l2(0.0005), name = 'convolution_1'),\n    \n    # Layer 2\n    Conv2D(filters = 32, kernel_size = 5, strides = 1, name = 'convolution_2', use_bias=False),\n    \n    # Layer 3    \n    BatchNormalization(name = 'batchnorm_1'),\n        \n    # -------------------------------- #  \n    Activation(\"relu\"),\n    MaxPooling2D(pool_size = 2, strides = 2, name = 'max_pool_1'),\n    Dropout(0.25, name = 'dropout_1'),\n    # -------------------------------- #  \n        \n    # Layer 3\n    Conv2D(filters = 64, kernel_size = 3, strides = 1, activation = 'relu', kernel_regularizer=l2(0.0005), name = 'convolution_3'),\n        \n    # Layer 4\n    Conv2D(filters = 64, kernel_size = 3, strides = 1, name = 'convolution_4', use_bias=False),\n        \n    # Layer 5\n    BatchNormalization(name = 'batchnorm_2'),\n        \n    # -------------------------------- #  \n    Activation(\"relu\"),\n    MaxPooling2D(pool_size = 2, strides = 2, name = 'max_pool_2'),\n    Dropout(0.25, name = 'dropout_2'),\n    Flatten(name = 'flatten'),\n    # -------------------------------- #  \n        \n    # Layer 6\n    Dense(units = 256, name = 'fully_connected_1', use_bias=False),\n        \n    # Layer 7\n    BatchNormalization(name = 'batchnorm_3'),\n    \n    # -------------------------------- #  \n    Activation(\"relu\"),\n    # -------------------------------- #  \n        \n    # Layer 8\n    Dense(units = 128, name = 'fully_connected_2', use_bias=False),\n        \n    # Layer 9\n    BatchNormalization(name = 'batchnorm_4'),\n        \n    # -------------------------------- #  \n    Activation(\"relu\"),\n    # -------------------------------- #  \n        \n    # Layer 10\n    Dense(units = 84, name = 'fully_connected_3', use_bias=False),\n        \n    # Layer 11\n    BatchNormalization(name = 'batchnorm_5'),\n        \n    # -------------------------------- #  \n    Activation(\"relu\"),\n    Dropout(0.25, name = 'dropout_3'),\n    # -------------------------------- #  \n\n    # Output\n    Dense(units = 10, activation = 'softmax', name = 'output')\n        \n    ])\n    \n    model._name = 'LeNet5v2'\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-10-23T18:06:39.204489Z","iopub.execute_input":"2024-10-23T18:06:39.204907Z","iopub.status.idle":"2024-10-23T18:06:39.218925Z","shell.execute_reply.started":"2024-10-23T18:06:39.204869Z","shell.execute_reply":"2024-10-23T18:06:39.217715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lenet_model = LeNet5()\nlenet_model.compile(optimizer='adam',\n                    loss='categorical_crossentropy', \n                    metrics=['categorical_accuracy'])\nlenet_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-10-23T18:06:43.534566Z","iopub.execute_input":"2024-10-23T18:06:43.535624Z","iopub.status.idle":"2024-10-23T18:06:43.719039Z","shell.execute_reply.started":"2024-10-23T18:06:43.535577Z","shell.execute_reply":"2024-10-23T18:06:43.718024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#data preprocessing\ndatagen = ImageDataGenerator(\n        featurewise_center = False,  # set input mean to 0 over the dataset\n        samplewise_center = False,  # set each sample mean to 0\n        featurewise_std_normalization = False,  # divide inputs by std of the dataset\n        samplewise_std_normalization = False,  # divide each input by its std\n        zca_whitening = False,  # apply ZCA whitening\n        rotation_range = 10,  # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.1, # Randomly zoom image \n        width_shift_range = 0.1,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range = 0.1,  # randomly shift images vertically (fraction of total height)\n        horizontal_flip = False,  # randomly flip images\n        vertical_flip = False)  # randomly flip images\n\n\n     ","metadata":{"execution":{"iopub.status.busy":"2024-10-23T18:06:51.982305Z","iopub.execute_input":"2024-10-23T18:06:51.983177Z","iopub.status.idle":"2024-10-23T18:06:51.989281Z","shell.execute_reply.started":"2024-10-23T18:06:51.983131Z","shell.execute_reply":"2024-10-23T18:06:51.988177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = datagen.flow(X_train, y_train, batch_size=32)\nvalid_generator = ImageDataGenerator().flow(X_valid, y_valid, batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2024-10-23T18:06:55.214219Z","iopub.execute_input":"2024-10-23T18:06:55.214704Z","iopub.status.idle":"2024-10-23T18:06:55.272092Z","shell.execute_reply.started":"2024-10-23T18:06:55.214659Z","shell.execute_reply":"2024-10-23T18:06:55.270872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"variable_learning_rate = ReduceLROnPlateau(monitor='val_loss', factor = 0.2, patience = 2)","metadata":{"execution":{"iopub.status.busy":"2024-10-23T18:06:57.562210Z","iopub.execute_input":"2024-10-23T18:06:57.562662Z","iopub.status.idle":"2024-10-23T18:06:57.568151Z","shell.execute_reply.started":"2024-10-23T18:06:57.562618Z","shell.execute_reply":"2024-10-23T18:06:57.566922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = lenet_model.fit(X_train, y_train, epochs = 50, batch_size = 64, callbacks = [variable_learning_rate], validation_data = (X_valid,y_valid))\n     ","metadata":{"execution":{"iopub.status.busy":"2024-10-23T18:07:00.034850Z","iopub.execute_input":"2024-10-23T18:07:00.035877Z","iopub.status.idle":"2024-10-23T18:09:21.434269Z","shell.execute_reply.started":"2024-10-23T18:07:00.035827Z","shell.execute_reply":"2024-10-23T18:09:21.433281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test=pd.read_csv('/kaggle/input/digit-recognizer/test.csv').values\nX_test=X_test.reshape(-1,28,28,1)\nmean=X_test.mean()\nstd=X_test.std()\nX_test=(X_test-mean)/std","metadata":{"execution":{"iopub.status.busy":"2024-10-23T18:09:24.723908Z","iopub.execute_input":"2024-10-23T18:09:24.724384Z","iopub.status.idle":"2024-10-23T18:09:27.126593Z","shell.execute_reply.started":"2024-10-23T18:09:24.724319Z","shell.execute_reply":"2024-10-23T18:09:27.125421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions=lenet_model.predict(X_test)\nprint(test_predictions)","metadata":{"execution":{"iopub.status.busy":"2024-10-23T18:09:27.290343Z","iopub.execute_input":"2024-10-23T18:09:27.290783Z","iopub.status.idle":"2024-10-23T18:09:30.138168Z","shell.execute_reply.started":"2024-10-23T18:09:27.290741Z","shell.execute_reply":"2024-10-23T18:09:30.136990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_class=np.argmax(test_predictions,axis=1)\nprint(predicted_class)","metadata":{"execution":{"iopub.status.busy":"2024-10-23T18:09:33.103738Z","iopub.execute_input":"2024-10-23T18:09:33.104198Z","iopub.status.idle":"2024-10-23T18:09:33.111221Z","shell.execute_reply.started":"2024-10-23T18:09:33.104145Z","shell.execute_reply":"2024-10-23T18:09:33.109987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.DataFrame({\n    'ImageId':np.arange(1,len(predicted_class)+1),\n    'Label':predicted_class\n})\nsubmission.to_csv('submission.csv',index=False)\nprint('Submission Saved')\n     ","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}