The Vertebral Fracture Detection System is an end-to-end deep learning pipeline designed to classify spinal X-rays as Healthy or Abnormal with exceptional accuracy. Leveraging advanced digital image processing techniques and a custom CNN architecture, this system demonstrates the potential of AI in medical diagnostics.
π Live Deployment: Try the interactive web application at https://spine-fracture-ai.streamlit.app/
β οΈ Medical Disclaimer: This is an educational proof-of-concept project. It should NOT be used for actual medical diagnosis without proper clinical validation and physician oversight.
- β Build a robust binary classifier for vertebral abnormality detection
- β Implement OOD detection to prevent misdiagnosis on invalid inputs
- β Implement production-grade preprocessing pipelines using OpenCV
- β Overcome common ML challenges (class imbalance, overfitting, gradient issues)
- β Deploy an interactive web interface for real-time predictions
- β Achieve research-level performance metrics
- AI Gatekeeper validates input images before diagnosis
- Rejects non-X-ray images (photos, charts, random images)
- Prevents false diagnoses on invalid inputs
- Dual-model architecture for robust prediction
- Custom CNN architecture optimized for medical imaging
- Global Average Pooling to prevent parameter explosion
- Class-weighted training to handle dataset imbalance
- Strategic dropout (60%) for robust generalization
- CLAHE (Contrast Limited Adaptive Histogram Equalization) for enhanced contrast
- Gaussian blur filtering for noise reduction
- Standardized preprocessing pipeline
- Automated image quality normalization
- Dynamic rotation (Β±20Β°)
- Random zoom (80%-120%)
- Horizontal flipping
- Width/height shifts for translation invariance
- Interactive Streamlit web interface
- Real-time image upload and prediction
- Confidence score visualization
- Responsive design for all devices
streamlit run app.pyUpload an X-ray β Get Instant Diagnosis
| Input X-Ray | Preprocessing | Prediction |
|---|---|---|
| Raw Medical Image | CLAHE Enhancement | Healthy / Abnormal |
Live Demo Interface Features:
- π€ Drag-and-drop file upload (JPG/PNG supported)
- π Side-by-side comparison (Original vs. CLAHE-enhanced)
- π Real-time diagnosis with confidence scores
- π¨ Clean, dark-themed professional UI
- β‘ Instant predictions (<2 seconds)
To keep this repository lightweight and adhere to Git best practices, the raw image datasets are not included in version control.
If you wish to retrain the gatekeeper_v1.keras Out-of-Distribution (OOD) detection model from scratch, please follow these steps to assemble the training data:
1. Download the Data:
- Random Images (Class 0 - Not X-Ray): Download the Natural Images Dataset from Kaggle. Select ~200 random images (dogs, cars, people, etc.).
- Digital Charts (Class 0 - Not X-Ray): Download the Data Visualization Charts Dataset from Kaggle or save ~50 images of pie charts and bar graphs from Google Images.
- Real X-Rays (Class 1 - Is X-Ray): Gather ~200 of your existing healthy and abnormal spinal X-rays.
2. Assemble the Folder Structure: Create the following directory structure and place the images inside:
data/
βββ gatekeeper_data/
βββ random/ # Place the ~250 natural images and charts here
βββ xrays/ # Place the ~200 real spinal X-rays here
3. Train the Model: Once the folders are populated, run the training script from the root directory:
python -m src.train_gatekeeper| Category | Technologies |
|---|---|
| Deep Learning | |
| Computer Vision | |
| ML Tools | |
| Data Science | |
| Visualization | |
| Web Framework |
Raw Image Upload
β
π‘οΈ OOD Gatekeeper (Is this an X-ray?)
βββ β Not X-ray β Reject
βββ β
Valid X-ray
β
Preprocessing (Grayscale Conversion)
β
CLAHE Enhancement (Clip Limit: 1.2)
β
Gaussian Blur (3Γ3 Kernel)
β
Resize to 224Γ224
β
π§ CNN Classifier Model
β
Prediction (Healthy/Abnormal)
β
π Confidence Score
Two-Stage Architecture:
- Stage 1 - OOD Detection:
gatekeeper_v1.kerasvalidates input is a real X-ray - Stage 2 - Classification:
cnn_spine_v1.kerasdiagnoses healthy vs. abnormal
Input (224Γ224Γ1)
β
Rescaling (1/255)
β
Data Augmentation Layer
ββ RandomFlip (horizontal)
ββ RandomRotation (Β±10%)
ββ RandomZoom (Β±10%)
β
Conv2D(32, 3Γ3, padding='same') β ReLU β MaxPool(2Γ2)
β
Conv2D(64, 3Γ3, padding='same') β ReLU β MaxPool(2Γ2)
β
Conv2D(128, 3Γ3, padding='same') β ReLU β MaxPool(2Γ2)
β
GlobalAveragePooling2D
β
Dense(128, he_normal) β ReLU
β
Dropout(0.6)
β
Dense(1, sigmoid) β Output
Key Innovation: GlobalAveragePooling reduced parameters from 12.8M β 16K while maintaining accuracy!
- Python 3.8 or higher
- pip package manager
- Virtual environment (recommended)
# 1. Clone the repository
git clone https://github.com/RaviKumarYadav15/vertebral-disease-detection-v1.0.git
cd vertebral-disease-detection-v1.0
# 2. Create virtual environment
python -m venv venv
# 3. Activate virtual environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate
# 4. Install dependencies
pip install -r requirements.txtopencv-python==4.8.0.76
tensorflow==2.16.1
scikit-learn==1.3.2
numpy==1.26.2
matplotlib==3.8.2
streamlit==1.29.0
pillow>=9.3.0
pandas>=1.5.0***removed exact version as it needed locally for pip, and while deployment change python version to 11 for tensor flow ***
# Step 1: Preprocess raw X-ray images
python -m src.preprocess
# Step 2: Train baseline logistic regression model
python -m src.train_baseline
# Step 3: Train custom CNN model
python -m src.train_cnn
# Step 4: Compare model performance
python -m src.compare_models# Launch Streamlit interface
streamlit run app.pyThen navigate to http://localhost:8501 in your browser.
from src.predict import predict_image
# Predict on a single image
result = predict_image('path/to/xray.jpg')
print(f"Prediction: {result['class']}")
print(f"Confidence: {result['confidence']:.2%}")vertebral-disease-detection/
β
βββ π data/
β βββ gatekeeper_data/ # OOD detection training data
β β βββ random/ # Non-X-ray images (natural images, charts)
β β βββ xrays/ # Real X-ray images
β βββ processed/ # Enhanced images after DIP pipeline
β β βββ abnormal/ # Preprocessed abnormal X-rays
β β βββ healthy/ # Preprocessed healthy X-rays
β βββ raw/ # Original X-ray images
β βββ abnormal/ # 500 abnormal spine X-rays
β βββ healthy/ # 500 healthy spine X-rays
β
βββ π models/ # Saved model checkpoints
β βββ baseline_logistic.pkl # Baseline ML model
β βββ cnn_spine_v1.keras # Trained CNN classifier
β βββ gatekeeper_v1.keras # OOD detection model
β
βββ π src/ # Source code
β βββ __init__.py # Package initialization
β βββ config.py # Configuration & hyperparameters
β βββ preprocess.py # Image preprocessing pipeline
β βββ model_cnn.py # CNN architecture definition
β βββ train_baseline.py # Baseline model training
β βββ train_cnn.py # CNN training script with callbacks
β βββ train_gatekeeper.py # OOD detection model training
β βββ predict.py # Inference engine
β βββ compare_models.py # Model evaluation & comparison
β
βββ π uploads/ # Temporary upload directory for web app
βββ π venv/ # Virtual environment (not in git)
β
βββ π app.py # Streamlit web application
βββ π training_history.png # Training/validation curves
βββ π requirements.txt # Python dependencies
βββ π README.md # Project documentation
βββ π .gitignore # Git ignore rules
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Dataset: Vertebral X-ray images from Kaggle - Spine Fracture Prediction from X-rays
- Inspiration: Medical AI research community
- Libraries: TensorFlow, OpenCV, Streamlit teams
- Guidance: Deep learning and computer vision best practices
Ravi - Project Author
- GitHub: @RaviKumarYadav15
Made with β€οΈ and π§ for advancing medical AI
