Edit model card

ryefoxlime/PneumoniaDetection

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

Uses

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')
model.evaluate('/path/to/image')

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
os.chdir('datasets/chest_xray/chest_xray/')
if os.path.isdir('val/NORMAL') is False:
  os.makedirs('val/NORMAL')
  os.makedirs('val/PNEUMONIA')

# 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(base_model)
    # model.add(Conv2D(filters=32, kernel_size=3, strides=(2, 2)))
    model.add(AveragePooling2D(pool_size=(2, 2), strides=2))
    model.add(Flatten())
    model.add(Dense(256, activation="relu"))
    model.add(Dense(128, activation="relu"))
    model.add(Dense(2, activation="softmax"))
    model.compile(
        loss="sparse_categorical_crossentropy",
        optimizer=Adam(learning_rate=0.000035),
        metrics=["sparse_categorical_accuracy"],
    )
    return model

Fitting the model

%%time
history = model.fit(
    train_datagen,
    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
)

Preprocessing

train_image_generator = ImageDataGenerator(
    rotation_range= 0.5,
    horizontal_flip=True,
    vertical_flip=True,
    zoom_range=0.5,
    rescale= 1./255
)

train_datagen = train_image_generator.flow_from_directory(
    train_dir,
    target_size= (IMG_HEIGHT,IMG_WIDTH),
    color_mode='rgb',
    batch_size= batch_size,
    class_mode= 'binary',
    classes=['NORMAL','PNEUMONIA'],
    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(
    monitor="val_loss",
    factor=0.01,
    patience=2,
    min_lr=0.000035,
    min_delta=0.01,
    verbose=2,
)
checkpoint = ModelCheckpoint(
    filepath=f"../Checkpoints/{{val_sparse_categorical_accuracy:.2f}}",
    save_weights_only=True,
    monitor="val_sparse_categorical_accuracy",
    mode="max",
    save_best_only=True,
    verbose=2,
    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

Metrics

Evaluation metrics used are recall and pricision

Results

image/png

Summary

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
43
Inference Examples
This model does not have enough activity to be deployed to Inference API (serverless) yet. Increase its social visibility and check back later, or deploy to Inference Endpoints (dedicated) instead.

Model tree for ryefoxlime/PneumoniaDetection

Finetuned
(123)
this model

Spaces using ryefoxlime/PneumoniaDetection 4