{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"colab":{"provenance":[],"gpuType":"T4"},"accelerator":"GPU","kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":13696955,"sourceType":"datasetVersion","datasetId":8712456}],"dockerImageVersionId":31193,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Step 1: Load the labels from the CSV file\nimport pandas as pd\nimport os\nimport numpy as np\nfrom PIL import Image\nfrom tensorflow.keras.utils import to_categorical\nimage_width = 128\nimage_height = 128\ndataset_path = '/kaggle/input/aptos2019-blindness-dataset/'\ntry:\n    labels_df = pd.read_csv(dataset_path + 'train.csv') # Replace with the actual path to your labels CSV file\nexcept FileNotFoundError:\n    print(\"Error: Your labels CSV file not found. Please make sure the path is correct.\")","metadata":{"id":"JdMUob4ectI9","outputId":"7bc85811-5377-47c8-f317-fb2624caa470","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Process training images\ndef preproc_dataset(dataset_path, image_height, image_width):\n  image_list = []\n  y_labels = [] # List to store corresponding labels\n  image_dir = dataset_path + '/train_images'\n  label_dict = labels_df.set_index('id_code')['diagnosis'].to_dict()\n  for filename in os.listdir(image_dir):\n      # Extract image ID from filename (assuming filename format is 'image_id.jpg')\n      image_id = os.path.splitext(filename)[0]\n      if filename.endswith(('.jpg', '.png', '.jpeg')) and image_id in label_dict: # Add more image extensions if needed and check if label exists\n          img_path = os.path.join(image_dir, filename)\n          try:\n              img = Image.open(img_path)\n              img = img.resize((image_width, image_height))\n              img = img - np.mean(img)\n              # TODO : Added this only for alexnet, need to be checked whether needed for resnet and vgg.\n              # img = np.array(img) / 255.0 # Normalize pixel values\n              image_list.append(img)\n              y_labels.append(label_dict[image_id]) # Get the label from the dictionary\n          except Exception as e:\n              print(f\"Error loading image {filename}: {e}\")\n\n  all_images = np.array(image_list)\n  all_labels = np.array(y_labels)\n  all_labels = to_categorical(all_labels) #One hot encoding\n  return all_images, all_labels","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Step 1: Split the dataset into training and testing sets\nfrom sklearn.model_selection import train_test_split\n\nimage_width = 128\nimage_height = 128\n\n# Read and preprocess images.\nall_images, all_labels = preproc_dataset(dataset_path, image_height, image_width)\n\n\n# Split the data (e.g., 80% train, 20% test split)\n# random_state is used for reproducibility\nx_train, x_test, y_train, y_test = train_test_split(\n    all_images, all_labels, test_size=0.2, random_state=42\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# How to do data augmentation using ImageDataGenerator\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Create an ImageDataGenerator with desired augmentation parameters\ndatagen = ImageDataGenerator(\n    rotation_range=20,       # Rotate images by up to 20 degrees\n    width_shift_range=0.1,   # Shift images horizontally by up to 10% of the width\n    height_shift_range=0.1,  # Shift images vertically by up to 10% of the height\n    shear_range=0.1,         # Apply shear transformation\n    zoom_range=0.1,          # Zoom in or out by up to 10%\n    horizontal_flip=True,    # Randomly flip images horizontall\n)\n\n# Fit the data generator on your training data\ndatagen.fit(x_train)\n\n# Now you can use the datagen.flow() method or pass the generator directly to model.fit()\n# Example of using it with model.fit():\n# history = model.fit(datagen.flow(x_train, y_train, batch_size=32),\n#                     steps_per_epoch=len(x_train) // 32, # Or calculate based on your batch size\n#                     epochs=20,\n#                     validation_data=(x_test, y_test))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Step 2: Implement VGG-16 baseline\n\nimport tensorflow as tf\nfrom tensorflow.keras.applications import VGG16\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.optimizers import Adam # Import the Adam optimizer\nfrom sklearn.metrics import classification_report # Import classification_report\nimport numpy as np # Import numpy\n\n# Load the pre-trained VGG16 model, excluding the top classification layer\n# Use include_top=False to remove the ImageNet classification head\n# Use weights='imagenet' to load weights pre-trained on ImageNet\n# Input shape should match your image dimensions (128x128x3 in your case)\nbase_model_vgg = VGG16(weights='imagenet', include_top=False, input_shape=(image_width, image_height, 3)) # Assuming your image size is 128x128x3\n\n# Freeze the weights of the pre-trained layers\nfor layer in base_model_vgg.layers:\n    layer.trainable = False\n\n# Add a new classification head for 5 classes\nlast_layer = Flatten()(base_model_vgg.output)\nlast_layer = Dense(512, activation='relu')(last_layer) # Add a Dense layer before the output layer\nlast_layer = Dense(256, activation='relu')(last_layer) # Add a Dense layer before the output layer\nlast_layer = Dense(5, activation='softmax')(last_layer) # Final Dense layer for 5 classes with softmax activation\n\n# Create the new model\nmodel_vgg = Model(inputs=base_model_vgg.input, outputs=last_layer)\n\n# Compile the model (only the new head's weights will be updated)\n\nmodel_vgg.compile(optimizer='adam', # Use the defined optimizer\n                  loss='categorical_crossentropy',\n                  metrics=['accuracy'])\n\n# Train the VGG model (only the newly added classification head)\n# Use the train_dataset\nhistory_vgg = model_vgg.fit(x_train, y_train, epochs=30, batch_size=128, # Increased batch size to 64\n                                  validation_data=(x_test, y_test)) # Use test_dataset for evaluation\n\n# Evaluate the VGG model on the test data\n# Use the test_dataset\nloss_vgg, accuracy_vgg = model_vgg.evaluate(x_test, y_test, verbose=2)\n\nprint(f\"VGG Test Accuracy: {accuracy_vgg * 100:.2f}%\")\n\n\ny_pred_probs = model_vgg.predict(x_test)\ny_pred = np.argmax(y_pred_probs, axis=1) # Convert probabilities to class predictions\n\n# Convert one-hot encoded y_test back to integer labels for classification_report\ny_test_labels = np.argmax(y_test, axis=1)\n\n# Print classification report\nprint(\"VGG Classification Report:\")\nprint(classification_report(y_test_labels, y_pred))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import InceptionV3\nfrom tensorflow.keras import layers, models\nfrom sklearn.metrics import classification_report # Import classification_report\nimport numpy as np # Import numpy\n\n\nbase_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(128, 128, 3))\nbase_model.trainable = False  # freeze base initially\n\noutput = base_model.output\noutput = layers.GlobalAveragePooling2D()(output)\noutput = layers.Dropout(0.4)(output)\noutput = layers.Dense(150, activation='relu')(output)\noutput = layers.Dropout(0.3)(output)\noutputs = layers.Dense(5, activation='softmax')(output)\n\nmodel_inception = models.Model(inputs=base_model.input, outputs=outputs)\nmodel_inception.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nhistory_inception = model_inception.fit(train_dataset, epochs=20, # Use train_dataset\n                                  validation_data=test_dataset) # Use test_dataset\n\n# Evaluate the Inception model on the test data\n# Use the test_dataset\nloss_inception, accuracy_inception = model_inception.evaluate(test_dataset, verbose=2)\n\nprint(f\"Inception Test Accuracy: {accuracy_inception * 100:.2f}%\")\n\n\ny_pred_probs = model_inception.predict(x_test)\ny_pred = np.argmax(y_pred_probs, axis=1) # Convert probabilities to class predictions\n\n# Convert one-hot encoded y_test back to integer labels for classification_report\ny_test_labels = np.argmax(y_test, axis=1)\n\n# Print classification report\nprint(\"Inception Classification Report:\")\nprint(classification_report(y_test_labels, y_pred))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}