image_output_1.png
from keras.preprocessing import image

from keras.preprocessing.image import ImageDataGenerator

import numpy as np # linear algebra

import tensorflow as tf

ROOT = '/kaggle/input/covid19-xray-dataset-train-test-sets/xray_dataset_covid19/'
TRAIN_DIR = ROOT + 'train'
TEST_IMAGE_DIR = '/kaggle/input/covid19-test-sample/pneumonia_test_1.jpg'
VAL_DIR = ROOT + 'test'
NORMAL_DIR = ROOT + 'train/NORMAL/IM-0101-0001.jpeg'
PNEUMONIA_DIR = ROOT + 'train/PNEUMONIA/B59DD164-51D5-40DF-A926-6A42DD52EBE8.jpeg'
RESIZE_WIDTH = 256
RESIZE_HEIGHT = 256
NORMAL_TITLE = 'Normal'
PNEUMONIA_TITLE = 'Pneumonia'
BATCH_SIZE = 32
TARGET_WIDTH = 64
TARGET_HEIGHT = 64
CLASS_MODE = 'binary'
RESCALE = 255
SHEAR_RANGE = 0.25
VERTICAL_FLIP = False
HORIZONTAL_FLIP = True
ZOOM_RANGE = 0.25
VALIDATION_SPLIT = 0.15
EPOCH = 20
SUPTITLE = 'Train vs Validation'
LOSS_LABEL = 'Loss'
ACCURACY_LABEL = 'Accuracy'
AUC_LABEL = 'ROC-AUC'
RECALL_LABEL = 'Recall'
TRAIN_LOSS_LABEL = 'Training Loss'
VALIDATION_LOSS_LABEL = 'Validation Loss'
TRAIN_ACCURACY_LABEL = 'Training Accuracy'
VALIDATION_ACCURACY_LABEL = 'Validation Accuracy'
TRAIN_AUC_LABEL = 'Training AUC'
VALIDATION_AUC_LABEL = 'Validation AUC'
TRAIN_RECALL_LABEL = 'Training Recall'
VALIDATION_RECALL_LABEL = 'Validation Recall'
UPPER_RIGHT = 'upper right'
LOWER_RIGHT = 'lower right'
COLOR_CHANNEL = 3
CONV1_FILTERS = 48
CONV1_KERNEL = 3
CONV1_ACTIVATION = 'relu'
POOL1_SIZE = 3
POOL1_STRIDE = 1
CONV2_FILTERS = 32
CONV2_KERNEL = 3
CONV2_ACTIVATION = 'relu'
DROPOUT = 0.3
POOL2_SIZE = 2
POOL2_STRIDE = 1
CONV3_FILTERS = 16
CONV3_KERNEL = 3
CONV3_ACTIVATION = 'relu'
FC_ACTIVATION = 'relu'
FC_UNITS = 128
OUTPUT_ACTIVATION = 'sigmoid'
OUTPUT_UNITS = 1
OPTIMIZER = 'adam'
LOSS_CLASS = 'binary_crossentropy'
ACCURACY_METRICS = 'accuracy'
AUC_METRICS = tf.keras.metrics.AUC()
RECALL_METRICS = tf.keras.metrics.Recall()

train_gen = ImageDataGenerator(rescale=1.0 / RESCALE, shear_range=SHEAR_RANGE, vertical_flip=VERTICAL_FLIP, horizontal_flip=HORIZONTAL_FLIP, zoom_range=ZOOM_RANGE, validation_split=VALIDATION_SPLIT)

train_set = train_gen.flow_from_directory(TRAIN_DIR, batch_size=BATCH_SIZE, target_size=(TARGET_WIDTH, TARGET_HEIGHT), class_mode=CLASS_MODE)

val_gen = ImageDataGenerator(rescale=1.0 / RESCALE)
val_set = val_gen.flow_from_directory(VAL_DIR, batch_size=BATCH_SIZE, target_size=(TARGET_WIDTH, TARGET_HEIGHT), class_mode=CLASS_MODE)

model = tf.keras.models.Sequential()

model.add(tf.keras.layers.Conv2D(filters=CONV1_FILTERS, kernel_size=CONV1_KERNEL, input_shape=[TARGET_WIDTH, TARGET_HEIGHT, COLOR_CHANNEL]))
model.add(tf.keras.layers.Activation(CONV1_ACTIVATION))
model.add(tf.keras.layers.MaxPool2D(pool_size=POOL1_SIZE, strides=POOL1_STRIDE))
model.add(tf.keras.layers.Conv2D(filters=CONV2_FILTERS, kernel_size=CONV2_KERNEL))
model.add(tf.keras.layers.Activation(CONV2_ACTIVATION))
model.add(tf.keras.layers.MaxPool2D(pool_size=POOL2_SIZE, strides=POOL2_STRIDE))
model.add(tf.keras.layers.Dropout(DROPOUT))
model.add(tf.keras.layers.Conv2D(filters=CONV3_FILTERS, kernel_size=CONV3_KERNEL))
model.add(tf.keras.layers.Activation(CONV3_ACTIVATION))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(units=FC_UNITS, activation=FC_ACTIVATION))
model.add(tf.keras.layers.Dense(units=OUTPUT_UNITS, activation=OUTPUT_ACTIVATION))

model.compile(optimizer=OPTIMIZER, loss=LOSS_CLASS, metrics=[AUC_METRICS, ACCURACY_METRICS, RECALL_METRICS])
model.summary()

hist = model.fit(x=train_set, validation_data=val_set, epochs=EPOCH)

test_sample = image.load_img(TEST_IMAGE_DIR, target_size=(TARGET_WIDTH, TARGET_HEIGHT))
test_sample = image.img_to_array(test_sample)
test_sample = np.expand_dims(test_sample, axis=0)
result = model.predict(test_sample)

if result[0][0] == 1:
    print(PNEUMONIA_TITLE)
else:
    print(NORMAL_TITLE)