<jupyter_start><jupyter_text>**Setup**<jupyter_code>import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator<jupyter_output><empty_output><jupyter_text>**Load the Data: COVID vs Pneumonia**- Load dataset from Kaggle<jupyter_code>mv /content/drive/MyDrive/kaggle.json ~/.kaggle/kaggle.json
!kaggle datasets download -d prashant268/chest-xray-covid19-pneumonia
!unzip -q /content/chest-xray-covid19-pneumonia.zip
!ls<jupyter_output>chest-xray-covid19-pneumonia.zip  Data	drive  sample_data<jupyter_text>**Generate a `Dataset`**<jupyter_code>image_size = (180, 180)
batch_size = 32

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "/content/Data/train",
    validation_split=0.2,
    subset="training",
    seed=122,
    image_size=image_size,
    batch_size=batch_size,
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "/content/Data/train",
    validation_split=0.2,
    subset="validation",
    seed=122,
    image_size=image_size,
    batch_size=batch_size,
)
train_ds
class_names = train_ds.class_names
print(class_names)<jupyter_output>['COVID19', 'NORMAL', 'PNEUMONIA']<jupyter_text>**Visualize the Data**<jupyter_code>import matplotlib.pyplot as plt

plt.figure(figsize=(10, 10))
for images, labels in train_ds.take(1):
  for i in range(9):
    ax = plt.subplot(3, 3, i + 1)
    plt.imshow(images[i].numpy().astype("uint8"))
    plt.title(class_names[labels[i]])
    plt.axis("off")<jupyter_output><empty_output><jupyter_text>**Image Data Augmentation**<jupyter_code>data_augmentation = keras.Sequential(
    [
     layers.experimental.preprocessing.RandomFlip("horizontal"),
     layers.experimental.preprocessing.RandomRotation(0.05),
     layers.experimental.preprocessing.RandomZoom(0.05)
    ]
)
plt.figure(figsize=(10, 10))
for images, _ in train_ds.take(1):
    for i in range(9):
        augmented_images = data_augmentation(images)
        ax = plt.subplot(3, 3, i + 1)
        plt.imshow(augmented_images[0].numpy().astype("uint8"))
        plt.axis("off")<jupyter_output><empty_output><jupyter_text>**Configure the dataset for performance**<jupyter_code>train_ds = train_ds.prefetch(buffer_size=32)
val_ds = val_ds.prefetch(buffer_size=32)<jupyter_output><empty_output><jupyter_text>**Build Model**<jupyter_code>keras.backend.clear_session()

def make_model(input_shape, num_classes):
    inputs = keras.Input(shape=input_shape)
    # Image augmentation block
    x = data_augmentation(inputs)

    # Entry block
    x = layers.experimental.preprocessing.Rescaling(1.0 / 255)(x)
    x = layers.Conv2D(32, 5, strides=2, padding="same")(x)
    x = layers.Activation("relu")(x)
    x = layers.MaxPool2D(3, strides=2, padding="same")(x)

    x = layers.Conv2D(64, 5, padding="same")(x)
    x = layers.Activation("relu")(x)
    x = layers.MaxPool2D(3, strides=2, padding="same")(x)

    for size in [128, 256, 512]:
        x = layers.Activation("relu")(x)
        x = layers.Conv2D(size, 5, padding="same")(x)
        x = layers.MaxPool2D(3, strides=2, padding="same")(x)

    x = layers.Dropout(0.5)(x)
    x = layers.SeparableConv2D(724, 3, padding="same")(x)
    x = layers.Activation("relu")(x)
    x = layers.MaxPool2D(3, strides=2, padding="same")(x)

    x = layers.Flatten()(x)
    x = layers.Dense(256, activation='relu')(x)

    if num_classes == 2:
        activation = "sigmoid"
        units = 1
    else:
        activation = "softmax"
        units = num_classes

    x = layers.Dropout(0.5)(x)
    outputs = layers.Dense(units, activation=activation)(x)
    return keras.Model(inputs, outputs)


model = make_model(input_shape=image_size + (3,), num_classes=3)
keras.utils.plot_model(model, show_shapes=True)<jupyter_output><empty_output><jupyter_text>**Train the Model**<jupyter_code>checkpoint_filepath = '/content/content'
model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_filepath,
    save_weights_only=True,
    monitor='val_accuracy',
    mode='max',
    save_best_only=True)

early_stop = tf.keras.callbacks.EarlyStopping(
    monitor="val_loss",
    min_delta=0,
    patience=6,
    verbose=0,
    restore_best_weights = True)

def scheduler(epoch, lr):
  if epoch < 9:
    return lr
  else:
    return lr * tf.math.exp(-0.01)

lr_callback = tf.keras.callbacks.LearningRateScheduler(scheduler)
epochs = 50

model.compile(optimizer= keras.optimizers.Adam(),
              loss = 'sparse_categorical_crossentropy',
              metrics = ["accuracy"])

model.fit(
    train_ds, epochs = epochs, 
    callbacks = [model_checkpoint_callback, early_stop, lr_callback],
    validation_data = val_ds)
#plot graph between training and validation loss
plt.figure(figsize=(8,6))
plt.plot(model.history.history['loss'])
plt.plot(model.history.history['val_loss'])
plt.legend(['Training', 'Validation'])
plt.title('Training and validation losses')
plt.xlabel('epoch')
#plot graph between training and validation loss
plt.figure(figsize=(8,6))
plt.plot(model.history.history['accuracy'])
plt.plot(model.history.history['val_accuracy'])
plt.legend(['Training', 'Validation'])
plt.title('Training and validation accuracy')
plt.xlabel('epoch')
tf.keras.models.save_model(
    model,
    "/content/drive/MyDrive/Projects/COVID_CNN/covid_pneum_model2",
    overwrite=True,
    include_optimizer=True,
    signatures=None,
    options=None
)
# restored_model = tf.keras.models.load_model(
#     "/content/drive/MyDrive/Projects/COVID_CNN/covid_pneum_model2", custom_objects=None, compile=True, options=None
# )
image_path = "/content/Data/test/COVID19/COVID19(469).jpg"

img = keras.preprocessing.image.load_img(
    image_path, target_size=image_size
)
img_array = keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create a batch

predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])

print(
    "This image most likely belongs to {} with a {:.2f} percent confidence."
    .format(class_names[np.argmax(score)], 100 * np.max(score))
)
from google.colab import files
from keras.preprocessing import image

uploaded = files.upload()

for filename in uploaded.keys():
  img_path = '/content/'+filename
  img = image.load_img(img_path, target_size=image_size)
  img_array = image.img_to_array(img)
  img_array = tf.expand_dims(img_array, 0)

  predictions = model.predict(img_array)
  score = tf.nn.softmax(predictions[0])

  print(
    "This image most likely belongs to {} with a {:.2f} percent confidence."
    .format(class_names[np.argmax(score)], 100 * np.max(score))
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1364510877608406, 'fraction_numerical': 0.02175521681219476, 'mean_word_length': 4.8765217391304345, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '573829', 'license': 'Apache-2.0', 'n_tokens_mistral': 2539, 'n_tokens_neox': 2413, 'n_words': 405, 'path': 'COVID_v_PNEUM_CNN.ipynb', 'repo_name': 'alexlabuda/COVID_v_PNEUM_Chest_XRay_Classification', 'size': '6757'}