CoolFace
Apppublic

benedictpepper/pneumonia-classifier-efficientnetb4

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
App README

EfficientNetB4 Pneumonia Classification: Fixed Size vs. Progressive Resizing

![License: MIT](LICENSE) ![Python 3.9+](https://www.python.org/) ![TensorFlow 2.19](https://www.tensorflow.org/) ![Platform: Kaggle](https://www.kaggle.com/)

Paper: Perbandingan Strategi Pelatihan EfficientNetB4 dengan dan tanpa Progressive Resizing untuk Klasifikasi Pneumonia Bakterial dan Viral pada Citra X-Ray Dada Presented at SEMNAS (National Seminar on Technology and Applied Science), 2025.

Overview

This repository accompanies a comparative study evaluating two training strategies for EfficientNetB4 on the task of distinguishing bacterial from viral pneumonia in chest X-ray images:

  1. 1.Fixed Size — trains exclusively at the target resolution (384 × 384 px) using a two-phase head-training + global fine-tuning schedule.
  2. 2.Progressive Resizing — a curriculum learning approach that trains through progressively higher resolutions (224 → 256 → 384 px) with continuous weight transfer between stages.

Both models share an identical architecture, loss function, optimizer, and augmentation pipeline. The only variable is the training resolution schedule.

Key finding: Fixed-size training at 384 × 384 px achieves 86.92% accuracy and 0.9198 AUC, outperforming Progressive Resizing (78.46%, AUC 0.8011) by 8.46 percentage points, despite using fewer total epochs (32 vs 41). The result suggests that sustained high-resolution exposure during fine-tuning is more critical than curriculum-based resolution scaling for this task.


Results

Main Experiments — Test Set (N=390, 242 bacterial : 148 viral, with 2-step TTA)

ModelStrategyEpochsAccuracyROC AUCMacro F1Viral Recall
EfficientNetB4Fixed Size (384×384)3286.92%0.919885.02%67.57%
EfficientNetB4Progressive Resizing (224→256→384)4178.46%0.801174.72%52.70%

Per-Class Metrics

ClassPrecision (Fixed)Recall (Fixed)F1 (Fixed)Precision (Prog)Recall (Prog)F1 (Prog)
Bacterial83.28%98.76%90.36%76.51%94.21%84.44%
Viral97.09%67.57%79.68%84.78%52.70%65.00%
Note: The test set uses the original Kaggle test/PNEUMONIA distribution (unbalanced: 242 bacterial, 148 viral). Both models show higher false-negative rates for the viral class, reflecting the intrinsic difficulty of this clinically important sub-task.

Legacy Architecture Comparison (v1 pipeline, archive/legacy_experiments/)

ModelAccuracy
VGG16 v255.47%
EfficientNetB4 v152.34%
EfficientNetB0 v240.62%
ResNet50 v137.50%

Architecture

EfficientNetB4 (ImageNet pretrained, include_top=False)
    → top_activation feature maps  [None, H, W, 1792]
    ├── GlobalAveragePooling2D      → [None, 1792]
    └── GlobalMaxPooling2D          → [None, 1792]
    → Concatenate                   → [None, 3584]   ← Concat Pooling
    → Dense(512, activation='swish')
    → BatchNormalization
    → Dropout(0.5)
    → Dense(2, activation='softmax', dtype='float32')

Total parameters:      18,594,913
Head parameters:          921,090
Fine-tuned (block6a+):  9,556,294
ComponentDetail
LossCategoricalFocalCrossentropy (α=0.25, γ=2.0, label_smoothing=0.1)
OptimizerAdamW + LossScaleOptimizer (fp16)
LR — head1×10⁻³
LR — Fixed Size FT1×10⁻⁴ → 6.25×10⁻⁶ (ReduceLROnPlateau ×0.5)
LR — Progressive FTCosineDecayRestarts (initial 1×10⁻⁵, tmul=2.0, mmul=0.9)
AugmentationCLAHE · ShiftScaleRotate · RandomBrightnessContrast · HorizontalFlip · MixUp
TTA2-step deterministic (original + H-flip)
PlatformKaggle Notebooks — Tesla P100-PCIE-16GB
FrameworkTensorFlow 2.19.0, Keras, mixed precision fp16

Dataset

All experiments use the Kermany et al. Chest X-Ray dataset (bacterial vs. viral pneumonia subset).

  • Source: Kaggle — Chest X-Ray Images (Pneumonia)
  • Classes used: Bacterial Pneumonia (label 0), Viral Pneumonia (label 1). Normal class excluded.
  • Labeling: Derived from filename prefix (bacteria_* → 0, virus_* → 1)

Split protocol (v2 pipeline):

SubsetBacterialViralTotal
Training (80% of pool)~2,030~1,0763,106
Validation (20% of pool)~508~269777
Test (Kaggle original, isolated)242148390
Pool (train + val combined)2,5381,3453,883
Class imbalance handling: Computed via sklearn.compute_class_weight('balanced'). Bacterial weight = 0.765 · Viral weight = 1.444. No physical oversampling was performed.

Visualizations

Architecture & Pipeline

<table> <tr> <td align="center"><b>Model Architecture — Concat Pooling</b></td> <td align="center"><b>Training Strategy Comparison</b></td> </tr> <tr> <td><img src="figures/01architectureconcatpooling.png" width="420"/></td> <td><img src="figures/02trainingstrategycomparison.png" width="420"/></td> </tr> <tr> <td align="center" colspan="2"><b>Data Pipeline &amp; Augmentation Strategy</b></td> </tr> <tr> <td colspan="2" align="center"><img src="figures/03datapipeline.png" width="840"/></td> </tr> </table>

Experimental Results (from Kaggle Execution)

<table> <tr> <td align="center"><b>ROC Curve — Both Models (N=390)</b></td> <td align="center"><b>Confusion Matrix — Both Models (N=390, TTA)</b></td> </tr> <tr> <td><img src="kaggleoutput/roccomparison.png" width="420"/></td> <td><img src="kaggleoutput/confusionmatrixcomparison.png" width="420"/></td> </tr> <tr> <td align="center"><b>Training Curves — Fixed Size (32 epochs)</b></td> <td align="center"><b>Training Curves — Progressive Resizing (41 epochs)</b></td> </tr> <tr> <td><img src="kaggleoutput/learningcurvesfixed.png" width="420"/></td> <td><img src="kaggleoutput/learningcurves_progressive.png" width="420"/></td> </tr> </table>

Grad-CAM Interpretability: Fixed Size vs. Progressive Resizing

<table> <tr> <td align="center"><b>Grad-CAM Comparison (Both Models)</b></td> </tr> <tr> <td align="center"><img src="figures/09gradcamcomparison.png" width="840"/></td> </tr> </table>

Grad-CAM activations computed on the top_activation layer (12×12×1,792 feature maps). Fixed Size (left column) shows precise, focused activation on the pathological lung parenchyma. Progressive Resizing (right column) shows slightly more diffuse activation patterns. Both models misclassify the viral samples (bottom two rows), reacting to the bilateral interstitial infiltrates but failing to find sufficient discriminative features for a correct viral prediction.

Repository Structure

.
├── kaggle_pipeline/           # 8-cell Kaggle notebook, broken into scripts
│   ├── 01_setup_and_imports.py
│   ├── 02_data_pipeline.py    # tf.data + Albumentations + MixUp
│   ├── 03_model_architecture.py  # Concat Pooling + Focal Loss
│   ├── 04_experiment_1_fixed_size.py
│   ├── 05_experiment_1_evaluation.py
│   ├── 06_experiment_2_progressive.py
│   ├── 07_experiment_2_evaluation.py
│   ├── 08_final_comparison_and_gradcam.py
│   └── README.md
│
├── kaggle_output/             # Figures and execution log from Kaggle run
│   ├── *.png                  # Training curves, ROC, confusion matrices
│   ├── gradcam_images/        # Grad-CAM sample visualizations
│   └── pneumonia-fix-vs-progressive-resizing-v2.md  # Full execution log
│
├── figures/                   # Generated diagrams (architecture, pipeline, strategy)
│   ├── 01_architecture_concat_pooling.png
│   ├── 02_training_strategy_comparison.png
│   └── 03_data_pipeline.png
├── paper/                     # Conference paper (manuskrip_deep_learning.md)
├── demo/                      # Flask demo — Fixed Size vs Progressive comparison
│   ├── app.py
│   ├── model.py               # .keras loader + Concat Pooling Grad-CAM
│   ├── templates/index.html
│   └── static/
│       ├── style.css
│       └── script.js
│
├── models/                    # Model weights — NOT tracked in git
│   ├── .gitkeep
│   ├── README.md              # Download instructions
│   ├── fixed_size_model.keras     ← download from GitHub Releases
│   └── progressive_model.keras   ← download from GitHub Releases
│
├── test_dataset/              # Small sample subset for local Grad-CAM testing
├── scripts/                   # Utility scripts
├── archive/
│   └── legacy_experiments/    # v1 pipeline results (old architecture, H5 weights)
│
├── requirements.txt
├── CITATION.cff
└── LICENSE

Local Demo

A Flask-based web demo is included in demo/. It supports:

  • Single model inference — select Fixed Size or Progressive Resizing
  • Compare mode — run both models on the same image side-by-side with Grad-CAM
bash
# 1. Download the pre-trained weights (see models/README.md)
#    Place both .keras files in models/

# 2. Install dependencies
pip install -r requirements.txt

# 3. Run the demo server
cd demo
python app.py
# → open http://localhost:5000

Upload any chest X-ray (JPEG/PNG). The interface returns:

  • Predicted class with confidence scores
  • Per-class probability bars
  • Grad-CAM activation heatmap highlighting diagnostically relevant lung regions

Model Weights

Trained weights are tracked via GitHub Releases (not in the repository — each ~332 MB).

ModelFileDownload
Fixed Size (384×384)fixed_size_model.kerasGitHub Releases →
Progressive Resizingprogressive_model.kerasGitHub Releases →

Place both files in the models/ directory at the repository root.


Reproducing the Experiments

The full pipeline is available as 8 modular Python scripts in kaggle_pipeline/. See `kaggle_pipeline/README.md` for execution instructions.

The original Kaggle notebook is available on Kaggle at:

(link to be added upon public Kaggle release)

Update History

VersionDateSummary
v2.0May 2025Complete pipeline overhaul: native tf.data, Concat Pooling, AdamW, CLAHE+MixUp augmentation, .keras format, strict data isolation, dynamic class weights.
v1.0Apr 2025Initial release: legacy H5 models, GAP-only pooling, manual oversampling. See archive/legacy_experiments/.

Citation

bibtex
@inproceedings{pepper2025efficientnetb4pneumonia,
  title     = {Perbandingan Strategi Pelatihan EfficientNetB4 dengan dan tanpa
               Progressive Resizing untuk Klasifikasi Pneumonia Bakterial dan
               Viral pada Citra X-Ray Dada},
  author    = {Pepper, Benedict},
  booktitle = {Prosiding Seminar Nasional Teknologi dan Sains Terapan},
  year      = {2025}
}

License

Released under the MIT License.

The Kermany chest X-ray dataset is made available under CC BY 4.0 — please cite the original paper when using it:

Kermany, D. S., et al. (2018). Identifying Medical Diagnoses and Treatable Diseases by Image-Based Deep Learning. Cell, 172(5), 1122–1131. https://doi.org/10.1016/j.cell.2018.02.010