{"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":16017,"databundleVersionId":708189,"sourceType":"competition"}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential, load_model\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-26T00:31:34.394173Z","iopub.execute_input":"2024-04-26T00:31:34.394931Z","iopub.status.idle":"2024-04-26T00:31:38.555144Z","shell.execute_reply.started":"2024-04-26T00:31:34.394893Z","shell.execute_reply":"2024-04-26T00:31:38.554175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load datasets\ntrain = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntest = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-04-26T00:31:38.556723Z","iopub.execute_input":"2024-04-26T00:31:38.557217Z","iopub.status.idle":"2024-04-26T00:31:42.681407Z","shell.execute_reply.started":"2024-04-26T00:31:38.557189Z","shell.execute_reply":"2024-04-26T00:31:42.680510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare data\nX = train.drop('label', axis=1).values.reshape(-1, 28, 28, 1) / 255.0\ny = pd.get_dummies(train['label']).values\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1, random_state=42)\nX_test = test.drop(test.columns[0], axis=1).values.reshape(-1, 28, 28, 1) / 255.0\n\n# Data augmentation\ndatagen = ImageDataGenerator(\n    rotation_range=8,\n    zoom_range=[0.95, 1.05],\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    shear_range=0.15,\n    horizontal_flip=False,\n    fill_mode=\"nearest\"\n)\n\n# One-cycle learning rate scheduler\ndef one_cycle_lr(epoch, max_lr, total_epochs, start_lr=None, final_lr=None):\n    if start_lr is None:\n        start_lr = max_lr / 10\n    if final_lr is None:\n        final_lr = start_lr / 1000\n    if epoch < total_epochs / 2:\n        return (max_lr - start_lr) / (total_epochs / 2) * epoch + start_lr\n    else:\n        return -(max_lr - final_lr) / (total_epochs / 2) * (epoch - total_epochs / 2) + max_lr\n\ntotal_epochs = 25\nmax_lr = 0.001\n\n# Model architecture\nmodel = Sequential([\n    Conv2D(64, (3, 3), activation='relu', input_shape=(28, 28, 1)),\n    MaxPooling2D(2, 2),\n    Conv2D(128, (3, 3), activation='relu'),\n    MaxPooling2D(2, 2),\n    Flatten(),\n    Dense(265, activation='relu'),\n    Dropout(0.5),\n    Dense(10, activation='softmax')\n])\n\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-26T00:35:44.458816Z","iopub.execute_input":"2024-04-26T00:35:44.459275Z","iopub.status.idle":"2024-04-26T00:35:45.350839Z","shell.execute_reply.started":"2024-04-26T00:35:44.459241Z","shell.execute_reply":"2024-04-26T00:35:45.349950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model checkpoint and learning rate scheduler\ncheckpoint = ModelCheckpoint('best_model.keras', save_best_only=True, monitor='val_accuracy', verbose=1)\nlr_scheduler = LearningRateScheduler(lambda epoch: one_cycle_lr(epoch, max_lr=max_lr, total_epochs=total_epochs))\n\n# Train the model\nhistory = model.fit(\n    datagen.flow(X_train, y_train, batch_size=32),\n    epochs=total_epochs,\n    validation_data=(X_val, y_val),\n    callbacks=[checkpoint, lr_scheduler]\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-26T00:35:52.233662Z","iopub.execute_input":"2024-04-26T00:35:52.234410Z","iopub.status.idle":"2024-04-26T00:44:34.724669Z","shell.execute_reply.started":"2024-04-26T00:35:52.234373Z","shell.execute_reply":"2024-04-26T00:44:34.723705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot training & validation accuracy values\nplt.figure(figsize=(12, 5))\nplt.subplot(1, 2, 1)\nplt.plot(history.history['accuracy'], label='Train Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Model Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend(loc='lower right')\n\n# Plot training & validation loss values\nplt.subplot(1, 2, 2)\nplt.plot(history.history['loss'], label='Train Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Model Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend(loc='upper right')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-26T00:44:34.726851Z","iopub.execute_input":"2024-04-26T00:44:34.727523Z","iopub.status.idle":"2024-04-26T00:44:35.235064Z","shell.execute_reply.started":"2024-04-26T00:44:34.727486Z","shell.execute_reply":"2024-04-26T00:44:35.234172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the best model\nbest_model = load_model('best_model.keras')\n\n# Evaluate the best model on train set\nloss, accuracy = best_model.evaluate(X_train, y_train)\nprint(f\"Training Accuracy: {accuracy:.4f}\")\nprint(f\"Training Loss: {loss:.4f}\")\n\n# Evaluate the best model on validation set\nval_loss, val_accuracy = best_model.evaluate(X_val, y_val)\nprint(f\"Validation Accuracy: {val_accuracy:.4f}\")\nprint(f\"Validation Loss: {val_loss:.4f}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-04-26T00:49:51.067755Z","iopub.execute_input":"2024-04-26T00:49:51.068176Z","iopub.status.idle":"2024-04-26T00:49:55.798148Z","shell.execute_reply.started":"2024-04-26T00:49:51.068145Z","shell.execute_reply":"2024-04-26T00:49:55.797105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict the validation set\nval_predictions = best_model.predict(X_val)\nval_predicted_classes = np.argmax(val_predictions, axis=1)\ny_val_classes = np.argmax(y_val, axis=1)\n\n# Confusion matrix\ncm = confusion_matrix(y_val_classes, val_predicted_classes)\nplt.figure(figsize=(10, 8))\nsns.heatmap(cm, annot=True, fmt=\"d\", cmap='Greens')\nplt.title('Confusion Matrix')\nplt.ylabel('True Labels')\nplt.xlabel('Predicted Labels')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-26T00:50:13.007018Z","iopub.execute_input":"2024-04-26T00:50:13.007721Z","iopub.status.idle":"2024-04-26T00:50:14.339040Z","shell.execute_reply.started":"2024-04-26T00:50:13.007687Z","shell.execute_reply":"2024-04-26T00:50:14.338134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare submission file for the test predictions\ntest_predictions = best_model.predict(X_test)\ntest_predicted_classes = np.argmax(test_predictions, axis=1)\nsubmission_df = pd.DataFrame({\n    'id': test['id'],  # Ensure the 'id' column in test DataFrame is correct\n    'label': test_predicted_classes\n})\nsubmission_filename = 'submission.csv'\nsubmission_df.to_csv(submission_filename, index=False)\nprint(\"Submission file created and saved as\", submission_filename)\nprint(submission_df.head())\n","metadata":{"execution":{"iopub.status.busy":"2024-04-26T00:50:27.164994Z","iopub.execute_input":"2024-04-26T00:50:27.165622Z","iopub.status.idle":"2024-04-26T00:50:27.700723Z","shell.execute_reply.started":"2024-04-26T00:50:27.165585Z","shell.execute_reply":"2024-04-26T00:50:27.699774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}