3v324v23 commited on
Commit
a0b53d4
·
2 Parent(s): c1de23d 42d05d9

Merge branch 'main' of https://huggingface.co/spaces/Ci-Dave/DR_Classification

Browse files
{training → Model}/Pretrained_Densenet-121.pth RENAMED
File without changes
pages/Model_Evaluation.py CHANGED
@@ -105,12 +105,12 @@ def load_test_data(csv_path):
105
  def load_model():
106
  model = models.densenet121(pretrained=False)
107
  model.classifier = nn.Linear(model.classifier.in_features, len(class_names))
108
- model.load_state_dict(torch.load(r"training\Pretrained_Densenet-121.pth", map_location=torch.device('cpu')))
109
  model.eval()
110
  return model
111
 
112
  # ---- Main UI Buttons ----
113
- csv_path = r"splits\test_labels.csv"
114
  model = load_model()
115
  test_loader = load_test_data(csv_path)
116
 
 
105
  def load_model():
106
  model = models.densenet121(pretrained=False)
107
  model.classifier = nn.Linear(model.classifier.in_features, len(class_names))
108
+ model.load_state_dict(torch.load(r"D:\DR_Classification\Model\Pretrained_Densenet-121.pth", map_location=torch.device('cpu')))
109
  model.eval()
110
  return model
111
 
112
  # ---- Main UI Buttons ----
113
+ csv_path = r"D:\DR_Classification\dataset\Splitted_data\splits\test_labels.csv"
114
  model = load_model()
115
  test_loader = load_test_data(csv_path)
116
 
pages/Upload_and_Predict.py CHANGED
@@ -57,7 +57,7 @@ class_names = ['No DR', 'Mild', 'Moderate', 'Severe', 'Proliferative DR']
57
 
58
  # Load sample images from CSV with proper label mapping
59
  @st.cache_data
60
- def load_sample_images_from_csv(csv_path=r'D:\DR_Classification\splits\test_labels.csv'):
61
  df = pd.read_csv(csv_path)
62
  samples = defaultdict(list)
63
 
@@ -76,7 +76,7 @@ def load_sample_images_from_csv(csv_path=r'D:\DR_Classification\splits\test_labe
76
  def load_model():
77
  model = models.densenet121(pretrained=False)
78
  model.classifier = torch.nn.Linear(model.classifier.in_features, len(class_names))
79
- model.load_state_dict(torch.load("training/Pretrained_Densenet-121.pth", map_location='cpu'))
80
  model.eval()
81
  return model
82
 
 
57
 
58
  # Load sample images from CSV with proper label mapping
59
  @st.cache_data
60
+ def load_sample_images_from_csv(csv_path=r'D:\DR_Classification\dataset\Splitted_data\splits\test_labels.csv'):
61
  df = pd.read_csv(csv_path)
62
  samples = defaultdict(list)
63
 
 
76
  def load_model():
77
  model = models.densenet121(pretrained=False)
78
  model.classifier = torch.nn.Linear(model.classifier.in_features, len(class_names))
79
+ model.load_state_dict(torch.load("Model/Pretrained_Densenet-121.pth", map_location='cpu'))
80
  model.eval()
81
  return model
82
 
training/training.ipynb CHANGED
@@ -16,7 +16,7 @@
16
  "source": [
17
  "### STEPS for model training\n",
18
  "##### Step 1. Define Preprocessing Function \n",
19
- "###### Median Filtering, CLAHE, Gamma Correction, ESRGAN(optional)\n",
20
  "##### Step 2. Create Custom Dataset for preprocessing (Pytorch doesn't supports Custom Preprocessing)\n",
21
  "##### Step 3. Define Transform (with data augmentation)\n",
22
  "##### Step 4. Create datasets and Dataloaders\n",
@@ -48,26 +48,57 @@
48
  },
49
  {
50
  "cell_type": "code",
51
- "execution_count": 1,
52
  "id": "80aeae51",
53
  "metadata": {},
54
  "outputs": [],
55
  "source": [
 
 
 
56
  "import os\n",
 
 
57
  "import cv2\n",
 
 
58
  "import numpy as np\n",
 
 
59
  "import pandas as pd\n",
 
 
60
  "from PIL import Image\n",
 
 
61
  "from matplotlib import pyplot as plt\n",
 
 
62
  "from sklearn.model_selection import train_test_split\n",
 
 
63
  "import shutil \n",
 
 
64
  "import torch\n",
 
 
65
  "import torch.nn as nn\n",
 
 
66
  "from tqdm import tqdm\n",
 
 
67
  "import torch.optim as optim\n",
 
 
68
  "from torch.optim.lr_scheduler import StepLR\n",
 
 
69
  "from torch.utils.data import Dataset, DataLoader\n",
70
- "from torchvision import models, transforms"
 
 
71
  ]
72
  },
73
  {
@@ -80,7 +111,7 @@
80
  },
81
  {
82
  "cell_type": "code",
83
- "execution_count": 2,
84
  "id": "99b3890e",
85
  "metadata": {},
86
  "outputs": [
@@ -96,55 +127,71 @@
96
  ],
97
  "source": [
98
  "# Load CSV\n",
99
- "df = pd.read_csv(\"D:\\\\DR_Classification\\\\dataset\\\\DR_grading.csv\")\n",
100
- "df['image_path'] = df['id_code'].apply(lambda x: os.path.join(\"D:\\\\DR_Classification\\\\dataset\\\\images\", x))\n",
 
 
 
 
 
101
  "df['label'] = df['diagnosis']\n",
102
  "\n",
103
- "# Create output directories\n",
104
- "output_root = \"D:\\\\DR_Classification\\\\splits\"\n",
105
- "os.makedirs(os.path.join(output_root, \"train\"), exist_ok=True)\n",
106
- "os.makedirs(os.path.join(output_root, \"test\"), exist_ok=True)\n",
107
  "\n",
108
- "# Split: train vs test\n",
 
 
 
109
  "train_df, test_df = train_test_split(df, test_size=0.3, stratify=df['label'], random_state=42)\n",
110
  "\n",
111
- "# Function to copy images and save CSV\n",
112
  "def save_split(df_split, split_name):\n",
 
113
  " split_folder = os.path.join(output_root, split_name)\n",
114
  " new_paths = []\n",
115
  "\n",
116
- " # Check if the CSV for the current split already exists\n",
117
  " csv_path = os.path.join(output_root, f\"{split_name}_labels.csv\")\n",
 
 
118
  " if os.path.exists(csv_path):\n",
119
  " print(f\"{split_name}_labels.csv already exists. Skipping this split.\")\n",
120
- " return # Skip processing if the CSV file exists\n",
121
  "\n",
122
- " # Proceed with copying images and saving the new CSV if the CSV does not exist\n",
123
  " for _, row in df_split.iterrows():\n",
124
- " src = row['image_path']\n",
125
- " dst = os.path.join(split_folder, os.path.basename(src))\n",
126
  "\n",
 
127
  " if os.path.exists(src):\n",
128
  " shutil.copy(src, dst)\n",
129
- " new_paths.append(dst)\n",
130
  " else:\n",
131
- " print(f\"Warning: Missing image file {src}\")\n",
132
  "\n",
 
133
  " df_split = df_split.copy()\n",
134
  " df_split['new_path'] = new_paths\n",
 
 
135
  " df_split[['id_code', 'label', 'new_path']].to_csv(csv_path, index=False)\n",
136
  " print(f\"{split_name}_labels.csv saved successfully!\")\n",
137
  "\n",
138
- "# Save each split\n",
139
  "save_split(train_df, \"train\")\n",
140
  "save_split(test_df, \"test\")\n",
141
  "\n",
 
142
  "print(\"Splits and CSVs checked and saved successfully!\")"
143
  ]
144
  },
145
  {
146
  "cell_type": "code",
147
- "execution_count": 3,
148
  "id": "d6bda349",
149
  "metadata": {},
150
  "outputs": [
@@ -162,7 +209,7 @@
162
  }
163
  ],
164
  "source": [
165
- "print(df['image_path'].head())"
166
  ]
167
  },
