Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

MedVision AI πŸ₯

An AI-powered medical image disease detection web application using Convolutional Neural Networks (CNNs). Built with PyTorch, Streamlit, and OpenCV, featuring GPU-accelerated training, explainable AI with Grad-CAM, and professional PDF report generation.

Features ✨

  • Deep Learning Models: EfficientNet-B0 (default) and ResNet50 with transfer learning
  • GPU Support: Automatic CUDA detection and GPU-accelerated training/inference
  • Explainable AI: Grad-CAM heatmaps to visualize model decision-making
  • Modern UI: Beautiful, responsive medical-themed Streamlit interface
  • PDF Reports: Professional medical report generation with patient information
  • Multiple Image Types: Support for Chest X-rays, Brain MRI, Skin Disease images, and CT scans
  • Performance Metrics: Accuracy, Precision, Recall, F1-Score, and Confusion Matrix
  • Prediction History: Track and review previous analyses
  • Fast Inference: Optimized for real-time predictions

Project Structure πŸ“

MedVision-AI/
β”œβ”€β”€ models/              # Model architectures and factory
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── model_factory.py
β”œβ”€β”€ training/            # Training module with transfer learning
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── trainer.py
β”œβ”€β”€ inference/           # Inference and prediction module
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── predictor.py
β”œβ”€β”€ gradcam/             # Grad-CAM for explainable AI
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── gradcam.py
β”œβ”€β”€ utils/               # Utility functions and configuration
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ config.py
β”‚   └── helpers.py
β”œβ”€β”€ reports/             # PDF report generation
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── report_generator.py
β”œβ”€β”€ frontend/            # Streamlit web application
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── app.py
β”œβ”€β”€ data/                # Data directory
β”‚   └── sample_images/
β”œβ”€β”€ checkpoints/         # Model checkpoints
β”œβ”€β”€ requirements.txt     # Python dependencies
└── README.md           # This file

Installation πŸš€

Prerequisites

  • Python 3.8 or higher
  • CUDA-capable GPU (optional, for GPU acceleration)
  • pip package manager

Step 1: Clone or Download the Project

cd C:\Users\LENOVO\CascadeProjects\MedVision-AI

Step 2: Create Virtual Environment (Recommended)

python -m venv venv
venv\Scripts\activate  # On Windows
# source venv/bin/activate  # On Linux/Mac

Step 3: Install Dependencies

pip install -r requirements.txt

Note for CUDA Users: If you have a CUDA-capable GPU, you may want to install PyTorch with CUDA support. Visit PyTorch's website for the appropriate installation command for your CUDA version.

Step 4: Verify Installation

python -c "import torch; print(f'PyTorch version: {torch.__version__}'); print(f'CUDA available: {torch.cuda.is_available()}')"

Usage πŸ“–

Running the Web Application

  1. Start the Streamlit app:
streamlit run frontend/app.py
  1. Open your browser: Navigate to http://localhost:8501

  2. Use the application:

    • Upload a medical image
    • Select a model (EfficientNet-B0 or ResNet50)
    • Click "Analyze Image"
    • View results, Grad-CAM heatmap, and generate PDF reports

Training a Custom Model

If you have your own dataset, you can train a custom model:

from training.trainer import train_model

# Train with your data
history = train_model(
    train_dir="path/to/train/data",
    val_dir="path/to/val/data",
    model_name="efficientnet_b0"
)

Using the Predictor Programmatically

from inference.predictor import load_predictor
import cv2

# Load predictor
predictor = load_predictor(model_name="efficientnet_b0")

