This project implements and compares two deep learning architectures (ResNet50 and DenseNet169) for classifying glomeruli images into globally sclerotic and non-globally sclerotic categories.
- Globally Sclerotic: 1,054 images
- Non-Globally Sclerotic: 4,704 images
- Class Ratio: 1:4.46
- Training set: 60%
- Validation set: 20%
- Test set: 20%
| Feature | ResNet50 | DenseNet169 |
|---|---|---|
| Base Layers | 50 | 169 |
| Connection Type | Skip Connections | Dense Connections |
| Parameter Efficiency | Moderate | High |
| Feature Reuse | Through Residuals | Through Dense Connections |
| Trainable Layers | Last 30 unfrozen | Last 50 unfrozen |
model = Sequential([
ResNet50Base,
GlobalAveragePooling2D(),
Dense(512, activation='relu'),
BatchNormalization(),
Dropout(0.4),
Dense(256, activation='relu'),
BatchNormalization(),
Dropout(0.3),
Dense(2, activation='softmax')
])model = Sequential([
DenseNet169Base,
GlobalAveragePooling2D(),
Dense(512, activation='relu'),
LayerNormalization(),
Dense(512, activation='relu', kernel_regularizer=l2(0.01)),
LayerNormalization(),
Dropout(0.5),
Dense(256, activation='relu', kernel_regularizer=l2(0.01)),
LayerNormalization(),
Dropout(0.4),
Dense(2, activation='softmax')
])| Feature | ResNet Implementation | DenseNet Implementation |
|---|---|---|
| Normalization | BatchNormalization | LayerNormalization |
| Dropout Rates | 0.4, 0.3 | 0.5, 0.4 |
| Regularization | None | L2 (0.01) |
| Learning Rate | 1e-4 | 5e-5 |
| Optimizer | Adam | AdamW with weight decay |
| Loss Function | Basic Categorical Crossentropy | Categorical Crossentropy with label smoothing |
| Data Augmentation | Basic | Enhanced with RandomRotation and RandomZoom |
- Simpler training setup
- Fixed learning rate
- Basic metrics (accuracy, AUC)
- Standard optimization
- Advanced training configuration
- Adaptive learning rate with weight decay
- Comprehensive metrics (accuracy, precision, recall, F1, AUC)
- Custom F1 score monitoring
- Label smoothing for better generalization
Input → Conv → BatchNorm → MaxPool → [ResBlock×N] → GlobalAvgPool → Dense → Output
Input → Conv → [DenseBlock → Transition]×N → GlobalAvgPool → Dense → Output
| Metric | Model 1: ResNet | Model 2: DenseNet |
|---|---|---|
| Training Accuracy | 94.53% | 98.18% |
| Precision | 0.8542 | 0.9406 |
| Recall | 0.8241 | 0.9548 |
| Validation Accuracy | 94.18% | 98% |
| Training Time | ~2 hours on M2 Mac | ~1 hours on M2 Mac |
- Clone this repository
- Create a Python virtual environment:
# For macOS/Linux
python3 -m venv venv
source venv/bin/activate
# For Windows
python -m venv venv
.\venv\Scripts\activate- Install all dependencies using the requirements.txt file:
pip install -r requirements.txtfinal_glomeruli/
├── venv/ # Virtual environment directory
├── data/
│ ├── globally_sclerotic_glomeruli/
│ └── non_globally_sclerotic_glomeruli/
│ └── public.csv
├── densenet/
│ ├── densenet_model_builder/
│ └── densenet_model_trainer/
├── resnet/
│ ├── resnet_model_builder/
│ └── resnet_model_trainer/
├── config.py # Configuration settings
├── data_analyzer.py # Visualizes the data and shows all data information
├── data_generator.py # Custom data generator
├── model_evaluator.py # Evaluation metrics for the trained models
├── evaluation.py # Evaluation script for the custom new data
├── main.ipynb # Main Notebook to run the code
├── requirements.txt # Project dependencies
├── model/
│ └── checkpoints/ # Saved model weights
└── results/ # Training results and plots
- Activate the virtual environment:
# For macOS/Linux
source venv/bin/activate
# For Windows
.\venv\Scripts\activate- Run each cell in the main.ipynb notebook
The models are available in the following folder at: finalModels
- Place all the images you want to evaluate in a new folder and give that folder path in the cell under "7.Evaluation on New dataset"
- Ensure you have the model downloaded and stored in a folder named: 'model'
- Once you have the paths all set, change the paths accordingly in "7.Evaluation on New dataset" and run the cell
- Your desired prediction csv file will be stored as 'evaluation.csv'
The script will:
- Process all images in the folder storing your images
- Generate 'evaluation.csv' with predictions
- CSV format: name,ground_truth (where ground_truth is the predicted class)
ResNet and its application to medical image processing: Research progress and challenges link Deep Learning in Image Classification using Residual Network (ResNet) Variants for Detection of Colorectal Cancer link Dense Convolutional Network and Its Application in Medical Image Analysis link Optimization and fine-tuning of DenseNet model for classification of COVID-19 cases in medical imaging link