168
  {
@@ -214,21 +261,21 @@
214
  },
215
  {
216
  "cell_type": "code",
217
- "execution_count": 15,
218
  "id": "3264f03c",
219
  "metadata": {},
220
  "outputs": [],
221
  "source": [
222
  "# Load the split CSVs\n",
223
- "train_df = pd.read_csv(\"D:/DR_Classification/splits/train_labels.csv\")\n",
224
- "test_df = pd.read_csv(\"D:/DR_Classification/splits/test_labels.csv\")\n",
225
  "\n",
226
  "# Extract paths and labels\n",
227
  "train_paths = train_df['new_path'].tolist()\n",
228
  "train_labels = train_df['label'].tolist()\n",
229
  "\n",
230
- "test_paths = test_df['new_path'].tolist()\n",
231
- "test_labels = test_df['label'].tolist()"
232
  ]
233
  },
234
  {
@@ -241,28 +288,56 @@
241
  },
242
  {
243
  "cell_type": "code",
244
- "execution_count": 16,
245
  "id": "0b0e9212",
246
  "metadata": {},
247
  "outputs": [],
248
  "source": [
 
249
  "def apply_median_filter(image):\n",
 
250
  " return cv2.medianBlur(image, 3)\n",
251
  "\n",
 
 
252
  "def apply_clahe(image):\n",
 
253
  " lab = cv2.cvtColor(image, cv2.COLOR_RGB2LAB)\n",
 
 
254
  " l, a, b = cv2.split(lab)\n",
 
 
255
  " clahe = cv2.createCLAHE(clipLimit=2.0)\n",
 
 
256
  " cl = clahe.apply(l)\n",
 
 
257
  " merged = cv2.merge((cl, a, b))\n",
 
 
258
  " return cv2.cvtColor(merged, cv2.COLOR_LAB2RGB)\n",
259
  "\n",
 
 
260
  "def apply_gamma_correction(image, gamma=1.2):\n",
 
261
  " invGamma = 1.0 / gamma\n",
 
 
 
262
  " table = np.array([(i / 255.0) ** invGamma * 255 for i in np.arange(0, 256)]).astype(\"uint8\")\n",
 
 
263
  " return cv2.LUT(image, table)\n",
264
  "\n",
 
 
265
  "def apply_gaussian_filter(image, kernel_size=(5, 5), sigma=1.0):\n",
 
 
 
266
  " return cv2.GaussianBlur(image, kernel_size, sigma)\n"
267
  ]
268
  },
