{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":9894932,"sourceType":"datasetVersion","datasetId":6077592},{"sourceId":9901558,"sourceType":"datasetVersion","datasetId":6082500}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Import necessary libraries\nimport os\nimport pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom keras.applications.inception_v3 import preprocess_input\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.applications import VGG16  # Example: Using VGG16 pretrained model\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout, BatchNormalization\nfrom tensorflow.keras.optimizers import Adam\nfrom sklearn.model_selection import train_test_split\nfrom keras.utils import to_categorical\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Path to the CSV file\ncsv_file = '/kaggle/input/eyepack2nd/Processed_DR_Images02/processed_labels.csv'\n\n# Load the CSV\ndf = pd.read_csv(csv_file)\n\n# Display the first few rows of the CSV\ndf.head()\n\n# Path to the image folder\nimage_folder = '/kaggle/input/eyepack2nd/Processed_DR_Images02'\n\n# Image size\nIMG_SIZE = (512, 512)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nimport os\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import img_to_array\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\n\n# Augmentation function using InceptionV3 methodology\ndef tf_image_loader(out_size,\n                    horizontal_flip=True,\n                    vertical_flip=False,\n                    random_brightness=True,\n                    random_contrast=True,\n                    random_saturation=True,\n                    random_hue=True,\n                    color_mode='rgb',\n                    preproc_func=None,\n                    on_batch=False):\n    def _func(X):\n        with tf.name_scope('image_augmentation'):\n            with tf.name_scope('input'):\n                X = tf.io.decode_jpeg(tf.io.read_file(X), channels=3 if color_mode == 'rgb' else 0)\n                X = tf.image.resize(X, out_size)\n            with tf.name_scope('augmentation'):\n                if horizontal_flip:\n                    X = tf.image.random_flip_left_right(X)\n                if vertical_flip:\n                    X = tf.image.random_flip_up_down(X)\n                if random_brightness:\n                    X = tf.image.random_brightness(X, max_delta=0.1)\n                if random_saturation:\n                    X = tf.image.random_saturation(X, lower=0.75, upper=1.5)\n                if random_hue:\n                    X = tf.image.random_hue(X, max_delta=0.15)\n                if random_contrast:\n                    X = tf.image.random_contrast(X, lower=0.75, upper=1.5)\n                if preproc_func:\n                    X = preproc_func(X)\n                return X\n    if on_batch:\n        def _batch_func(X, y):\n            return tf.map_fn(_func, X), y\n        return _batch_func\n    else:\n        def _all_func(X, y):\n            return _func(X), y\n        return _all_func\n\n\ndef tf_augmentor(out_size,\n                 batch_size=16,\n                 horizontal_flip=True,\n                 vertical_flip=False,\n                 random_brightness=True,\n                 random_contrast=True,\n                 random_saturation=True,\n                 random_hue=True,\n                 color_mode='rgb',\n                 preproc_func=None):\n    \n    def load_ops(image, label):\n        # Decode image\n        image = tf.io.read_file(image)\n        image = tf.image.decode_jpeg(image, channels=3 if color_mode == 'rgb' else 0)\n        \n        # Perform augmentation operations on both image and label\n        image = tf.image.resize(image, out_size)  # Resize image to target size\n        # Apply augmentations: horizontal flip, brightness, contrast, etc.\n        if horizontal_flip:\n            image = tf.image.random_flip_left_right(image)\n        if vertical_flip:\n            image = tf.image.random_flip_up_down(image)\n        if random_brightness:\n            image = tf.image.random_brightness(image, max_delta=0.2)\n        if random_contrast:\n            image = tf.image.random_contrast(image, lower=0.5, upper=1.5)\n        if random_saturation:\n            image = tf.image.random_saturation(image, lower=0.5, upper=1.5)\n        if random_hue:\n            image = tf.image.random_hue(image, max_delta=0.2)\n        \n        # Apply preprocessing function (optional)\n        if preproc_func:\n            image = preproc_func(image)\n        \n        return image, label  # Return augmented image and its label\n\n    return load_ops\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Ensure that image paths are constructed correctly\nimage_paths = [os.path.join(image_folder, img_name + '.jpeg') for img_name in df['image']]\n\n# Check if the files exist\nfor path in image_paths:\n    if not os.path.exists(path):\n        print(f\"File not found: {path}\")\n\nlabels = to_categorical(df['level'], num_classes=5)\n\n# Split data into training and testing sets\nimage_paths_train, image_paths_test, labels_train, labels_test = train_test_split(\n    image_paths, labels, test_size=0.4, random_state=42\n)\n\n# Create tf.data.Dataset objects\ntrain_dataset = tf.data.Dataset.from_tensor_slices((image_paths_train, labels_train))\n\n# Apply the augmentations using the map function on the dataset\ntrain_dataset = train_dataset.map(\n    lambda x, y: tf_augmentor(out_size=(512, 512))(x, y), \n    num_parallel_calls=tf.data.experimental.AUTOTUNE\n)\n\ntrain_dataset = train_dataset.batch(16).prefetch(tf.data.experimental.AUTOTUNE)\n\n# For the test dataset (no augmentation)\ntest_dataset = tf.data.Dataset.from_tensor_slices((image_paths_test, labels_test))\ntest_dataset = test_dataset.map(\n    lambda x, y: tf_augmentor(out_size=(512, 512), random_brightness=False, random_saturation=False, random_hue=False, random_contrast=False)(x, y),\n    num_parallel_calls=tf.data.experimental.AUTOTUNE\n)\ntest_dataset = test_dataset.batch(16).prefetch(tf.data.experimental.AUTOTUNE)\n\n\nearly_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)\nlr_scheduler = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3, verbose=1)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ReduceLROnPlateau\nfrom tensorflow.keras.callbacks import EarlyStopping\n\nearly_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)\n\nlr_scheduler = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3, verbose=1)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load VGG16 model without the top layers\nbase_model = VGG16(weights='imagenet', include_top=False, input_shape=(IMG_SIZE[0], IMG_SIZE[1], 3))\n\n# Freeze the base model layers\nfor layer in base_model.layers:\n    layer.trainable = False\n\n# Add custom layers to improve model performance\nx = base_model.output\nx = GlobalAveragePooling2D()(x)\n\n# Additional fully connected layers for enhanced learning capacity\nx = Dense(256, activation='relu')(x)\nx = BatchNormalization()(x)\nx = Dropout(0.5)(x)\n\n# Fully connected layers\nx = Dense(512, activation='relu')(x)\nx = BatchNormalization()(x)\nx = Dropout(0.4)(x)\n\nx = Dense(256, activation='relu')(x)\nx = BatchNormalization()(x)\nx = Dropout(0.3)(x)\n\n# New additional dense layer\nx = Dense(1024, activation='relu')(x)\nx = BatchNormalization()(x)\nx = Dropout(0.5)(x)\n\n# Output layer for classification (adjust number of units as needed)\noutput_layer = Dense(5, activation='softmax')(x)\n\n# Create the final model\nmodel = Model(inputs=base_model.input, outputs=output_layer)\n\n# Compile the model\nmodel.compile(optimizer=Adam(learning_rate=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Display the model summary\nmodel.summary()\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# This will prepare the datasets with augmentations applied for training and plain resizing for testing.\n\n# Train the model\nhistory = model.fit(\n    train_dataset,\n    epochs=50,\n    validation_data=test_dataset,\n    callbacks=[lr_scheduler, early_stopping]\n)\n\n# Evaluate the model\ntest_loss, test_acc = model.evaluate(test_dataset, verbose=2)\nprint(f\"Test accuracy: {test_acc}\")\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Plot training and validation accuracy\nplt.plot(history.history['accuracy'], label='accuracy')\nplt.plot(history.history['val_accuracy'], label='val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.ylim([0, 1])\nplt.legend(loc='lower right')\nplt.show()\n\n# Plot training and validation loss\nplt.plot(history.history['loss'], label='loss')\nplt.plot(history.history['val_loss'], label='val_loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend(loc='upper right')\nplt.show()\n","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}