Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions Kidney Stone Images Classification/Dataset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Dataset β€” Kidney Stone Images

## Source
**Kaggle:** https://www.kaggle.com/datasets/safurahajiheidari/kidney-stone-images

## About the Dataset
- **Total Images:** ~1,299 CT scan images (Roboflow Universe export)
- **Classes:** `nc: 1` β€” single class `Tas_Var` (Turkish: "Stone Present") β€” all images contain stones
- **Task:** Binary classification β€” **Small Stone vs Large Stone** (median bounding box area split, threshold = 0.0015)
- **Annotation Format:** YOLO format (bounding box labels per image)
- **Splits:** Pre-divided into `train`, `valid`, and `test` (1054 / 123 / 123)

## Directory Structure (after download)
```
/kaggle/input/datasets/safurahajiheidari/
└── kidney-stone-images/
β”œβ”€β”€ train/
β”‚ β”œβ”€β”€ images/ ← .jpg CT scan images
β”‚ └── labels/ ← .txt YOLO annotation files
β”œβ”€β”€ valid/
β”‚ β”œβ”€β”€ images/
β”‚ └── labels/
β”œβ”€β”€ test/
β”‚ β”œβ”€β”€ images/
β”‚ └── labels/
β”œβ”€β”€ data.yaml
β”œβ”€β”€ README.dataset.txt
└── README.roboflow.txt
```

## Label Format (YOLO)
Each `.txt` label file contains one line per annotated stone:
```
class_id x_center y_center width height
```
All values (except `class_id`) are normalised to [0, 1].
`class_id` is always `0` (`Tas_Var`). Multiple lines = multiple stones in the image.

## Classification Strategy
Since all images contain stones (`nc: 1`), classification uses the **largest bounding box area** per image:

- `max_area >= median (0.0015)` β†’ **`large`** stone (label = 1)
- `max_area < median (0.0015)` β†’ **`small`** stone (label = 0)

This guarantees a perfectly balanced 50/50 split and provides visually distinct classes β€” large stones occupy more of the CT image.

## How to Add in Kaggle
1. Open your Kaggle Notebook
2. Click **+ Add Data** (top right)
3. Search: `kidney-stone-images safurahajiheidari`
4. Click **Add**
5. Dataset mounts at `/kaggle/input/datasets/safurahajiheidari/kidney-stone-images/`

> **Note:** Set `DATASET_PATH = '/kaggle/input/datasets/safurahajiheidari/kidney-stone-images'` in Cell 1.
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 89 additions & 0 deletions Kidney Stone Images Classification/Model/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Kidney Stone Images Classification using Deep Learning

## 🎯 Aim of the Project
To classify CT scan images from the kidney stone dataset using deep learning and compare four transfer learning models by performance metrics to identify the best approach for stone size classification.

## πŸ“‚ Dataset
- **Source:** [Kaggle β€” Kidney Stone Images with Bounding Box Annotations](https://www.kaggle.com/datasets/safurahajiheidari/kidney-stone-images)
- **Total Images:** ~1,299 CT scan images
- **Splits:** Pre-divided into `train / valid / test` (1054 / 123 / 123)
- **Label format:** YOLO `.txt` annotations (bounding boxes), `nc: 1`, class `Tas_Var`
- **Classification task:** Binary β€” **Small Stone vs Large Stone** (median bounding box area split, threshold = 0.0015)

> All images contain kidney stones (`nc: 1`). Classification is based on the largest bounding box area per image β€” images above the dataset median are labelled `large`, below are labelled `small`. Large stones visually dominate more of the CT image, providing more learnable signal than stone count.

## 🧠 Models Used

| Model | Key Strength |
|---|---|
| **Xception** | Depthwise separable convolutions β€” captures subtle stone texture patterns |
| **InceptionV3** | Multi-scale inception modules β€” handles variation in stone size across the image |
| **EfficientNetV2S** | Fused-MBConv + progressive learning β€” outperforms V1 on small medical datasets |
| **ConvNeXt-Tiny** | Modern (2022) pure-CNN β€” matches transformer accuracy with standard convolutions |

All models use **two-phase transfer learning**: frozen base (phase 1) β†’ fine-tuning top 30 layers at `lr=1e-5` (phase 2).

## πŸ”¬ Approach

1. **Dataset Structure Check** β€” Auto-detect path, walk directory, read `data.yaml`
2. **YOLO Label Parsing** β€” Extract bounding boxes, compute max area per image
3. **EDA** β€” Class distribution, bounding box visualisation, stone size distribution, augmentation preview
4. **Median Split Labelling** β€” `large` if `max_area β‰₯ 0.0015`, else `small` β€” guarantees balanced classes
5. **tf.data Pipeline** β€” AUTOTUNE, caching, shuffling, augmentation (flip, brightness, contrast)
6. **Class Weights** β€” Applied during training as best practice
7. **Training** β€” Each model trained independently; histories saved with `pickle` after every model
8. **Evaluation** β€” Test set evaluation using Accuracy, Precision, Recall, F1-Score, ROC-AUC, Cohen's Kappa
9. **Visualisation** β€” Training curves, confusion matrices, ROC curves, multi-metric bar chart

## πŸ“Š Exploratory Data Analysis

![EDA Overview](../Images/eda_overview.png)

![Bounding Boxes](../Images/eda_bboxes.png)

![Class Imbalance](../Images/eda_imbalance.png)

![Augmented Samples](../Images/eda_augmented_samples.png)

## πŸ“ˆ Training Curves

![Training Curves](../Images/training_curves.png)

## πŸ”² Confusion Matrices

![Confusion Matrices](../Images/confusion_matrices.png)

## πŸ“‰ ROC Curves

![ROC Curves](../Images/roc_curves.png)

## πŸ† Performance Comparison

![Final Comparison](../Images/final_comparison.png)

| Model | Accuracy | Precision | Recall | F1-Score | ROC-AUC | Cohen ΞΊ |
|---|---|---|---|---|---|---|
| **ConvNeXtTiny** | **0.7154** | **0.7201** | **0.7148** | **0.7136** | **0.7501** | **0.4302** |
| InceptionV3 | 0.6911 | 0.6918 | 0.6908 | 0.6905 | 0.7496 | 0.3817 |
| EfficientNetV2S | 0.5935 | 0.5937 | 0.5936 | 0.5935 | 0.6214 | 0.1872 |
| Xception | 0.5447 | 0.5447 | 0.5444 | 0.5440 | 0.5933 | 0.0889 |

## βœ… Conclusion

**Best Model: ConvNeXtTiny** β€” Accuracy: 71.54% | F1-Score: 0.7136 | ROC-AUC: 0.7501 | Cohen ΞΊ: 0.4302

ConvNeXtTiny and InceptionV3 are closely matched on ROC-AUC (0.7501 vs 0.7496), but ConvNeXtTiny edges ahead on all other metrics. Its modern large-kernel convolution design likely captures the spatial spread of larger stones more effectively.

EfficientNetV2S and Xception underperformed, suggesting that depthwise and compound-scaled architectures need more data to generalise well on this subtle medical imaging task.

All models were evaluated beyond plain accuracy using ROC-AUC and Cohen's Kappa β€” metrics designed for balanced binary tasks in medical imaging.

## πŸ“¦ Libraries Used
- TensorFlow / Keras
- NumPy, Pandas
- Matplotlib, Seaborn
- scikit-learn (classification_report, roc_auc_score, cohen_kappa_score)
- pickle (history persistence)

## πŸ–₯️ Platform
Kaggle Notebook β€” T4 GPU (2Γ—)
Loading