An Explainable AI prototype for lung cancer CT classification with visual and natural-language explanations.
BLACKBOX MD is an Explainable Artificial Intelligence (XAI) based clinical decision-support project designed to make medical AI predictions more understandable to clinicians.
The current prototype focuses on lung cancer CT image classification using a pretrained DenseNet-121 model. In addition to predicting the cancer category, BLACKBOX MD uses Grad-CAM to identify image regions that influenced the model's decision and an NLP-based explanation layer to convert this information into a human-readable explanation.
The goal is not to replace a doctor, but to provide an additional, interpretable AI perspective that can support clinical decision-making.
Deep learning models can achieve strong performance on medical image classification, but their predictions are often difficult to interpret.
A model may predict:
Adenocarcinoma โ 97.10% confidence
but this alone does not tell a clinician:
- Which region of the CT image influenced the prediction?
- How strongly did the model focus on that region?
- How can the AI result be communicated in understandable language?
- Can the prediction be presented as an interpretable second opinion rather than a black-box result?
BLACKBOX MD addresses this problem by combining:
Medical Image โ AI Prediction โ Visual Explanation โ Natural-Language Explanation
The current working prototype consists of three major layers:
A pretrained DenseNet-121 model performs four-class CT image classification.
Grad-CAM generates a heatmap showing the regions that contributed to the model's prediction.
The prediction confidence and Grad-CAM attention information are converted into a clinician-readable explanation.
This creates an explainability pipeline rather than providing only a raw classification result.
โโโโโโโโโโโโโโโโโโโโโโโ
โ CT Scan Image โ
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโ
โ Image Preprocessing โ
โ 256 ร 256 โ
โ Normalization โ
โ Augmentation โ
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโ
โ DenseNet-121 โ
โ ImageNet Pretrained โ
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
โโโโโโโโโโโโดโโโโโโโโโโโ
โผ โผ
โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ Prediction โ โ Grad-CAM โ
โ Class + Confidenceโ โ Attention Map โ
โโโโโโโโโโฌโโโโโโโโโโ โโโโโโโโโโฌโโโโโโโโโโ
โ โ
โโโโโโโโโโโโฌโโโโโโโโโโโโ
โผ
โโโโโโโโโโโโโโโโโโโโโโโ
โ NLP Explanation โ
โ Layer โ
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโ
โ Human-Readable โ
โ AI Explanation โ
โโโโโโโโโโโโโโโโโโโโโโโ
The current prototype uses the:
Kaggle Chest CT-Scan Images Dataset
Dataset characteristics used in the current implementation:
| Property | Value |
|---|---|
| Task | Lung cancer CT classification |
| Number of classes | 4 |
| Test images | 315 |
| Input size | 256 ร 256 |
| Dataset source | Kaggle Chest CT-Scan Images |
| Classification type | Multi-class |
The current model predicts:
- Adenocarcinoma
- Large Cell Carcinoma
- Normal
- Squamous Cell Carcinoma
The preprocessing pipeline includes:
- Image resizing to 256 ร 256
- ImageNet mean/std normalization
- Training-only augmentation
- Random affine transformations
- Rotation of approximately ยฑ10ยฐ
- Scale variation between 0.90โ1.10
- Visual verification of sample images before training
The augmentation is applied only during training to avoid modifying validation and test distributions.
The classification backbone is an ImageNet-pretrained DenseNet-121.
The final classification head is adapted for the four lung CT classes.
| Parameter | Configuration |
|---|---|
| Backbone | DenseNet-121 |
| Pretraining | ImageNet |
| Optimizer | Adam |
| Learning rate | 0.0001 |
| Loss function | Cross-Entropy |
| Epochs | 20 |
| Number of classes | 4 |
| Input size | 256 ร 256 |
The best validation checkpoint was selected rather than automatically using the final epoch. The best validation accuracy was 95.83% at epoch 7.
The current prototype achieved:
on the current test set of 315 images.
| Class | Precision | Recall | F1-Score |
|---|---|---|---|
| Adenocarcinoma | 0.991 | 0.917 | 0.952 |
| Large Cell Carcinoma | 0.879 | 1.000 | 0.936 |
| Normal | 1.000 | 0.982 | 0.991 |
| Squamous Cell Carcinoma | 0.957 | 0.989 | 0.973 |
The main systematic confusion in the current evaluation occurred between:
Adenocarcinoma โ Large Cell Carcinoma
while the Normal class was classified with very high performance.
Important: These results represent the current prototype's performance on its available test set and should not be interpreted as clinical diagnostic accuracy.
A major component of BLACKBOX MD is Grad-CAM (Gradient-weighted Class Activation Mapping).
Instead of returning only:
Prediction: Adenocarcinoma
Confidence: 97.10%
the system also produces a visual heatmap showing the regions that contributed to the model's prediction.
- Prediction-specific attention visualization
- Localization of influential image regions
- Visual interpretation of model decisions
- A way to inspect whether the model is focusing on meaningful regions
The Grad-CAM implementation was also validated across test batches and rewritten as a reusable function after resolving gradient-hook handling issues.
The major addition to the existing image-classification/XAI pipeline is the NLP Explanation Layer.
The layer takes structured information from the prediction and Grad-CAM analysis and converts it into a human-readable explanation.
Current inputs include:
Predicted Class
+
Confidence Score
+
Grad-CAM Attention
โ
NLP Explanation
The system currently supports two explanation approaches.
The rule-based generator:
- Categorizes confidence into predefined levels
- Categorizes attention into predefined levels
- Uses a deterministic explanation template
- Produces explanations with zero model-inference cost
- Adds an appropriate clinical caveat
Example:
The AI model predicts Adenocarcinoma with a 97.10% confidence level. The Grad-CAM analysis shows strong model attention, with approximately 22.32% of the image showing strong activation. These highlighted areas indicate regions that influenced the model's classification. The result is an AI prediction and should be interpreted together with appropriate clinical assessment.
The second approach structures the same model information into a prompt for a language model.
The prompt is designed to:
- Explain the model output
- Describe the significance of the highlighted regions
- Avoid unsupported medical claims
- Clearly distinguish AI prediction from diagnosis
- Maintain a clinical safety caveat
The system explicitly treats Grad-CAM regions as areas that influenced the model, rather than claiming that those regions independently prove a diagnosis.
1. Upload CT Image
โ
2. Preprocess Image
โ
3. DenseNet-121 Classification
โ
4. Generate Prediction + Confidence
โ
5. Generate Grad-CAM Heatmap
โ
6. Calculate Attention Information
โ
7. NLP Explanation Generation
โ
8. Display Prediction + Explanation
The next stage is to integrate the current ML/XAI pipeline into a full application.
โโโโโโโโโโโโโโโโโโโโโโโ
โ React Dashboard โ
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโ
โ FastAPI Backend โ
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโดโโโโโโโโโโโโโ
โผ โผ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ DenseNet-121 โ โ Explanation โ
โ Classifier โ โ Engine โ
โโโโโโโโโโฌโโโโโโโโโ โโโโโโโโโโฌโโโโโโโโโ
โ โ
โผ โผ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ Grad-CAM โ โ NLP Explanation โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
- CT image preprocessing
- Four-class DenseNet-121 classifier
- Model training and evaluation
- Best-checkpoint selection
- Grad-CAM implementation
- Grad-CAM validation
- Rule-based NLP explanation
- LLM-prompt explanation design
- FastAPI backend
- Model inference API
- Grad-CAM generation API
- NLP explanation API
- React dashboard
- Upload-and-predict workflow
- Prediction + heatmap visualization
- Human-readable explanation display
- EHR integration
- FHIR-based data exchange
- Patient-context integration
- Clinical alert framework
- Audit logging
- Larger and more diverse datasets
- Cross-validation
- Robustness testing
- Additional medical conditions
- Hindi/Hinglish NLP support
- Broader multimodal clinical information
BLACKBOX MD is being developed around the principle that medical AI should provide not only predictions but also interpretable evidence about the model's decision process.
Grad-CAM is particularly useful for medical imaging because it can provide spatial information about regions influencing a prediction.
However, explainability maps should be treated as model-attribution information, not as independent medical evidence.
The current research direction also recognizes that generalization depends on dataset diversity, imaging variations, and evaluation across broader patient populations.
The current prototype has several important limitations:
- The model is currently trained and evaluated on a specific Kaggle dataset.
- The current dataset is relatively limited compared with real-world clinical data.
- The four-class classification task does not represent the full complexity of lung cancer diagnosis.
- Grad-CAM highlights regions influencing the model but does not establish causality or diagnosis.
- NLP-generated explanations must not introduce information unsupported by the model output.
- The current system has not undergone clinical validation.
- The current performance should not be interpreted as real-world clinical performance.
BLACKBOX MD is designed as a clinical decision-support / second-opinion prototype, not an autonomous diagnostic system.
AI Prediction
+
Visual Explanation
+
Natural-Language Explanation
โ
Clinical Support
โ
NOT Autonomous Diagnosis
The final clinical decision must remain with a qualified healthcare professional.
The project follows these principles:
- AI output should be treated as advisory.
- The system should not replace qualified medical professionals.
- Explanations should not overstate model certainty.
- Grad-CAM visualizations should not be presented as definitive lesion localization.
- Patient privacy should be considered when integrating clinical data.
- Future clinical deployment requires appropriate validation and regulatory consideration.
The structure will evolve as the working prototype is integrated into the application.
BLACKBOX-MD/
โ
โโโ data/
โ โโโ train/
โ โโโ validation/
โ โโโ test/
โ
โโโ model/
โ โโโ train.py
โ โโโ evaluate.py
โ โโโ classifier.py
โ
โโโ explainability/
โ โโโ gradcam.py
โ
โโโ nlp/
โ โโโ rule_based.py
โ โโโ llm_prompt.py
โ
โโโ backend/
โ โโโ FastAPI/
โ
โโโ frontend/
โ โโโ React/
โ
โโโ notebooks/
โ
โโโ research/
โ
โโโ requirements.txt
โ
โโโ README.md
- Python
- PyTorch
- DenseNet-121
- Torchvision
- Grad-CAM
- Gradient-based visual attribution
- Rule-based explanation generation
- LLM prompt-based explanation generation
- FastAPI (integration stage)
- React (integration stage)
- FHIR
- EHR systems
- Healthcare APIs
git clone <YOUR_REPOSITORY_URL>
cd BLACKBOX-MDpython -m venv venvvenv\Scripts\activatesource venv/bin/activatepip install -r requirements.txtPlace the CT dataset according to the project data structure:
data/
โโโ train/
โโโ validation/
โโโ test/
python model/train.pypython model/evaluate.pyRun the Grad-CAM pipeline using a trained model checkpoint and input CT image.
Pass the prediction and Grad-CAM information to the explanation layer.
| Component | Status |
|---|---|
| CT preprocessing | โ Completed |
| DenseNet-121 classifier | โ Completed |
| Model evaluation | โ Completed |
| 96.19% test accuracy | โ Achieved on current test set |
| Grad-CAM | โ Implemented |
| Grad-CAM validation | โ Completed |
| Rule-based NLP | โ Implemented |
| LLM-prompt explanation | โ Implemented |
| FastAPI integration | ๐ Next |
| React dashboard | ๐ Next |
| Cross-validation | ๐ Planned |
| Larger dataset validation | ๐ Planned |
| EHR/FHIR integration | ๐ฎ Future |
| Hindi/Hinglish NLP | ๐ฎ Future |
The long-term vision of BLACKBOX MD is to move from a conventional medical AI classifier toward an interpretable multimodal clinical decision-support system.
The intended progression is:
Medical Images
+
Clinical Data
+
Medical Reports
โ
AI Prediction
โ
Explainability
โ
Natural-Language Reasoning
โ
Clinician-Friendly Decision Support
The current lung CT prototype serves as the foundation for this broader system.
BLACKBOX MD
Developed as a student research and engineering project focused on:
- Artificial Intelligence
- Deep Learning
- Explainable AI
- Medical Imaging
- NLP
- Clinical Decision Support
BLACKBOX MD is an experimental research prototype.
It is not a medical device and is not intended to diagnose, treat, cure, or prevent any disease.
Model predictions and explanations should not be used as a substitute for professional medical evaluation.
Any future clinical deployment would require extensive validation, appropriate datasets, expert review, privacy protections, and applicable regulatory approvals.