Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2023-159
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2023-159
2023-159
Commits
056add31
Commit
056add31
authored
Sep 11, 2023
by
Dilshan K. B. G. L
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
final model
parent
40d4bc34
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
578 additions
and
0 deletions
+578
-0
IT20151188/IT20151188_Glaucoma_detection.ipynb
IT20151188/IT20151188_Glaucoma_detection.ipynb
+578
-0
No files found.
IT20151188/IT20151188_Glaucoma_detection.ipynb
0 → 100644
View file @
056add31
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"Dive mouting"
],
"metadata": {
"id": "-7qxmsARqk8H"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "NitO_CjRp8FA"
},
"outputs": [],
"source": [
"from google.colab import drive\n",
"drive.mount(\"/content/drive/\",force_remount=False)"
]
},
{
"cell_type": "markdown",
"source": [
"**Preprosessing**"
],
"metadata": {
"id": "HN2-UCN2qpYw"
}
},
{
"cell_type": "markdown",
"source": [
"grayscaling"
],
"metadata": {
"id": "RaFDyl6qqv-s"
}
},
{
"cell_type": "code",
"source": [
"# Grayscalling\n",
"\n",
"import cv2\n",
"import os\n",
"\n",
"# Path to the glaucoma images\n",
"glaucoma_folder = \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Glaucoma\"\n",
"\n",
"# Path to the non-glaucoma images\n",
"non_glaucoma_folder = \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/NonGlaucoma\"\n",
"\n",
"# grayscale fucntion\n",
"def grayscale_images(input_folder, output_folder):\n",
" # folder eka ndda balana eka\n",
" if not os.path.exists(output_folder):\n",
" os.makedirs(output_folder)\n",
"\n",
" # name reading\n",
" image_filenames = os.listdir(input_folder)\n",
"\n",
" for filename in image_filenames:\n",
" # Read the image\n",
" image_path = os.path.join(input_folder, filename)\n",
" img = cv2.imread(image_path)\n",
"\n",
" # Convert\n",
" gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n",
"\n",
" # Save\n",
" output_path = os.path.join(output_folder, filename)\n",
" cv2.imwrite(output_path, gray_img)\n",
"\n",
"\n",
"grayscale_images(glaucoma_folder, \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Grayscale/Glaucoma\")\n",
"\n",
"grayscale_images(non_glaucoma_folder, \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Grayscale/NonGlaucoma\")\n"
],
"metadata": {
"id": "jo61nmauqDtx"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Treshalding"
],
"metadata": {
"id": "oF3ZcyD-q0CK"
}
},
{
"cell_type": "code",
"source": [
"# Tresholding\n",
"\n",
"# import cv2\n",
"# import os\n",
"\n",
"# Path to the grayscale glaucoma images\n",
"glaucoma_folder = \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Grayscale/Glaucoma\"\n",
"\n",
"# Path to the grayscale non-glaucoma images\n",
"non_glaucoma_folder = \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Grayscale/NonGlaucoma\"\n",
"\n",
"# threshalding funciton\n",
"def threshold_images(input_folder, output_folder, threshold_value=127, max_value=255):\n",
" \n",
" if not os.path.exists(output_folder):\n",
" os.makedirs(output_folder)\n",
"\n",
" # Get the list of image filenames in the input folder\n",
" image_filenames = os.listdir(input_folder)\n",
"\n",
" for filename in image_filenames:\n",
" \n",
" image_path = os.path.join(input_folder, filename)\n",
" img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)\n",
"\n",
" # Thresholding\n",
" _, threshold_img = cv2.threshold(img, threshold_value, max_value, cv2.THRESH_BINARY)\n",
"\n",
" # Save \n",
" output_path = os.path.join(output_folder, filename)\n",
" cv2.imwrite(output_path, threshold_img)\n",
"\n",
"\n",
"threshold_images(glaucoma_folder, \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Thresholded/Glaucoma\")\n",
"\n",
"\n",
"threshold_images(non_glaucoma_folder, \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Thresholded/NonGlaucoma\")\n"
],
"metadata": {
"id": "OItsBlPxqK3V"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Demoising"
],
"metadata": {
"id": "2k4KepOBq2_7"
}
},
{
"cell_type": "code",
"source": [
"# Denoiseing\n",
"# import cv2\n",
"# import os\n",
"\n",
"\n",
"glaucoma_folder = \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Thresholded/Glaucoma\"\n",
"\n",
"\n",
"non_glaucoma_folder = \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Thresholded/NonGlaucoma\"\n",
"\n",
"# noice remove\n",
"def remove_noise(input_folder, output_folder, kernel_size=3):\n",
" \n",
" if not os.path.exists(output_folder):\n",
" os.makedirs(output_folder)\n",
"\n",
" # Get the list of image filenames in the input folder\n",
" image_filenames = os.listdir(input_folder)\n",
"\n",
" for filename in image_filenames:\n",
" # Read the image\n",
" image_path = os.path.join(input_folder, filename)\n",
" img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)\n",
"\n",
" # Remove noise using median filtering\n",
" denoised_img = cv2.medianBlur(img, kernel_size)\n",
"\n",
" # Save\n",
" output_path = os.path.join(output_folder, filename)\n",
" cv2.imwrite(output_path, denoised_img)\n",
"\n",
"\n",
"remove_noise(glaucoma_folder, \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Denoised/Glaucoma\")\n",
"\n",
"\n",
"remove_noise(non_glaucoma_folder, \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Denoised/NonGlaucoma\")\n"
],
"metadata": {
"id": "jHssBYS6qPaH"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**Analizing**"
],
"metadata": {
"id": "4EqkdJavq7MT"
}
},
{
"cell_type": "markdown",
"source": [
"Highlighting"
],
"metadata": {
"id": "8D8NfL9LrBfW"
}
},
{
"cell_type": "code",
"source": [
"# masking - pre\n",
"# Path to the original glaucoma images\n",
"glaucoma_folder = \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Glaucoma\"\n",
"\n",
"# Path to the original non-glaucoma images\n",
"non_glaucoma_folder = \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/NonGlaucoma\"\n",
"\n",
"# Path to the thresholded glaucoma images\n",
"thresholded_glaucoma_folder = \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Thresholded/Glaucoma\"\n",
"\n",
"# Path to the thresholded non-glaucoma images\n",
"thresholded_non_glaucoma_folder = \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Thresholded/NonGlaucoma\"\n",
"\n",
"# analyzing fucntion\n",
"def analyze_images(original_folder, thresholded_folder, output_folder):\n",
" \n",
" if not os.path.exists(output_folder):\n",
" os.makedirs(output_folder)\n",
"\n",
" # Geting original ffile names\n",
" image_filenames = os.listdir(original_folder)\n",
"\n",
" for filename in image_filenames:\n",
" # Reading the original images\n",
" original_image_path = os.path.join(original_folder, filename)\n",
" original_img = cv2.imread(original_image_path)\n",
"\n",
" # Reading the thresholded images\n",
" thresholded_image_path = os.path.join(thresholded_folder, filename)\n",
" thresholded_img = cv2.imread(thresholded_image_path, cv2.IMREAD_GRAYSCALE)\n",
"\n",
" # Find contours in the thresholded image\n",
" contours, _ = cv2.findContours(thresholded_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
"\n",
" if len(contours) > 0:\n",
" # ganna puluwan lokuma area eka gannwa\n",
" largest_contour = max(contours, key=cv2.contourArea)\n",
"\n",
" # Draw a circle around the optic nerve head\n",
" cv2.drawContours(original_img, [largest_contour], -1, (0, 255, 0), 2)\n",
"\n",
" # Save\n",
" output_path = os.path.join(output_folder, filename)\n",
" cv2.imwrite(output_path, original_img)\n",
"# out put glucoma\n",
"analyze_images(glaucoma_folder, thresholded_glaucoma_folder, \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Analyzed/Glaucoma\")\n",
"\n",
"# output non-gocoma\n",
"analyze_images(non_glaucoma_folder, thresholded_non_glaucoma_folder, \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Analyzed/NonGlaucoma\")\n"
],
"metadata": {
"id": "vg-FUEGoqTMc"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Resizing"
],
"metadata": {
"id": "iK1d5DX6rpZa"
}
},
{
"cell_type": "code",
"source": [
"# import cv2\n",
"# import os\n",
"\n",
"# glaucoma_folder = \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Analyzed/Glaucoma\"\n",
"# non_glaucoma_folder = \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Analyzed/NonGlaucoma\"\n",
"\n",
"# def resize_images(input_folder, output_folder, target_size=(256, 256)):\n",
"# if not os.path.exists(output_folder):\n",
"# os.makedirs(output_folder)\n",
"# image_filenames = os.listdir(input_folder)\n",
"# for filename in image_filenames:\n",
"# image_path = os.path.join(input_folder, filename)\n",
"# img = cv2.imread(image_path)\n",
"# resized_img = cv2.resize(img, target_size)\n",
"# output_path = os.path.join(output_folder, filename)\n",
"# cv2.imwrite(output_path, resized_img)\n",
"\n",
"# resize_images(glaucoma_folder, \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Resized/Glaucoma\")\n",
"# resize_images(non_glaucoma_folder, \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Resized/NonGlaucoma\")\n"
],
"metadata": {
"id": "ih5KreLIrtDq"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Augmentation"
],
"metadata": {
"id": "ckhE_NVIrvIU"
}
},
{
"cell_type": "code",
"source": [
"# import os\n",
"# from keras.preprocessing.image import ImageDataGenerator\n",
"\n",
"# glaucoma_folder = \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Resized/Glaucoma\"\n",
"# non_glaucoma_folder = \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Resized/NonGlaucoma\"\n",
"\n",
"# def augment_images(input_folder, output_folder, augmentation_parameters):\n",
"# if not os.path.exists(output_folder):\n",
"# os.makedirs(output_folder)\n",
"\n",
"# data_generator = ImageDataGenerator(**augmentation_parameters)\n",
"\n",
"# image_filenames = os.listdir(input_folder)\n",
"\n",
"# for filename in image_filenames:\n",
"# image_path = os.path.join(input_folder, filename)\n",
"# img = cv2.imread(image_path)\n",
"# img = np.expand_dims(img, axis=0)\n",
"# augmented_images = data_generator.flow(img, batch_size=1, save_to_dir=output_folder, save_prefix=\"aug\", save_format=\"jpg\")\n",
"# for i, augmented_image in enumerate(augmented_images):\n",
"# if i >= 5:\n",
"# break\n",
"\n",
"# data_generator.close()\n",
"\n",
"# augmentation_parameters = {\n",
"# 'rotation_range': 10,\n",
"# 'width_shift_range': 0.1,\n",
"# 'height_shift_range': 0.1,\n",
"# 'shear_range': 0.2,\n",
"# 'zoom_range': 0.2,\n",
"# 'horizontal_flip': True,\n",
"# 'vertical_flip': True\n",
"# }\n",
"\n",
"# augment_images(glaucoma_folder, \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Augmented/Glaucoma\", augmentation_parameters)\n",
"\n",
"# augment_images(non_glaucoma_folder, \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Augmented/NonGlaucoma\", augmentation_parameters)\n"
],
"metadata": {
"id": "LjKrA-yprxN0"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Augmentation and resizing"
],
"metadata": {
"id": "5idbO770rIV8"
}
},
{
"cell_type": "code",
"source": [
"# augmentaion and resizing\n",
"\n",
"import os\n",
"import cv2\n",
"import numpy as np\n",
"from keras.preprocessing.image import ImageDataGenerator\n",
"\n",
"# Path to the analyzed glaucoma images\n",
"glaucoma_folder = \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Analyzed/Glaucoma\"\n",
"\n",
"# Path to the analyzed non-glaucoma images\n",
"non_glaucoma_folder = \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Analyzed/NonGlaucoma\"\n",
"\n",
"\n",
"def resize_and_augment_images(input_folder, output_folder, augmentation_parameters, target_size=(256, 256)):\n",
" \n",
" if not os.path.exists(output_folder):\n",
" os.makedirs(output_folder)\n",
"\n",
" # Createing ImageDataGenerator\n",
" data_generator = ImageDataGenerator(**augmentation_parameters)\n",
"\n",
" # name list\n",
" image_filenames = os.listdir(input_folder)\n",
"\n",
" for filename in image_filenames:\n",
" # Read the image\n",
" image_path = os.path.join(input_folder, filename)\n",
" img = cv2.imread(image_path)\n",
"\n",
" # Resize the image\n",
" resized_img = cv2.resize(img, target_size)\n",
"\n",
" # expanding image to fit to genarator\n",
" resized_img = np.expand_dims(resized_img, axis=0)\n",
"\n",
" # generate augmented images\n",
" augmented_images = data_generator.flow(resized_img, batch_size=1, save_to_dir=output_folder, save_prefix=\"aug\", save_format=\"jpg\")\n",
"\n",
" # generate and save augmented images\n",
" for i, augmented_image in enumerate(augmented_images):\n",
" if i >= 5: # Generate 5 augmented images per input image\n",
" break\n",
"\n",
"# augmentation parameters\n",
"augmentation_parameters = {\n",
" 'rotation_range': 10,\n",
" 'width_shift_range': 0.1,\n",
" 'height_shift_range': 0.1,\n",
" 'shear_range': 0.2,\n",
" 'zoom_range': 0.2,\n",
" 'horizontal_flip': True,\n",
" 'vertical_flip': True\n",
"}\n",
"\n",
"\n",
"resize_and_augment_images(glaucoma_folder, \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Resized_Augmented/Glaucoma\", augmentation_parameters)\n",
"\n",
"\n",
"resize_and_augment_images(non_glaucoma_folder, \"/content/drive/MyDrive/Colab_files/KaggelDataSet1/Resized_Augmented/NonGlaucoma\", augmentation_parameters)\n",
"\n"
],
"metadata": {
"id": "LdBdEzHJqX3Z"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**Model Building**"
],
"metadata": {
"id": "4FecUyrqrN4q"
}
},
{
"cell_type": "markdown",
"source": [
"Appling U-net model with normalization"
],
"metadata": {
"id": "2E68zneqrRwO"
}
},
{
"cell_type": "code",
"source": [
"# new model \n",
"\n",
"import os\n",
"import cv2\n",
"import numpy as np\n",
"from keras.models import Model\n",
"from keras.layers import Input, Conv2D, MaxPooling2D, Dropout, UpSampling2D, concatenate, Flatten, Dense\n",
"from keras.optimizers import Adam\n",
"import tensorflow as tf\n",
"\n",
"def load_and_normalize_images(input_folder):\n",
" image_filenames = os.listdir(input_folder)\n",
" normalized_images = []\n",
" for filename in image_filenames:\n",
" image_path = os.path.join(input_folder, filename)\n",
" img = cv2.imread(image_path)\n",
" normalized_img = img / 255.0\n",
" normalized_images.append(normalized_img)\n",
" normalized_images = np.array(normalized_images)\n",
" return normalized_images\n",
"\n",
"glaucoma_folder = \"/content/drive/MyDrive/Colab_files/tepmT/Resized_Augmented/Glaucoma\"\n",
"non_glaucoma_folder = \"/content/drive/MyDrive/Colab_files/tepmT/Resized_Augmented/NonGlaucoma\"\n",
"glaucoma_images = load_and_normalize_images(glaucoma_folder)\n",
"non_glaucoma_images = load_and_normalize_images(non_glaucoma_folder)\n",
"all_images = np.concatenate((glaucoma_images, non_glaucoma_images))\n",
"glaucoma_labels = np.ones((glaucoma_images.shape[0],), dtype=int)\n",
"non_glaucoma_labels = np.zeros((non_glaucoma_images.shape[0],), dtype=int)\n",
"all_labels = np.concatenate((glaucoma_labels, non_glaucoma_labels))\n",
"random_indices = np.random.permutation(all_images.shape[0])\n",
"all_images = all_images[random_indices]\n",
"all_labels = all_labels[random_indices]\n",
"\n",
"def unet_model(input_shape):\n",
" inputs = Input(input_shape)\n",
" conv1 = Conv2D(64, 3, activation='relu', padding='same')(inputs)\n",
" conv1 = Conv2D(64, 3, activation='relu', padding='same')(conv1)\n",
" pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)\n",
" conv2 = Conv2D(128, 3, activation='relu', padding='same')(pool1)\n",
" conv2 = Conv2D(128, 3, activation='relu', padding='same')(conv2)\n",
" pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)\n",
" conv3 = Conv2D(256, 3, activation='relu', padding='same')(pool2)\n",
" conv3 = Conv2D(256, 3, activation='relu', padding='same')(conv3)\n",
" up1 = UpSampling2D(size=(2, 2))(conv3)\n",
" up1 = Conv2D(128, 2, activation='relu', padding='same')(up1)\n",
" merge1 = concatenate([conv2, up1], axis=3)\n",
" conv4 = Conv2D(128, 3, activation='relu', padding='same')(merge1)\n",
" conv4 = Conv2D(128, 3, activation='relu', padding='same')(conv4)\n",
" up2 = UpSampling2D(size=(2, 2))(conv4)\n",
" up2 = Conv2D(64, 2, activation='relu', padding='same')(up2)\n",
" merge2 = concatenate([conv1, up2], axis=3)\n",
" conv5 = Conv2D(64, 3, activation='relu', padding='same')(merge2)\n",
" conv5 = Conv2D(64, 3, activation='relu', padding='same')(conv5)\n",
" flatten = Flatten()(conv5)\n",
" output = Dense(1, activation='sigmoid')(flatten)\n",
" model = Model(inputs=inputs, outputs=output)\n",
" return model\n",
"\n",
"input_shape = all_images[0].shape\n",
"model = unet_model(input_shape)\n",
"model.compile(optimizer=Adam(lr=1e-4), loss='binary_crossentropy', metrics=['accuracy'])\n",
"\n",
"with tf.device('/GPU:0'):\n",
" model.fit(all_images, all_labels, batch_size=32, epochs=10, validation_split=0.2)\n",
"\n",
"\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Plot accuracy curve\n",
"plt.plot(model.history.history['accuracy'])\n",
"plt.plot(model.history.history['val_accuracy'])\n",
"plt.title('Model Accuracy')\n",
"plt.xlabel('Epoch')\n",
"plt.ylabel('Accuracy')\n",
"plt.legend(['train', 'val'], loc='upper left')\n",
"plt.show()\n",
"\n",
"plt.plot(model.history.history['loss'])\n",
"plt.plot(model.history.history['val_loss'])\n",
"plt.title('Model Loss')\n",
"plt.xlabel('Epoch')\n",
"plt.ylabel('Loss')\n",
"plt.legend(['train', 'val'], loc='upper left')\n",
"plt.show()"
],
"metadata": {
"id": "DUNO1LxiLbUo"
},
"execution_count": null,
"outputs": []
}
]
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment