Skip to content
Célya Bakala-Yame
← Back to work

Pneumonia Detection CNN

Chest X-Ray Classification with Deep Learning

A computer-vision project exploring convolutional neural networks for classifying chest X-ray images.

Educational project, not a medical diagnostic system.

Type
Deep Learning / Computer Vision
PythonTensorFlowKerasCNN

Overview

This project explores how a convolutional neural network can classify chest X-ray images as NORMAL or PNEUMONIA. It covers a complete deep learning pipeline for image classification: loading and preprocessing the images, defining a CNN, training it and evaluating it.

It was developed as a Jupyter notebook in Google Colab for an AI & Deep Learning course. It is a learning project, not a medical diagnostic system.

Problem

Chest X-rays are a standard tool for detecting pneumonia, but reading them takes expertise that is not always available. The project asks a narrower technical question: can a compact CNN learn to separate NORMAL from PNEUMONIA images, and how reliable is that separation?

Dataset

The dataset contains approximately 5,856 chest X-ray images in two classes, NORMAL and PNEUMONIA. It is imbalanced, with more PNEUMONIA than NORMAL images, which affects how the results should be read. The VIRUS / BACTERIA distinctions present in the dataset were also explored.

Preprocessing

  • Images are read with OpenCV, converted to grayscale and resized to 150 × 150 pixels.
  • Labels are encoded as 0 for NORMAL and 1 for PNEUMONIA.
  • Pixel values are scaled between 0 and 1, and arrays are reshaped for a single-channel CNN input.
  • The data is split into 80% training and 20% test sets with a fixed random state.

CNN approach

The model is a sequential CNN built with Keras on TensorFlow: three convolutional layers (32, 64 and 64 filters of size 3 × 3) with ReLU activations and two max-pooling stages, followed by a dense layer of 64 units and a single sigmoid output that returns the probability of pneumonia.

Training

The model is compiled with the Adam optimizer and binary cross-entropy, then trained for 5 epochs with a batch size of 32, holding out 20% of the training data for validation.

Training and validation curves for loss and accuracy were plotted to check generalisation. A second version of the network adds a Dropout layer (rate 0.5) to test whether regularisation helps.

Evaluation

The model was evaluated on the held-out test set using accuracy and a confusion matrix, to see how errors are distributed between the two classes. Accuracy alone is not enough on an imbalanced dataset, which is why the confusion matrix is part of the evaluation. Exact figures are available in the notebook.

Error analysis

The confusion matrix shows where the model confuses the two classes. In a medical context, false negatives, meaning missed pneumonia cases, are the most costly errors, and class imbalance can bias a model toward predicting pneumonia more often. Precision and recall are the natural next metrics to examine.

Limitations

  • This is an educational project and must not be used for diagnosis.
  • The dataset is imbalanced, and precision and recall are not yet reported.
  • Training is short (5 epochs) and evaluation relies on a single train/test split.
  • Data augmentation, class balancing and more advanced architectures have not been explored yet.

What I learned

Skills learned by building

Image preprocessing with OpenCVBuilding CNNs with Keras and TensorFlowBinary classification with a sigmoid outputReading training and validation curvesEvaluating with a confusion matrixClass imbalance as an evaluation issueRegularisation with dropout

Resources