@@ -753,7 +828,7 @@
753
  },
754
  {
755
  "cell_type": "code",
756
- "execution_count": 27,
757
  "id": "2090199a",
758
  "metadata": {},
759
  "outputs": [
@@ -809,8 +884,8 @@
809
  "# 2. Load the model architecture\n",
810
  "# -------------------------------\n",
811
  "model = models.densenet121(pretrained=False) # Use DenseNet-121 architecture\n",
812
- "model.classifier = nn.Linear(model.classifier.in_features, 5) # Adjust for 5 classes in DDR dataset\n",
813
- "model.load_state_dict(torch.load(r\"D:\\DR_Classification\\training\\Pretrained_Densenet-121.pth\", map_location=torch.device('cpu')))\n",
814
  "\n",
815
  "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
816
  "model.to(device)\n",
@@ -997,7 +1072,7 @@
997
  },
998
  {
999
  "cell_type": "code",
1000
- "execution_count": 30,
1001
  "id": "30d1e549",
1002
  "metadata": {},
1003
  "outputs": [
@@ -1013,39 +1088,57 @@
1013
  }
1014
  ],
1015
  "source": [
 
1016
  "from collections import Counter\n",
1017
  "\n",
 
1018
  "correct_per_class = Counter()\n",
 
 
1019
  "total_per_class = Counter()\n",
1020
  "\n",
 
1021
  "with torch.no_grad():\n",
 
1022
  " for inputs, labels in test_loader:\n",
 
1023
  " inputs = inputs.to(device)\n",
1024
  " labels = labels.to(device)\n",
 
 
1025
  " outputs = model(inputs)\n",
 
 
1026
  " _, predicted = torch.max(outputs, 1)\n",
1027
  "\n",
 
1028
  " for label, prediction in zip(labels, predicted):\n",
1029
- " total_per_class[label.item()] += 1\n",
1030
  " if label == prediction:\n",
1031
- " correct_per_class[label.item()] += 1\n",
1032
  "\n",
1033
- "class_acc = {class_names[i]: (correct_per_class[i] / total_per_class[i]) * 100 for i in range(n_classes)}\n",
 
 
 
 
 
1034
  "\n",
1035
- "# Plot\n",
1036
  "plt.figure(figsize=(8, 5))\n",
1037
- "plt.bar(class_acc.keys(), class_acc.values(), color='skyblue')\n",
1038
  "plt.ylabel(\"Accuracy (%)\")\n",
1039
  "plt.title(\"Per-Class Accuracy\")\n",
1040
- "plt.xticks(rotation=45)\n",
1041
- "plt.ylim(0, 100)\n",
1042
  "plt.grid(True)\n",
1043
- "plt.show()\n"
 
1044
  ]
1045
  },
1046
  {
1047
  "cell_type": "code",
1048
- "execution_count": 31,
1049
  "id": "653632e3",
1050
  "metadata": {},
1051
  "outputs": [
@@ -1061,37 +1154,56 @@
1061
  }
1062
  ],
1063
  "source": [
 
1064
  "def show_misclassified(model, test_loader, class_names, device='cpu', max_images=6):\n",
 
1065
  " model.eval()\n",
 
 
1066
  " misclassified = []\n",
1067
  "\n",
 
1068
  " with torch.no_grad():\n",
 
1069
  " for inputs, labels in test_loader:\n",
 
1070
  " inputs, labels = inputs.to(device), labels.to(device)\n",
 
 
1071
  " outputs = model(inputs)\n",
1072
  " _, preds = torch.max(outputs, 1)\n",
1073
  "\n",
 
1074
  " for img, true_label, pred_label in zip(inputs, labels, preds):\n",
1075
  " if true_label != pred_label:\n",
 
1076
  " misclassified.append((img.cpu(), true_label.item(), pred_label.item()))\n",
 
 
1077
  " if len(misclassified) >= max_images:\n",
1078
  " break\n",
1079
  " if len(misclassified) >= max_images:\n",
1080
  " break\n",
1081
  "\n",
 
1082
  " plt.figure(figsize=(12, 8))\n",
1083
  " for i, (img, true_label, pred_label) in enumerate(misclassified):\n",
1084
- " plt.subplot(2, 3, i+1)\n",
1085
- " img = img.permute(1, 2, 0).numpy()\n",
1086
- " img = (img * [0.229, 0.224, 0.225]) + [0.485, 0.456, 0.406] # Unnormalize\n",
1087
- " img = np.clip(img, 0, 1)\n",
 
 
 
 
1088
  " plt.imshow(img)\n",
1089
  " plt.title(f'True: {class_names[true_label]}\\nPred: {class_names[pred_label]}')\n",
1090
- " plt.axis('off')\n",
 
1091
  " plt.tight_layout()\n",
1092
  " plt.show()\n",
1093
  "\n",
1094
- "# Call it:\n",
1095
  "show_misclassified(model, test_loader, class_names, device=device)\n"
1096
  ]
1097
  },
@@ -1105,7 +1217,7 @@
1105
  },
