-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eye Disease Classified by RNN
1 lines (1 loc) · 35.9 KB
/
Eye Disease Classified by RNN
1
{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":4130910,"sourceType":"datasetVersion","datasetId":2440665}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<a id=\"1\"></a>\n<div id=\"animated-container\" style=\"position: relative;\n border-radius: 10px;\n padding: 10px;\n font-family: Arial, sans-serif;\n font-size: 36px;\n font-weight: 900;\n color: black;\n display: inline-block;\n text-transform: uppercase;\n box-shadow: 2px 2px 4px #888;\n text-shadow: 2px 2px 4px #888;\n overflow: hidden;\n background-color: #800606 ;\n cursor: pointer;\" onclick=\"toggleAnimation()\"> <!-- Added cursor pointer and onclick event -->\n <div style=\"position: absolute;\n top: 50%;\n right: 0; /* Adjusted to place on the right side */\n transform: translateY(45%);\">\n <!-- <img src=\"https://www.kaggle.com/static/images/tier-animation-transparent.gif\" alt=\"Cartoon Character\" style=\"height: 100px; width: auto;\"> -->\n </div>\n <div style=\"font-weight: bold;\"></div>\n <span style=\"font-style: italic; text-shadow: 2px 2px 4px #888; color: white;\">Eye Disease 🦠(RNN)</span>\n <!-- <img src=\"https://pin.it/6mmgqwOvV\" alt=\"Cartoon Character\" style=\"height: 50px; width: auto;\"> -->\n</div>\n\n<script>\n function toggleAnimation() {\n var container = document.getElementById('animated-container');\n var animationDiv = container.querySelector('div');\n if (animationDiv.style.animationPlayState === 'paused') {\n animationDiv.style.animationPlayState = 'running';\n } else {\n animationDiv.style.animationPlayState = 'paused';\n }\n }\n</script>\n","metadata":{}},{"cell_type":"markdown","source":"<img src=\"https://www.mississippivision.com/wp-content/uploads/diabetic-retinopathy-1-2048x902.jpeg\" style=\"width:100%\">","metadata":{}},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n<div id=\"animated-container\" style=\"position: relative;\n border-radius: 10px;\n padding: 10px;\n font-family: Arial, sans-serif;\n font-size: 36px;\n font-weight: 900;\n color: black;\n display: inline-block;\n text-transform: uppercase;\n box-shadow: 2px 2px 4px #888;\n text-shadow: 2px 2px 4px #888;\n overflow: hidden;\n background-color: #800606 ;\n cursor: pointer;\" onclick=\"toggleAnimation()\"> <!-- Added cursor pointer and onclick event -->\n <div style=\"position: absolute;\n top: 50%;\n right: 0; /* Adjusted to place on the right side */\n transform: translateY(45%);\">\n <!-- <img src=\"https://www.kaggle.com/static/images/tier-animation-transparent.gif\" alt=\"Cartoon Character\" style=\"height: 100px; width: auto;\"> -->\n </div>\n <div style=\"font-weight: bold;\"></div>\n <span style=\"font-style: italic; text-shadow: 2px 2px 4px #888; color: white;\">About author 👨💻 </span>\n <!-- <img src=\"https://pin.it/6mmgqwOvV\" alt=\"Cartoon Character\" style=\"height: 50px; width: auto;\"> -->\n</div>\n\n<script>\n function toggleAnimation() {\n var container = document.getElementById('animated-container');\n var animationDiv = container.querySelector('div');\n if (animationDiv.style.animationPlayState === 'paused') {\n animationDiv.style.animationPlayState = 'running';\n } else {\n animationDiv.style.animationPlayState = 'paused';\n }\n }\n</script>\n","metadata":{}},{"cell_type":"markdown","source":"**Hi 👋 everyone! Welcome to my notebook. 📓 I'm Danish Mubashar and passionate about data science, and I'm excited to share my findings with you. 🤓 In my recent notebook, I successfully implemented a Convolutional Neural Network (RNN) on the Eye Disease dataset. If you find this notebook helpful, please upvote ! ❤️**\n\n**🤝 I am eager to cultivate enriching collaborations, foster knowledge exchange, and forge strategic alliances within the vibrant data science community. Join me on Kaggle or any other suitable platform to co-create, ideate, and elevate our collective expertise. Let's embark on a journey of innovation and discovery together!**\n\n### **Connect Me**\n\n* [Kaggle](https://www.kaggle.com/danishmubashar) \n* [Gmail]([email protected]) \n* [Linkedin](https://www.linkedin.com/in/muhammad-danish-mubashar-002b912a0/?originalSubdomain=pk)\n\n","metadata":{}},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n<div id=\"animated-container\" style=\"position: relative;\n border-radius: 10px;\n padding: 10px;\n font-family: Arial, sans-serif;\n font-size: 36px;\n font-weight: 900;\n color: black;\n display: inline-block;\n text-transform: uppercase;\n box-shadow: 2px 2px 4px #888;\n text-shadow: 2px 2px 4px #888;\n overflow: hidden;\n background-color: #800606 ;\n cursor: pointer;\" onclick=\"toggleAnimation()\"> <!-- Added cursor pointer and onclick event -->\n <div style=\"position: absolute;\n top: 50%;\n right: 0; /* Adjusted to place on the right side */\n transform: translateY(45%);\">\n <!-- <img src=\"https://www.kaggle.com/static/images/tier-animation-transparent.gif\" alt=\"Cartoon Character\" style=\"height: 100px; width: auto;\"> -->\n </div>\n <div style=\"font-weight: bold;\"></div>\n <span style=\"font-style: italic; text-shadow: 2px 2px 4px #888; color: white;\">About dataset 📊</span>\n <!-- <img src=\"https://pin.it/6mmgqwOvV\" alt=\"Cartoon Character\" style=\"height: 50px; width: auto;\"> -->\n</div>\n\n<script>\n function toggleAnimation() {\n var container = document.getElementById('animated-container');\n var animationDiv = container.querySelector('div');\n if (animationDiv.style.animationPlayState === 'paused') {\n animationDiv.style.animationPlayState = 'running';\n } else {\n animationDiv.style.animationPlayState = 'paused';\n }\n }\n</script>\n","metadata":{}},{"cell_type":"markdown","source":"# **Summary**\n**Our eyes are one of the most important senses that help us to make contact with each other and allow us to see the beauty and abomination around us. Eyes are susceptible to diseases. Eye diseases include **Glaucoma**, **Diabetic retinopathy**, and **Cataracts**.**Early diagnosis and treatment of eye diseases can preserve eyesight.**\n\n# **Eye Diseases**\n- **Glaucoma: Group of eye diseases that can damage the optic nerve. The optic nerve is responsible for transmitting visual information from the eye to the brain. Glaucoma can lead to permanent vision loss and, in some cases, blindness.**\n\n- **Diabetic retinopathy: It's an eye condition that affects people with diabetes. High blood sugar leaves damaged vessels in the retina. The retina is that part of your eyes that can detect light and send signals to the brain. Damage to these blood vessels can lead to vision loss and blindness in some cases.**\n\n- **Cataracts: Cataracts cloud the natural eye lens, leading to blurry vision and difficulty seeing clearly. Aging is one of the main reasons for this disease, but it can also be caused by factors such as injury, genetics, or medication effects. Cataracts can make natural vision blurry, cloudy, and even double it.**\n\n**By better understanding these eye diseases, their causes, and their effects on vision, we can develop insights to aid their early detection and effective management. This notebook tries to classify these diseases from pictures.**\n","metadata":{}},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n<div id=\"animated-container\" style=\"position: relative;\n border-radius: 10px;\n padding: 10px;\n font-family: Arial, sans-serif;\n font-size: 36px;\n font-weight: 900;\n color: black;\n display: inline-block;\n text-transform: uppercase;\n box-shadow: 2px 2px 4px #888;\n text-shadow: 2px 2px 4px #888;\n overflow: hidden;\n background-color: #800606 ;\n cursor: pointer;\" onclick=\"toggleAnimation()\"> <!-- Added cursor pointer and onclick event -->\n <div style=\"position: absolute;\n top: 50%;\n right: 0; /* Adjusted to place on the right side */\n transform: translateY(45%);\">\n <!-- <img src=\"https://www.kaggle.com/static/images/tier-animation-transparent.gif\" alt=\"Cartoon Character\" style=\"height: 100px; width: auto;\"> -->\n </div>\n <div style=\"font-weight: bold;\"></div>\n <span style=\"font-style: italic; text-shadow: 2px 2px 4px #888; color: white;\">about model(RNN) 🚀</span>\n <!-- <img src=\"https://pin.it/6mmgqwOvV\" alt=\"Cartoon Character\" style=\"height: 50px; width: auto;\"> -->\n</div>\n\n<script>\n function toggleAnimation() {\n var container = document.getElementById('animated-container');\n var animationDiv = container.querySelector('div');\n if (animationDiv.style.animationPlayState === 'paused') {\n animationDiv.style.animationPlayState = 'running';\n } else {\n animationDiv.style.animationPlayState = 'paused';\n }\n }\n</script>\n","metadata":{}},{"cell_type":"markdown","source":"# **Recurrent Neural Networks (RNNs) 🔄🧠**\n\n**RNNs are a type of deep learning architecture widely used for processing sequential data. They excel at capturing temporal dependencies and have applications in various domains. 📚🔍**\n\n## **How RNNs Work 🔄**\n\n**RNNs mimic the human brain's ability to process and understand sequential information. They introduce recurrent connections within the network, allowing information to persist and be passed from one step to another within the sequence. 🕒🔁**\n\n**RNNs process sequential data step by step, considering the context of previous steps while analyzing the current step. At each step, the network takes an input and combines it with the hidden state from the previous step to generate an output and update the hidden state for the next step. 🚶♂️🔄**\n\n## **Capturing Dependencies and Long-term Context 🧠🔗**\n\n**RNNs are particularly effective at capturing temporal dependencies in sequences. They can model and understand the relationships between elements that appear at different positions within the sequence. This ability enables them to capture long-term dependencies, making them valuable for tasks involving sequential data. 📈🔍**\n\n**However, RNNs can face challenges in maintaining and propagating information over long sequences due to the vanishing gradient problem. To address this limitation, variants of RNNs such as Long Short-Term Memory (LSTM) and Gated Recurrent Unit (GRU) have been developed. These variants introduce gating mechanisms that help the network selectively update and retain information, mitigating the vanishing gradient problem. 📉🔒**\n\n## **Applications of RNNs 🌐📊**\n\n**RNNs find applications in several domains, including:**\n\n- **Natural Language Processing (NLP): RNNs can process and generate human language, making them useful for tasks like language modeling, sentiment analysis, and machine translation. 🗣️✍️🌐**\n- **Speech Recognition: RNNs are employed in speech recognition systems to convert spoken language into written text. 🎙️📝**\n- **Time Series Analysis: RNNs can analyze and predict patterns in time-dependent data, making them valuable for tasks such as stock market forecasting or weather prediction. ⏰📈🌦️**\n","metadata":{}},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n<div id=\"animated-container\" style=\"position: relative;\n border-radius: 10px;\n padding: 10px;\n font-family: Arial, sans-serif;\n font-size: 36px;\n font-weight: 900;\n color: black;\n display: inline-block;\n text-transform: uppercase;\n box-shadow: 2px 2px 4px #888;\n text-shadow: 2px 2px 4px #888;\n overflow: hidden;\n background-color: #800606 ;\n cursor: pointer;\" onclick=\"toggleAnimation()\"> <!-- Added cursor pointer and onclick event -->\n <div style=\"position: absolute;\n top: 50%;\n right: 0; /* Adjusted to place on the right side */\n transform: translateY(45%);\">\n <!-- <img src=\"https://www.kaggle.com/static/images/tier-animation-transparent.gif\" alt=\"Cartoon Character\" style=\"height: 100px; width: auto;\"> -->\n </div>\n <div style=\"font-weight: bold;\"></div>\n <span style=\"font-style: italic; text-shadow: 2px 2px 4px #888; color: white;\">Libraries 📚</span>\n <!-- <img src=\"https://pin.it/6mmgqwOvV\" alt=\"Cartoon Character\" style=\"height: 50px; width: auto;\"> -->\n</div>\n\n<script>\n function toggleAnimation() {\n var container = document.getElementById('animated-container');\n var animationDiv = container.querySelector('div');\n if (animationDiv.style.animationPlayState === 'paused') {\n animationDiv.style.animationPlayState = 'running';\n } else {\n animationDiv.style.animationPlayState = 'paused';\n }\n }\n</script>\n","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\nimport random\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nfrom PIL import Image\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator # Corrected import statement\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\nfrom tensorflow.keras.callbacks import TensorBoard\nfrom sklearn.metrics import classification_report\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2024-04-29T04:20:02.649726Z","iopub.execute_input":"2024-04-29T04:20:02.65089Z","iopub.status.idle":"2024-04-29T04:20:18.91232Z","shell.execute_reply.started":"2024-04-29T04:20:02.65085Z","shell.execute_reply":"2024-04-29T04:20:18.910993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n<div id=\"animated-container\" style=\"position: relative;\n border-radius: 10px;\n padding: 10px;\n font-family: Arial, sans-serif;\n font-size: 36px;\n font-weight: 900;\n color: black;\n display: inline-block;\n text-transform: uppercase;\n box-shadow: 2px 2px 4px #888;\n text-shadow: 2px 2px 4px #888;\n overflow: hidden;\n background-color: #800606 ;\n cursor: pointer;\" onclick=\"toggleAnimation()\"> <!-- Added cursor pointer and onclick event -->\n <div style=\"position: absolute;\n top: 50%;\n right: 0; /* Adjusted to place on the right side */\n transform: translateY(45%);\">\n <!-- <img src=\"https://www.kaggle.com/static/images/tier-animation-transparent.gif\" alt=\"Cartoon Character\" style=\"height: 100px; width: auto;\"> -->\n </div>\n <div style=\"font-weight: bold;\"></div>\n <span style=\"font-style: italic; text-shadow: 2px 2px 4px #888; color: white;\">Preprocessing 🛠️</span>\n <!-- <img src=\"https://pin.it/6mmgqwOvV\" alt=\"Cartoon Character\" style=\"height: 50px; width: auto;\"> -->\n</div>\n\n<script>\n function toggleAnimation() {\n var container = document.getElementById('animated-container');\n var animationDiv = container.querySelector('div');\n if (animationDiv.style.animationPlayState === 'paused') {\n animationDiv.style.animationPlayState = 'running';\n } else {\n animationDiv.style.animationPlayState = 'paused';\n }\n }\n</script>\n","metadata":{}},{"cell_type":"code","source":"# Define rescaling layer\nrescale = tf.keras.layers.Rescaling(1./255)\n\n# Load train dataset\ntrain_ds = tf.keras.utils.image_dataset_from_directory(\n directory='/kaggle/input/eye-diseases-classification/dataset',\n batch_size=32,\n image_size=(256, 256),\n validation_split=0.2,\n subset=\"training\",\n seed=123,\n label_mode='categorical', # Assuming you have multiple classes\n)\n\n# Preprocess train dataset (rescale)\ntrain_ds = train_ds.map(lambda x, y: (rescale(x), y))\n\n# Load validation dataset\nvalidation_ds = tf.keras.utils.image_dataset_from_directory(\n directory='/kaggle/input/eye-diseases-classification/dataset',\n batch_size=32,\n image_size=(256, 256),\n validation_split=0.2,\n subset=\"validation\",\n seed=123,\n label_mode='categorical', # Assuming you have multiple classes\n)\n\n# Preprocess validation dataset (rescale)\nvalidation_ds = validation_ds.map(lambda x, y: (rescale(x), y))\n\n# Load test dataset\ntest_ds = tf.keras.utils.image_dataset_from_directory(\n directory='/kaggle/input/eye-diseases-classification/dataset', # Specify the directory for the test dataset\n batch_size=32,\n image_size=(256, 256),\n label_mode='categorical', # Assuming you have multiple classes\n shuffle=False,\n)\n\n# Preprocess test dataset (rescale)\ntest_ds = test_ds.map(lambda x, y: (rescale(x), y))","metadata":{"execution":{"iopub.status.busy":"2024-04-29T04:20:18.914172Z","iopub.execute_input":"2024-04-29T04:20:18.914862Z","iopub.status.idle":"2024-04-29T04:20:20.94018Z","shell.execute_reply.started":"2024-04-29T04:20:18.914826Z","shell.execute_reply":"2024-04-29T04:20:20.938929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Check Shape After Preprocessing**","metadata":{}},{"cell_type":"code","source":"# Check the first image shape in the training dataset\nprint(\"Shape of the first image in the training dataset:\", next(iter(train_ds))[0][0].shape)\n# Check the first image shape in the validation dataset\nprint(\"Shape of the first image in the validation dataset:\", next(iter(validation_ds))[0][0].shape)\n# Check the first image shape in the test dataset\nprint(\"Shape of the first image in the test dataset:\", next(iter(test_ds))[0][0].shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-29T04:20:24.327757Z","iopub.execute_input":"2024-04-29T04:20:24.328179Z","iopub.status.idle":"2024-04-29T04:20:28.91454Z","shell.execute_reply.started":"2024-04-29T04:20:24.328146Z","shell.execute_reply":"2024-04-29T04:20:28.913103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Check Pixel Value After Preprocessing**","metadata":{}},{"cell_type":"code","source":"# Initialize variables to store minimum and maximum pixel values\nmin_pixel_value = float('inf')\nmax_pixel_value = float('-inf')\n\n# Iterate through the dataset\nfor images, _ in train_ds:\n # Compute the minimum and maximum pixel values in the current batch of images\n batch_min = tf.reduce_min(images)\n batch_max = tf.reduce_max(images)\n \n # Update overall minimum and maximum pixel values\n min_pixel_value = tf.minimum(min_pixel_value, batch_min)\n max_pixel_value = tf.maximum(max_pixel_value, batch_max)\n\n# Print the minimum and maximum pixel values\nprint(\"Minimum pixel value:\", min_pixel_value.numpy())\nprint(\"Maximum pixel value:\", max_pixel_value.numpy())\n","metadata":{"execution":{"iopub.status.busy":"2024-04-29T04:20:35.125112Z","iopub.execute_input":"2024-04-29T04:20:35.125572Z","iopub.status.idle":"2024-04-29T04:20:57.145605Z","shell.execute_reply.started":"2024-04-29T04:20:35.12554Z","shell.execute_reply":"2024-04-29T04:20:57.14435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n<div id=\"animated-container\" style=\"position: relative;\n border-radius: 10px;\n padding: 10px;\n font-family: Arial, sans-serif;\n font-size: 36px;\n font-weight: 900;\n color: black;\n display: inline-block;\n text-transform: uppercase;\n box-shadow: 2px 2px 4px #888;\n text-shadow: 2px 2px 4px #888;\n overflow: hidden;\n background-color: #800606 ;\n cursor: pointer;\" onclick=\"toggleAnimation()\"> <!-- Added cursor pointer and onclick event -->\n <div style=\"position: absolute;\n top: 50%;\n right: 0; /* Adjusted to place on the right side */\n transform: translateY(45%);\">\n <!-- <img src=\"https://www.kaggle.com/static/images/tier-animation-transparent.gif\" alt=\"Cartoon Character\" style=\"height: 100px; width: auto;\"> -->\n </div>\n <div style=\"font-weight: bold;\"></div>\n <span style=\"font-style: italic; text-shadow: 2px 2px 4px #888; color: white;\">Visualization 🕵</span>\n <!-- <img src=\"https://pin.it/6mmgqwOvV\" alt=\"Cartoon Character\" style=\"height: 50px; width: auto;\"> -->\n</div>\n\n<script>\n function toggleAnimation() {\n var container = document.getElementById('animated-container');\n var animationDiv = container.querySelector('div');\n if (animationDiv.style.animationPlayState === 'paused') {\n animationDiv.style.animationPlayState = 'running';\n } else {\n animationDiv.style.animationPlayState = 'paused';\n }\n }\n</script>\n","metadata":{}},{"cell_type":"code","source":"def visualize_images(path, target_size=(256, 256), num_images=5):\n\n # Get a list of image filenames\n image_filenames = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]\n\n if not image_filenames:\n raise ValueError(\"No images found in the specified path\")\n\n # Select random images\n selected_images = random.sample(image_filenames, min(num_images, len(image_filenames)))\n\n # Create a figure and axes\n fig, axes = plt.subplots(1, num_images, figsize=(15, 3), facecolor='white')\n\n # Display each image\n for i, image_filename in enumerate(selected_images):\n # Load image and resize\n image_path = os.path.join(path, image_filename)\n image = Image.open(image_path)\n image = image.resize(target_size)\n\n # Display image\n axes[i].imshow(image)\n axes[i].axis('off')\n axes[i].set_title(image_filename) # Set image filename as title\n\n # Adjust layout and display\n plt.tight_layout()\n plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-29T04:21:12.746537Z","iopub.execute_input":"2024-04-29T04:21:12.747792Z","iopub.status.idle":"2024-04-29T04:21:12.759122Z","shell.execute_reply.started":"2024-04-29T04:21:12.747738Z","shell.execute_reply":"2024-04-29T04:21:12.757789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n<div id=\"animated-container\" style=\"position: relative;\n border-radius: 10px;\n padding: 10px;\n font-family: Arial, sans-serif;\n font-size: 36px;\n font-weight: 900;\n color: black;\n display: inline-block;\n text-transform: uppercase;\n box-shadow: 2px 2px 4px #888;\n text-shadow: 2px 2px 4px #888;\n overflow: hidden;\n background-color: #800606 ;\n cursor: pointer;\" onclick=\"toggleAnimation()\"> <!-- Added cursor pointer and onclick event -->\n <div style=\"position: absolute;\n top: 50%;\n right: 0; /* Adjusted to place on the right side */\n transform: translateY(45%);\">\n <!-- <img src=\"https://www.kaggle.com/static/images/tier-animation-transparent.gif\" alt=\"Cartoon Character\" style=\"height: 100px; width: auto;\"> -->\n </div>\n <div style=\"font-weight: bold;\"></div>\n <span style=\"font-style: italic; text-shadow: 2px 2px 4px #888; color: white;\">Cataract 👁️🗨️</span>\n <!-- <img src=\"https://pin.it/6mmgqwOvV\" alt=\"Cartoon Character\" style=\"height: 50px; width: auto;\"> -->\n</div>\n\n<script>\n function toggleAnimation() {\n var container = document.getElementById('animated-container');\n var animationDiv = container.querySelector('div');\n if (animationDiv.style.animationPlayState === 'paused') {\n animationDiv.style.animationPlayState = 'running';\n } else {\n animationDiv.style.animationPlayState = 'paused';\n }\n }\n</script>\n","metadata":{}},{"cell_type":"code","source":"# Specify the path containing the images to visualize\npath_to_visualize = \"/kaggle/input/eye-diseases-classification/dataset/cataract\"\n\n# Visualize 5 random images\nvisualize_images(path_to_visualize, num_images=5)","metadata":{"execution":{"iopub.status.busy":"2024-04-29T04:21:23.106034Z","iopub.execute_input":"2024-04-29T04:21:23.107105Z","iopub.status.idle":"2024-04-29T04:21:24.448278Z","shell.execute_reply.started":"2024-04-29T04:21:23.107067Z","shell.execute_reply":"2024-04-29T04:21:24.446962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n<div id=\"animated-container\" style=\"position: relative;\n border-radius: 10px;\n padding: 10px;\n font-family: Arial, sans-serif;\n font-size: 36px;\n font-weight: 900;\n color: black;\n display: inline-block;\n text-transform: uppercase;\n box-shadow: 2px 2px 4px #888;\n text-shadow: 2px 2px 4px #888;\n overflow: hidden;\n background-color: #800606 ;\n cursor: pointer;\" onclick=\"toggleAnimation()\"> <!-- Added cursor pointer and onclick event -->\n <div style=\"position: absolute;\n top: 50%;\n right: 0; /* Adjusted to place on the right side */\n transform: translateY(45%);\">\n <!-- <img src=\"https://www.kaggle.com/static/images/tier-animation-transparent.gif\" alt=\"Cartoon Character\" style=\"height: 100px; width: auto;\"> -->\n </div>\n <div style=\"font-weight: bold;\"></div>\n <span style=\"font-style: italic; text-shadow: 2px 2px 4px #888; color: white;\">Diabetic-Retinopathy 🦠</span>\n <!-- <img src=\"https://pin.it/6mmgqwOvV\" alt=\"Cartoon Character\" style=\"height: 50px; width: auto;\"> -->\n</div>\n\n<script>\n function toggleAnimation() {\n var container = document.getElementById('animated-container');\n var animationDiv = container.querySelector('div');\n if (animationDiv.style.animationPlayState === 'paused') {\n animationDiv.style.animationPlayState = 'running';\n } else {\n animationDiv.style.animationPlayState = 'paused';\n }\n }\n</script>\n","metadata":{}},{"cell_type":"code","source":"# Specify the path containing the images to visualize\npath_to_visualize = \"/kaggle/input/eye-diseases-classification/dataset/diabetic_retinopathy\"\n\n# Visualize 5 random images\nvisualize_images(path_to_visualize, num_images=5)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n<div id=\"animated-container\" style=\"position: relative;\n border-radius: 10px;\n padding: 10px;\n font-family: Arial, sans-serif;\n font-size: 36px;\n font-weight: 900;\n color: black;\n display: inline-block;\n text-transform: uppercase;\n box-shadow: 2px 2px 4px #888;\n text-shadow: 2px 2px 4px #888;\n overflow: hidden;\n background-color: #800606 ;\n cursor: pointer;\" onclick=\"toggleAnimation()\"> <!-- Added cursor pointer and onclick event -->\n <div style=\"position: absolute;\n top: 50%;\n right: 0; /* Adjusted to place on the right side */\n transform: translateY(45%);\">\n <!-- <img src=\"https://www.kaggle.com/static/images/tier-animation-transparent.gif\" alt=\"Cartoon Character\" style=\"height: 100px; width: auto;\"> -->\n </div>\n <div style=\"font-weight: bold;\"></div>\n <span style=\"font-style: italic; text-shadow: 2px 2px 4px #888; color: white;\">Glaucoma 🧿</span>\n <!-- <img src=\"https://pin.it/6mmgqwOvV\" alt=\"Cartoon Character\" style=\"height: 50px; width: auto;\"> -->\n</div>\n\n<script>\n function toggleAnimation() {\n var container = document.getElementById('animated-container');\n var animationDiv = container.querySelector('div');\n if (animationDiv.style.animationPlayState === 'paused') {\n animationDiv.style.animationPlayState = 'running';\n } else {\n animationDiv.style.animationPlayState = 'paused';\n }\n }\n</script>\n","metadata":{}},{"cell_type":"code","source":"# Specify the path containing the images to visualize\npath_to_visualize = \"/kaggle/input/eye-diseases-classification/dataset/glaucoma\"\n\n# Visualize 5 random images\nvisualize_images(path_to_visualize, num_images=5)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n<div id=\"animated-container\" style=\"position: relative;\n border-radius: 10px;\n padding: 10px;\n font-family: Arial, sans-serif;\n font-size: 36px;\n font-weight: 900;\n color: black;\n display: inline-block;\n text-transform: uppercase;\n box-shadow: 2px 2px 4px #888;\n text-shadow: 2px 2px 4px #888;\n overflow: hidden;\n background-color: #800606 ;\n cursor: pointer;\" onclick=\"toggleAnimation()\"> <!-- Added cursor pointer and onclick event -->\n <div style=\"position: absolute;\n top: 50%;\n right: 0; /* Adjusted to place on the right side */\n transform: translateY(45%);\">\n <!-- <img src=\"https://www.kaggle.com/static/images/tier-animation-transparent.gif\" alt=\"Cartoon Character\" style=\"height: 100px; width: auto;\"> -->\n </div>\n <div style=\"font-weight: bold;\"></div>\n <span style=\"font-style: italic; text-shadow: 2px 2px 4px #888; color: white;\">Normal 𓆩 👁️ 𓆪</span>\n <!-- <img src=\"https://pin.it/6mmgqwOvV\" alt=\"Cartoon Character\" style=\"height: 50px; width: auto;\"> -->\n</div>\n\n<script>\n function toggleAnimation() {\n var container = document.getElementById('animated-container');\n var animationDiv = container.querySelector('div');\n if (animationDiv.style.animationPlayState === 'paused') {\n animationDiv.style.animationPlayState = 'running';\n } else {\n animationDiv.style.animationPlayState = 'paused';\n }\n }\n</script>\n","metadata":{}},{"cell_type":"code","source":"# Specify the path containing the images to visualize\npath_to_visualize = \"/kaggle/input/eye-diseases-classification/dataset/normal\"\n\n# Visualize 5 random images\nvisualize_images(path_to_visualize, num_images=5)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n<div id=\"animated-container\" style=\"position: relative;\n border-radius: 10px;\n padding: 10px;\n font-family: Arial, sans-serif;\n font-size: 36px;\n font-weight: 900;\n color: black;\n display: inline-block;\n text-transform: uppercase;\n box-shadow: 2px 2px 4px #888;\n text-shadow: 2px 2px 4px #888;\n overflow: hidden;\n background-color: #800606 ;\n cursor: pointer;\" onclick=\"toggleAnimation()\"> <!-- Added cursor pointer and onclick event -->\n <div style=\"position: absolute;\n top: 50%;\n right: 0; /* Adjusted to place on the right side */\n transform: translateY(45%);\">\n <!-- <img src=\"https://www.kaggle.com/static/images/tier-animation-transparent.gif\" alt=\"Cartoon Character\" style=\"height: 100px; width: auto;\"> -->\n </div>\n <div style=\"font-weight: bold;\"></div>\n <span style=\"font-style: italic; text-shadow: 2px 2px 4px #888; color: white;\">Model 🤖</span>\n <!-- <img src=\"https://pin.it/6mmgqwOvV\" alt=\"Cartoon Character\" style=\"height: 50px; width: auto;\"> -->\n</div>\n\n<script>\n function toggleAnimation() {\n var container = document.getElementById('animated-container');\n var animationDiv = container.querySelector('div');\n if (animationDiv.style.animationPlayState === 'paused') {\n animationDiv.style.animationPlayState = 'running';\n } else {\n animationDiv.style.animationPlayState = 'paused';\n }\n }\n</script>\n","metadata":{}},{"cell_type":"markdown","source":"## **Build Model & Check Summary**","metadata":{}},{"cell_type":"code","source":"\n# Define RNN model\nmodel = tf.keras.Sequential([\n tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(256, 256, 3)),\n tf.keras.layers.MaxPooling2D((2, 2)),\n tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),\n tf.keras.layers.MaxPooling2D((2, 2)),\n tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),\n tf.keras.layers.MaxPooling2D((2, 2)),\n tf.keras.layers.Flatten(),\n tf.keras.layers.Dense(64, activation='relu'),\n tf.keras.layers.Reshape((64, 1)), # Reshape for RNN input\n tf.keras.layers.SimpleRNN(32), # Simple RNN layer\n tf.keras.layers.Dense(4, activation='softmax') # Output layer\n])\n\n# Compile the model\nmodel.compile(optimizer='adam',\n loss='categorical_crossentropy',\n metrics=['accuracy'])\n\n# Print model summary\nprint(model.summary())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Fit Model**","metadata":{}},{"cell_type":"code","source":"\n# Define early stopping callback\nfrom tensorflow.keras.callbacks import EarlyStopping\nearly_stopping = EarlyStopping(patience=5, restore_best_weights=True)\n\n# Fit the model with callbacks\nhistory = model.fit(train_ds,\n validation_data=validation_ds,\n epochs=5,\n callbacks=[early_stopping])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Evaluate Model**","metadata":{}},{"cell_type":"code","source":"# Evaluate on test data\ntest_loss, test_accuracy = model.evaluate(test_ds)\nprint(\"Test accuracy:\", test_accuracy)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **History Plot**","metadata":{}},{"cell_type":"code","source":"\n# Define epochs\nepochs = range(1, len(history.history['loss']) + 1)\n\n# Plot training & validation loss\nplt.plot(epochs, history.history['loss'], label='Training Loss', marker='o')\nplt.plot(epochs, history.history['val_loss'], label='Validation Loss', marker='o')\nplt.title('Model Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()\n\n# Plot training & validation accuracy\nplt.plot(epochs, history.history['accuracy'], label='Training Accuracy', marker='o')\nplt.plot(epochs, history.history['val_accuracy'], label='Validation Accuracy', marker='o')\nplt.title('Model Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{},"execution_count":null,"outputs":[]}]}