{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"colab":{"gpuType":"T4","provenance":[]},"accelerator":"GPU","widgets":{"application/vnd.jupyter.widget-state+json":{"0068625932d84cf4a649ce1204487a51":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"06ffcb8dd7c547cb903dc44abcbf532e":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"LabelModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"LabelModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"LabelView","description":"","description_tooltip":null,"layout":"IPY_MODEL_693f532540324d6faf346c7e84ec9c9f","placeholder":"​","style":"IPY_MODEL_dc6c74e1cc3041cf8d2cccc52a2d5f4b","value":"/tmp/user_image.png"}},"1533b93595d04b61a586fb95c08f760b":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"ButtonStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"ButtonStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","button_color":"green","font_weight":"bold"}},"571ac2a04aa9457bba47ff0bb3045f51":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"657c6a1332e24a988eff618dd37ff64d":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"DescriptionStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"65b12fe53b1a48278b13f66532309518":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"ButtonModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"ButtonModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"ButtonView","button_style":"","description":"Click To Predict","disabled":false,"icon":"","layout":"IPY_MODEL_7fba0c6db3254db5b94a58be7217f808","style":"IPY_MODEL_1533b93595d04b61a586fb95c08f760b","tooltip":""}},"693f532540324d6faf346c7e84ec9c9f":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"72bef181d8e94ef0ba0950f1fba3263e":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"LabelModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"LabelModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"LabelView","description":"","description_tooltip":null,"layout":"IPY_MODEL_9279aac9b26b4930ba4e9a9a24fa54a3","placeholder":"​","style":"IPY_MODEL_657c6a1332e24a988eff618dd37ff64d","value":"Choose how to input your image:"}},"7fba0c6db3254db5b94a58be7217f808":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"8bbcf0330ce1465cbc57070581a50aca":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"TextModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"TextModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"TextView","continuous_update":true,"description":"Path to image:","description_tooltip":null,"disabled":false,"layout":"IPY_MODEL_571ac2a04aa9457bba47ff0bb3045f51","placeholder":"​","style":"IPY_MODEL_b6158ad0d08c49c38cca5a7694a31778","value":""}},"9279aac9b26b4930ba4e9a9a24fa54a3":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"9ee2c256a530434fa9c42d04589b321b":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"HBoxModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HBoxModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HBoxView","box_style":"","children":["IPY_MODEL_a51e99caffde42e7bc927ad19e41c82f","IPY_MODEL_8bbcf0330ce1465cbc57070581a50aca"],"layout":"IPY_MODEL_acad088ba11b46ee849db76dbc57aab5"}},"a51e99caffde42e7bc927ad19e41c82f":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"FileUploadModel","state":{"_counter":1,"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"FileUploadModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"FileUploadView","accept":"image/*","button_style":"","data":[null],"description":"Upload","description_tooltip":null,"disabled":false,"error":"","icon":"upload","layout":"IPY_MODEL_0068625932d84cf4a649ce1204487a51","metadata":[{"lastModified":1739306608113,"name":"num.jpg","size":120042,"type":"image/jpeg"}],"multiple":false,"style":"IPY_MODEL_fade06f7d8e64cd6b580a7e7a0b62c81"}},"acad088ba11b46ee849db76dbc57aab5":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"b6158ad0d08c49c38cca5a7694a31778":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"DescriptionStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"dc6c74e1cc3041cf8d2cccc52a2d5f4b":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"DescriptionStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"fade06f7d8e64cd6b580a7e7a0b62c81":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"ButtonStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"ButtonStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","button_color":null,"font_weight":""}}}},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":3004,"databundleVersionId":861823},{"sourceType":"datasetVersion","sourceId":242592,"datasetId":102285,"databundleVersionId":254413}],"dockerImageVersionId":30886,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<!-- Waving Header with Custom Width -->\n<div style=\"width: 80%; margin: 0 auto;\">\n  <img width=\"100%\" src=\"https://capsule-render.vercel.app/api?type=waving&color=00abf0&height=70&section=header\"/>\n</div>\n\n<!-- Typing Animation with Custom Width -->\n<div align=\"center\" style=\"width: 80%; margin: 0 auto;\">\n    <img src=\"https://readme-typing-svg.demolab.com?font=Fira+Code&size=25&pause=1000&color=00abf0&center=true&vCenter=true&width=1005&height=25&lines=Hello+there!;My+name+is+Abdulrahman;MNIST+Project\" alt=\"Typing SVG\" />\n</div>","metadata":{"id":"BrPitopeL6pm"}},{"cell_type":"markdown","source":"<div style=\"background-color:#2D2D2D; color:#D6E906FF; padding:10px; border-radius:5px;\">\n    <h2>🎯 TensorFlow & Keras Setup for Deep Learning</h2>\n    <p>This script imports essential libraries for deep learning using TensorFlow and Keras.  \n    It also includes utilities for handling datasets, visualization, and suppressing warnings.</p>\n</div>","metadata":{"id":"4myltgSbCeoA"}},{"cell_type":"code","source":"import os  # OS module for file and directory management\nimport struct  # Struct module for binary data handling\nimport random  # Random module for generating random numbers\nimport numpy as np  # NumPy for numerical computations\nimport matplotlib.pyplot as plt  # Matplotlib for data visualization\nimport warnings  # Warnings module to suppress unnecessary warnings\nfrom array import array  # Array module for handling array data structures\nimport kagglehub  # KaggleHub for accessing datasets from Kaggle\nimport tensorflow as tf  # TensorFlow for deep learning\nfrom tensorflow import keras  # Keras API within TensorFlow\nfrom tensorflow.keras import layers, Sequential  # Keras layers and Sequential API\n# Suppress warnings for cleaner output\nwarnings.filterwarnings(action='ignore')","metadata":{"id":"IwIGnJUqCjmx","trusted":true,"execution":{"iopub.status.busy":"2025-02-13T23:43:17.403828Z","iopub.execute_input":"2025-02-13T23:43:17.404198Z","iopub.status.idle":"2025-02-13T23:43:36.049684Z","shell.execute_reply.started":"2025-02-13T23:43:17.404168Z","shell.execute_reply":"2025-02-13T23:43:36.048664Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"background-color:#2D2D2D; color:#D6E906FF; padding:10px; border-radius:5px;\">\n    <h2>📥 Downloading MNIST Dataset using KaggleHub</h2>\n    <p>This script utilizes <strong>KaggleHub</strong> to download the latest version of the MNIST dataset.</p>\n</div>","metadata":{"id":"QYY8WXrkECm3"}},{"cell_type":"code","source":"import kagglehub  # KaggleHub for accessing datasets from Kaggle\n\n# Download the latest version of the MNIST dataset\npath = kagglehub.dataset_download(\"hojjatk/mnist-dataset\")\n\n# Display the path where the dataset is stored\nprint(\"Path to dataset files:\", path)\n","metadata":{"id":"t1sjJVCkEroW","outputId":"d3bd599d-6127-45dd-cd8e-12f31ea2c0fd","trusted":true,"execution":{"iopub.status.busy":"2025-02-13T23:43:36.051164Z","iopub.execute_input":"2025-02-13T23:43:36.052086Z","iopub.status.idle":"2025-02-13T23:43:38.578180Z","shell.execute_reply.started":"2025-02-13T23:43:36.052037Z","shell.execute_reply":"2025-02-13T23:43:38.576981Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"background-color:#2D2D2D; color:#D6E906FF; padding:10px; border-radius:5px;\">\n\n# Defining MNIST Data File Paths 📁\n\nIn this cell, we define the file paths for the MNIST dataset's training and testing images and labels using `os.path.join()` for compatibility across systems.\n\n- `train-images.idx3-ubyte` and `train-labels.idx1-ubyte` are for the training data.\n- `t10k-images.idx3-ubyte` and `t10k-labels.idx1-ubyte` are for the test data.\n\nThese paths will be used to load the dataset. 🔄","metadata":{"id":"ciozjwERJ176"}},{"cell_type":"code","source":"# Define the file paths for the training and testing data\ntrain_image = os.path.join(path, 'train-images.idx3-ubyte')  # Path to the training images file\ntrain_label = os.path.join(path, 'train-labels.idx1-ubyte')  # Path to the training labels file\n\ntest_image = os.path.join(path, 't10k-images.idx3-ubyte')  # Path to the test images file\ntest_label = os.path.join(path, 't10k-labels.idx1-ubyte')  # Path to the test labels file","metadata":{"id":"-8kEE337JZDT","trusted":true,"execution":{"iopub.status.busy":"2025-02-13T23:43:38.584319Z","iopub.execute_input":"2025-02-13T23:43:38.584705Z","iopub.status.idle":"2025-02-13T23:43:38.590350Z","shell.execute_reply.started":"2025-02-13T23:43:38.584664Z","shell.execute_reply":"2025-02-13T23:43:38.589057Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"background-color:#2D2D2D; color:#D6E906FF; padding:10px; border-radius:5px;\">\n    <h2>📂 MNIST Data Loader Class</h2>\n    <p>This class is responsible for loading and parsing the MNIST dataset from binary files.</p>\n</div>\n","metadata":{"id":"uoB_fyxzFsxj"}},{"cell_type":"code","source":"import struct  # For handling binary data\nimport numpy as np  # NumPy for array operations\nfrom array import array  # Array module for handling byte data\n\nclass MnistDataLoader:\n    \"\"\"\n    A class to load the MNIST dataset from binary files.\n    \"\"\"\n\n    def __init__(self, train_image, train_label, test_image, test_label):\n        self.train_image = train_image\n        self.train_label = train_label\n        self.test_image = test_image\n        self.test_label = test_label\n\n    def read_images_labels(self, img_path, label_path):\n        \"\"\"Reads images and labels from the given file paths.\"\"\"\n\n        # Read labels\n        with open(label_path, 'rb') as file:\n            magic, size = struct.unpack(\">II\", file.read(8))\n            if magic != 2049:\n                raise ValueError(f'Magic number mismatch, expected 2049, got {magic}')\n            labels = np.array(array(\"B\", file.read()))\n\n        # Read images\n        with open(img_path, 'rb') as file:\n            magic, size, rows, cols = struct.unpack(\">IIII\", file.read(16))\n            if magic != 2051:\n                raise ValueError(f'Magic number mismatch, expected 2051, got {magic}')\n            image_data = np.array(array(\"B\", file.read())).reshape(size, rows, cols)\n\n        return image_data, labels\n\n    def load_data(self):\n        \"\"\"Loads training and test data.\"\"\"\n        x_train, y_train = self.read_images_labels(self.train_image, self.train_label)\n        x_test, y_test = self.read_images_labels(self.test_image, self.test_label)\n        return (x_train, y_train), (x_test, y_test)","metadata":{"id":"Bt61v0TgLTmA","trusted":true,"execution":{"iopub.status.busy":"2025-02-13T23:43:38.591693Z","iopub.execute_input":"2025-02-13T23:43:38.592096Z","iopub.status.idle":"2025-02-13T23:43:38.612318Z","shell.execute_reply.started":"2025-02-13T23:43:38.592061Z","shell.execute_reply":"2025-02-13T23:43:38.611087Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"background-color:#2D2D2D; color:#D6E906FF; padding:10px; border-radius:5px;\">\n\n# Simulating MNIST Dataset Loading 🚀\n\nThis code simulates the loading process of the MNIST dataset with a progress bar. It includes the following steps:\n\n1. **Progress Bar Simulation**: A simple loop simulates the loading process of the MNIST dataset. The progress is displayed in percentage, updating every second.\n\n2. **DataLoader Initialization**: Once the progress bar completes, the `MnistDataLoader` object is initialized with training and testing data (`train_image`, `train_label`, `test_image`, `test_label`). This object is assumed to handle data loading.\n\n3. **Loading Data**: The `Load_Data()` method of the `MnistDataLoader` class is called to fetch the training and test data, which are then unpacked into variables `x_train`, `y_train`, `x_test`, and `y_test`.\n\n4. **Completion Message**: After the data is successfully loaded, a confirmation message `MNIST loaded` is printed.\n\nThis approach helps in visualizing the progress during the dataset loading process while preparing the data for machine learning models.\n","metadata":{"id":"K1-fc1VeHHCE"}},{"cell_type":"code","source":"import time\n\n# Starting the process of loading the MNIST dataset\nprint('MNIST Loading ...')\n\n# Total number of steps for the loading simulation\ntotal_steps = 5\n\n# Loop to simulate the loading process with a progress bar\nfor i in range(total_steps):\n    # Calculate the completion percentage\n    percent = (i + 1) * 100 / total_steps\n\n    # Print the current progress on the same line\n    print(f\"\\r{percent:.1f}% Loading\", end=\"\", flush=True)\n\n    # Simulate loading delay\n    time.sleep(1)\n\n# Assuming MnistDataLoader is defined and takes the training and test data as input\nmnist_dataloader = MnistDataLoader(train_image, train_label, test_image, test_label)\n\n# Loading the data through the MnistDataLoader object\n(x_train, y_train), (x_test, y_test) = mnist_dataloader.load_data() # Changed Load_Data to load_data\n# Print a message indicating successful loading of the MNIST dataset\nprint(\"\\nMNIST loaded \")","metadata":{"id":"bhxvauGEr0Ik","outputId":"22bb1bf1-5552-42f4-965d-d278244b593f","trusted":true,"execution":{"iopub.status.busy":"2025-02-13T23:43:38.613491Z","iopub.execute_input":"2025-02-13T23:43:38.613863Z","iopub.status.idle":"2025-02-13T23:43:44.241429Z","shell.execute_reply.started":"2025-02-13T23:43:38.613829Z","shell.execute_reply":"2025-02-13T23:43:44.240222Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"background-color:#2D2D2D; color:#D6E906FF; padding:10px; border-radius:5px;\">\n\n# Data Normalization and Reshaping 🔄\n\nAfter loading the MNIST dataset, we perform two key preprocessing steps:\n\n1. **Normalization**: The pixel values of the images are scaled to the range [0, 1] by dividing by 255.0. This ensures that the values are in a suitable range for neural network training.\n   \n2. **Reshaping**: The images are reshaped to have the shape `(28, 28, 1)` to match the input dimensions expected by Convolutional Neural Networks (CNNs), where:\n   - `28 x 28` represents the image dimensions.\n   - `1` indicates that the images are grayscale (one color channel).\n\nThese steps are essential to prepare the data for model training. 🧠⚙️","metadata":{"id":"Urpm0XyjHyu3"}},{"cell_type":"code","source":"# Normalize the pixel values to be in the range [0, 1]\nx_train, x_test = x_train / 255.0, x_test / 255.0\n\n# Reshape the data to match the input shape for CNNs (28x28 pixels, 1 channel)\nx_train = x_train.reshape(-1, 28, 28, 1)\nx_test = x_test.reshape(-1, 28, 28, 1)","metadata":{"id":"cSvoFdw4wNy6","trusted":true,"execution":{"iopub.status.busy":"2025-02-13T23:43:44.242685Z","iopub.execute_input":"2025-02-13T23:43:44.242980Z","iopub.status.idle":"2025-02-13T23:43:44.624257Z","shell.execute_reply.started":"2025-02-13T23:43:44.242955Z","shell.execute_reply":"2025-02-13T23:43:44.622965Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"background-color:#2D2D2D; color:#D6E906FF; padding:10px; border-radius:5px;\">\n\n# Building the Convolutional Neural Network (CNN) Model 🧠🔧\n\nIn this cell, we define the structure of our CNN model using Keras:\n\n1. **Convolutional Layers**:\n   - The model starts with a series of convolutional layers (Conv2D) to extract features from the input images. Each convolutional layer is followed by a ReLU activation function.\n   - The filters in the convolutional layers increase as we progress deeper into the model (32 → 64 → 64), which allows the model to capture more complex patterns.\n\n2. **Max Pooling Layers**:\n   - After each convolutional layer, we use max pooling layers (MaxPooling2D) with a 2x2 pool size to reduce the spatial dimensions of the feature maps and prevent overfitting.\n\n3. **Fully Connected Layers**:\n   - After the feature extraction, the output is flattened into a 1D vector and passed through a fully connected layer (Dense) with 64 neurons and ReLU activation.\n   \n4. **Output Layer**:\n   - The final layer has 10 neurons, corresponding to the 10 classes in the MNIST dataset, with a softmax activation function to output class probabilities.\n\nThis CNN is designed for image classification, particularly for the MNIST digit dataset. 📊💻","metadata":{"id":"ogffQV0oH9Xp"}},{"cell_type":"code","source":"# Build the Convolutional Neural Network (CNN) model using Keras\nmodel = keras.Sequential([\n    # First convolutional layer: 32 filters, 3x3 kernel, ReLU activation\n    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),\n\n    # Max pooling layer: 2x2 pool size\n    layers.MaxPooling2D((2, 2)),\n\n    # Second convolutional layer: 64 filters, 3x3 kernel, ReLU activation\n    layers.Conv2D(64, (3, 3), activation='relu'),\n\n    # Max pooling layer: 2x2 pool size\n    layers.MaxPooling2D((2, 2)),\n\n    # Third convolutional layer: 64 filters, 3x3 kernel, ReLU activation\n    layers.Conv2D(64, (3, 3), activation='relu'),\n\n    # Flatten the output to feed it into the fully connected layers\n    layers.Flatten(),\n\n    # Fully connected layer with 64 neurons and ReLU activation\n    layers.Dense(64, activation='relu'),\n\n    # Output layer with 10 neurons (one for each class) and softmax activation\n    layers.Dense(10, activation='softmax')\n])","metadata":{"id":"UyUDS-Ooxc0x","trusted":true,"execution":{"iopub.status.busy":"2025-02-13T23:43:44.627062Z","iopub.execute_input":"2025-02-13T23:43:44.627629Z","iopub.status.idle":"2025-02-13T23:43:44.763159Z","shell.execute_reply.started":"2025-02-13T23:43:44.627595Z","shell.execute_reply":"2025-02-13T23:43:44.762237Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"background-color:#2D2D2D; color:#D6E906FF; padding:10px; border-radius:5px;\">\n\n# Model Compilation, Training, and Evaluation 📈\n\n1. **Model Compilation**:\n   - The model is compiled using the **Adam optimizer**, which is widely used for its efficiency in training deep learning models.\n   - The **loss function** used is `sparse_categorical_crossentropy`, suitable for multi-class classification where labels are integers.\n   - The **accuracy metric** is used to track the performance of the model during training and evaluation.\n\n2. **Model Training**:\n   - The model is trained using the `fit()` method for 5 epochs. During training, it is validated using the test dataset `(x_test, y_test)`.\n\n3. **Model Evaluation**:\n   - After training, the model is evaluated on the test dataset using the `evaluate()` method. This returns the test loss and test accuracy.\n   - The test accuracy is printed to evaluate the model's performance on unseen data.\n\nThis process trains the CNN on the MNIST dataset and provides a measure of its accuracy on the test set. 🚀🎯","metadata":{"id":"MWaj1nyZIIkw"}},{"cell_type":"code","source":"# Compile the CNN model with Adam optimizer, sparse categorical crossentropy loss, and accuracy metric\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\n# Train the model using the training data and validate it with the test data\nmodel.fit(x_train, y_train, epochs=5, validation_data=(x_test, y_test))\n\n# Evaluate the model on the test data and obtain test loss and accuracy\ntest_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)\n\n# Print the test accuracy after evaluation\nprint(f'\\n Test ACC : {test_acc}')","metadata":{"id":"sQvraL7Ey656","outputId":"c71f7f4b-2112-4702-c57d-dc138099b118","trusted":true,"execution":{"iopub.status.busy":"2025-02-13T23:43:44.764436Z","iopub.execute_input":"2025-02-13T23:43:44.764709Z","iopub.status.idle":"2025-02-13T23:46:44.876634Z","shell.execute_reply.started":"2025-02-13T23:43:44.764687Z","shell.execute_reply":"2025-02-13T23:46:44.875452Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"background-color:#2D2D2D; color:#D6E906FF; padding:10px; border-radius:5px;\">\n\n\n# Displaying Sample MNIST Images with Labels 🖼️📊\n\nThis function visualizes a grid of sample images from the MNIST dataset, displaying them alongside their true labels:\n\n### Key Steps:\n1. **Grid Layout**:\n   - The grid layout is defined with a fixed number of columns (`cols=5`), and the number of rows is dynamically calculated based on the number of images to display (`num_images`).\n\n2. **Image Display**:\n   - Each image is reshaped to its original dimensions (28x28 pixels) and displayed using `plt.imshow()` with a grayscale color map (`cmap='gray'`).\n\n3. **Labels**:\n   - Each image is labeled with its corresponding class using `plt.title()`.\n\n4. **Plot Display**:\n   - The images are arranged in a grid, and the axes are hidden to focus on the visual content of the images.\n\nThis function is useful for quickly inspecting a subset of images from the dataset. 🎨🔍","metadata":{"id":"Us0P3RGWI20s"}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Function to display a grid of images with their corresponding labels\ndef show(images, labels, num_images=10):\n    # Define number of columns and calculate number of rows for the subplot grid\n    cols = 5\n    rows = (num_images // cols) + 1\n\n    # Create a figure to display the images\n    plt.figure(figsize=(12, 12))\n\n    # Loop through the images and plot them on the grid\n    for i in range(num_images):\n        plt.subplot(rows, cols, i + 1)  # Create a subplot for each image\n        plt.imshow(images[i].reshape(28, 28), cmap='gray')  # Reshape and display the image\n        plt.title(f'label:{labels[i]}')  # Set the title to the label of the image\n        plt.axis('off')  # Turn off the axis to focus on the image\n\n    # Show the plot with all images\n    plt.show()","metadata":{"id":"bugd6CjBP11_","trusted":true,"execution":{"iopub.status.busy":"2025-02-13T23:46:44.877731Z","iopub.execute_input":"2025-02-13T23:46:44.878147Z","iopub.status.idle":"2025-02-13T23:46:44.884639Z","shell.execute_reply.started":"2025-02-13T23:46:44.878079Z","shell.execute_reply":"2025-02-13T23:46:44.883563Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"background-color:#2D2D2D; color:#D6E906FF; padding:10px; border-radius:5px;\">\n\n\n# Displaying Random Sample Images from Training and Test Datasets 🎲🖼️\n\nIn this cell, we randomly select a subset of images from both the training and test datasets, then display them along with their labels:\n\n### Key Steps:\n1. **Random Index Selection**:\n   - `np.random.choice()` is used to randomly select indices from the training (`x_train`) and test (`x_test`) datasets. We select 10 images from the training set and 5 from the test set, without replacement (`replace=False`).\n\n2. **Concatenate the Samples**:\n   - The randomly selected images and their labels are concatenated from both datasets into `sample_images` and `sample_labels` using `np.concatenate()`.\n\n3. **Image Display**:\n   - The `show()` function is then called to display the selected images and their corresponding labels in a grid layout.\n\nThis provides a quick way to visually inspect random samples from both the training and test datasets. 🎨🔍","metadata":{"id":"fQICZaE4JFsq"}},{"cell_type":"code","source":"import numpy as np\n\n# Select random indices from the training and test datasets\ntrain_indices = np.random.choice(len(x_train), 10, replace=False)  # 10 random indices from the training set\ntest_indices = np.random.choice(len(x_test), 5, replace=False)  # 5 random indices from the test set\n\n# Concatenate the selected sample images and labels from both training and test datasets\nsample_images = np.concatenate((x_train[train_indices], x_test[test_indices]))\nsample_labels = np.concatenate((y_train[train_indices], y_test[test_indices]))\n\n# Display the sample images with their corresponding labels\nshow(sample_images, sample_labels, num_images=15)","metadata":{"id":"MKbhTJDPTuQv","outputId":"a0a96e64-f655-4e0e-f2df-d996a0387533","trusted":true,"execution":{"iopub.status.busy":"2025-02-13T23:46:44.885736Z","iopub.execute_input":"2025-02-13T23:46:44.886043Z","iopub.status.idle":"2025-02-13T23:46:46.285489Z","shell.execute_reply.started":"2025-02-13T23:46:44.886017Z","shell.execute_reply":"2025-02-13T23:46:46.284236Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"background-color:#2D2D2D; color:#D6E906FF; padding:10px; border-radius:5px;\">\n\n# Image Upload and Prediction for MNIST Dataset 🖼️🔮\n\nIn this notebook, we allow users to upload an image of a handwritten digit or provide a path to the image. The image is then processed, and the model predicts the digit with confidence. Here's how the system works:\n\n### Key Steps:\n1. **Image Upload**:\n   - Users can upload an image of a handwritten digit via the file upload widget (`upload_button`), or provide a file path directly through the `path_input` text box.\n\n2. **Image Preprocessing**:\n   - The uploaded image is resized to 28x28 pixels and converted to grayscale. The pixel values are inverted and normalized to a range between 0 and 1 for the model input.\n\n3. **Prediction**:\n   - The preprocessed image is passed to the trained model, which predicts the digit. The prediction, along with the confidence level, is displayed alongside the image.\n\n4. **Widgets**:\n   - The interactive widgets (file upload, path input, and prediction button) make the process user-friendly.\n\nThis provides an interactive way to input an image, get a prediction, and visualize the result. 🚀💡","metadata":{"id":"Ocqm0SMeJYWV"}},{"cell_type":"code","source":"from PIL import Image\nfrom google.colab import files\nimport ipywidgets as widgets\nfrom IPython.display import display\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Define the function to handle file uploads\ndef handle_upload(change):\n    # Get the uploaded file and save it as 'user_image.png'\n    input_path = next(iter(upload_button.value))\n    with open(\"/tmp/user_image.png\", \"wb\") as f:\n        f.write(upload_button.value[input_path]['content'])\n    path_label.value = \"/tmp/user_image.png\"\n    print(\"UPLOAD DONE\")\n\n# Define the function to handle path input\ndef handle_path_input(change):\n    path_label.value = path_input.value  # Update path_label with the input path\n    print(f'PATH DONE ENTERED')\n\n# Define the function to handle the prediction\ndef handle_prediction(change):\n    # Get the image path from the label\n    input_path = path_label.value\n    if not input_path:\n        print(\" input invalid \")\n        return\n\n    # Load and preprocess the image\n    img = Image.open(input_path).convert('L')\n    img = img.resize((28, 28))  # Resize to 28x28\n    img_array = np.array(img)\n\n    # Invert the colors and normalize the image\n    img_array = 255 - img_array\n    img_array = img_array / 255.0\n    img_array = img_array.reshape(1, 28, 28, 1)\n\n    # Make the prediction\n    prediction = model.predict(img_array)\n    prediction_label = np.argmax(prediction)  # Get the predicted label\n    confidence = np.max(prediction) * 100  # Get the prediction confidence\n\n    # Display the image and prediction result\n    plt.imshow(img, cmap='gray')\n    plt.title(f'predict:{prediction_label} (Confidence: {confidence:.2f}%)')\n    plt.axis('off')\n    plt.show()\n\n    print(f'Predicted digit: {prediction_label} with {confidence:.2f}% confidence.')\n\n# Create the necessary widgets\nupload_button = widgets.FileUpload(accept='image/*', multiple=False)  # Upload button\npath_input = widgets.Text(description=\"Path to image:\")  # Text input for image path\nsubmit_button = widgets.Button(description=\"Click To Predict\", style={'button_color': 'green', 'font_weight': 'bold'})  # Button to trigger prediction\npath_label = widgets.Label(value=\"\")  # Label to show the image path\n\ndef predict_input():\n    \"\"\"\n    This function initializes the widgets and displays them. It also binds the\n    handle_upload, handle_path_input, and handle_prediction functions to the\n    corresponding widgets.\n    \"\"\"\n    # Bind the functions to the respective widgets\n    upload_button.observe(handle_upload, names='value')\n    path_input.observe(handle_path_input, names='value')\n    submit_button.on_click(handle_prediction)\n\n    # Display the widgets\n    display(widgets.Label(\"Choose how to input your image:\"))\n    display(widgets.HBox([upload_button, path_input]))  # Upload button and path input side by side\n    display(path_label)  # Display the path label\n    display(submit_button)  # Display the submit button\n\n# Initialize the widgets and display them\npredict_input()\n","metadata":{"id":"4eUFeLph1nnP","outputId":"e40c028f-fbd2-4296-ba4d-3f20bef42bae","trusted":true,"execution":{"iopub.status.busy":"2025-02-13T23:46:46.286823Z","iopub.execute_input":"2025-02-13T23:46:46.287263Z","iopub.status.idle":"2025-02-13T23:46:46.344290Z","shell.execute_reply.started":"2025-02-13T23:46:46.287207Z","shell.execute_reply":"2025-02-13T23:46:46.343101Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div align=\"center\">\n<br>\n<a href=\"https://git.io/typing-svg\" ><img src=\"https://readme-typing-svg.demolab.com?font=Fira+Code&size=25%&pause=1000&color=00abf0&center=true&vCenter=true&width=435&height=25%&lines=Thank+you+for+visiting!;Have+a+good+day!;\" alt=\"Typing SVG\" /></a>\n</div>\n<img width=100% src=\"https://capsule-render.vercel.app/api?type=waving&color=00abf0&height=70&section=footer\"/>","metadata":{"id":"4OlNJXP8QOh8"}}]}