THIS MODEL IS NOT APPROVED FOR CLINICAL USE
This cancer detection AI model is intended for research and educational purposes ONLY. It has NOT been:
- β Validated in clinical settings
- β Approved by FDA, EMA, or other regulatory bodies
- β Tested for safety in medical decision-making
- β Peer-reviewed or published in medical journals
- β Certified for diagnostic accuracy in real-world scenarios
- β Clinical diagnosis or screening
- β Patient care decisions
- β Medical treatment planning
- β Commercial medical applications
- β Any life-critical healthcare decisions
If you suspect cancer, consult a qualified medical professional immediately.
This project implements an advanced custom Convolutional Neural Network (CNN) architecture designed from scratch for binary cancer classification from medical imaging data. The model combines state-of-the-art deep learning techniques to achieve robust feature extraction and classification.
- Advanced CNN Architecture with modern deep learning components
- Multi-scale feature extraction using Inception-style modules
- Attention mechanisms (Channel + Spatial Attention - CBAM)
- Residual connections with Squeeze-and-Excitation blocks
- Transfer learning ready with customizable architecture
- Comprehensive data augmentation pipeline
- Detailed performance metrics and visualization tools
Input (224x224x3 RGB Images)
β
[Stage 1: Initial Feature Extraction]
β
[Stage 2: Residual Block + SE Attention]
β
[Stage 3: Inception Module + CBAM]
β
[Stage 4: Deep Feature Processing]
β
[Global Average Pooling]
β
[Dense Classification Head with Dropout]
β
Output (Binary: Benign/Malignant)
| Component | Description |
|---|---|
| Residual Connections | Skip connections for gradient flow and deeper networks |
| Squeeze-and-Excitation (SE) Blocks | Channel-wise feature recalibration |
| Inception Modules | Multi-scale feature extraction with parallel convolutions |
| CBAM Attention | Channel + Spatial attention for focused learning |
| Global Average Pooling | Reduces overfitting compared to fully connected layers |
| Dropout Regularization | Prevents overfitting in classification head |
- Input Shape: 224Γ224Γ3 (RGB images)
- Output: Binary classification (2 classes)
- Total Parameters: 2,563,793 (9.78 MB)
- Trainable Parameters: 2,551,185 (9.73 MB)
- Non-trainable Parameters: 12,608 (49.25 KB)
- Architecture Depth: 4 main processing stages
- Activation Functions: ReLU, Sigmoid (output)
- Optimization: Adam optimizer with learning rate scheduling
This model is trained on combined public medical imaging datasets:
- Total Training Samples: 61,876 images
- Total Validation Samples: 10,918 images
- Total Test Samples: 7,400 images
- Image Format: RGB images (224Γ224 pixels)
- Classes: Binary (Benign vs Malignant)
- Class Distribution:
- Benign: Higher representation
- Malignant: Lower representation
- Class Weights Applied: {0: 1.628, 1: 0.722} to handle imbalance
Applied augmentations to improve model generalization:
- Rotation: Β±20 degrees
- Width/Height Shift: Β±20%
- Shear Transformation
- Zoom: Β±20%
- Horizontal Flip
- Fill Mode: Nearest neighbor- Normalization: Pixel values scaled to [0, 1]
- Resizing: All images resized to 224Γ224
- Color Mode: RGB (3 channels)
- Batch Size: 32
- Total Epochs: 100 (early stopping may apply)
- Batch Size: 32
- Optimizer: Adam with learning rate decay
- Initial Learning Rate: 0.001
- Loss Function: Binary Cross-Entropy
- Hardware: CPU (Google Colab/Kaggle GPU recommended)
- Training Time per Epoch: ~2.5 hours on CPU (~5 seconds per step)
| Epoch | Train Acc | Val Acc | Train AUC | Val AUC | Train Loss | Val Loss | Time |
|---|---|---|---|---|---|---|---|
| 1 | 79.11% | 43.88% | 0.8545 | 0.5195 | 0.5827 | 1.5807 | 2h 44m |
| 2 | 87.09% | 61.95% | 0.9257 | 0.6797 | 0.3744 | 1.1490 | 2h 39m |
| 3 | 87.80% | ~65%* | 0.9400 | ~0.70* | 0.3183 | ~1.00* | In progress |
*Estimated based on training trend
Below are the comprehensive training metrics visualized across epochs:
Figure 1: Comprehensive view of all training metrics including Accuracy, Loss, AUC, Precision, Recall, and Learning Rate
Figure 2: Training vs Validation Accuracy showing model learning progression and potential overfitting
Figure 3: Training vs Validation Loss demonstrating convergence behavior
Figure 4: Area Under Curve metric showing discrimination capability between classes
Current Observations:
- β Training accuracy improving: 79% β 87% β 88%
β οΈ Validation accuracy lagging: 44% β 62% β ~65%β οΈ Gap indicates overfitting: Model memorizing training data- β AUC improving steadily: Shows good class discrimination ability
β οΈ High validation loss: Suggests need for regularization
Model Behavior:
- Overfitting detected: Training accuracy (87%) significantly higher than validation (62%)
- Learning rate decay: Gradually reducing from 0.001 to prevent overshooting
- Class imbalance handling: Class weights applied successfully
Python >= 3.8
TensorFlow >= 2.8.0
Keras >= 2.8.0
NumPy >= 1.21.0
Matplotlib >= 3.4.0
scikit-learn >= 0.24.0
Pillow >= 8.0.0
seaborn >= 0.11.0- Clone the repository:
git clone https://github.com/yourusername/cancer-detection-model.git
cd cancer-detection-model- Create virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Organize your dataset:
dataset/
βββ train/
β βββ benign/
β βββ malignant/
βββ validation/
β βββ benign/
β βββ malignant/
βββ test/
βββ benign/
βββ malignant/
python train.py --epochs 100 --batch-size 32 --learning-rate 0.001Training Options:
--epochs Number of training epochs (default: 100)
--batch-size Batch size for training (default: 32)
--learning-rate Initial learning rate (default: 0.001)
--dataset-path Path to dataset directory
--save-path Path to save trained modelAfter training, generate comprehensive plots:
from visualize_training import visualize_from_history
# If you have history object from training
visualize_from_history(history, save_dir='results/plots/')
# Or use manual data entry
from visualize_training import visualize_from_manual_data
visualize_from_manual_data()from tensorflow import keras
import numpy as np
from PIL import Image
# Load trained model
model = keras.models.load_model('best_multi_dataset_cancer_model.h5')
# Load and preprocess image
img = Image.open('test_image.jpg').resize((224, 224))
img_array = np.array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)
# Make prediction
prediction = model.predict(img_array)
confidence = prediction[0][0]
if confidence > 0.5:
print(f"Prediction: Malignant (Confidence: {confidence:.2%})")
else:
print(f"Prediction: Benign (Confidence: {1-confidence:.2%})")After training completion, evaluate with confusion matrix:
from sklearn.metrics import confusion_matrix, classification_report
import seaborn as sns
# Get predictions
y_pred = model.predict(test_generator)
y_pred_classes = (y_pred > 0.5).astype(int)
# Generate confusion matrix
cm = confusion_matrix(y_true, y_pred_classes)
# Visualize
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.title('Confusion Matrix')
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.savefig('confusion_matrix.png', dpi=300)print(classification_report(y_true, y_pred_classes,
target_names=['Benign', 'Malignant']))Expected output format:
precision recall f1-score support
Benign 0.85 0.82 0.83 4500
Malignant 0.80 0.84 0.82 2900
accuracy 0.83 7400
macro avg 0.83 0.83 0.83 7400
weighted avg 0.83 0.83 0.83 7400
cancer-detection-model/
β
βββ train.py # Main training script
βββ predict.py # Inference script
βββ model.py # Model architecture definition
βββ visualize_training.py # Visualization utilities
βββ utils.py # Helper functions
βββ requirements.txt # Python dependencies
βββ README.md # This file
β
βββ dataset/ # Dataset directory
β βββ train/ # 61,876 images
β βββ validation/ # 10,918 images
β βββ test/ # 7,400 images
β
βββ models/ # Saved models
β βββ best_multi_dataset_cancer_model.h5
β βββ model_architecture.png
β βββ checkpoints/
β
βββ results/ # Training results
β βββ plots/
β β βββ training_history_complete.png
β β βββ accuracy_plot.png
β β βββ loss_plot.png
β β βββ auc_plot.png
β βββ logs/
β βββ training_log.csv
β
βββ notebooks/ # Jupyter notebooks
βββ exploratory_analysis.ipynb
βββ model_evaluation.ipynb
- Upload notebook to Google Colab
- Enable GPU: Runtime β Change runtime type β GPU (T4)
- Mount Google Drive for dataset access:
from google.colab import drive
drive.mount('/content/drive')- Run training script
- Save model to Drive to prevent loss
Expected speedup: CPU (2.5h/epoch) β GPU (2-3 min/epoch) = 50x faster
- Create new notebook on Kaggle
- Add dataset or upload your own
- Enable GPU in Settings β Accelerator
- Verify phone number for GPU access
- Use commit mode for background training
GPU Quotas:
- Colab: ~12 hours per session (free tier)
- Kaggle: 30-41 hours per week
Your 100-epoch training:
- CPU: ~250 hours (10+ days) β Not feasible
- GPU: ~4-5 hours β Recommended
Solutions:
# 1. Increase dropout
model.add(Dropout(0.5)) # Instead of 0.3
# 2. Add L2 regularization
from tensorflow.keras.regularizers import l2
Dense(128, kernel_regularizer=l2(0.01))
# 3. Early stopping
from tensorflow.keras.callbacks import EarlyStopping
early_stop = EarlyStopping(monitor='val_loss', patience=10,
restore_best_weights=True)
# 4. More aggressive data augmentation
datagen = ImageDataGenerator(
rotation_range=40, # Increased from 20
width_shift_range=0.3, # Increased from 0.2
zoom_range=0.3,
horizontal_flip=True,
brightness_range=[0.8, 1.2] # New
)Current handling: Class weights applied (1.628 vs 0.722)
Additional options:
# 1. Oversample minority class
from imblearn.over_sampling import SMOTE
X_train, y_train = SMOTE().fit_resample(X_train, y_train)
# 2. Use focal loss (focuses on hard examples)
def focal_loss(gamma=2., alpha=0.25):
def focal_loss_fixed(y_true, y_pred):
pt = tf.where(tf.equal(y_true, 1), y_pred, 1 - y_pred)
return -K.mean(alpha * K.pow(1. - pt, gamma) * K.log(pt))
return focal_loss_fixed
model.compile(loss=focal_loss())Out of Memory Error:
# Reduce batch size
train_generator = datagen.flow_from_directory(
batch_size=16 # Instead of 32
)Slow Training on CPU:
β
Solution: Use Google Colab or Kaggle GPU
Expected: 50-100x speedup
Your case: 2.5h/epoch β 2-3min/epoch
Validation Accuracy Stuck:
- Check for data leakage
- Verify validation set is representative
- Reduce model complexity
- Increase regularization
Model Not Improving After Epoch X:
# Implement learning rate reduction
from tensorflow.keras.callbacks import ReduceLROnPlateau
reduce_lr = ReduceLROnPlateau(
monitor='val_loss',
factor=0.5,
patience=5,
min_lr=1e-7
)- ResNet: Deep Residual Learning for Image Recognition
- Inception: Going Deeper with Convolutions
- SENet: Squeeze-and-Excitation Networks
- CBAM: Convolutional Block Attention Module
Copyright (C) 2025-2026 Abhijeet Prabhakar
This project is licensed under the GNU Affero General Public License v3.0 with additional restrictions for medical use.
ADDITIONAL RESTRICTION FOR MEDICAL/CLINICAL USE:
This software is provided for research and educational purposes only. Any clinical, diagnostic, or commercial medical use requires:
- Explicit written permission from the copyright holder
- Appropriate regulatory approval (FDA, CE marking, etc.)
- Clinical validation studies
- Compliance with medical device regulations
β
Academic research
β
Educational purposes
β
Algorithm development
β
Non-commercial experimentation
β
Dataset benchmarking
β Clinical diagnosis
β Patient care decisions
β Medical device integration
β Commercial medical products
β Healthcare screening programs
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. The authors and copyright holders shall not be liable for any claims, damages, or liabilities arising from the use of this software, including but not limited to medical misdiagnosis, patient harm, or financial losses.
Email: mr.robotcyber01@gmail.com
Include in your inquiry:
- Intended use case
- Organization details
- Expected deployment scale
- Regulatory compliance plans
Open to collaborations with:
- Academic institutions
- Research laboratories
- Non-profit healthcare organizations
Please use GitHub Issues for:
- Bug reports
- Feature requests
- Documentation improvements
- Performance issues
- TensorFlow and Keras development teams
- Medical imaging dataset contributors
- Open-source deep learning community
- Researchers advancing AI in healthcare
- Initial release
- Custom CNN architecture with attention mechanisms
- Binary classification (benign/malignant)
- 2.5M parameter model
- Trained on 80K+ medical images
- Comprehensive data augmentation
- Training visualization tools
- β Architecture defined and tested
- π Training in progress (Epoch 3/100)
- β³ Expected completion: ~250 hours on CPU
- π‘ Recommendation: Continue on GPU for 4-5 hours total
If you find this project useful for your research, please consider giving it a star! It helps others discover the project and motivates continued development.
Remember: This is a research tool. Always consult qualified medical professionals for health concerns.