Image Classification
Understanding data
- Chest X-ray images (anterior-posterior) taken of pediatric patients, aged one to five years old
-
from Guangzhou Women and Children’s Medical Center, Guangzhou.
- physicians labelled images with pneumonia, part of normal diagnosis
Prepare data
from tensorflow.keras.preprocessing import image_dataset_from_directory
train_batches = image_dataset_from_directory(train_path)
val_batches = image_dataset_from_directory(test_path)
#Found 5216 files belonging to 2 classes.
#Found 624 files belonging to 2 classes.
-
image_dataset_from_directory function reads data in format:
-
main_directory/
- class_a/
- a_image_1.jpg
- a_image_2.jpg
- class_b/
- b_image_1.jpg
- b_image_2.jpg
- class_a/
Model construction
Rescaling Layer
-
standarising layer : CNN converges faster on [0 to 1] data than on [0 to 255]
-
tf.keras.layers.Rescaling(1./255) added into model to standarise pixel values
model = Sequential()
model.add(Rescaling(1./255))
model.add(Conv2D(32,(3,3),strides=(1, 1),activation='relu',padding='same'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Conv2D(64,(3,3),strides=(1, 1) ,padding='same',activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Conv2D(128,(3,3),strides=(1, 1),padding='same', activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Conv2D(256,(3,3),strides=(1, 1),padding='same', activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(2))
model.compile(
optimizer='adam',
loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
earlystopping = callbacks.EarlyStopping(monitor ="val_loss",
mode ="min", patience = 3,
restore_best_weights = True)
history = model.fit(
train_batches,
validation_data=val_batches,
epochs=50,
callbacks=[earlystopping]
)