Skip to content
ModelListedExternal
Download

Pneumonia Classifier

A lightweight custom CNN for binary classification of chest X-rays (Normal vs Pneumonia) with Grad-CAM explainability.

KindOtherVersionRevision 4d2f29c

Model source

Kind
Other
File size
58.4–74.7 KB
Version
vRevision 4d2f29c
Updated
Sep 17, 2026
Downloads
0
Source
Hugging Face

Source description

Pneumonia Classifier (Custom CNN)

A lightweight custom CNN for binary classification of chest X-rays (Normal vs Pneumonia) with Grad-CAM explainability.

Model Details

  • Architecture: Custom CNN (Net) — 9 Conv2d layers + Global Average Pooling
  • Input: RGB chest X-ray images, resized to 224×224
  • Output: Binary classification (Normal / Pneumonia) with softmax probabilities
  • Parameters: ~35K (lightweight, edge-friendly)
  • Framework: PyTorch

Demo

Try the live demo: Pneumonia AI Diagnostic Suite

Performance

MetricScore
Accuracy98%
Pneumonia Precision97%
Pneumonia Recall100%
Normal Precision100%
Normal Recall96%
Macro F1-Score0.98
ROC AUC1.00

Evaluated on 57 test samples with optimized threshold (0.1) for high sensitivity.

Cross-Validation (5-Fold Stratified)

FoldAccuracyF1-Score
192.86%0.927
297.62%0.976
392.86%0.933
495.24%0.950
597.62%0.977
Mean95.24%0.953

Training

  • Optimizer: SGD with Momentum (0.8)
  • Loss: Negative Log Likelihood (NLL) on log_softmax output
  • Epochs: 18 (early stopping, patience=5)
  • Best Training Accuracy: 98.25%
  • Data Augmentation: Baseline (no augmentation) — the unaugmented model performed better (95.24% CV vs 92.38% with heavy augmentation)

Quantization (INT8)

FP32INT8Improvement
Size243 KB52 KB4.64x smaller
Latency108 ms27 ms3.99x faster

Usage

import torch
from huggingface_hub import hf_hub_download
from pneumonia_classifier.ml.model.arch import Net

# Download model
model_path = hf_hub_download(
    repo_id="24f2004275/pneumonia_classifier",
    filename="pneumonia_classifier_cnn_uza7heywpgthvahb.pt"
)

# Load model
model = Net()
model.load_state_dict(torch.load(model_path, map_location="cpu", weights_only=False))
model.eval()

# Inference
from torchvision import transforms

transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

# image = Image.open("chest_xray.jpg").convert("RGB")
# tensor = transform(image).unsqueeze(0)
# with torch.no_grad():
#     output = model(tensor)
#     probs = torch.exp(output)
#     prediction = "Pneumonia" if probs.argmax() == 1 else "Normal"

Available Models

FileDescription
pneumonia_classifier_cnn_uza7heywpgthvahb.ptPrimary model (FP32, best accuracy)
pneumonia_classifier_cnn_int8_tgzwzsqwqw54dahb.ptINT8 quantized variant
pneumonia_classifier_cnn_int8_ra32tviwyo4u3ahb.ptINT8 quantized variant (alt)
pneumonia_classifier_aug_baseline_no_augmentation_zvyot3qwqgnsfahb.ptBaseline (no augmentation)
pneumonia_classifier_aug_augmented_heavy_wypxe3qwqsx75ahb.ptHeavy augmentation variant

Explainability

This model integrates Grad-CAM (Gradient-weighted Class Activation Mapping) to visualize which regions of the chest X-ray the model focuses on for its predictions. The heatmap highlights areas of radiographical density associated with pneumonia.

License

MIT License

Citation

