{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-12T10:16:56.536839Z","iopub.execute_input":"2024-03-12T10:16:56.537221Z","iopub.status.idle":"2024-03-12T10:16:58.035742Z","shell.execute_reply.started":"2024-03-12T10:16:56.537191Z","shell.execute_reply":"2024-03-12T10:16:58.033861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\ndata_path = '/kaggle/input/digit-recognizer/train.csv'","metadata":{"execution":{"iopub.status.busy":"2024-03-12T10:16:59.732359Z","iopub.execute_input":"2024-03-12T10:16:59.732947Z","iopub.status.idle":"2024-03-12T10:16:59.737443Z","shell.execute_reply.started":"2024-03-12T10:16:59.732912Z","shell.execute_reply":"2024-03-12T10:16:59.736698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\ndef load_data(csv_file):\n    # Read the CSV file into a DataFrame\n    df = pd.read_csv(csv_file)\n    \n    # Extract the pixel data from the DataFrame\n    pixel_data = df.values\n#     print(pixel_data[0].shape[0]-1)\n#     Reshape the pixel data to images\n    images = []\n    labels = []\n    for pixels in pixel_data:\n        # Reshape the pixel data to the original image shape (e.g., 224x224x3 for RGB images)\n        image = pixels[1:].reshape((28, 28, 1))\n        label = pixels[0]\n        # Append the image to the list of images\n        images.append(image)\n        labels.append(label)\n    \n    return images, labels","metadata":{"execution":{"iopub.status.busy":"2024-03-12T10:17:01.244804Z","iopub.execute_input":"2024-03-12T10:17:01.245294Z","iopub.status.idle":"2024-03-12T10:17:01.255036Z","shell.execute_reply.started":"2024-03-12T10:17:01.245263Z","shell.execute_reply":"2024-03-12T10:17:01.253304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images, labels = load_data(data_path)","metadata":{"execution":{"iopub.status.busy":"2024-03-12T10:17:06.236244Z","iopub.execute_input":"2024-03-12T10:17:06.236652Z","iopub.status.idle":"2024-03-12T10:17:10.735957Z","shell.execute_reply.started":"2024-03-12T10:17:06.236621Z","shell.execute_reply":"2024-03-12T10:17:10.734451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Define the indices of the images you want to display\nindices = [882, 883, 884]\n\n# Create a subplot for each image\nfor i, idx in enumerate(indices, start=1):\n    plt.subplot(1, len(indices), i)\n    plt.imshow(images[idx])\n    plt.title(labels[idx])  # Add a title to the image\n    plt.axis('off')  # Turn off axis\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-12T10:17:13.282296Z","iopub.execute_input":"2024-03-12T10:17:13.282745Z","iopub.status.idle":"2024-03-12T10:17:13.553098Z","shell.execute_reply.started":"2024-03-12T10:17:13.282710Z","shell.execute_reply":"2024-03-12T10:17:13.551586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(images),len(labels)","metadata":{"execution":{"iopub.status.busy":"2024-03-12T10:17:26.936209Z","iopub.execute_input":"2024-03-12T10:17:26.936683Z","iopub.status.idle":"2024-03-12T10:17:26.944606Z","shell.execute_reply.started":"2024-03-12T10:17:26.936631Z","shell.execute_reply":"2024-03-12T10:17:26.943746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport numpy as np\n\ndef augment_images(images, rotation_range=20, width_shift_range=0.1,\n                   height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,\n                   horizontal_flip=True, fill_mode='nearest'):\n    # Create an ImageDataGenerator object with the specified augmentations\n    datagen = ImageDataGenerator(\n        rotation_range=rotation_range,\n        width_shift_range=width_shift_range,\n        height_shift_range=height_shift_range,\n        shear_range=shear_range,\n        zoom_range=zoom_range,\n        horizontal_flip=horizontal_flip,\n        fill_mode=fill_mode\n    )\n\n    # Fit the ImageDataGenerator object to your data (optional)\n    datagen.fit(images)\n\n    # Generate augmented images\n    augmented_images = []\n    for img in images:\n        augmented_img = next(datagen.flow(np.expand_dims(img, axis=0), batch_size=1))[0]\n        augmented_images.append(augmented_img.squeeze())\n\n    return augmented_images","metadata":{"execution":{"iopub.status.busy":"2024-03-12T10:17:32.496488Z","iopub.execute_input":"2024-03-12T10:17:32.497045Z","iopub.status.idle":"2024-03-12T10:17:48.086910Z","shell.execute_reply.started":"2024-03-12T10:17:32.497006Z","shell.execute_reply":"2024-03-12T10:17:48.085117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate augmented images for the first set\naugmented_images1 = augment_images(images)","metadata":{"execution":{"iopub.status.busy":"2024-03-12T10:18:17.827876Z","iopub.execute_input":"2024-03-12T10:18:17.828736Z","iopub.status.idle":"2024-03-12T10:18:34.093366Z","shell.execute_reply.started":"2024-03-12T10:18:17.828698Z","shell.execute_reply":"2024-03-12T10:18:34.091775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate augmented images for the second set with different parameters\naugmented_images2 = augment_images(images, rotation_range=30, width_shift_range=0.2,height_shift_range=0.2, shear_range=0.3, zoom_range=0.3)","metadata":{"execution":{"iopub.status.busy":"2024-03-12T10:18:41.261212Z","iopub.execute_input":"2024-03-12T10:18:41.261686Z","iopub.status.idle":"2024-03-12T10:18:57.640671Z","shell.execute_reply.started":"2024-03-12T10:18:41.261632Z","shell.execute_reply":"2024-03-12T10:18:57.639546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Combine lists into a list of tuples\nall_images = images + augmented_images1 + augmented_images2\nall_labels = labels*3","metadata":{"execution":{"iopub.status.busy":"2024-03-12T10:19:03.905526Z","iopub.execute_input":"2024-03-12T10:19:03.905999Z","iopub.status.idle":"2024-03-12T10:19:03.920566Z","shell.execute_reply.started":"2024-03-12T10:19:03.905967Z","shell.execute_reply":"2024-03-12T10:19:03.919136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\n\nall_images = [img.reshape(28, 28, 1) for img in all_images]\nall_images = np.stack(all_images)\nall_labels = np.array(all_labels)","metadata":{"execution":{"iopub.status.busy":"2024-03-12T10:19:05.656014Z","iopub.execute_input":"2024-03-12T10:19:05.657217Z","iopub.status.idle":"2024-03-12T10:19:07.378838Z","shell.execute_reply.started":"2024-03-12T10:19:05.657166Z","shell.execute_reply":"2024-03-12T10:19:07.376768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_images.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-12T10:19:09.041794Z","iopub.execute_input":"2024-03-12T10:19:09.042254Z","iopub.status.idle":"2024-03-12T10:19:09.050086Z","shell.execute_reply.started":"2024-03-12T10:19:09.042218Z","shell.execute_reply":"2024-03-12T10:19:09.048364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(all_images),len(all_labels)","metadata":{"execution":{"iopub.status.busy":"2024-03-12T10:19:09.995063Z","iopub.execute_input":"2024-03-12T10:19:09.995698Z","iopub.status.idle":"2024-03-12T10:19:10.003512Z","shell.execute_reply.started":"2024-03-12T10:19:09.995645Z","shell.execute_reply":"2024-03-12T10:19:10.002462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\n\ndef show_random_images(images, labels, num_images=12):\n    # Get random indices\n    random_indices = np.random.choice(len(images), size=num_images, replace=False)\n    # Create subplots\n    fig, axes = plt.subplots(3, 4, figsize=(7, 7))\n\n    # Plot each image with its label\n    for i, idx in enumerate(random_indices):\n        row = i // 4\n        col = i % 4\n        ax = axes[row, col]\n        ax.imshow(images[idx])\n        ax.set_title(f\"Label: {labels[idx]}\")\n        ax.axis('off')\n\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-12T10:19:10.868688Z","iopub.execute_input":"2024-03-12T10:19:10.869151Z","iopub.status.idle":"2024-03-12T10:19:10.877042Z","shell.execute_reply.started":"2024-03-12T10:19:10.869116Z","shell.execute_reply":"2024-03-12T10:19:10.875748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_random_images(all_images, all_labels)","metadata":{"execution":{"iopub.status.busy":"2024-03-12T10:19:14.573501Z","iopub.execute_input":"2024-03-12T10:19:14.573997Z","iopub.status.idle":"2024-03-12T10:19:15.800727Z","shell.execute_reply.started":"2024-03-12T10:19:14.573963Z","shell.execute_reply":"2024-03-12T10:19:15.799087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain_images, temp_images , train_labels , temp_labels = train_test_split(all_images, all_labels, test_size=0.4, random_state=42)\nval_images, test_images , val_labels , test_labels = train_test_split(temp_images, temp_labels, test_size=0.5, random_state=123)","metadata":{"execution":{"iopub.status.busy":"2024-03-12T10:19:19.241644Z","iopub.execute_input":"2024-03-12T10:19:19.243147Z","iopub.status.idle":"2024-03-12T10:19:21.272581Z","shell.execute_reply.started":"2024-03-12T10:19:19.243084Z","shell.execute_reply":"2024-03-12T10:19:21.271262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Model\nfrom keras.models import Sequential, load_model\nfrom keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout\nfrom keras.layers import Dense, BatchNormalization\n\nmodel = Sequential()\n\nmodel.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu', input_shape=(28, 28, 1)))\nmodel.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(pool_size=(2, 2)))\nmodel.add(Dropout(rate=0.25))\n\nmodel.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))\nmodel.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(pool_size=(2, 2)))\nmodel.add(Dropout(rate=0.20))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dropout(rate=0.55))\nmodel.add(Dense(10, activation='softmax'))\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n## Summary about the Model\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-12T10:19:23.459753Z","iopub.execute_input":"2024-03-12T10:19:23.460251Z","iopub.status.idle":"2024-03-12T10:19:23.758078Z","shell.execute_reply.started":"2024-03-12T10:19:23.460215Z","shell.execute_reply":"2024-03-12T10:19:23.756760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model (replace this with your training data and labels)\nhistory = model.fit(train_images, train_labels, epochs=20, batch_size=64, validation_data=(val_images, val_labels))","metadata":{"execution":{"iopub.status.busy":"2024-03-12T10:19:25.487773Z","iopub.execute_input":"2024-03-12T10:19:25.488808Z","iopub.status.idle":"2024-03-12T11:04:08.277349Z","shell.execute_reply.started":"2024-03-12T10:19:25.488770Z","shell.execute_reply":"2024-03-12T11:04:08.275936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model\ntest_loss, test_accuracy = model.evaluate(test_images, test_labels)\nprint(\"Test Accuracy:\", test_accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-03-12T11:04:26.983421Z","iopub.execute_input":"2024-03-12T11:04:26.983930Z","iopub.status.idle":"2024-03-12T11:04:38.883765Z","shell.execute_reply.started":"2024-03-12T11:04:26.983892Z","shell.execute_reply":"2024-03-12T11:04:38.882750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Plot Accuracy Curve\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-12T11:05:05.909682Z","iopub.execute_input":"2024-03-12T11:05:05.910181Z","iopub.status.idle":"2024-03-12T11:05:06.197514Z","shell.execute_reply.started":"2024-03-12T11:05:05.910144Z","shell.execute_reply":"2024-03-12T11:05:06.195874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_images)\nprediction_labels = []\n\nfor prediction in predictions:\n  prediction_labels.append(np.argmax(prediction))","metadata":{"execution":{"iopub.status.busy":"2024-03-12T11:05:08.190356Z","iopub.execute_input":"2024-03-12T11:05:08.190817Z","iopub.status.idle":"2024-03-12T11:05:20.339435Z","shell.execute_reply.started":"2024-03-12T11:05:08.190784Z","shell.execute_reply":"2024-03-12T11:05:20.338176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix\nimport seaborn as sns\n\n# Calculate the confusion matrix\ncm = confusion_matrix(test_labels, prediction_labels)\n\n# Plot the confusion matrix\nplt.figure(figsize=(10, 8))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues')\nplt.xlabel('Predicted labels')\nplt.ylabel('True labels')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-12T11:05:29.088163Z","iopub.execute_input":"2024-03-12T11:05:29.088638Z","iopub.status.idle":"2024-03-12T11:05:30.399199Z","shell.execute_reply.started":"2024-03-12T11:05:29.088600Z","shell.execute_reply":"2024-03-12T11:05:30.397014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}