Edit model card


Model Details

I have developed a robust model that utilizes transfer learning and the powerful ResNet50V2 architecture to effectively classify chest X-ray images into two categories: pneumonia and normal. This model demonstrates high accuracy and generalizability, making it a promising tool for assisting in pneumonia diagnosis.

Model Description

The ResNet50V2: ResNet50V2 is a deep convolutional neural network (CNN) architecture, part of the ResNet (Residual Networks) family. It's known for its depth, utilizing residual blocks that help address the vanishing gradient problem during training. The "V2" signifies an improvement over the original ResNet50, incorporating tweaks to enhance performance.

Transfer Learning: Transfer learning involves leveraging a pre-trained model's knowledge on a large dataset and applying it to a different but related task. For our use case, ResNet50V2, which has been trained on a diverse dataset, is adapted to classify pneumonia-related images.

Image Classification: The core task of the model is to categorize images into two classes: "affected by pneumonia" and "normal." This binary classification is crucial for diagnosing medical conditions based on visual information in images.

Model Training: During training, the pre-trained ResNet50V2 model is used as a feature extractor. The existing weights are frozen, preventing further training on the original dataset, and a new classifier tailored to this specific task is added. This new classifier is trained using the labeled dataset of pneumonia and normal images.

Loss Function and Optimization: To guide the training process, a loss function is employed to measure the difference between predicted and actual labels. Common choices for image classification tasks include categorical cross-entropy. An optimizer, such as stochastic gradient descent (SGD) or Adam. In our case we have used Adam as our optimier of choice, which is used to adjust the model's weights based on the calculated loss.

Evaluation: The model's performance is assessed using a separate dataset not seen during training. Metrics like accuracy, precision, recall, and F1-score are often used to gauge how well the model generalizes to new, unseen data.

Deployment: Once the model demonstrates satisfactory performance, it can be deployed for real-world use. This involves integrating it into a system or application where it can receive new images, make predictions, and aid in the diagnosis of pneumonia.

  • Developed by: Nitin Kausik Remella
  • Model type: Sequential
  • Language(s): Python
  • Finetuned from model: ResNet50V2

Model Sources


This tool is used to assist medical professional in cross-validation of the diagnosis

Out-of-Scope Use

This model is in no form or way to replace an actual medical professional but only in assist them

Bias, Risks, and Limitations

The model cant handle 4d images such as CT scans

How to Get Started with the Model

import tensorflow as tf
from tensorflow import keras
from keras import models
model = load_model('/path/to/model')

Training Details

Training Data

Downloading the dataset from kaggle split the data into 3 parts

  • train
  • test
  • val

code to split into 25% 75% split of training data

# Creating Val folder
if os.path.isdir('val/NORMAL') is False:

# Moving Images from train folder to val folder
source = 'chest_xray/train/PNEUMONIA/'
dest = 'datasets/chest_xray/chest_xray/val/PNEUMONIA'
files = os.listdir(source)
np_of_files = len(files) // 25
for file_name in random.sample(files, np_of_files):
        shutil.move(os.path.join(source, file_name), dest)

# Moving Normal Images from train folder to val folder
source = 'datasets/chest_xray/chest_xray/train/NORMAL/'
dest = 'datasets/chest_xray/chest_xray/val/NORMAL'
files = os.listdir(source)
np_of_files = len(files) // 25
for file_name in random.sample(files, np_of_files):
        shutil.move(os.path.join(source, file_name), dest)

Training Procedure

The training of the data requires ResNet50V2 to start as the base model and then using further layers to extract more information and to help in classification

Building the model

from keras.applications import VGG16, ResNet50V2

base_model = ResNet50V2(
    include_top=False, input_shape=(224, 224, 3), weights="imagenet"
base_model.trainable = False

def CreateModel():
    model = Sequential()
    # model.add(Conv2D(filters=32, kernel_size=3, strides=(2, 2)))
    model.add(AveragePooling2D(pool_size=(2, 2), strides=2))
    model.add(Dense(256, activation="relu"))
    model.add(Dense(128, activation="relu"))
    model.add(Dense(2, activation="softmax"))
    return model

Fitting the model

history = model.fit(
    steps_per_epoch = train_datagen.n//train_datagen.batch_size,
    epochs = 10,
    validation_data= val_datagen,
    validation_steps= val_datagen.n//val_datagen.batch_size,
    callbacks=[callback, reduceLR, checkpoint],
    verbose = 1


train_image_generator = ImageDataGenerator(
    rotation_range= 0.5,
    rescale= 1./255

train_datagen = train_image_generator.flow_from_directory(
    target_size= (IMG_HEIGHT,IMG_WIDTH),
    batch_size= batch_size,
    class_mode= 'binary',
    shuffle= True,
    seed= 42

set the value shuffle=False for val_datagen and test_datagen and change the value of train_dir to val_dir and 'test_dir' respectively

Training Hyperparameters

  • Training regime:
  • Using keras callbacks to reduce the load on the gpu/cpu by checking the model growth and early stopping or reducing the learning rate accordingly.
  • Saving the best accuracy as a checkpoint to resume the training from
  from keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint

callback = EarlyStopping(
    monitor="val_loss", patience=6, restore_best_weights=True, min_delta=0.03, verbose=2
reduceLR = ReduceLROnPlateau(
checkpoint = ModelCheckpoint(
    initial_value_threshold= baseline

Define Defaults

Batch_size = 32 smaller batch size for weaker systems IMG_HEIGHTS = 224 IMG_WEIGHTS = 224 epochs = 10 train_dir = path/to/chest_xray/train val_dir = path/to/chest_xray/val test_dir = path/to/chest_xray/test


Evaluation metrics used are recall and pricision




The model is capable of detecting pneumonia with an accuracy of 91%

The following hyperparameters were used during training:

Hyperparameters Value
name Adam
learning_rate 3.5e-05
decay 0.0
beta_1 0.9
beta_2 0.999
epsilon 1e-07
amsgrad False
training_precision float32
Downloads last month

Finetuned from

Space using ryefoxlime/PneumoniaDetection 1