1106
  {
1107
  "cell_type": "code",
1108
- "execution_count": 31,
1109
  "id": "45a03f67",
1110
  "metadata": {},
1111
  "outputs": [
@@ -1207,47 +1319,56 @@
1207
  }
1208
  ],
1209
  "source": [
 
1210
  "def visualize_predictions(model, dataloader, class_names, device='cuda', num_images=6):\n",
1211
- " model.eval()\n",
1212
- " images_shown = 0\n",
1213
- " correct_preds = 0\n",
1214
  "\n",
1215
- " with torch.no_grad():\n",
1216
  " for inputs, labels in dataloader:\n",
1217
- " inputs = inputs.to(device)\n",
1218
  " labels = labels.to(device)\n",
1219
- " outputs = model(inputs)\n",
1220
- " _, preds = torch.max(outputs, 1)\n",
1221
  "\n",
 
 
 
 
1222
  " inputs = inputs.cpu()\n",
1223
  " labels = labels.cpu()\n",
1224
  " preds = preds.cpu()\n",
1225
  "\n",
 
1226
  " for i in range(inputs.size(0)):\n",
1227
  " if images_shown >= num_images:\n",
 
1228
  " print(f\"\\n�� Total Correct: {correct_preds}/{num_images} — Accuracy: {(correct_preds / num_images) * 100:.2f}%\")\n",
1229
  " return\n",
1230
  "\n",
1231
- " img = inputs[i].permute(1, 2, 0).numpy()\n",
1232
- " img = (img - img.min()) / (img.max() - img.min()) # normalize for display\n",
 
1233
  "\n",
 
1234
  " is_correct = preds[i] == labels[i]\n",
1235
  " correctness = \"✔️ Correct\" if is_correct else \"❌ Wrong\"\n",
1236
  " if is_correct:\n",
1237
  " correct_preds += 1\n",
1238
  "\n",
 
1239
  " plt.imshow(img)\n",
1240
  " plt.title(f\"True: {class_names[labels[i]]}\\nPred: {class_names[preds[i]]} | {correctness}\")\n",
1241
  " plt.axis(\"off\")\n",
1242
  " plt.show()\n",
1243
  "\n",
1244
- " images_shown += 1\n",
1245
  "\n",
 
1246
  " print(f\"\\n✅ Total Correct: {correct_preds}/{num_images} — Accuracy: {(correct_preds / num_images) * 100:.2f}%\")\n",
1247
  "\n",
1248
  "# Example usage\n",
1249
  "class_names = ['No DR', 'Mild', 'Moderate', 'Severe', 'Proliferative DR']\n",
1250
- "visualize_predictions(model, test_loader, class_names, device=device, num_images=8)"
1251
  ]
1252
  },
1253
  {
@@ -1279,7 +1400,7 @@
1279
  },
