Skip to content

Repository files navigation

AI-Driven Osteoporosis Detection from Knee X-Ray Images

📌 Overview

This project presents an AI-based system for early detection of osteoporosis using knee X-ray images.
Leveraging a fine-tuned ResNet-18 CNN model and Grad-CAM++ visualizations, the system classifies knee X-rays as Normal or Osteoporotic.

The model is deployed as a Flask web application, where users can:

  • Upload knee X-ray images and enter patient details
  • Get instant predictions (Normal / Osteoporosis)
  • View Grad-CAM++ heatmaps showing severity regions
  • Download a detailed patient report (PDF) with uploaded + heatmapped images

🚀 Features

  • ResNet-18 based CNN trained on augmented Kaggle dataset
  • Dataset expanded from ~1,900 → 8,175 X-rays using augmentation (flips, rotations, zoom, contrast)
  • Achieved 93% test accuracy with balanced performance across classes
  • Grad-CAM++ for explainable AI and severity region localization
  • Flask app interface for real-time inference and report generation

📂 Project Structure

.
├── app.py                  # Flask app (entry point)
├── cnn_model_train.py       # Model training script
├── grad_cam_plus_plus.py    # Grad-CAM++ visualization
├── run_prediction.py        # Inference utilities
├── model.pth                # Trained ResNet-18 weights (download separately)
├── templates/               # HTML templates for UI
├── model/                   # Model architecture files
├── requirements.txt         # Dependencies
├── README.md                # Project documentation
└── training_plots.png       # Training/validation trends

⚙️ Installation

1️⃣ Clone the repository

git clone https://github.com/your-username/osteoporosis-detection.git
cd osteoporosis-detection

2️⃣ Create virtual environment

python -m venv venv
source venv/bin/activate    # Linux / macOS
venv\Scripts�ctivate       # Windows

3️⃣ Install dependencies

Dependencies are listed in requirements.txt (includes PyTorch with CUDA 12.1, Flask, OpenCV, ReportLab).

pip install -r requirements.txt

📌 If you don’t have CUDA 12.1, install CPU-only PyTorch:

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

4️⃣ Download trained model

Place the trained weights model.pth in the project root.
(Link to be provided — e.g., Google Drive, HuggingFace, or GitHub Releases.)


▶️ Running the Application

python app.py

Then open in browser:
👉 http://127.0.0.1:5000/


🖥️ Usage Flow

  1. Enter patient details (name, age, gender, etc.)
  2. Upload a knee X-ray
  3. Click Submit
  4. The system will:
    • Predict Normal / Osteoporosis
    • Show a Grad-CAM++ heatmap
    • Generate a downloadable PDF report containing:
      • Patient details
      • Original X-ray
      • Grad-CAM++ visualization
      • Model result

📊 Results

  • Dataset: 8,175 knee X-ray images (augmented from Kaggle)
  • Architecture: Fine-tuned ResNet-18
  • Performance:
    • Accuracy: 93%
    • Precision (Osteoporosis): 0.96
    • Recall (Normal): 0.96
    • Balanced F1-score across classes

✅ The model generalizes well with minimal overfitting and provides interpretable predictions.


🛠️ Tech Stack

  • Deep Learning: PyTorch, TorchVision
  • Visualization: Grad-CAM++
  • Image Processing: OpenCV, Pillow, Matplotlib
  • Backend: Flask
  • PDF Reports: ReportLab

🌍 Future Scope

  • Multi-class classification for different osteoporosis stages
  • Early-stage detection by combining X-ray + DEXA scans
  • Non-knee image rejection module
  • Mobile / cloud deployment (AWS, GCP, HuggingFace Spaces)
  • Training on larger, more diverse datasets

⚠️ Disclaimer

This project is for academic and research purposes only.
It is not a medical diagnostic tool and should not be used as a substitute for professional medical advice.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages