Detecting Pneumonia Using CNNs In TensorFlow
By: Ashley C
Tags: TensorFlow, Deep Learning, Convolutional Network, Artificial Intelligence, Machine Learning

train_batches = ImageDataGenerator(

preprocessing_function=tf.keras.applications.vgg16.preprocess_input, rescale=1/255.

).flow_from_directory(

directory=train_path,

target_size=(224, 224),

classes=['NORMAL', 'PNEUMONIA'],

batch_size=BATCH_SIZE,

class_mode='binary'

)



test_batches = ImageDataGenerator(

preprocessing_function=tf.keras.applications.vgg16.preprocess_input, rescale=1/255.

).flow_from_directory(

directory=test_path,

target_size=(224, 224),

classes=['NORMAL', 'PNEUMONIA'],

batch_size=BATCH_SIZE,

class_mode='binary',

shuffle=False

)



valid_batches = ImageDataGenerator(

preprocessing_function=tf.keras.applications.vgg16.preprocess_input, rescale=1/255.

).flow_from_directory(

directory=valid_path,

target_size=(224, 224),

classes=['NORMAL', 'PNEUMONIA'],

batch_size=BATCH_SIZE,

class_mode='binary'

)

A lot is going on here, but don’t feel overwhelmed. Essentially we are creating ImageDataGenerators for our training, testing, and validation paths. To simplify things, we use ImageDataGenerators to prepare and process our images for our CNN.

All the batches have the same ImageDataGenerators, with the exception of each batch having its own directory, and the testing batch shuffles being set to false (we never want to shuffle our data in the testing stages).

Due to the similarity of the generators, I’ll break down what is happening in the train_batches ImageDataGenerator.

train_batches = ImageDataGenerator(

preprocessing_function=tf.keras.applications.vgg16.preprocess_input, rescale=1/255.

).flow_from_directory(

directory=train_path,

target_size=(224, 224),

classes=['NORMAL', 'PNEUMONIA'],

batch_size=BATCH_SIZE,

class_mode='binary'

)

The first step of our ImageDataGenerator includes applying a preprocessing_function, which will be: tf.keras.applications.vgg16.preprocess_input.

The vgg16 is a transfer learning model. Essentially transfer learning can be used in machine learning, and what transfer learning does is reuse a pre-trained model on a new problem. In all essence, the model exploits its knowledge from a prior task to improve generalization on a future task.

Next, we will need to rescale. If you don’t rescale your images could look like this (this actually happened when I didn’t rescale the image):

I accidentally burned the lungs, please rescale your image to avoid this.

We rescale to 1/255. to transform every pixel in the range 0,255 to 0,1. Rescaling will treat all the images in the same matter since images will have varying pixel ranges.

Next, we will call the flow_from_directory() method on our ImageDataGenerator. Flow_from_directory() takes a directory path and generates batches of augmented data.

Within this method we will do the following:

directory=train_path,

target_size=(224, 224),

classes=['NORMAL', 'PNEUMONIA'],

batch_size=BATCH_SIZE,

class_mode='binary',

Firstly, we must initialize our directory, which is our train_path.

Next, we will target_size, resize our images to have the same dimensions.

Additionally, we will declare our classes for the model, which include NORMAL and PNEUMONIA.

Then we will set our batch_size to our BATCH_SIZE variable that we defined earlier. Our batch size determines the number of samples that will be fed through our network.

Lastly, our class_mode will be set to binary, as a result of the program only having 2 classes.