{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"},{"sourceId":7885121,"sourceType":"datasetVersion","datasetId":4628642}],"dockerImageVersionId":30665,"isInternetEnabled":false,"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":{"execution":{"iopub.status.busy":"2024-03-19T11:20:06.967902Z","iopub.execute_input":"2024-03-19T11:20:06.968839Z","iopub.status.idle":"2024-03-19T11:20:06.977742Z","shell.execute_reply.started":"2024-03-19T11:20:06.968798Z","shell.execute_reply":"2024-03-19T11:20:06.976549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'  # Suppress TensorFlow info and warning messages\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.cm as cm\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization\nfrom keras.datasets import mnist\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nfrom keras.callbacks import LearningRateScheduler\nimport warnings\n\n# Ignore warnings\nwarnings.filterwarnings(\"ignore\")\n","metadata":{"execution":{"iopub.status.busy":"2024-03-19T11:26:09.195253Z","iopub.execute_input":"2024-03-19T11:26:09.195657Z","iopub.status.idle":"2024-03-19T11:26:09.204025Z","shell.execute_reply.started":"2024-03-19T11:26:09.195629Z","shell.execute_reply":"2024-03-19T11:26:09.203091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2024-03-19T11:26:15.683323Z","iopub.execute_input":"2024-03-19T11:26:15.683703Z","iopub.status.idle":"2024-03-19T11:26:21.355557Z","shell.execute_reply.started":"2024-03-19T11:26:15.683672Z","shell.execute_reply":"2024-03-19T11:26:21.354546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-19T11:26:25.112758Z","iopub.execute_input":"2024-03-19T11:26:25.113454Z","iopub.status.idle":"2024-03-19T11:26:25.140558Z","shell.execute_reply.started":"2024-03-19T11:26:25.113422Z","shell.execute_reply":"2024-03-19T11:26:25.139633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-19T11:26:26.344982Z","iopub.execute_input":"2024-03-19T11:26:26.345802Z","iopub.status.idle":"2024-03-19T11:26:26.362575Z","shell.execute_reply.started":"2024-03-19T11:26:26.345771Z","shell.execute_reply":"2024-03-19T11:26:26.361613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\n# Path to the mnist.npz file\nmnist_path = \"/kaggle/input/mnist-datasets/mnist.npz\"\n\n# Load the dataset\nwith np.load(mnist_path, allow_pickle=True) as f:\n    x_train, y_train = f[\"x_train\"], f[\"y_train\"]\n    x_test, y_test = f[\"x_test\"], f[\"y_test\"]\n\n# Optionally, concatenate train and test sets\nX_train = np.vstack((x_train, x_test))\n# Optionally, concatenate train and test sets\nX_train = X_train.reshape(-1, 28, 28, 1)\ny_train = np.concatenate([y_train, y_test])\n","metadata":{"execution":{"iopub.status.busy":"2024-03-19T11:41:22.145303Z","iopub.execute_input":"2024-03-19T11:41:22.145693Z","iopub.status.idle":"2024-03-19T11:41:22.382765Z","shell.execute_reply.started":"2024-03-19T11:41:22.145664Z","shell.execute_reply":"2024-03-19T11:41:22.381947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2024-03-19T11:41:23.674162Z","iopub.execute_input":"2024-03-19T11:41:23.674535Z","iopub.status.idle":"2024-03-19T11:41:23.684339Z","shell.execute_reply.started":"2024-03-19T11:41:23.674508Z","shell.execute_reply":"2024-03-19T11:41:23.683440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2024-03-19T11:41:25.493775Z","iopub.execute_input":"2024-03-19T11:41:25.494391Z","iopub.status.idle":"2024-03-19T11:41:25.500709Z","shell.execute_reply.started":"2024-03-19T11:41:25.494360Z","shell.execute_reply":"2024-03-19T11:41:25.499582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape, y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-19T11:41:26.887186Z","iopub.execute_input":"2024-03-19T11:41:26.887934Z","iopub.status.idle":"2024-03-19T11:41:26.892346Z","shell.execute_reply.started":"2024-03-19T11:41:26.887902Z","shell.execute_reply":"2024-03-19T11:41:26.891468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv').values\ny_val = train[:,0].astype('int32')\nX_val = train[:,1:].astype('float32')\nX_val = X_val.reshape(-1,28,28,1)\nprint(X_val.shape, y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2024-03-19T11:41:38.105683Z","iopub.execute_input":"2024-03-19T11:41:38.106032Z","iopub.status.idle":"2024-03-19T11:41:40.630187Z","shell.execute_reply.started":"2024-03-19T11:41:38.106004Z","shell.execute_reply":"2024-03-19T11:41:40.629266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(20,20))\nfor i in range(6):\n    ax = fig.add_subplot(1, 6, i+1, xticks=[], yticks=[])\n    ax.imshow(X_train[i].reshape(28,28), cmap='gray')\n    ax.set_title(str(y_train[i]))","metadata":{"execution":{"iopub.status.busy":"2024-03-19T11:41:49.974084Z","iopub.execute_input":"2024-03-19T11:41:49.974943Z","iopub.status.idle":"2024-03-19T11:41:50.597225Z","shell.execute_reply.started":"2024-03-19T11:41:49.974911Z","shell.execute_reply":"2024-03-19T11:41:50.596228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\n\n# Assuming y_train and y_val are your label arrays\ny_train = to_categorical(y_train, num_classes=10)\ny_val = to_categorical(y_val, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2024-03-19T11:43:40.865442Z","iopub.execute_input":"2024-03-19T11:43:40.865801Z","iopub.status.idle":"2024-03-19T11:43:40.873873Z","shell.execute_reply.started":"2024-03-19T11:43:40.865777Z","shell.execute_reply":"2024-03-19T11:43:40.872886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Modeling**","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D\n\n# define the model\nmodel = Sequential()\nmodel.add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu', input_shape=(28,28,1)))\nmodel.add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))\nmodel.add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))\nmodel.add(Conv2D(filters=192, kernel_size=3, padding='same', activation='relu'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=192, kernel_size=5, padding='same', activation='relu'))\nmodel.add(MaxPooling2D(pool_size=2, padding='same'))\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dense(10, activation='softmax'))\n# summarize the model\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-19T11:43:55.662784Z","iopub.execute_input":"2024-03-19T11:43:55.663592Z","iopub.status.idle":"2024-03-19T11:43:55.810450Z","shell.execute_reply.started":"2024-03-19T11:43:55.663561Z","shell.execute_reply":"2024-03-19T11:43:55.809586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\nfrom keras.callbacks import ModelCheckpoint   \nfrom keras.callbacks import ReduceLROnPlateau\n\n# train the model\n#checkpointer = ModelCheckpoint(filepath='mnist.model.best.hdf5', \n#                               verbose=1, save_best_only=True)\nreduce_lr = ReduceLROnPlateau(monitor='loss', factor=0.3, verbose=1,\n                              patience=2, min_lr=0.00000001)\nhist = model.fit(X_train, y_train, batch_size=100, epochs=25,\n          validation_data=(X_val, y_val), callbacks=[reduce_lr],\n          verbose=1, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-19T11:44:16.795577Z","iopub.execute_input":"2024-03-19T11:44:16.795938Z","iopub.status.idle":"2024-03-19T11:54:30.512131Z","shell.execute_reply.started":"2024-03-19T11:44:16.795910Z","shell.execute_reply":"2024-03-19T11:54:30.511150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nX_test = test.values.astype('float32')\nX_test = X_test.reshape(-1, 28, 28, 1)\nX_test /= 255","metadata":{"execution":{"iopub.status.busy":"2024-03-19T11:54:30.514033Z","iopub.execute_input":"2024-03-19T11:54:30.514335Z","iopub.status.idle":"2024-03-19T11:54:32.190657Z","shell.execute_reply.started":"2024-03-19T11:54:30.514309Z","shell.execute_reply":"2024-03-19T11:54:32.189823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test, verbose=2)","metadata":{"execution":{"iopub.status.busy":"2024-03-19T11:54:32.191712Z","iopub.execute_input":"2024-03-19T11:54:32.191982Z","iopub.status.idle":"2024-03-19T11:54:37.202301Z","shell.execute_reply.started":"2024-03-19T11:54:32.191959Z","shell.execute_reply":"2024-03-19T11:54:37.201456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# File Submission","metadata":{}},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId': np.arange(1, len(predictions) + 1), 'Label': predictions.argmax(axis=1)})\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-19T11:54:37.204482Z","iopub.execute_input":"2024-03-19T11:54:37.204788Z","iopub.status.idle":"2024-03-19T11:54:37.256954Z","shell.execute_reply.started":"2024-03-19T11:54:37.204763Z","shell.execute_reply":"2024-03-19T11:54:37.256283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-19T11:54:38.201564Z","iopub.execute_input":"2024-03-19T11:54:38.201872Z","iopub.status.idle":"2024-03-19T11:54:38.211116Z","shell.execute_reply.started":"2024-03-19T11:54:38.201846Z","shell.execute_reply":"2024-03-19T11:54:38.210089Z"},"trusted":true},"execution_count":null,"outputs":[]}]}