# Model Weights Trained model weights are **not tracked in this repository** due to file size. Download from the [GitHub Releases page](https://github.com/bennypepper/pneumonia-efficientnetb4/releases/tag/v2.0.0) and place files here in `models/`. --- ## Available Formats | File | Size | Format | Notes | |------|------|--------|-------| | `fixed_size_model.weights.h5` | ~76 MB | Weights-only HDF5 | ✅ Recommended for most users | | `progressive_model.weights.h5` | ~76 MB | Weights-only HDF5 | ✅ Recommended for most users | | `fixed_size_model.keras` | ~317 MB | Full Keras SavedModel | For research / retraining only | | `progressive_model.keras` | ~317 MB | Full Keras SavedModel | For research / retraining only | --- ## Loading `.weights.h5` (Recommended) Requires rebuilding the architecture first, then loading weights: ```python import tensorflow as tf def build_model(img_size=384): """Concat Pooling EfficientNetB4 head — matches training architecture.""" tf.keras.mixed_precision.set_global_policy('float32') inputs = tf.keras.Input(shape=(img_size, img_size, 3)) backbone = tf.keras.applications.EfficientNetB4( include_top=False, weights=None, input_tensor=inputs ) x = backbone.output gap = tf.keras.layers.GlobalAveragePooling2D()(x) gmp = tf.keras.layers.GlobalMaxPooling2D()(x) x = tf.keras.layers.Concatenate()([gap, gmp]) x = tf.keras.layers.Dense(512, activation='swish')(x) x = tf.keras.layers.BatchNormalization()(x) x = tf.keras.layers.Dropout(0.5)(x) out = tf.keras.layers.Dense(2, activation='softmax')(x) return tf.keras.Model(inputs, out) model = build_model(img_size=384) model.load_weights("models/fixed_size_model.weights.h5") # Inference — preprocess: pixel / 255.0 (NOT preprocess_input) import numpy as np from PIL import Image img = np.array(Image.open("xray.jpg").resize((384, 384))) / 255.0 pred = model(img[np.newaxis].astype(np.float32), training=False).numpy()[0] print(f"Bacterial: {pred[0]:.3f} Viral: {pred[1]:.3f}") ``` --- ## Loading `.keras` (Full Model) ```python import tensorflow as tf model = tf.keras.models.load_model("models/fixed_size_model.keras", compile=False) ``` Requires TensorFlow 2.19+. Mixed precision is embedded in the model config. --- ## Performance | Model | Test Accuracy | Test AUC | FNR Viral | |-------|-------------|---------|-----------| | Fixed Size (384×384) | 86.92% | 0.9198 | 32.4% | | Progressive Resizing (224→256→384) | 78.46% | 0.8011 | 47.3% | *Evaluated on 390-image Kermany test set (242 bacterial, 148 viral) with 2-pass TTA.* --- ## Architecture - **Backbone**: EfficientNetB4 (ImageNet pretrained, `include_top=False`) - **Head**: Concat Pooling — GlobalAveragePooling2D + GlobalMaxPooling2D → Concatenate(3,584-dim) → Dense(512, Swish) → BatchNorm → Dropout(0.5) → Dense(2, Softmax) - **Preprocessing**: pixel ÷ 255.0 — **not** `preprocess_input` - **Loss**: CategoricalFocalCrossentropy (γ=2.0, α=0.25, label_smoothing=0.1)