{"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":"code","source":"\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-09T20:58:02.324364Z","iopub.execute_input":"2023-08-09T20:58:02.324773Z","iopub.status.idle":"2023-08-09T20:58:02.331956Z","shell.execute_reply.started":"2023-08-09T20:58:02.324741Z","shell.execute_reply":"2023-08-09T20:58:02.330883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Import necessary libraries\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, BatchNormalization\nfrom keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\n","metadata":{"execution":{"iopub.status.busy":"2023-08-09T20:58:02.333759Z","iopub.execute_input":"2023-08-09T20:58:02.334119Z","iopub.status.idle":"2023-08-09T20:58:02.343706Z","shell.execute_reply.started":"2023-08-09T20:58:02.334088Z","shell.execute_reply":"2023-08-09T20:58:02.342777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the data\ntrain = 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":"2023-08-09T20:58:02.345187Z","iopub.execute_input":"2023-08-09T20:58:02.345531Z","iopub.status.idle":"2023-08-09T20:58:06.301688Z","shell.execute_reply.started":"2023-08-09T20:58:02.345502Z","shell.execute_reply":"2023-08-09T20:58:06.300533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Explore data distribution\nplt.figure(figsize=(10,5))\nsns.countplot(train['label'])\nplt.title('Distribution of labels in the training set')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-09T20:58:06.304127Z","iopub.execute_input":"2023-08-09T20:58:06.304479Z","iopub.status.idle":"2023-08-09T20:58:06.536602Z","shell.execute_reply.started":"2023-08-09T20:58:06.304448Z","shell.execute_reply":"2023-08-09T20:58:06.535481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display some sample images\nfig, axes = plt.subplots(2, 5, figsize=(12, 5))\naxes = axes.ravel()\n\nfor i in np.arange(0, 10):\n    axes[i].imshow(train.iloc[i, 1:].values.reshape(28,28), cmap='gray')\n    axes[i].set_title(f\"Digit: {train.iloc[i, 0]}\")\n    axes[i].axis('off')\nplt.subplots_adjust(hspace=0.5)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-08-09T20:58:06.537868Z","iopub.execute_input":"2023-08-09T20:58:06.538262Z","iopub.status.idle":"2023-08-09T20:58:07.291396Z","shell.execute_reply.started":"2023-08-09T20:58:06.538231Z","shell.execute_reply":"2023-08-09T20:58:07.290034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split data into images and labels\ny_train = train['label']\nX_train = train.drop('label', axis=1)\n\n# Normalize\nX_train = X_train / 255.0\ntest = test / 255.0\n\n","metadata":{"execution":{"iopub.status.busy":"2023-08-09T20:58:07.292813Z","iopub.execute_input":"2023-08-09T20:58:07.293169Z","iopub.status.idle":"2023-08-09T20:58:07.507387Z","shell.execute_reply.started":"2023-08-09T20:58:07.293137Z","shell.execute_reply":"2023-08-09T20:58:07.506409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape\nX_train = X_train.values.reshape(-1, 28, 28, 1)\ntest = test.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-08-09T20:58:07.508497Z","iopub.execute_input":"2023-08-09T20:58:07.508908Z","iopub.status.idle":"2023-08-09T20:58:07.516137Z","shell.execute_reply.started":"2023-08-09T20:58:07.508871Z","shell.execute_reply":"2023-08-09T20:58:07.514820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# One-hot encoding\nfrom tensorflow.keras.utils import to_categorical\n\ny_train = to_categorical(y_train, num_classes=10)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-08-09T20:58:07.518424Z","iopub.execute_input":"2023-08-09T20:58:07.519685Z","iopub.status.idle":"2023-08-09T20:58:07.540755Z","shell.execute_reply.started":"2023-08-09T20:58:07.519630Z","shell.execute_reply":"2023-08-09T20:58:07.539446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Augmentation\ndatagen = ImageDataGenerator(\n    rotation_range=10,\n    zoom_range=0.1,\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)\n\n# Display augmented images\nX_sample, y_sample = next(datagen.flow(X_train, y_train, batch_size=9))\n\nfig, axes = plt.subplots(3, 3, figsize=(10,10))\naxes = axes.ravel()\n\nfor i in range(9):\n    axes[i].imshow(X_sample[i].reshape(28,28), cmap='gray')\n    axes[i].axis('off')\nplt.subplots_adjust(wspace=0.5)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-09T20:58:07.544092Z","iopub.execute_input":"2023-08-09T20:58:07.545035Z","iopub.status.idle":"2023-08-09T20:58:07.977193Z","shell.execute_reply.started":"2023-08-09T20:58:07.544988Z","shell.execute_reply":"2023-08-09T20:58:07.976045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the model\n\nmodel = Sequential()\n\nmodel.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu', input_shape=(28,28,1)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters=64, kernel_size=(5,5), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(10, activation='softmax'))\n\nmodel.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-09T20:58:07.980043Z","iopub.execute_input":"2023-08-09T20:58:07.980545Z","iopub.status.idle":"2023-08-09T20:58:08.093381Z","shell.execute_reply.started":"2023-08-09T20:58:07.980500Z","shell.execute_reply":"2023-08-09T20:58:08.092223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Learning Rate Schedule and Early Stopping\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_loss', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)\n\nearly_stop = EarlyStopping(monitor='val_loss', patience=5)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-09T20:58:08.095065Z","iopub.execute_input":"2023-08-09T20:58:08.095486Z","iopub.status.idle":"2023-08-09T20:58:08.102404Z","shell.execute_reply.started":"2023-08-09T20:58:08.095443Z","shell.execute_reply":"2023-08-09T20:58:08.101311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training the model with callbacks\nbatch_size = 64\nepochs = 50\n\nhistory = model.fit(datagen.flow(X_train, y_train, batch_size=batch_size),\n                    epochs=epochs, validation_data=(X_train, y_train),\n                    steps_per_epoch=X_train.shape[0] // batch_size, \n                    callbacks=[learning_rate_reduction, early_stop])","metadata":{"execution":{"iopub.status.busy":"2023-08-09T20:58:08.103966Z","iopub.execute_input":"2023-08-09T20:58:08.104381Z","iopub.status.idle":"2023-08-09T21:31:10.299314Z","shell.execute_reply.started":"2023-08-09T20:58:08.104344Z","shell.execute_reply":"2023-08-09T21:31:10.298203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualize Training and Validation Accuracy & Loss\nfig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))\n\nax1.plot(history.history['accuracy'], color='b', label='Training Accuracy')\nax1.plot(history.history['val_accuracy'], color='r', label='Validation Accuracy')\nax1.set_title('Training and Validation Accuracy')\nax1.legend(loc='best')\n\nax2.plot(history.history['loss'], color='b', label='Training Loss')\nax2.plot(history.history['val_loss'], color='r', label='Validation Loss')\nax2.set_title('Training and Validation Loss')\nax2.legend(loc='best')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-08-09T21:31:10.300688Z","iopub.execute_input":"2023-08-09T21:31:10.301091Z","iopub.status.idle":"2023-08-09T21:31:10.981445Z","shell.execute_reply.started":"2023-08-09T21:31:10.301006Z","shell.execute_reply":"2023-08-09T21:31:10.980179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predictions and Submission\npredictions = model.predict(test)\npredictions = np.argmax(predictions, axis=1)\n\nsubmission = pd.DataFrame({\n    'ImageId': range(1, len(predictions)+1),\n    'Label': predictions\n})\n\nsubmission.to_csv('submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-09T21:31:10.982890Z","iopub.execute_input":"2023-08-09T21:31:10.983283Z","iopub.status.idle":"2023-08-09T21:31:17.248910Z","shell.execute_reply.started":"2023-08-09T21:31:10.983242Z","shell.execute_reply":"2023-08-09T21:31:17.247863Z"},"trusted":true},"execution_count":null,"outputs":[]}]}