{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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":30733,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"raw","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"}},{"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":{"execution":{"iopub.status.busy":"2024-06-21T10:37:08.661946Z","iopub.execute_input":"2024-06-21T10:37:08.662703Z","iopub.status.idle":"2024-06-21T10:37:09.668623Z","shell.execute_reply.started":"2024-06-21T10:37:08.662667Z","shell.execute_reply":"2024-06-21T10:37:09.667415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:37:13.835239Z","iopub.execute_input":"2024-06-21T10:37:13.835735Z","iopub.status.idle":"2024-06-21T10:37:17.675103Z","shell.execute_reply.started":"2024-06-21T10:37:13.835703Z","shell.execute_reply":"2024-06-21T10:37:17.674135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:37:21.106115Z","iopub.execute_input":"2024-06-21T10:37:21.106983Z","iopub.status.idle":"2024-06-21T10:37:21.112675Z","shell.execute_reply.started":"2024-06-21T10:37:21.106948Z","shell.execute_reply":"2024-06-21T10:37:21.111571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest_df","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:37:23.203761Z","iopub.execute_input":"2024-06-21T10:37:23.204428Z","iopub.status.idle":"2024-06-21T10:37:25.396658Z","shell.execute_reply.started":"2024-06-21T10:37:23.204392Z","shell.execute_reply":"2024-06-21T10:37:25.395403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train_df.drop(columns=['label'],axis=1)\nY_train = train_df['label']\n\n\nX_train_scaled = X_train/255\nX_test_scaled = test_df/255","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:37:29.865463Z","iopub.execute_input":"2024-06-21T10:37:29.865803Z","iopub.status.idle":"2024-06-21T10:37:30.052837Z","shell.execute_reply.started":"2024-06-21T10:37:29.865776Z","shell.execute_reply":"2024-06-21T10:37:30.051765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:37:32.676467Z","iopub.execute_input":"2024-06-21T10:37:32.676834Z","iopub.status.idle":"2024-06-21T10:37:32.685505Z","shell.execute_reply.started":"2024-06-21T10:37:32.676803Z","shell.execute_reply":"2024-06-21T10:37:32.684285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.utils import to_categorical\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:37:35.133835Z","iopub.execute_input":"2024-06-21T10:37:35.134179Z","iopub.status.idle":"2024-06-21T10:37:47.866999Z","shell.execute_reply.started":"2024-06-21T10:37:35.134151Z","shell.execute_reply":"2024-06-21T10:37:47.866015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = to_categorical(Y_train,num_classes=10)\nY_train","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:37:55.748688Z","iopub.execute_input":"2024-06-21T10:37:55.749799Z","iopub.status.idle":"2024-06-21T10:37:55.758468Z","shell.execute_reply.started":"2024-06-21T10:37:55.749764Z","shell.execute_reply":"2024-06-21T10:37:55.757505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_scaled.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:37:58.583842Z","iopub.execute_input":"2024-06-21T10:37:58.584201Z","iopub.status.idle":"2024-06-21T10:37:58.590314Z","shell.execute_reply.started":"2024-06-21T10:37:58.584170Z","shell.execute_reply":"2024-06-21T10:37:58.589392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_scaled = X_train_scaled.values.reshape(-1,28,28,1)\nX_test_scaled = X_test_scaled.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:38:01.144557Z","iopub.execute_input":"2024-06-21T10:38:01.145425Z","iopub.status.idle":"2024-06-21T10:38:01.149891Z","shell.execute_reply.started":"2024-06-21T10:38:01.145389Z","shell.execute_reply":"2024-06-21T10:38:01.148987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_scaled.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:38:03.513747Z","iopub.execute_input":"2024-06-21T10:38:03.514452Z","iopub.status.idle":"2024-06-21T10:38:03.519963Z","shell.execute_reply.started":"2024-06-21T10:38:03.514420Z","shell.execute_reply":"2024-06-21T10:38:03.519047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,5))\nfor i in range(30):\n    plt.subplot(3,10,i+1)\n    plt.imshow(X_train_scaled[i],cmap=plt.cm.binary)\n    plt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:38:06.368153Z","iopub.execute_input":"2024-06-21T10:38:06.369024Z","iopub.status.idle":"2024-06-21T10:38:07.520143Z","shell.execute_reply.started":"2024-06-21T10:38:06.368992Z","shell.execute_reply":"2024-06-21T10:38:07.519237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:38:11.198612Z","iopub.execute_input":"2024-06-21T10:38:11.198971Z","iopub.status.idle":"2024-06-21T10:38:11.204354Z","shell.execute_reply.started":"2024-06-21T10:38:11.198941Z","shell.execute_reply":"2024-06-21T10:38:11.203459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DataGen = ImageDataGenerator(\n    rotation_range=10,\n    zoom_range = 0.10,\n    height_shift_range = 0.1,\n    width_shift_range = 0.1\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:38:13.124040Z","iopub.execute_input":"2024-06-21T10:38:13.124449Z","iopub.status.idle":"2024-06-21T10:38:13.129643Z","shell.execute_reply.started":"2024-06-21T10:38:13.124418Z","shell.execute_reply":"2024-06-21T10:38:13.128506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential([\n    keras.layers.Conv2D(filters=32,kernel_size=3,activation='relu',input_shape=(28,28,1)),\n    keras.layers.BatchNormalization(),\n    keras.layers.Conv2D(filters=32,kernel_size=3,activation='relu'),\n    keras.layers.BatchNormalization(),\n    keras.layers.Conv2D(filters=32,kernel_size=5,strides=2,padding='same',activation='relu'),\n    keras.layers.BatchNormalization(),\n    keras.layers.Dropout(rate=0.4),\n    \n    keras.layers.Conv2D(filters=64,kernel_size=3,activation='relu'),\n    keras.layers.BatchNormalization(),\n    keras.layers.Conv2D(filters=64,kernel_size=3,activation='relu'),\n    keras.layers.BatchNormalization(),\n    keras.layers.Conv2D(filters=64,kernel_size=5,strides=2,padding='same',activation='relu'),\n    keras.layers.BatchNormalization(),\n    keras.layers.Dropout(rate=0.4),\n    \n    keras.layers.Conv2D(filters=128,kernel_size=4,activation='relu'),\n    keras.layers.BatchNormalization(),\n    keras.layers.Flatten(),\n    keras.layers.Dropout(rate=0.4),\n    keras.layers.Dense(10,activation='softmax')\n])\n\nmodel.compile(optimizer = 'adam', loss='categorical_crossentropy', metrics = ['acc'])","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:38:16.003370Z","iopub.execute_input":"2024-06-21T10:38:16.004312Z","iopub.status.idle":"2024-06-21T10:38:16.859801Z","shell.execute_reply.started":"2024-06-21T10:38:16.004279Z","shell.execute_reply":"2024-06-21T10:38:16.858834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:38:20.293968Z","iopub.execute_input":"2024-06-21T10:38:20.294853Z","iopub.status.idle":"2024-06-21T10:38:20.334895Z","shell.execute_reply.started":"2024-06-21T10:38:20.294819Z","shell.execute_reply":"2024-06-21T10:38:20.334004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\n\ncallBack = EarlyStopping(\n    monitor = 'val_loss',\n    patience=5,\n    restore_best_weights = True,\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:38:24.389972Z","iopub.execute_input":"2024-06-21T10:38:24.390389Z","iopub.status.idle":"2024-06-21T10:38:24.396609Z","shell.execute_reply.started":"2024-06-21T10:38:24.390357Z","shell.execute_reply":"2024-06-21T10:38:24.395655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = DataGen.flow(X_train_scaled,Y_train,batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:38:27.614875Z","iopub.execute_input":"2024-06-21T10:38:27.615255Z","iopub.status.idle":"2024-06-21T10:38:27.669759Z","shell.execute_reply.started":"2024-06-21T10:38:27.615221Z","shell.execute_reply":"2024-06-21T10:38:27.668855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    epochs=50,\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-21T10:47:13.183092Z","iopub.execute_input":"2024-06-21T10:47:13.183497Z","iopub.status.idle":"2024-06-21T11:01:03.004756Z","shell.execute_reply.started":"2024-06-21T10:47:13.183466Z","shell.execute_reply":"2024-06-21T11:01:03.003950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred = model.predict(X_test_scaled)","metadata":{"execution":{"iopub.status.busy":"2024-06-21T11:01:52.112869Z","iopub.execute_input":"2024-06-21T11:01:52.113787Z","iopub.status.idle":"2024-06-21T11:01:55.202380Z","shell.execute_reply.started":"2024-06-21T11:01:52.113744Z","shell.execute_reply":"2024-06-21T11:01:55.201512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred","metadata":{"execution":{"iopub.status.busy":"2024-06-21T11:02:05.804824Z","iopub.execute_input":"2024-06-21T11:02:05.805182Z","iopub.status.idle":"2024-06-21T11:02:05.812870Z","shell.execute_reply.started":"2024-06-21T11:02:05.805144Z","shell.execute_reply":"2024-06-21T11:02:05.811706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred_1 = [np.argmax(i) for i in Y_pred]","metadata":{"execution":{"iopub.status.busy":"2024-06-21T11:02:25.189969Z","iopub.execute_input":"2024-06-21T11:02:25.190458Z","iopub.status.idle":"2024-06-21T11:02:25.280566Z","shell.execute_reply.started":"2024-06-21T11:02:25.190426Z","shell.execute_reply":"2024-06-21T11:02:25.279799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_id = [i for i in range(1,28001)]\nlen(img_id)","metadata":{"execution":{"iopub.status.busy":"2024-06-21T11:03:18.026339Z","iopub.execute_input":"2024-06-21T11:03:18.027183Z","iopub.status.idle":"2024-06-21T11:03:18.034533Z","shell.execute_reply.started":"2024-06-21T11:03:18.027143Z","shell.execute_reply":"2024-06-21T11:03:18.033565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = pd.DataFrame({'ImageId':img_id,'Label':Y_pred_1})\nres","metadata":{"execution":{"iopub.status.busy":"2024-06-21T11:04:15.137539Z","iopub.execute_input":"2024-06-21T11:04:15.138130Z","iopub.status.idle":"2024-06-21T11:04:15.254160Z","shell.execute_reply.started":"2024-06-21T11:04:15.138096Z","shell.execute_reply":"2024-06-21T11:04:15.253182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-21T11:04:34.293502Z","iopub.execute_input":"2024-06-21T11:04:34.293857Z","iopub.status.idle":"2024-06-21T11:04:34.334164Z","shell.execute_reply.started":"2024-06-21T11:04:34.293832Z","shell.execute_reply":"2024-06-21T11:04:34.333420Z"},"trusted":true},"execution_count":null,"outputs":[]}]}