Prerequisites: Python 3.8+, TensorFlow/Keras installed. The training takes 15-30 minutes on CPU, 5-10 minutes with GPU.
What You’ll Build
A Convolutional Neural Network that:- Classifies chest X-ray images as NORMAL or PNEUMONIA
- Achieves 80-85% accuracy on test data
- Reaches 96%+ sensitivity (catches most pneumonia cases)
- Delivers 0.90-0.93 AUC-ROC score
Download the Chest X-Ray Dataset
The model requires the Kaggle Chest X-Ray Pneumonia dataset (~1.15 GB, 5,863 images).Get the dataset:Verify your setup:
- Visit kaggle.com/datasets/paultimothymooney/chest-xray-pneumonia
- Download
archive.zip(requires free Kaggle account) - Extract to your project’s
data/directory
Train Your Model
Train the 3-layer CNN with data augmentation and early stopping.What happens during training:
- 20 epochs maximum (early stopping may finish sooner)
- Batch size: 32
- Data augmentation: rotations (±15°), shifts (±10%), zoom (±10%)
- Saves best model to
models/best_model.keras - Generates training history graphs in
results/
- CPU: 15-30 minutes
- GPU: 5-10 minutes
If you encounter memory errors, edit
src/train.py and reduce the batch_size from 32 to 16 or 8.Evaluate Model Performance
Run comprehensive evaluation on the test set with clinical metrics.Evaluation outputs:The script generates four key visualizations in
results/:training_history.png- Loss and accuracy curves over epochsconfusion_matrix.png- True/false positives and negativesroc_curve.png- ROC curve with AUC scorepredictions.png- Sample predictions with ground truth
| Metric | Target | Clinical Meaning |
|---|---|---|
| Accuracy | 80-85% | Overall correctness |
| Sensitivity (Recall) | >95% | Catches pneumonia cases |
| Specificity | 70-80% | Identifies healthy cases |
| AUC-ROC | 0.90-0.93 | Discrimination ability |
High sensitivity is critical - missing a pneumonia case (false negative) is worse than a false alarm (false positive) in clinical screening.
Review Results and Next Steps
Check your model’s performance and understand the outputs.Review the confusion matrix (Understanding overfitting:You may notice the training accuracy is higher than test accuracy. This is expected with medical imaging data. The model uses dropout (0.5) and data augmentation to minimize this.Next steps:
results/confusion_matrix.png):- True Positives (TP): Correctly identified pneumonia
- True Negatives (TN): Correctly identified normal
- False Positives (FP): Healthy classified as pneumonia (acceptable for screening)
- False Negatives (FN): Missed pneumonia cases (minimize these)
- Review
DISEÑO_MODELO.mdfor architecture details - Check
src/model.pyfor the CNN implementation - Read
PLANTEAMIENTO.mdfor clinical context - Explore improvements: transfer learning, Grad-CAM visualization
Quick Command Reference
Troubleshooting
Dataset not found error
Dataset not found error
data/chest_xray/. Run verificar_dataset.py to confirm structure.Out of memory error
Out of memory error
src/train.py, find the line with batch_size=32 and reduce to 16 or 8.Training is very slow
Training is very slow
Solution: Normal on CPU. Expected time: 15-30 minutes. To speed up:
- Use GPU if available
- Reduce epochs in
src/train.py - Reduce training data size (not recommended)
Model accuracy is low (<75%)
Model accuracy is low (<75%)
Check these issues:
- Dataset structure is correct (run
verificar_dataset.py) - Images are loading properly (check
src/data_loader.pyoutput) - Training completed at least 10 epochs
- Class imbalance is being handled (weighted loss)
Project Structure
Dataset Details
- Source: Kaggle - Chest X-Ray Images (Pneumonia)
- Author: Paul Mooney
- Size: 5,863 pediatric chest X-rays (ages 1-5)
- License: CC BY 4.0
- Classes: NORMAL (1,575 images) and PNEUMONIA (4,265 images)
- Split: 89% train, 11% test
CNN Architecture
Loss: Categorical Crossentropy
Regularization: Dropout + Data Augmentation
You’re now ready to train and evaluate a pneumonia classification model. For detailed architecture explanation, see
DISEÑO_MODELO.md. For clinical context, read PLANTEAMIENTO.md.