Keras | Diagnosing of pneumonia using DEEP LEARNING

Download kaggle dataset

First we download a dataset from kaggle (https://www.kaggle.com/paultimothymooney/chest-xray-pneumonia) . Your data structure should be that this bellow.

Architecture 

We train ResNet 50 from keras framework. Our model is then trained using the categorical cross entropy loss function and adam optimizer with lr = 1e-4. 

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras.applications.resnet50 import ResNet50
from keras.models import Model
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator 


base_model = ResNet50(weights=None, include_top=None, input_shape=(416, 416, 1))
model = base_model.output
model = GlobalAveragePooling2D()(model)
model = Dense(1024*4, activation='relu')(model)
model = Dense(1024*4, activation='relu')(model)
model = Dense(2, activation='softmax')(model)

model = Model(inputs=base_model.input, outputs=model)
model.compile(optimizer=Adam(lr=1e-4), loss='categorical_crossentropy',
                   metrics=['accuracy'])

Next we set a path to dataset, count of images, number of epochs and batch size.

train_data_dir = 'chest_xray/train'
validation_data_dir = 'chest_xray/test'
nb_train_samples =5232  
nb_validation_samples = 624
epochs = 20
batch_size = 16

Install a livelossplot for plotting while training and import necessary dependencies.

pip install livelossplot
from livelossplot import PlotLossesKeras
from keras.callbacks import ReduceLROnPlateau
from sklearn.metrics import classification_report, confusion_matrix
import numpy as np

Data generator

To generate a batch we use a ImageDataGenerator. ImageDataGenerator is used for rescaling, normalizing imagees and applying some augmentation (such as random zoom manipulation, rotation, vertical and horizontal flip etc.).
Last things before starting training is setting reduce learning rate. In this case we will multiply loss function by 0.5 if val_loss did not improve since 4 epochs.

train_datagen = ImageDataGenerator( 
    rescale=1. / 255, 
    shear_range=0.1, 
    zoom_range=0.1, 
    rotation_range=10,
    width_shift_range=0.1,
    height_shift_range=0.1,
    horizontal_flip=True) 
  
test_datagen = ImageDataGenerator(rescale=1. / 255) 
  
train_generator = train_datagen.flow_from_directory( 
    train_data_dir, 
    target_size=(416, 416), 
    batch_size=batch_size, 
    color_mode='grayscale',
    class_mode='categorical') 

validation_generator = test_datagen.flow_from_directory( 
    validation_data_dir, 
    target_size=(416, 416), 
    batch_size=batch_size, 
    color_mode='grayscale',
    class_mode='categorical',
    shuffle=False) 
print(validation_generator.class_indices)

reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5,
                              patience=4, min_lr=1e-8, verbose=1)
model.fit_generator( 
    train_generator, 
    steps_per_epoch=nb_train_samples // batch_size, 
    epochs=epochs, 
    validation_data=validation_generator, 
    validation_steps=nb_validation_samples // batch_size,
    callbacks=[reduce_lr, PlotLossesKeras()]) 

Check out score

After training we check accuracy and type of errors using Confusion Matrix.

Y_pred = model.predict_generator(validation_generator, nb_validation_samples // batch_size)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix')
print(confusion_matrix(validation_generator.classes, y_pred))
print('Classification Report')
target_names = ['NORMAL', 'PNEUMONIA']
print(classification_report(validation_generator.classes, y_pred, target_names=target_names))

It works

Last, we show test data with predicted labels.

from keras.preprocessing import image
import matplotlib.pyplot as plt
from PIL import Image,ImageDraw
colors = [(0,255,0),(255,0,0)]
def show_prediction(model, img_path, target_names):
  img = image.load_img(img_path, target_size=(416, 416), color_mode="grayscale")
  x = image.img_to_array(img)/255
  x = np.expand_dims(x, axis=0)

  images = np.vstack([x])
  classes = model.predict(images, batch_size=10)
  
  img = image.load_img(img_path, target_size=(416, 416), color_mode="rgb")
  d = ImageDraw.Draw(img)
  
  d.text((10,10), target_names[np.argmax(classes)], fill=colors[np.argmax(classes)])
  print(target_names[np.argmax(classes)])
  plt.imshow(img)
  plt.show()
  

show_prediction(model, '/content/chest_xray/test/NORMAL/IM-0001-0001.jpeg', target_names)
show_prediction(model, './chest_xray/test/PNEUMONIA/person100_bacteria_475.jpeg', target_names)

Full code google Colab: https://colab.research.google.com/drive/1FfKalOJTwkJOPRxWiKhD8-P0x6IJtIxx

How to improve of this results?

Will be soon…

Leave a Reply

Your email address will not be published. Required fields are marked *