1280
  {
1281
  "cell_type": "code",
1282
- "execution_count": 48,
1283
  "id": "e1133810",
1284
  "metadata": {},
1285
  "outputs": [
@@ -1347,7 +1468,7 @@
1347
  "\n",
1348
  "# Example usage:\n",
1349
  "class_names = ['No DR', 'Mild', 'Moderate', 'Severe', 'Proliferative DR'] # Modify as per your dataset\n",
1350
- "image_path = r'D:\\DR_Classification\\splits\\train\\007-0025-000.jpg' # Replace with your image path\n",
1351
  "predicted_class, confidence_percentage = predict_image(model, image_path, class_names, device='cpu')\n",
1352
  "\n",
1353
  "print(f\"Predicted Class: {predicted_class}\")\n",
 
16
  "source": [
17
  "### STEPS for model training\n",
18
  "##### Step 1. Define Preprocessing Function \n",
19
+ "###### Median Filtering, CLAHE, Gamma Correction\n",
20
  "##### Step 2. Create Custom Dataset for preprocessing (Pytorch doesn't supports Custom Preprocessing)\n",
21
  "##### Step 3. Define Transform (with data augmentation)\n",
22
  "##### Step 4. Create datasets and Dataloaders\n",
 
48
  },
49
  {
50
  "cell_type": "code",
51
+ "execution_count": null,
52
  "id": "80aeae51",
53
  "metadata": {},
54
  "outputs": [],
55
  "source": [
56
+ "# Importing necessary libraries for various operations\n",
57
+ "\n",
58
+ "# OS module helps in interacting with the operating system (file and directory handling)\n",
59
  "import os\n",
60
+ "\n",
61
+ "# OpenCV library for image processing\n",
62
  "import cv2\n",
63
+ "\n",
64
+ "# NumPy for numerical operations and handling arrays\n",
65
  "import numpy as np\n",
66
+ "\n",
67
+ "# Pandas for working with structured data like CSV or Excel (DataFrames)\n",
68
  "import pandas as pd\n",
69
+ "\n",
70
+ "# PIL (Python Imaging Library) for handling images in Python\n",
71
  "from PIL import Image\n",
72
+ "\n",
73
+ "# Matplotlib for data visualization (plotting graphs and images)\n",
74
  "from matplotlib import pyplot as plt\n",
75
+ "\n",
76
+ "# Function to split dataset into training and testing sets\n",
77
  "from sklearn.model_selection import train_test_split\n",
78
+ "\n",
79
+ "# Shutil helps with file operations like copying and moving files\n",
80
  "import shutil \n",
81
+ "\n",
82
+ "# PyTorch main library - used for building and training deep learning models\n",
83
  "import torch\n",
84
+ "\n",
85
+ "# PyTorch neural network module - contains layers and loss functions\n",
86
  "import torch.nn as nn\n",
87
+ "\n",
88
+ "# TQDM for showing progress bars during training or data processing loops\n",
89
  "from tqdm import tqdm\n",
90
+ "\n",
91
+ "# PyTorch's optimizer module - includes optimizers like Adam, SGD, etc.\n",
92
  "import torch.optim as optim\n",
93
+ "\n",
94
+ "# Learning rate scheduler - automatically adjusts the learning rate during training\n",
95
  "from torch.optim.lr_scheduler import StepLR\n",
96
+ "\n",
97
+ "# Custom Dataset and DataLoader utilities for batch processing\n",
98
  "from torch.utils.data import Dataset, DataLoader\n",
99
+ "\n",
100
+ "# Pre-trained models and image transformation tools from torchvision\n",
101
+ "from torchvision import models, transforms\n"
102
  ]
103
  },
104
  {
 
111
  },
112
  {
113
  "cell_type": "code",
114
+ "execution_count": null,
115
  "id": "99b3890e",
116
  "metadata": {},
117
  "outputs": [
 
127
  ],
128
  "source": [
129
  "# Load CSV\n",
130
+ "# Load the CSV file that contains image IDs and their corresponding labels (diagnosis)\n",
131
+ "df = pd.read_csv(\"D:\\\\DR_Classification\\\\dataset\\\\DR_grading.csv\") #change path to your csv file\n",
132
+ "\n",
133
+ "# Add a new column 'image_path' which holds the full path to each image file\n",
134
+ "df['image_path'] = df['id_code'].apply(lambda x: os.path.join(\"D:\\\\DR_Classification\\\\dataset\\\\images\", x)) #change path to your image folder\n",
135
+ "\n",
136
+ "# Rename the 'diagnosis' column to 'label' to match deep learning convention (features vs label)\n",
137
  "df['label'] = df['diagnosis']\n",
138
  "\n",
139
+ "# Create output directories for storing train and test images\n",
140
+ "output_root = \"D:\\\\DR_Classification\\\\dataset\\\\Splitted_data\\\\splits\" #change path to your output folder\n",
141
+ "os.makedirs(os.path.join(output_root, \"train\"), exist_ok=True) # Creates 'train' folder if not existing\n",
142
+ "os.makedirs(os.path.join(output_root, \"test\"), exist_ok=True) # Creates 'test' folder if not existing\n",
143
  "\n",
144
+ "# Split the dataset into training and testing sets\n",
145
+ "# test_size=0.3 means 30% of the data will go to the test set\n",
146
+ "# stratify ensures each class is proportionally represented in both train and test\n",
147
+ "# random_state ensures reproducibility of the split\n",
148
  "train_df, test_df = train_test_split(df, test_size=0.3, stratify=df['label'], random_state=42)\n",
149
  "\n",
150
+ "# Define a function to copy the images to their respective folders and save split CSV files\n",
151
  "def save_split(df_split, split_name):\n",
152
+ " # Define the folder where the images will be saved (either train or test)\n",
153
  " split_folder = os.path.join(output_root, split_name)\n",
154
  " new_paths = []\n",
155
  "\n",
156
+ " # Define the path for the split's CSV file (e.g., train_labels.csv)\n",
157
  " csv_path = os.path.join(output_root, f\"{split_name}_labels.csv\")\n",
158
+ " \n",
159
+ " # If the CSV already exists, skip processing to avoid duplication\n",
160
  " if os.path.exists(csv_path):\n",
161
  " print(f\"{split_name}_labels.csv already exists. Skipping this split.\")\n",
162
+ " return\n",
163
  "\n",
164
+ " # Loop through each row in the split DataFrame\n",
165
  " for _, row in df_split.iterrows():\n",
166
+ " src = row['image_path'] # original image path\n",
167
+ " dst = os.path.join(split_folder, os.path.basename(src)) # destination path in train/test folder\n",
168
  "\n",
169
+ " # Copy the image if it exists\n",
170
  " if os.path.exists(src):\n",
171
  " shutil.copy(src, dst)\n",
172
+ " new_paths.append(dst) # store the new path for saving in CSV\n",
173
  " else:\n",
174
+ " print(f\"Warning: Missing image file {src}\") # error message if file not found\n",
175
  "\n",
176
+ " # Create a copy of the split DataFrame and add the new image paths\n",
177
  " df_split = df_split.copy()\n",
178
  " df_split['new_path'] = new_paths\n",
179
+ "\n",
180
+ " # Save relevant columns into a new CSV file (id, label, new image path)\n",
181
  " df_split[['id_code', 'label', 'new_path']].to_csv(csv_path, index=False)\n",
182
  " print(f\"{split_name}_labels.csv saved successfully!\")\n",
183
  "\n",
184
+ "# Save the training and testing splits\n",
185
  "save_split(train_df, \"train\")\n",
186
  "save_split(test_df, \"test\")\n",
187
  "\n",
188
+ "# Final print to confirm everything worked\n",
189
  "print(\"Splits and CSVs checked and saved successfully!\")"
190
  ]
191
  },
192
  {
193
  "cell_type": "code",
194
+ "execution_count": null,
195
  "id": "d6bda349",
196
  "metadata": {},
197
  "outputs": [
 
209
  }
210
  ],
211
  "source": [
212
+ "print(df['image_path'].head()) # Display the first few rows of the DataFrame to verify paths"
213
  ]
214
  },
215
  {
 
261
  },
262
  {
263
  "cell_type": "code",
264
+ "execution_count": null,
265
  "id": "3264f03c",
266
  "metadata": {},
267
  "outputs": [],
268
  "source": [
269
  "# Load the split CSVs\n",
270
+ "train_df = pd.read_csv(\"D:/DR_Classification/dataset/Splitted_data/splits/train_labels.csv\") #change path to your csv file\n",
271
+ "test_df = pd.read_csv(\"D:/DR_Classification/dataset/Splitted_data/splits/test_labels.csv\") \n",
272
  "\n",
273
  "# Extract paths and labels\n",
274
  "train_paths = train_df['new_path'].tolist()\n",
275
  "train_labels = train_df['label'].tolist()\n",
276
  "\n",
277
+ "test_paths = test_df['new_path'].tolist() \n",
278
+ "test_labels = test_df['label'].tolist() "
279
  ]
280
  },
281
  {
 
288
  },
289
  {
290
  "cell_type": "code",
291
+ "execution_count": null,
292
  "id": "0b0e9212",
293
  "metadata": {},
294
  "outputs": [],
295
  "source": [
296
+ "# Applies a Median Filter to remove noise (especially salt-and-pepper noise)\n",
297
  "def apply_median_filter(image):\n",
298
+ " # cv2.medianBlur applies a median filter with a 3x3 kernel\n",
299
  " return cv2.medianBlur(image, 3)\n",
300
  "\n",
301
+ "\n",
302
+ "# Applies CLAHE (Contrast Limited Adaptive Histogram Equalization) to enhance local contrast\n",
303
  "def apply_clahe(image):\n",
304
+ " # Convert the image from RGB color space to LAB color space\n",
305
  " lab = cv2.cvtColor(image, cv2.COLOR_RGB2LAB)\n",
306
+ " \n",
307
+ " # Split LAB into individual channels: L (lightness), A (green–red), B (blue–yellow)\n",
308
  " l, a, b = cv2.split(lab)\n",
309
+ " \n",
310
+ " # Create a CLAHE object with clipLimit=2.0 (limits contrast amplification)\n",
311
  " clahe = cv2.createCLAHE(clipLimit=2.0)\n",
312
+ " \n",
313
+ " # Apply CLAHE to the L channel (intensity)\n",
314
  " cl = clahe.apply(l)\n",
315
+ " \n",
316
+ " # Merge the enhanced L channel with the original A and B channels\n",
317
  " merged = cv2.merge((cl, a, b))\n",
318
+ " \n",
319
+ " # Convert the image back from LAB to RGB color space\n",
320
  " return cv2.cvtColor(merged, cv2.COLOR_LAB2RGB)\n",
321
  "\n",
322
+ "\n",
323
+ "# Applies Gamma Correction to adjust image brightness\n",
324
  "def apply_gamma_correction(image, gamma=1.2):\n",
325
+ " # Calculate the inverse of gamma\n",
326
  " invGamma = 1.0 / gamma\n",
327
+ "\n",
328
+ " # Create a lookup table for all pixel values (0–255)\n",
329
+ " # Each pixel is transformed based on gamma value\n",
330
  " table = np.array([(i / 255.0) ** invGamma * 255 for i in np.arange(0, 256)]).astype(\"uint8\")\n",
331
+ " \n",
332
+ " # Apply the gamma correction using the lookup table\n",
333
  " return cv2.LUT(image, table)\n",
334
  "\n",
335
+ "\n",
336
+ "# Applies a Gaussian Blur to smooth the image and reduce noise/detail\n",
337
  "def apply_gaussian_filter(image, kernel_size=(5, 5), sigma=1.0):\n",
338
+ " # cv2.GaussianBlur uses a Gaussian kernel to blur the image\n",
339
+ " # kernel_size defines the width and height of the filter\n",
340
+ " # sigma defines how much to blur (higher = more blur)\n",
341
  " return cv2.GaussianBlur(image, kernel_size, sigma)\n"
342
  ]
343
  },
 
828
  },
