This project implements a deep learning-based system for detecting lung cancer from CT scan images using state-of-the-art Convolutional Neural Networks (CNNs) with transfer learning. The system classifies CT images into three categories: Benign, Malignant, and Normal. After evaluating multiple pre-trained architectures, MobileNetV2 emerged as the best-performing model with fine-tuning, achieving high accuracy and robust generalization.
The project includes comprehensive exploratory data analysis, model comparison, performance evaluation, and interpretability using Grad-CAM visualizations to provide explainable AI insights for medical diagnosis.
Dataset Link: https://www.kaggle.com/datasets/rayhankhan831/lung-cancer-ct-scan-image-for-federated-learning
The dataset consists of 3,206 lung CT scan images distributed across three classes:
| Class | Count | Percentage |
|---|---|---|
| Malignant | 1,308 | 40.8% |
| Normal | 986 | 30.8% |
| Benign | 912 | 28.4% |
Sample CT scan images from each class (Benign, Malignant, Normal)
The dataset is sourced from multiple clients (Client1-Client4), simulating a federated learning scenario, though this implementation uses centralized training for model comparison.
- Total Images: 3,206
- Image Format: CT scan images (grayscale converted to RGB)
- Input Size: 224x224 pixels (resized for model compatibility)
- Classes: 3 (Benign, Malignant, Normal)
- Split: Train (80%), Validation (10%), Test (10%)
To enhance model generalization and prevent overfitting, the following augmentation techniques were applied to the training data:
- Rescaling: Pixel values normalized to [0,1]
- Random Horizontal Flip: 50% probability
- Random Rotation: Up to 15 degrees
- Random Zoom: Up to 15%
- Width/Height Shift: Up to 10%
Four pre-trained CNN architectures were evaluated using transfer learning:
- MobileNetV2 - Lightweight and efficient architecture
- EfficientNetV2B0 - State-of-the-art efficiency
- DenseNet121 - Dense connectivity architecture
- Xception - Depthwise separable convolutions
Each model was initialized with ImageNet weights, and the top classification layer was replaced with a custom head:
- Global Average Pooling
- Dense Layer (256 units, ReLU activation)
- Dropout (0.5) for regularization
- Output Layer (3 units, Softmax activation)
- Optimizer: Adam
- Loss Function: Categorical Crossentropy
- Batch Size: 32
- Initial Epochs: 10
- Fine-tuning Epochs: 10
- Callbacks: Early Stopping, ReduceLROnPlateau, ModelCheckpoint
After initial training with frozen base layers, the best model (MobileNetV2) was fine-tuned by:
- Unfreezing the top 30 layers
- Using a lower learning rate (1e-5)
- Training for an additional 10 epochs
| Model | Accuracy | Precision | Recall | F1 Score | AUC |
|---|---|---|---|---|---|
| MobileNetV2 | 87.85% | 87.89% | 87.85% | 87.26% | 95.65% |
| Xception | 77.88% | 77.47% | 77.88% | 75.28% | 90.88% |
| DenseNet121 | 74.14% | 75.73% | 74.14% | 67.46% | 91.46% |
| EfficientNetV2B0 | 40.81% | 16.65% | 40.81% | 23.66% | 59.67% |
Comparison of accuracy across different CNN architectures
MobileNetV2 significantly outperformed other models, demonstrating excellent classification capabilities for lung CT scan analysis. The high AUC score (95.65%) indicates strong discrimination ability between classes.
Confusion Matrix:
| Benign | Malignant | Normal | |
|---|---|---|---|
| Benign | 61 | 20 | 11 |
| Malignant | 8 | 123 | 0 |
| Normal | 0 | 0 | 98 |
Performance Metrics:
- Overall Accuracy: 87.85%
- Precision (Weighted): 87.89%
- Recall (Weighted): 87.85%
- F1 Score (Weighted): 87.26%
- AUC (Macro): 95.65%
Class-wise Performance:
- Benign: Precision 88%, Recall 66%, F1 76%
- Malignant: Precision 86%, Recall 94%, F1 90%
- Normal: Precision 90%, Recall 100%, F1 95%
The model shows exceptional performance in detecting malignant and normal cases, with slightly lower recall for benign cases, which is clinically acceptable as the priority is to identify malignant cases.
Gradient-weighted Class Activation Mapping (Grad-CAM) provides visual explanations of the model's decision-making process by highlighting the regions in CT images that most influenced the prediction.
Grad-CAM visualization showing the model's focus regions for lung cancer detection
Random test samples with actual labels, predictions, and confidence scores
The Grad-CAM visualizations show that the model focuses on relevant anatomical regions (lung nodules, tissue abnormalities) when making predictions, providing interpretability crucial for medical applications.
- Python 3.8+
- TensorFlow 2.19.0
- CUDA-capable GPU (recommended)
- Clone the repository:
git clone https://github.com/stephinjacob007/Lung-Cancer-Detection-Transfer-Learning.git
cd Lung-Cancer-Detection-Transfer-Learning- Install dependencies:
pip install -r requirements.txt- Download the dataset:
- Place the dataset in the appropriate directory structure
- Dataset should have the following structure:
LungData/
โโโ Client1/
โ โโโ Benign/
โ โโโ Malignant/
โ โโโ Normal/
โโโ Client2/
โ โโโ Benign/
โ โโโ Malignant/
โ โโโ Normal/
โโโ Client3/
โ โโโ Benign/
โ โโโ Malignant/
โ โโโ Normal/
โโโ Client4/
โโโ Benign/
โโโ Malignant/
โโโ Normal/
- Open the Jupyter Notebook:
jupyter notebook Lung_Cancer_Detection_CNNs.ipynb- Execute cells sequentially to:
- Load and preprocess data
- Perform exploratory data analysis
- Train models
- Evaluate performance
- Generate visualizations
Lung-Cancer-Detection-Transfer-Learning/
โโโ Lung_Cancer_Detection_CNNs.ipynb # Main Jupyter notebook
โโโ requirements.txt # Python dependencies
โโโ Results/ # README images
| โโโ Confusion-Matrices/ # Confusion matrices of all models
โ โโโ Models/ # Accuracy and Loss curves of models
| โโโ Class_Distribution.png
โ โโโ Grad-CAM_Example.png
โ โโโ Model_Comparison.png
โ โโโ Prediction_With_GRAD-CAM.png
โ โโโ Sample_Images.png
|
โโโ README.md
| Layer Type | Output Shape | Parameters |
|---|---|---|
| MobileNetV2 Base (Frozen) | (7, 7, 1280) | 2.2M |
| Global Average Pooling | (1280) | 0 |
| Dense (ReLU) | (256) | 327,936 |
| Dropout (0.5) | (256) | 0 |
| Dense (Softmax) | (3) | 771 |
Total Parameters: ~2.6M (after fine-tuning)
- Lightweight: Fewer parameters suitable for deployment
- Efficiency: Depthwise separable convolutions reduce computational cost
- Performance: Best accuracy among tested models
- Generalization: High AUC score indicates robust discrimination
- Accuracy: Overall correctness of predictions
- Precision: Proportion of correct positive predictions
- Recall: Proportion of actual positives correctly identified
- F1 Score: Harmonic mean of precision and recall
- AUC-ROC: Area under the Receiver Operating Characteristic curve
-
Early Detection: The model can assist radiologists in early detection of lung cancer, potentially improving patient outcomes through timely intervention.
-
Reduced Subjectivity: AI-based analysis provides consistent and reproducible results, reducing inter-observer variability.
-
Triage Support: Can prioritize high-risk cases for immediate review by specialists.
-
Educational Tool: Grad-CAM visualizations help in understanding radiological features associated with different classes.
Fine-tuning the MobileNetV2 model improved performance:
| Metric | Before Fine-tuning | After Fine-tuning |
|---|---|---|
| Training Accuracy | 87.85% | 88.18% |
| Validation Accuracy | 86.29% | 86.60% |
| Test Accuracy | 87.85% | ~88% |
Training and validation accuracy/loss curves during fine-tuning
- Dataset sourced from Kaggle: Lung Cancer CT Scan Image for Federated Learning
- TensorFlow and Keras teams for providing pre-trained models
- Research community for advancing medical imaging AI
Project Link: https://github.com/stephinjacob007/Lung-Cancer-Detection-Transfer-Learning
This project is for research and educational purposes only. The model should not be used as a standalone diagnostic tool. Always consult qualified medical professionals for clinical decisions. The "Not for diagnostic use" label on predictions emphasizes this limitation.
- Implement federated learning approach
- Integrate with a web-based interface for clinical use
- Expand dataset with more diverse CT scans
- Implement multi-modal analysis (combine with patient history)
- Deploy as a mobile application using TensorFlow Lite
- Ensemble methods for improved accuracy
- 3D CNN for volumetric analysis of CT scans




