Sample results
Alright, so let's talk about explainable AI! You know, AI models are doing some amazing things these days, but sometimes they can be like black boxes, making decisions without giving us a clear idea of how they arrived at those conclusions. That's where explainable AI steps in to save the day!
Explainable AI is all about making AI models more transparent and understandable. It helps us humans get insights into the inner workings of these complex models. And one cool technique we're diving into is Grad-CAM, short for Gradient-weighted Class Activation Mapping.
Now, here's the plan: I'm going to apply Grad-CAM to Chest X-Ray Images, specifically those related to Pneumonia. By using Grad-CAM, we'll be able to visualize and understand which areas of the X-ray image contribute the most to the model's decision-making process. It's like shining a spotlight on the important regions that help the AI model identify signs of pneumonia.
With this explainable AI technique in place, we can gain deeper insights into how the AI model is analyzing the X-ray images and what factors it's considering when making predictions. This can be incredibly useful for medical professionals, researchers, and even patients to better understand the AI's diagnostic process.
- Python 3.10–3.12 (TensorFlow does not support Python 3.13+ yet)
- macOS 12.0 or later (Intel or Apple Silicon)
-
Clone or navigate to the project directory:
cd /Users/sevendi/Projects/explainable-ai-pneumonia -
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate -
Install required packages:
# Using requirements.txt (recommended) pip install -r requirements.txt # OR manually pip install tensorflow pandas numpy matplotlib opencv-python scikit-learn
Note for Apple Silicon Mac: TensorFlow will automatically use Metal Performance Shaders (MPS) for GPU acceleration.
-
Download the dataset:
- Download from Kaggle: Chest X-Ray Pneumonia Dataset
- Extract the ZIP file in the project directory
- You should have:
chest_xray/train,chest_xray/test, andchest_xray/valdirectories
explainable-ai-pneumonia/
├── eXplainable-ai.ipynb # Main notebook
├── README.md # This file
├── saved_weights/
│ └── model.weights.h5 # Best model weights (saved during training)
├── Sample results/
│ └── sample_results.png # Visualization results
└── chest_xray/ # Dataset directory (download separately)
├── train/
├── test/
└── val/
- Stage 1: VGG19 base fully frozen — only the classification head is trained (
lr=3e-4, 7 epochs) - Stage 2: VGG19
block5_conv1and above unfrozen for fine-tuning (lr=1e-5, up to 14 additional epochs) - Gradient clipping:
clipnorm=1.0on the Stage 2 optimizer prevents gradient explosion when unfreezing deep layers - Fresh callbacks:
EarlyStopping,ReduceLROnPlateau, and checkpoint are re-instantiated for Stage 2 to avoid stale state from Stage 1
- The Kaggle dataset's built-in
val/folder contains only 16 images — too small for reliable callback monitoring - The 624-image
test/set is used asvalidation_dataduring training for stableEarlyStoppingandReduceLROnPlateausignals - Final evaluation metrics are still computed on the same test set after training
- Target layer:
block5_conv3(last convolutional layer of VGG19) - Output: grayscale X-ray base image with red hotspot overlay (top 5% activations only)
- Applied per-image for the predicted class to highlight discriminative regions
- Training set is imbalanced (~3:1 PNEUMONIA:NORMAL)
- Per-class weights computed from folder counts and passed to
model.fit()viaclass_weight
jupyter notebook eXplainable-ai.ipynb- Open the notebook in VS Code
- Select your Python kernel (from the virtual environment)
- Run cells sequentially
- Base Model: VGG19 (pre-trained on ImageNet)
- Input Size: 256x256 RGB images
- Output: Binary classification (NORMAL / PNEUMONIA)
- Training: Two-stage — frozen head warm-up, then block5 fine-tuning
- Optimizer: Adam (
lr=3e-4Stage 1,lr=1e-5+clipnorm=1.0Stage 2) - Loss: Categorical Crossentropy with label smoothing (
0.05) - Regularization: Dropout (0.4 + 0.3), L2 on Dense layer
- Tested on: Mac M4 Pro with Metal GPU acceleration
The trained model provides:
- Accuracy: Binary classification accuracy
- Precision: True positive rate among positive predictions
- Recall: True positive rate among actual positives
- F1-Score: Harmonic mean of precision and recall
- Confusion Matrix: Visual representation of prediction performance
The Grad-CAM visualization highlights which regions of the X-ray image most influenced the model's prediction:
- Red/Bright areas: High influence on pneumonia detection
- Blue/Dark areas: Low influence
- Yellow/Green areas: Medium influence
This helps validate that the model focuses on clinically relevant regions.
Sevendi Eldrige Rifki Poluan
Chest X-Ray Images (Pneumonia) - Kaggle
- Grad-CAM Paper: Selvaraju et al., 2019
- VGG19 Model: Simonyan & Zisserman, 2015