@misc{pneumonia_classifier,
  title={Pneumonia Detection from Chest X-Rays using Custom CNN with Grad-CAM},
  author={Ayush Dubey},
  year={2024},
  url={https://huggingface.co/24f2004275/pneumonia_classifier}
}

Sources

1 source
Hugging FacePrimaryVersion 4d2f29c340a9da499ba0da528ca47a8889f19799
huggingface.co/24f2004275/pneumonia_classifier/tree/4d2f29c340a9da499ba0da528ca47a8889f19799

Verified Sep 17

Model artifacts

5 artifacts

pneumonia_classifier_aug_augmented_heavy_wypxe3qwqsx75ahb.pt

pt · 74.7 KB · SHA-256 1664549ec1d0…b6d3 · Hugging Face

Download

pneumonia_classifier_aug_baseline_no_augmentation_zvyot3qwqgnsfahb.pt

pt · 74.7 KB · SHA-256 7b7e6c222351…9906 · Hugging Face

Download

pneumonia_classifier_cnn_int8_ra32tviwyo4u3ahb.pt

pt · 58.4 KB · SHA-256 7baf81828226…6fe7 · Hugging Face

Download

pneumonia_classifier_cnn_int8_tgzwzsqwqw54dahb.pt

pt · 74.4 KB · SHA-256 a5df6f830da4…040e · Hugging Face

Download

pneumonia_classifier_cnn_uza7heywpgthvahb.pt

pt · 74.7 KB · SHA-256 d72a3192cbac…d1d2 · Hugging Face

Download

Source excerpts

2 excerpts

--- language: - en library_name: pytorch license: mit tags: - pneumonia - chest-xray - medical-imaging - cnn - image-classification - grad-cam - explainability - pytorch - custom-cnn pipeline_tag: image-classification metrics: - accuracy - f1 - precision - recall - roc_auc datasets: - chest-xray-pneumonia framework: - PyTorch - TorchVision --- # Pneumonia Classifier (Custom CNN) A lightweight custom CNN for binary classification of chest X-rays (Normal vs Pneumonia) with Grad-CAM explainability. ## Model Details - **Architecture**: Custom CNN (Net) — 9 Conv2d layers + Global Average Pooling - **Input**: RGB chest X-ray images, resized to 224×224 - **Output**: Binary classification (Normal / Pneumonia) with softmax probabilities - **Parameters**: ~35K (lightweight, edge-friendly) - **Framework**: PyTorch ## Demo Try the live demo: [Pneumonia AI Diagnostic Suite](https://pneumonia-classifie.streamlit.app/) ## Performance | Metric | Score | |--------|-------| | Accuracy | 98% | | Pneumonia Precision | 97% | | Pneumonia Recall | 100% | | Normal Precision | 100% | | Normal Recall | 96% | | Macro F1-Score | 0.98 | | ROC AUC | 1.00 | > Evaluated on 57 test samples with optimized threshold (0.1) for high sensitivity. ### Cross-Validation (5-Fold Stratified) | Fold | Accuracy | F1-Score | |------|----------|----------| | 1 | 92.86% | 0.927 | | 2 | 97.62% | 0.976 | | 3 | 92.86% | 0.933 | | 4 | 95.24% | 0.950 | | 5 | 97.62% | 0.977 | | **Mean** | **95.24%** | **0.953** | ## Training - **Optimizer**: SGD with Momentum (0.8) - **Loss**: Negative Log Likelihood (NLL) on log_softmax output - **Epochs**: 18 (early stopping, patience=5) - **Best Training Accuracy**: 98.25% - **Data Augmentation**: Baseline (no augmentation) — the unaugmented model performed better (95.24% CV vs 92.38% with heavy augmentation) ## Quantization (INT8) | | FP32 | INT8 | Improvement | |--|------|------|-------------| | Size | 243 KB | 52 KB | **4.64x** smaller | | Latency | 108 ms | 27 ms | **3.99x** faster | ## Usage ```python import torch from huggingface_hub import hf_hub_download from pneumonia_classifier.ml.model.arch import Net # Download model model_path = hf_hub_download( repo_id="24f2004275/pneumonia_classifier", filename="pneumonia_classifier_cnn_uza7heywpgthvahb.pt" ) # Load model model = Net() model.load_state_dict(torch.load(model_path, map_l...

Source context: 0 downloads · 0 likes · Pipeline image-classification · Library pytorch · Repo 24f2004275/pneumonia_classifier