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.
- 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
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
- Python 3.8 or higher
- CUDA-capable GPU (optional, for GPU acceleration)
- pip package manager
cd C:\Users\LENOVO\CascadeProjects\MedVision-AIpython -m venv venv
venv\Scripts\activate # On Windows
# source venv/bin/activate # On Linux/Macpip install -r requirements.txtNote 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.
python -c "import torch; print(f'PyTorch version: {torch.__version__}'); print(f'CUDA available: {torch.cuda.is_available()}')"- Start the Streamlit app:
streamlit run frontend/app.py-
Open your browser: Navigate to
http://localhost:8501 -
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
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"
)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']}")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)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
The system is configured to detect the following disease classes (customizable):
- Normal - No abnormalities detected
- Pneumonia - Lung infection requiring medical attention
- Tumor - Abnormal mass requiring specialist consultation
- Other Abnormality - Other detected abnormalities
You can modify these classes in utils/config.py to match your specific use case.
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
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']}")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
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)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()from inference.predictor import Predictor
predictor = Predictor(model_name="efficientnet_b0")
result = predictor.predict(image_path="path/to/image.jpg")from reports.report_generator import MedicalReportGenerator
generator = MedicalReportGenerator()
report_path = generator.generate_report(
prediction_result=result,
patient_info={"name": "John Doe", "id": "12345"}
)- CUDA Out of Memory: Reduce batch size in
config.py - Model Loading Error: Ensure checkpoint file exists in
checkpoints/directory - Image Upload Error: Check image format (supported: JPG, PNG, BMP, TIFF)
- Grad-CAM Generation Error: Ensure target layer is correctly configured
If you encounter issues:
- Check the console output for error messages
- Verify all dependencies are installed correctly
- Ensure your data directory structure is correct
- Check GPU availability if using CUDA
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.
Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.
This project is provided as-is for educational and research purposes.
- 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
For questions or support, please open an issue on the project repository.
Built with β€οΈ for better healthcare through AI