{"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":30762,"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# import libraries for EDA\nimport pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nimport plotly.graph_objects as go\nfrom plotly.subplots import make_subplots\nimport torch\nfrom torch.utils.data import DataLoader, TensorDataset\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom sklearn.model_selection import train_test_split\nimport pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping\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","execution":{"iopub.status.busy":"2024-09-23T14:57:10.548941Z","iopub.execute_input":"2024-09-23T14:57:10.549705Z","iopub.status.idle":"2024-09-23T14:57:10.560938Z","shell.execute_reply.started":"2024-09-23T14:57:10.549667Z","shell.execute_reply":"2024-09-23T14:57:10.560055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nsubmission_ex = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-09-23T14:57:10.562536Z","iopub.execute_input":"2024-09-23T14:57:10.562826Z","iopub.status.idle":"2024-09-23T14:57:14.793115Z","shell.execute_reply.started":"2024-09-23T14:57:10.562794Z","shell.execute_reply":"2024-09-23T14:57:14.792225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_ex","metadata":{"execution":{"iopub.status.busy":"2024-09-23T14:57:14.794607Z","iopub.execute_input":"2024-09-23T14:57:14.794950Z","iopub.status.idle":"2024-09-23T14:57:14.804882Z","shell.execute_reply.started":"2024-09-23T14:57:14.794915Z","shell.execute_reply":"2024-09-23T14:57:14.803972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-09-23T14:57:14.805985Z","iopub.execute_input":"2024-09-23T14:57:14.806295Z","iopub.status.idle":"2024-09-23T14:57:14.833099Z","shell.execute_reply.started":"2024-09-23T14:57:14.806256Z","shell.execute_reply":"2024-09-23T14:57:14.832222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test","metadata":{"execution":{"iopub.status.busy":"2024-09-23T14:57:14.834997Z","iopub.execute_input":"2024-09-23T14:57:14.835289Z","iopub.status.idle":"2024-09-23T14:57:14.857063Z","shell.execute_reply.started":"2024-09-23T14:57:14.835251Z","shell.execute_reply":"2024-09-23T14:57:14.856012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_training_images(df):\n    # Step 1: Separate the label\n    labels = df.iloc[:, 0]\n    pixel_data = df.iloc[:, 1:]\n\n    # Reshape each row of pixel_data into a 28x28 array\n    pixel_data = pixel_data.values.reshape(-1, 28, 28, 1).astype('float32') / 255\n    \n    return pixel_data, labels\n\ndef process_testing_images(df):\n\n    # Reshape each row of pixel_data into a 28x28 array\n    reshaped_data = df.values.reshape(-1, 28, 28, 1).astype('float32') / 255\n    return reshaped_data","metadata":{"execution":{"iopub.status.busy":"2024-09-23T14:57:14.858096Z","iopub.execute_input":"2024-09-23T14:57:14.858384Z","iopub.status.idle":"2024-09-23T14:57:14.864058Z","shell.execute_reply.started":"2024-09-23T14:57:14.858352Z","shell.execute_reply":"2024-09-23T14:57:14.863159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    rotation_range=10,       # Randomly rotate images by up to 10 degrees\n    width_shift_range=0.05,   # Randomly shift images horizontally by up to 10% of the width\n    height_shift_range=0.05,  # Randomly shift images vertically by up to 10% of the height\n    shear_range=0.05,         # Apply random shearing transformations\n    zoom_range=0.05,          # Randomly zoom in on images by up to 20%\n    horizontal_flip=False,    # Randomly flip images horizontally\n    fill_mode='nearest'      # Fill in newly created pixels using the nearest pixel value\n)\n\n\n# Load and preprocess the dataset\ndf = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nX = df.drop('label', axis=1).values.reshape(-1, 28, 28, 1).astype('float32') / 255\ny = df['label'].values\ny = tf.keras.utils.to_categorical(y, 10)  # One-hot encode labels\n\n# Split the dataset\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)\n\ntf.random.set_seed(42)\n\nmodel = models.Sequential()\nmodel.add(layers.Input(shape=(28, 28, 1))) # Define input shape here)\n# First Convolutional Block\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same')) # Add padding to maintain size\nmodel.add(layers.MaxPooling2D((2, 2)))\n\n# Second Convolutional Block\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu', padding='same'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\n# Third Convolutional Block\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu', padding='same'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\n# Fourth Convolutional Block\nmodel.add(layers.Conv2D(62, (3, 3), activation='relu', padding='same')) # Add padding here as well\nmodel.add(layers.MaxPooling2D((2, 2)))\n\n# Flatten the output from the convolutional layers\nmodel.add(layers.Flatten())\n\n# First Fully Connected (Dense) Layer\nmodel.add(layers.Dense(1024, activation='leaky_relu'))\nmodel.add(layers.Dropout(0.5))  # Dropout to prevent overfitting\n\n# Second Fully Connected (Dense) Layer\nmodel.add(layers.Dense(512, activation='leaky_relu'))\n\n# Output Layer\nmodel.add(layers.Dense(10, activation='softmax'))  # Assuming 10 classes\n\n# Early stopping callback\nearly_stopping = tf.keras.callbacks.EarlyStopping(monitor='accuracy', patience=10, restore_best_weights=True)\n\nreduce_lr = ReduceLROnPlateau(monitor='accuracy', factor=0.2, patience=5, min_lr=0.000001)\n\n# Compile the model with a custom learning rate\nmodel.compile(optimizer=Adam(learning_rate=0.0005),\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\ntrain_generator = datagen.flow(X_train, y_train, batch_size=64)\n\n# Train the model\nmodel.fit(train_generator, epochs=10, validation_data=(X_test, y_test), callbacks=[early_stopping, reduce_lr])\n\n# Evaluate the model\ntest_loss, test_acc = model.evaluate(X_test, y_test, verbose=2)\nprint('\\nTest accuracy:', round(test_acc*100, 2))","metadata":{"execution":{"iopub.status.busy":"2024-09-23T14:57:14.865566Z","iopub.execute_input":"2024-09-23T14:57:14.866076Z","iopub.status.idle":"2024-09-23T14:59:30.020461Z","shell.execute_reply.started":"2024-09-23T14:57:14.866034Z","shell.execute_reply":"2024-09-23T14:59:30.019604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-09-23T14:59:30.021579Z","iopub.execute_input":"2024-09-23T14:59:30.021906Z","iopub.status.idle":"2024-09-23T14:59:34.061643Z","shell.execute_reply.started":"2024-09-23T14:59:30.021837Z","shell.execute_reply":"2024-09-23T14:59:34.060810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training, labels = process_training_images(df)\nlabels = tf.keras.utils.to_categorical(labels, 10)\n\ntesting = process_testing_images(df_test)","metadata":{"execution":{"iopub.status.busy":"2024-09-23T14:59:34.062821Z","iopub.execute_input":"2024-09-23T14:59:34.063160Z","iopub.status.idle":"2024-09-23T14:59:34.185431Z","shell.execute_reply.started":"2024-09-23T14:59:34.063128Z","shell.execute_reply":"2024-09-23T14:59:34.184559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.random.set_seed(42)\n\nmodel = models.Sequential()\nmodel.add(layers.Input(shape=(28, 28, 1))) # Define input shape here)\n# First Convolutional Block\nmodel.add(layers.Conv2D(64, (3, 3), activation='leaky_relu', padding='same')) # Add padding to maintain size\nmodel.add(layers.MaxPooling2D((2, 2)))\n\n# Second Convolutional Block\nmodel.add(layers.Conv2D(64, (3, 3), activation='leaky_relu', padding='same'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\n# Third Convolutional Block\nmodel.add(layers.Conv2D(128, (3, 3), activation='leaky_relu', padding='same'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\n# Fourth Convolutional Block\nmodel.add(layers.Conv2D(62, (3, 3), activation='leaky_relu', padding='same')) # Add padding here as well\nmodel.add(layers.MaxPooling2D((2, 2)))\n\n# Flatten the output from the convolutional layers\nmodel.add(layers.Flatten())\n\n# First Fully Connected (Dense) Layer\nmodel.add(layers.Dense(1024, activation='leaky_relu'))\nmodel.add(layers.Dropout(0.5))  # Dropout to prevent overfitting\n\n# Second Fully Connected (Dense) Layer\nmodel.add(layers.Dense(512, activation='leaky_relu'))\n\n# Output Layer\nmodel.add(layers.Dense(10, activation='softmax'))  # Assuming 10 classes\n\n# Early stopping callback\nearly_stopping = tf.keras.callbacks.EarlyStopping(monitor='accuracy', patience=7, restore_best_weights=True)\n\nreduce_lr = ReduceLROnPlateau(monitor='accuracy', factor=0.2, patience=3, min_lr=0.000001)\n\n# Compile the model with a custom learning rate\nmodel.compile(optimizer=Adam(learning_rate=0.0005),\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\ntrain_generator = datagen.flow(training, labels, batch_size=128)\n\n# Train the model\nmodel.fit(train_generator, epochs=50, callbacks=[early_stopping, reduce_lr])\n","metadata":{"execution":{"iopub.status.busy":"2024-09-23T14:59:34.186957Z","iopub.execute_input":"2024-09-23T14:59:34.187347Z","iopub.status.idle":"2024-09-23T15:13:14.385915Z","shell.execute_reply.started":"2024-09-23T14:59:34.187303Z","shell.execute_reply":"2024-09-23T15:13:14.384901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(testing)\npredictions = np.argmax(predictions, axis=1)\nids = np.arange(1, len(predictions)+1, 1)\n\nsubmission = pd.DataFrame({\n    'ImageId':ids,\n    'Label':predictions\n})\n\nsubmission.set_index('ImageId', inplace=True)\n\nsubmission.to_csv('submission.csv')\n\nsubmission.to_csv('/kaggle/working/submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-09-23T15:13:14.388382Z","iopub.execute_input":"2024-09-23T15:13:14.388699Z","iopub.status.idle":"2024-09-23T15:13:16.625190Z","shell.execute_reply.started":"2024-09-23T15:13:14.388666Z","shell.execute_reply":"2024-09-23T15:13:16.624348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-09-23T15:13:16.626266Z","iopub.execute_input":"2024-09-23T15:13:16.626581Z","iopub.status.idle":"2024-09-23T15:13:16.636822Z","shell.execute_reply.started":"2024-09-23T15:13:16.626531Z","shell.execute_reply":"2024-09-23T15:13:16.635772Z"},"trusted":true},"execution_count":null,"outputs":[]}]}