{"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","trusted":true,"execution":{"iopub.status.busy":"2024-10-22T00:10:18.131690Z","iopub.execute_input":"2024-10-22T00:10:18.132357Z","iopub.status.idle":"2024-10-22T00:10:18.509736Z","shell.execute_reply.started":"2024-10-22T00:10:18.132315Z","shell.execute_reply":"2024-10-22T00:10:18.508848Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#dataset loading\ndata=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndata.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T00:25:56.866435Z","iopub.execute_input":"2024-10-22T00:25:56.866834Z","iopub.status.idle":"2024-10-22T00:25:59.702743Z","shell.execute_reply.started":"2024-10-22T00:25:56.866798Z","shell.execute_reply":"2024-10-22T00:25:59.701824Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"total_class=10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T00:24:50.583047Z","iopub.execute_input":"2024-10-22T00:24:50.583728Z","iopub.status.idle":"2024-10-22T00:24:50.587650Z","shell.execute_reply.started":"2024-10-22T00:24:50.583690Z","shell.execute_reply":"2024-10-22T00:24:50.586648Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#importing the libraries\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras import models,layers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T00:10:32.793473Z","iopub.execute_input":"2024-10-22T00:10:32.793867Z","iopub.status.idle":"2024-10-22T00:10:45.576326Z","shell.execute_reply.started":"2024-10-22T00:10:32.793829Z","shell.execute_reply":"2024-10-22T00:10:45.575272Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#reshaping data\nX = data.drop(columns=['label']).values\ny = data['label'].values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T00:26:31.227769Z","iopub.execute_input":"2024-10-22T00:26:31.228155Z","iopub.status.idle":"2024-10-22T00:26:31.313352Z","shell.execute_reply.started":"2024-10-22T00:26:31.228119Z","shell.execute_reply":"2024-10-22T00:26:31.312191Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = X.reshape(-1, 28, 28, 1) \nX = X / 255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T00:26:49.668697Z","iopub.execute_input":"2024-10-22T00:26:49.669090Z","iopub.status.idle":"2024-10-22T00:26:49.772575Z","shell.execute_reply.started":"2024-10-22T00:26:49.669054Z","shell.execute_reply":"2024-10-22T00:26:49.771739Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T00:27:26.999248Z","iopub.execute_input":"2024-10-22T00:27:26.999717Z","iopub.status.idle":"2024-10-22T00:27:27.424042Z","shell.execute_reply.started":"2024-10-22T00:27:26.999665Z","shell.execute_reply":"2024-10-22T00:27:27.422999Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#trying with LeNet-5 architecture\ndef build_lenet():\n    model = models.Sequential()\n\n    # Convolution Layer 1\n    model.add(layers.Conv2D(6, kernel_size=(5, 5), activation='tanh', input_shape=(28, 28, 1)))\n    # Average Pooling Layer 1\n    model.add(layers.AveragePooling2D(pool_size=(2, 2)))\n\n    # Convolution Layer 2\n    model.add(layers.Conv2D(16, kernel_size=(5, 5), activation='tanh'))\n    # Average Pooling Layer 2\n    model.add(layers.AveragePooling2D(pool_size=(2, 2)))\n\n    # Flatten layer\n    model.add(layers.Flatten())\n\n    # Fully Connected Layer 1\n    model.add(layers.Dense(120, activation='tanh'))\n\n    # Fully Connected Layer 2\n    model.add(layers.Dense(84, activation='tanh'))\n\n    # Output Layer (10 classes for MNIST)\n    model.add(layers.Dense(10, activation='softmax'))\n\n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T00:30:58.431901Z","iopub.execute_input":"2024-10-22T00:30:58.432328Z","iopub.status.idle":"2024-10-22T00:30:58.440476Z","shell.execute_reply.started":"2024-10-22T00:30:58.432272Z","shell.execute_reply":"2024-10-22T00:30:58.439397Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lenet_model = build_lenet()\nlenet_model.compile(optimizer='adam',\n                    loss='categorical_crossentropy', \n                    metrics=['categorical_accuracy'])\nlenet_model.summary()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T00:31:02.612268Z","iopub.execute_input":"2024-10-22T00:31:02.612930Z","iopub.status.idle":"2024-10-22T00:31:02.709643Z","shell.execute_reply.started":"2024-10-22T00:31:02.612872Z","shell.execute_reply":"2024-10-22T00:31:02.708639Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#a callback to avoid overfitting\nfrom keras.callbacks import EarlyStopping\ncallback = EarlyStopping(monitor=\"val_loss\", patience=5, restore_best_weights=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T00:31:09.476599Z","iopub.execute_input":"2024-10-22T00:31:09.477246Z","iopub.status.idle":"2024-10-22T00:31:09.481969Z","shell.execute_reply.started":"2024-10-22T00:31:09.477205Z","shell.execute_reply":"2024-10-22T00:31:09.481030Z"}},"outputs":[],"execution_count":null},{"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    horizontal_flip=False  # MNIST digits are not flipped\n)\ntrain_generator = datagen.flow(X_train, y_train, batch_size=32)\nvalid_generator = ImageDataGenerator().flow(X_valid, y_valid, batch_size=32)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T00:31:12.957014Z","iopub.execute_input":"2024-10-22T00:31:12.957851Z","iopub.status.idle":"2024-10-22T00:31:13.011962Z","shell.execute_reply.started":"2024-10-22T00:31:12.957812Z","shell.execute_reply":"2024-10-22T00:31:13.011151Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#training\nhistory =lenet_model.fit(\n    train_generator,\n    validation_data=valid_generator,\n    epochs=30,\n    callbacks=[callback],\n    verbose=1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T00:31:18.991835Z","iopub.execute_input":"2024-10-22T00:31:18.992241Z","iopub.status.idle":"2024-10-22T00:33:48.088330Z","shell.execute_reply.started":"2024-10-22T00:31:18.992205Z","shell.execute_reply":"2024-10-22T00:33:48.087391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#result for test data\nX_test=pd.read_csv('/kaggle/input/digit-recognizer/test.csv').values\nX_test=X_test.reshape(-1,28,28,1)\ntest_predictions=lenet_model.predict(X_test)\nprint(test_predictions)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T00:33:53.452160Z","iopub.execute_input":"2024-10-22T00:33:53.452892Z","iopub.status.idle":"2024-10-22T00:33:58.213697Z","shell.execute_reply.started":"2024-10-22T00:33:53.452854Z","shell.execute_reply":"2024-10-22T00:33:58.212592Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_predictions.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T00:34:02.859850Z","iopub.execute_input":"2024-10-22T00:34:02.860260Z","iopub.status.idle":"2024-10-22T00:34:02.867176Z","shell.execute_reply.started":"2024-10-22T00:34:02.860224Z","shell.execute_reply":"2024-10-22T00:34:02.866119Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#to get the predicted digit\npredicted_class=np.argmax(test_predictions,axis=1)\nprint(predicted_class)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T00:34:07.771893Z","iopub.execute_input":"2024-10-22T00:34:07.772780Z","iopub.status.idle":"2024-10-22T00:34:07.778826Z","shell.execute_reply.started":"2024-10-22T00:34:07.772738Z","shell.execute_reply":"2024-10-22T00:34:07.777836Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predicted_class.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T00:34:11.986301Z","iopub.execute_input":"2024-10-22T00:34:11.987300Z","iopub.status.idle":"2024-10-22T00:34:11.994100Z","shell.execute_reply.started":"2024-10-22T00:34:11.987248Z","shell.execute_reply":"2024-10-22T00:34:11.993164Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#submission\nsubmission=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')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-10-22T00:36:02.859870Z","iopub.execute_input":"2024-10-22T00:36:02.860889Z","iopub.status.idle":"2024-10-22T00:36:02.905610Z","shell.execute_reply.started":"2024-10-22T00:36:02.860844Z","shell.execute_reply":"2024-10-22T00:36:02.904457Z"}},"outputs":[],"execution_count":null}]}