829
  {
830
  "cell_type": "code",
831
+ "execution_count": null,
832
  "id": "2090199a",
833
  "metadata": {},
834
  "outputs": [
 
884
  "# 2. Load the model architecture\n",
885
  "# -------------------------------\n",
886
  "model = models.densenet121(pretrained=False) # Use DenseNet-121 architecture\n",
887
+ "model.classifier = nn.Linear(model.classifier.in_features, 5) # Adjust for 5 classes in DDR dataset \n",
888
+ "model.load_state_dict(torch.load(r\"D:\\DR_Classification\\Model\\Pretrained_Densenet-121.pth\", map_location=torch.device('cpu'))) # Load the trained model weights\n",
889
  "\n",
890
  "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
891
  "model.to(device)\n",
 
1072
  },
1073
  {
1074
  "cell_type": "code",
1075
+ "execution_count": null,
1076
  "id": "30d1e549",
1077
  "metadata": {},
1078
  "outputs": [
 
1088
  }
1089
  ],
1090
  "source": [
1091
+ "# Import Counter from collections to count occurrences per class\n",
1092
  "from collections import Counter\n",
1093
  "\n",
1094
+ "# Dictionary to track how many predictions were correct per class\n",
1095
  "correct_per_class = Counter()\n",
1096
+ "\n",
1097
+ "# Dictionary to track total number of samples per class\n",
1098
  "total_per_class = Counter()\n",
1099
  "\n",
1100
+ "# Disable gradient calculation for inference (saves memory and computation)\n",
1101
  "with torch.no_grad():\n",
1102
+ " # Loop over the test dataset\n",
1103
  " for inputs, labels in test_loader:\n",
1104
+ " # Move input tensors to the same device as the model (CPU or GPU)\n",
1105
  " inputs = inputs.to(device)\n",
1106
  " labels = labels.to(device)\n",
1107
+ "\n",
1108
+ " # Forward pass: get model predictions\n",
1109
  " outputs = model(inputs)\n",
1110
+ "\n",
1111
+ " # Get the index of the class with the highest score for each sample\n",
1112
  " _, predicted = torch.max(outputs, 1)\n",
1113
  "\n",
1114
+ " # Compare predictions with true labels, update counters\n",
1115
  " for label, prediction in zip(labels, predicted):\n",
1116
+ " total_per_class[label.item()] += 1 # increment total count for true class\n",
1117
  " if label == prediction:\n",
1118
+ " correct_per_class[label.item()] += 1 # increment correct count if prediction is correct\n",
1119
  "\n",
1120
+ "# Compute per-class accuracy: (correct / total) * 100 for each class\n",
1121
+ "# class_names[i] is used to label each class properly\n",
1122
+ "class_acc = {\n",
1123
+ " class_names[i]: (correct_per_class[i] / total_per_class[i]) * 100\n",
1124
+ " for i in range(n_classes)\n",
1125
+ "}\n",
1126
  "\n",
1127
+ "# Visualization: Bar plot of per-class accuracy\n",
1128
  "plt.figure(figsize=(8, 5))\n",
1129
+ "plt.bar(class_acc.keys(), class_acc.values(), color='skyblue') # bar plot with class labels and their accuracy\n",
1130
  "plt.ylabel(\"Accuracy (%)\")\n",
1131
  "plt.title(\"Per-Class Accuracy\")\n",
1132
+ "plt.xticks(rotation=45) # Rotate class names on x-axis for better readability\n",
1133
+ "plt.ylim(0, 100) # Set y-axis range from 0 to 100 percent\n",
1134
  "plt.grid(True)\n",
1135
+ "plt.show()\n",
1136
+ "\n"
1137
  ]
1138
  },