# Load image
image = cv2.imread("path/to/image.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Make prediction
result = predictor.predict(image)

# Access results
print(f"Predicted disease: {result['predicted_disease']}")
print(f"Confidence: {result['confidence']:.2f}%")
print(f"Severity: {result['severity']}")

Generating Grad-CAM Visualizations

from gradcam.gradcam import generate_gradcam_visualization
from models.model_factory import ModelFactory
import cv2

# Load model
model = ModelFactory.create_model("efficientnet_b0")
model.load_state_dict(torch.load("checkpoints/efficientnet_b0_best.pth")['model_state_dict'])
model.eval()

# Load image
image = cv2.imread("path/to/image.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Generate Grad-CAM
overlay, heatmap = generate_gradcam_visualization(model, image, "efficientnet_b0")

# Save visualization
cv2.imwrite("gradcam_overlay.jpg", overlay)

Configuration βš™οΈ

Edit utils/config.py to customize:

  • Model hyperparameters (batch size, learning rate, epochs)
  • Image transformations and augmentations
  • Disease classes and descriptions
  • UI theme colors
  • File paths and directories

Disease Classes πŸ₯

The system is configured to detect the following disease classes (customizable):

  1. Normal - No abnormalities detected
  2. Pneumonia - Lung infection requiring medical attention
  3. Tumor - Abnormal mass requiring specialist consultation
  4. Other Abnormality - Other detected abnormalities

You can modify these classes in utils/config.py to match your specific use case.

Model Performance πŸ“Š

The application tracks and displays:

  • Accuracy: Overall prediction accuracy
  • Precision: Precision score (weighted average)
  • Recall: Recall score (weighted average)
  • F1-Score: F1 score (weighted average)
  • Confusion Matrix: Visual representation of predictions vs ground truth

GPU Acceleration πŸš€

The application automatically detects and uses CUDA if available:

from utils.config import Config

device_info = Config.get_device_info()
print(f"Device: {device_info['device']}")
if device_info['cuda_available']:
    print(f"CUDA Device: {device_info['cuda_device_name']}")

PDF Report Generation πŸ“„

Generate professional medical reports containing:

  • Patient information
  • Predicted disease and confidence
  • Severity level
  • Disease description
  • Recommended next steps
  • Original image and Grad-CAM heatmap
  • Top predictions
  • Medical disclaimer

API Reference πŸ“š

ModelFactory

from models.model_factory import ModelFactory

# Create a model
model = ModelFactory.create_model(
    model_name="efficientnet_b0",
    num_classes=4,
    pretrained=True
)

# Get model information
info = ModelFactory.get_model_info(model)

Trainer

from training.trainer import Trainer

trainer = Trainer(model_name="efficientnet_b0")
history = trainer.train(
    train_dir=Path("data/train"),
    val_dir=Path("data/val")
)
trainer.save_training_history()

Predictor

from inference.predictor import Predictor

predictor = Predictor(model_name="efficientnet_b0")
result = predictor.predict(image_path="path/to/image.jpg")

MedicalReportGenerator

from reports.report_generator import MedicalReportGenerator

generator = MedicalReportGenerator()
report_path = generator.generate_report(
    prediction_result=result,
    patient_info={"name": "John Doe", "id": "12345"}
)

Troubleshooting πŸ”§

Common Issues

  1. CUDA Out of Memory: Reduce batch size in config.py
  2. Model Loading Error: Ensure checkpoint file exists in checkpoints/ directory
  3. Image Upload Error: Check image format (supported: JPG, PNG, BMP, TIFF)
  4. Grad-CAM Generation Error: Ensure target layer is correctly configured

Getting Help

If you encounter issues:

  1. Check the console output for error messages
  2. Verify all dependencies are installed correctly
  3. Ensure your data directory structure is correct
  4. Check GPU availability if using CUDA

Disclaimer ⚠️

IMPORTANT: This AI-generated system is for informational and educational purposes only and should not be used as a substitute for professional medical diagnosis or treatment. Always consult with a qualified healthcare provider for medical advice.

The predictions provided by this system are not guaranteed to be accurate and should be verified by medical professionals.

Contributing 🀝

Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.

License πŸ“„

This project is provided as-is for educational and research purposes.

Acknowledgments πŸ™

  • PyTorch team for the deep learning framework
  • Streamlit team for the web framework
  • Medical imaging community for datasets and research
  • Open-source community for various tools and libraries

Contact πŸ“§

For questions or support, please open an issue on the project repository.


Built with ❀️ for better healthcare through AI

About

takes the x-ray images and detect the defect in it

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages