Convolutional neural network for classifying COVID-19 chest X-rays
I designed, trained and optimized a convolutional neural network (CNN) to distinguish normal chest X-rays from COVID-19 cases. The goal was not only high accuracy, but also choosing appropriate metrics, applying regularization and proposing systematic hyperparameter tuning so the model would generalize.
- Type
- Academic exercise · Computer vision
- Dataset
- COVID-19 X-ray (Kaggle) · ~2,100 images
- Stack
- TensorFlow · Keras · scikit-learn
- Result
- 92.86% accuracy · 94.29% COVID recall
01 The problem
During the pandemic, quickly classifying chest X-rays was a bottleneck. CNNs are particularly effective with images because they learn which features matter on their own, without requiring those features to be hand-coded. But a good model is not only its architecture: it also depends on metric selection, regularization and hyperparameter tuning. That was the core of the project.
02 Data and preprocessing
The dataset contains X-rays in two classes (COVID-19 and normal). Before training, I prepared the images:
- I resized them to 224×224 px, a standard input size for these architectures.
- I normalized pixel values to the range
[0, 1]by dividing by 255. - I applied data augmentation (±15° rotation, 10% zoom and horizontal flipping) to expand the dataset and reduce overfitting.
- I split the data into 80% training / 10% validation / 10% testing.
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255, # normalize to [0,1]
rotation_range=15, # rotate up to ±15°
zoom_range=0.1, # 10% zoom
horizontal_flip=True, # horizontal flip
validation_split=0.2
)
03 CNN architecture
I designed a network with three convolutional blocks (32 → 64 → 128 filters), incorporating BatchNormalization and Dropout as regularization from the beginning rather than as a final patch.
model = models.Sequential([
# Block 1
layers.Conv2D(32, (3,3), activation='relu', padding='same'),
layers.BatchNormalization(),
layers.MaxPooling2D(2,2),
layers.Dropout(0.25),
# ... blocks 2 and 3 (64 and 128 filters) ...
layers.Flatten(),
layers.Dense(256, activation='relu'),
layers.Dropout(0.5),
layers.Dense(1, activation='sigmoid') # binary output
])
For training I used the Adam optimizer with EarlyStopping (to prevent overtraining) and ReduceLROnPlateau (which lowers the learning rate when the model stalls). Training stopped automatically at epoch 35.
04 Results
The model achieved solid performance on the test set:
| Metric | Value | Ideal | Interpretation |
|---|---|---|---|
| Accuracy | 92.86% | → 100% | Strong overall performance |
| Recall (COVID) | 94.29% | → 100% | Detects 94% of real COVID cases |
| Precision (COVID) | 91.43% | → 100% | Low false-positive rate |
| F1-Score | 0.928 | → 1 | Strong precision/recall balance |
05 Optimization techniques
I combined three families of techniques with complementary effects on generalization:
- Batch normalization: normalizes activations by batch, allows higher learning rates and stabilizes training.
- Dropout: randomly deactivates neurons during training (0.25 in convolutional layers and 0.5 in the dense layer) to prevent memorization.
- Data augmentation: creates realistic variations of each image—essential when labeled images are scarce, as in medical contexts.
The result was stable convergence with a training/validation gap below 3%, indicating that the model is not overfitting.
06 Hyperparameter tuning
I proposed systematic tuning with Keras Tuner over the most influential parameters: lowering the learning rate with a scheduler, reducing the batch size, replacing Adam with AdamW and refining dropout. The comparison below presents the baseline model against a projected tuned scenario; it is not a second independent clinical validation.
Academic projection: +5.7 percentage points in accuracy and recall, with an estimated 33% reduction in error (MSE). These values do not replace independent evaluation or clinical validation.
Conclusion and future work
The project shows that a diagnostic support system requires balancing performance, methodological rigor and responsibility. A well-regularized CNN, evaluated with metrics chosen according to the real cost of each error, can deliver robust results. As a next step, I proposed exploring Transfer Learning with pretrained architectures such as ResNet50, EfficientNet and MobileNetV2, which often perform well on limited datasets.
Working on an image or prediction problem?
Classification, computer vision or predictive models—I can help take your idea to a prototype. Let’s talk.
Let’s talk →