1139
  {
1140
  "cell_type": "code",
1141
+ "execution_count": null,
1142
  "id": "653632e3",
1143
  "metadata": {},
1144
  "outputs": [
 
1154
  }
1155
  ],
1156
  "source": [
1157
+ "# Function to visualize misclassified images from the test set\n",
1158
  "def show_misclassified(model, test_loader, class_names, device='cpu', max_images=6):\n",
1159
+ " # Set model to evaluation mode (turns off dropout, batchnorm updates)\n",
1160
  " model.eval()\n",
1161
+ " \n",
1162
+ " # List to store misclassified images and their labels\n",
1163
  " misclassified = []\n",
1164
  "\n",
1165
+ " # Disable gradient computation for faster inference\n",
1166
  " with torch.no_grad():\n",
1167
+ " # Iterate through the test set\n",
1168
  " for inputs, labels in test_loader:\n",
1169
+ " # Move data to device (GPU or CPU)\n",
1170
  " inputs, labels = inputs.to(device), labels.to(device)\n",
1171
+ " \n",
1172
+ " # Get predictions from the model\n",
1173
  " outputs = model(inputs)\n",
1174
  " _, preds = torch.max(outputs, 1)\n",
1175
  "\n",
1176
+ " # Check for misclassified samples\n",
1177
  " for img, true_label, pred_label in zip(inputs, labels, preds):\n",
1178
  " if true_label != pred_label:\n",
1179
+ " # Save the misclassified image and its true/predicted labels\n",
1180
  " misclassified.append((img.cpu(), true_label.item(), pred_label.item()))\n",
1181
+ " \n",
1182
+ " # Stop once we’ve collected enough misclassified samples\n",
1183
  " if len(misclassified) >= max_images:\n",
1184
  " break\n",
1185
  " if len(misclassified) >= max_images:\n",
1186
  " break\n",
1187
  "\n",
1188
+ " # Plot the misclassified images\n",
1189
  " plt.figure(figsize=(12, 8))\n",
1190
  " for i, (img, true_label, pred_label) in enumerate(misclassified):\n",
1191
+ " plt.subplot(2, 3, i+1) # Plot up to 6 images in a 2x3 grid\n",
1192
+ " \n",
1193
+ " # Convert tensor image to NumPy format and undo normalization\n",
1194
+ " img = img.permute(1, 2, 0).numpy() # Change shape from [C, H, W] to [H, W, C]\n",
1195
+ " img = (img * [0.229, 0.224, 0.225]) + [0.485, 0.456, 0.406] # Undo normalization (ImageNet stats)\n",
1196
+ " img = np.clip(img, 0, 1) # Make sure pixel values are within [0, 1]\n",
1197
+ "\n",
1198
+ " # Display the image\n",
1199
  " plt.imshow(img)\n",
1200
  " plt.title(f'True: {class_names[true_label]}\\nPred: {class_names[pred_label]}')\n",
1201
+ " plt.axis('off') # Hide axis ticks\n",
1202
+ " \n",
1203
  " plt.tight_layout()\n",
1204
  " plt.show()\n",
1205
  "\n",
1206
+ "# Call the function to show misclassified samples\n",
1207
  "show_misclassified(model, test_loader, class_names, device=device)\n"
1208
  ]
1209
  },
 
1217
  },
1218
  {
1219
  "cell_type": "code",
1220
+ "execution_count": null,
1221
  "id": "45a03f67",
1222
  "metadata": {},
1223
  "outputs": [
 
1319
  }
1320
  ],
1321
  "source": [
1322
+ "# Function to visualize a few predictions from the model\n",
1323
  "def visualize_predictions(model, dataloader, class_names, device='cuda', num_images=6):\n",
1324
+ " model.eval() # Set the model to evaluation mode (no dropout, no gradients)\n",
1325
+ " images_shown = 0 # Counter for how many images we've displayed\n",
1326
+ " correct_preds = 0 # Counter for how many predictions were correct\n",
1327
  "\n",
1328
+ " with torch.no_grad(): # No need to calculate gradients during inference\n",
1329
  " for inputs, labels in dataloader:\n",
1330
+ " inputs = inputs.to(device) # Move input batch to GPU/CPU\n",
1331
  " labels = labels.to(device)\n",
 
 
1332
  "\n",
1333
+ " outputs = model(inputs) # Get model predictions\n",
1334
+ " _, preds = torch.max(outputs, 1) # Get predicted class indices\n",
1335
+ "\n",
1336
+ " # Move back to CPU for visualization\n",
1337
  " inputs = inputs.cpu()\n",
1338
  " labels = labels.cpu()\n",
1339
  " preds = preds.cpu()\n",
1340
  "\n",
1341
+ " # Loop through the batch and visualize one image at a time\n",
1342
  " for i in range(inputs.size(0)):\n",
1343
  " if images_shown >= num_images:\n",
1344
+ " # Print final summary and return\n",
1345
  " print(f\"\\n�� Total Correct: {correct_preds}/{num_images} — Accuracy: {(correct_preds / num_images) * 100:.2f}%\")\n",
1346
  " return\n",
1347
  "\n",
1348
+ " # Convert tensor image to NumPy array and normalize values for visualization\n",
1349
+ " img = inputs[i].permute(1, 2, 0).numpy() # [C, H, W] -> [H, W, C]\n",
1350
+ " img = (img - img.min()) / (img.max() - img.min()) # Scale pixel values to [0, 1]\n",
1351
  "\n",
1352
+ " # Determine if the prediction is correct\n",
1353
  " is_correct = preds[i] == labels[i]\n",
1354
  " correctness = \"✔️ Correct\" if is_correct else \"❌ Wrong\"\n",
1355
  " if is_correct:\n",
1356
  " correct_preds += 1\n",
1357
  "\n",
1358
+ " # Display the image with its true and predicted labels\n",
1359
  " plt.imshow(img)\n",
1360
  " plt.title(f\"True: {class_names[labels[i]]}\\nPred: {class_names[preds[i]]} | {correctness}\")\n",
1361
  " plt.axis(\"off\")\n",
1362
  " plt.show()\n",
1363
  "\n",
1364
+ " images_shown += 1 # Increment the counter\n",
1365
  "\n",
1366
+ " # Final accuracy summary if loop finishes naturally\n",
1367
  " print(f\"\\n✅ Total Correct: {correct_preds}/{num_images} — Accuracy: {(correct_preds / num_images) * 100:.2f}%\")\n",
1368
  "\n",
1369
  "# Example usage\n",
1370
  "class_names = ['No DR', 'Mild', 'Moderate', 'Severe', 'Proliferative DR']\n",
1371
+ "visualize_predictions(model, test_loader, class_names, device=device, num_images=8)\n"
1372
  ]
1373
  },
1374
  {
 
1400
  },
1401
  {
1402
  "cell_type": "code",
1403
+ "execution_count": null,
1404
  "id": "e1133810",
1405
  "metadata": {},
1406
  "outputs": [
 
1468
  "\n",
1469
  "# Example usage:\n",
1470
  "class_names = ['No DR', 'Mild', 'Moderate', 'Severe', 'Proliferative DR'] # Modify as per your dataset\n",
1471
+ "image_path = r'D:\\DR_Classification\\dataset\\Splitted_data\\splits\\train\\007-0025-000.jpg' # Replace with your image path\n",
1472
  "predicted_class, confidence_percentage = predict_image(model, image_path, class_names, device='cpu')\n",
1473
  "\n",
1474
  "print(f\"Predicted Class: {predicted_class}\")\n",