# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="nb3nO15Uv6ZC"
# # Pneumonia X-ray ML Model
#
# Pneumonia is an infection that inflames the air sacs in one or both lungs. The air sacs may fill with fluid or pus (purulent material), causing cough with phlegm or pus, fever, chills, and difficulty breathing. A variety of organisms, including bacteria, viruses and fungi, can cause pneumonia.

# + [markdown] id="eyXny-Wov6Rr"
# The normal chest X-ray depicts clear lungs without any areas of abnormal opacification in the image. Bacterial pneumonia typically exhibits a focal lobar consolidation, in this case in the right upper lobe, whereas viral pneumonia manifests with a more diffuse ‘‘interstitial’’ pattern in both lungs.
#
# Chest X-ray images (anterior-posterior) were selected from retrospective cohorts of pediatric patients of one to five years old from Guangzhou Women and Children’s Medical Center, Guangzhou. All chest X-ray imaging was performed as part of patients’ routine clinical care.
#
# For the analysis of chest x-ray images, all chest radiographs were initially screened for quality control by removing all low quality or unreadable scans. The diagnoses for the images were then graded by two expert physicians before being cleared for training the AI system. In order to account for any grading errors, the evaluation set was also checked by a third expert.
#
#

# + [markdown] id="SkMY_gyvv6Kq"
# **Data** for this project comes from [here](https://data.mendeley.com/datasets/rscbjbr9sj/2)
#
# **Citation :** <NAME>; <NAME>; <NAME> (2018), “Labeled Optical Coherence Tomography (OCT) and Chest X-Ray Images for Classification”, Mendeley Data, V2, doi: 10.17632/rscbjbr9sj.2
#
# ![Image](https://i.imgur.com/jZqpV51.png)

# + colab={"base_uri": "https://localhost:8080/"} id="ESEDykRqvSV6" outputId="52f07743-5abe-41f8-8b69-f85c50e326f4"
# Importing our data from the cloud and viewing and understanding it.
# !wget https://data.mendeley.com/public-files/datasets/rscbjbr9sj/files/f12eaf6d-6023-432f-acc9-80c9d7393433/file_downloaded

# + id="QlICC2b3EYqr"
# Importing dependancies
import zipfile
import os
import pathlib
import random
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras import layers
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# + id="PTmISEXbwT0h"
zip_ref = zipfile.ZipFile("file_downloaded")
zip_ref.extractall()
zip_ref.close()

# + colab={"base_uri": "https://localhost:8080/"} id="vCZv7I2qwZ5S" outputId="530b1dd4-a265-4280-d414-84ffc511ac45"
# Walkthrough
for dirpath, dirnames, filenames in os.walk("chest_xray"):
  print(f"There are {len(dirnames)} directories and {len(filenames)} images in '{dirpath}' .'")

# + id="OluOOosewZ2o" colab={"base_uri": "https://localhost:8080/"} outputId="96faa3a7-e43a-45df-8ded-2acff7427048"
data_dir = pathlib.Path("/content/chest_xray/train")
class_names = np.array(sorted([item.name for item in data_dir.glob("*")]))
class_names = class_names[1:]
print(class_names)


# + id="-vlGuMLOwZz6"
# Visualizing our data 
def view_random_image(target_dir, target_class):
  # Setup the target directory 
  target_folder = target_dir+target_class

  # Get a random image path 
  random_image = random.sample(os.listdir(target_folder), 1)
  print(random_image)

  # Read in the image and plot it using matplotlib
  img = mpimg.imread(target_folder + "/" + random_image[0])
  plt.imshow(img)
  plt.title(target_class)
  plt.axis("off");

  print(f"Image shape: {img.shape}") # Show the shape of the image 

  return img


# + id="JN-SjxEqwZxe" colab={"base_uri": "https://localhost:8080/", "height": 272} outputId="eb9ab21d-7b9d-4164-c9eb-3146f481855f"
# Viewing a random image from train
img = view_random_image(target_dir="/content/chest_xray/train/",
                        target_class="PNEUMONIA")

# + id="_5ipJjLCw3M2" colab={"base_uri": "https://localhost:8080/", "height": 234} outputId="9b14722d-b53f-4b20-adbf-148c81fd5aa7"
# Visualize both the data 
plt.figure()
plt.subplot(1, 2, 1)
normal = view_random_image("/content/chest_xray/train/", "NORMAL")
plt.subplot(1, 2, 2)
pneumonia = view_random_image("/content/chest_xray/train/", "PNEUMONIA")

# + colab={"base_uri": "https://localhost:8080/"} id="ZuV85_AxwgLN" outputId="9cd764d3-ea3a-4496-ae7a-a25c9fc26245"
# setup data inputs
IMAGE_SHAPE = (224, 224)
BATCH_SIZE = 32

train_dir = "/content/chest_xray/train/"
test_dir = "/content/chest_xray/test/"

train_datagen = ImageDataGenerator(rescale=1/255.)
test_datagen = ImageDataGenerator(rescale=1/255.)

print("Train images :")
train_data = train_datagen.flow_from_directory(train_dir,
                                               target_size=IMAGE_SHAPE,
                                               batch_size=BATCH_SIZE,
                                               class_mode="binary")

print("Testing images :")
test_data = test_datagen.flow_from_directory(test_dir,
                                             target_size=IMAGE_SHAPE,
                                             batch_size=BATCH_SIZE,
                                             class_mode="binary")

# + [markdown] id="xfxvJb4ux_ea"
# ## 2. Preprocessing the data 
#
# The data presented to us is not same, some images are of random shapes and also let's Normalize our data so that it becomes easier for our Neural Network to learn patterns.

# + id="EoYcgwpkwgIp"
# Defining directory dataset paths
train_dir = "/content/chest_xray/train/"
test_dir = "/content/chest_xray/test/"

# + id="FQjP8VAgwgFp"
# Normalization 
train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)

# + colab={"base_uri": "https://localhost:8080/"} id="4Ip7VotsyaTw" outputId="e3b68e44-82bd-40d6-bb5c-18018135a7ff"
# Import data from directories and turn it into batches
train_data = train_datagen.flow_from_directory(directory=train_dir,
                                               batch_size=32,
                                               target_size=(224, 224),
                                               class_mode="binary",
                                               seed=42)

test_data = test_datagen.flow_from_directory(directory=test_dir,
                                             batch_size=32,
                                             target_size=(224, 224),
                                             class_mode="binary",
                                             seed=42)

# + colab={"base_uri": "https://localhost:8080/"} id="BX3iFq7tyfvZ" outputId="de50b9ef-71f2-4825-9f48-ce81b5dd5804"
# Getting a sample of a train data batch
images, labels = train_data.next()  
len(images), len(labels)

# + [markdown] id="n7ymJIm4ylSf"
# ## 3. Creating our models
#
# We are using a CNNs (Convolutional Neural Networks) to get quick results on our model than a normal ANN (Artificial Neural Network)

# + [markdown] id="j6y6UuTwyo_g"
# ### 1. Model 1
#
# For this model we will be using a Conv2D layer then a MaxPool2D then Conv2D layer and then a MaxPool2D layer followed by a Flatten and output layer.
#
# *All the models are trained and saved in the drive, so they need not to be run again.*

# + id="lAKa_lev-6x-"
# Build a CNN model 
model_1 = tf.keras.models.Sequential([
     tf.keras.layers.Conv2D(filters=10,
                            kernel_size=3,
                            activation="relu",

                            input_shape=(224, 224, 3)),
     tf.keras.layers.MaxPool2D(pool_size=2,

                               padding="valid"),
     tf.keras.layers.Conv2D(10, 3, activation="relu"),
     tf.keras.layers.MaxPool2D(2),
     tf.keras.layers.Flatten(),
     tf.keras.layers.Dense(1, activation="sigmoid")
])

# + id="lATa6gRD_PUJ"
# Compiling the model
model_1.compile(loss="binary_crossentropy",
                optimizer=tf.keras.optimizers.Adam(),
                metrics=["accuracy"])

# + id="rpFb_0rg_SR3" colab={"base_uri": "https://localhost:8080/"} outputId="7a3e20b7-6f56-4edc-fb65-7194ad0a3048"
# seed  
tf.random.set_seed(42)

# Fit the model
history_1 = model_1.fit(train_data,
                        epochs=5,
                        steps_per_epoch=len(train_data),
                        validation_data=test_data,
                        validation_steps=len(test_data))

# + [markdown] id="dPb0-9gfzClr"
# ### 2. Model 2
#
# For this model we will be using a Conv2D layer, then a Conv2D layer, then a MaxPool2D layer, then Conv2D layer, then Conv2D layer and then a MaxPool2D layer followed by a Flatten and output layer.
#
# *All the models are trained and saved in the drive, so they need not to be run again.*

# + id="BJxWZUbUU5E4"
# Build a CNN model (Tiny VGG)
model_2 = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(filters=10,
                           kernel_size=3,
                           activation="relu",
                           input_shape=(224, 224, 3)),
    tf.keras.layers.Conv2D(10, 3, activation="relu"),
    tf.keras.layers.MaxPool2D(pool_size=2,
                              padding="valid"),
    tf.keras.layers.Conv2D(10, 3, activation="relu"),
    tf.keras.layers.Conv2D(10, 3, activation="relu"),
    tf.keras.layers.MaxPool2D(2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(1, activation="sigmoid")
])

# + id="sgmoX_KvWovD"
# Compiling the model
model_2.compile(loss="binary_crossentropy",
                optimizer=tf.keras.optimizers.Adam(),
                metrics=["accuracy"])

# + id="AHBIZrbQWsE7" colab={"base_uri": "https://localhost:8080/"} outputId="5d1cccdb-165d-46a1-e63d-dc81892e801a"
# seed  
tf.random.set_seed(42)

# Fit the model
history_2 = model_2.fit(train_data,
                        epochs=5,
                        steps_per_epoch=len(train_data),
                        validation_data=test_data,
                        validation_steps=len(test_data))

# + [markdown] id="rU_Ju0LuzQEa"
# ### 3. Model 3
#
# For this model we will be using a Conv2D layer, then a Conv2D layer, then a MaxPool2D layer, then Conv2D layer, then Conv2D layer and then a MaxPool2D layer followed by a Flatten and output layer. But the `Adam()`'s learning rate set to `0.01`.
#
# *All the models are trained and saved in the drive, so they need not to be run again.*

# + id="qDXVs1MHXU1i"
# Build a CNN model 
model_3 = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(filters=10,
                           kernel_size=3,
                           activation="relu",
                           input_shape=(224, 224, 3)),
    tf.keras.layers.Conv2D(10, 3, activation="relu"),
    tf.keras.layers.MaxPool2D(pool_size=2,
                              padding="valid"),
    tf.keras.layers.Conv2D(10, 3, activation="relu"),
    tf.keras.layers.Conv2D(10, 3, activation="relu"),
    tf.keras.layers.MaxPool2D(2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(1, activation="sigmoid")
])

# + id="XI8qmYnfaXA9"
# Compiling the model
model_3.compile(loss="binary_crossentropy",
                optimizer=tf.keras.optimizers.Adam(learning_rate=0.01),
                metrics=["accuracy"])

# + id="BwljjcODaYT8" colab={"base_uri": "https://localhost:8080/"} outputId="09463f51-6819-44a7-9048-7ce17bbed88a"
# seed  
tf.random.set_seed(42)

# Fit the model
history_3 = model_3.fit(train_data,
                        epochs=5,
                        steps_per_epoch=len(train_data),
                        validation_data=test_data,
                        validation_steps=len(test_data))

# + [markdown] id="Y2U5Z249zfDD"
# ### 4. Model 4
#
# For this model we will be using a Conv2D layer, then a Conv2D layer, then a MaxPool2D layer, then Conv2D layer, then Conv2D layer and then a MaxPool2D layer followed by a Flatten and output layer.
#
# *All the models are trained and saved in the drive, so they need not to be run again.*

# + id="cDJjPduOEQqX"
# Build a CNN model 
model_4 = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(filters=10,
                           kernel_size=3,
                           activation="relu",
                           input_shape=(224, 224, 3)),
    tf.keras.layers.MaxPool2D(pool_size=2,
                              padding="valid"),
    tf.keras.layers.Conv2D(10, 3, activation="relu"),
    tf.keras.layers.MaxPool2D(2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(1, activation="sigmoid")
])

# + id="dI8dk5BWER1E"
# Compiling the model
model_4.compile(loss="binary_crossentropy",
                optimizer=tf.keras.optimizers.Adam(learning_rate=0.01),
                metrics=["accuracy"])

# + id="jc36bNw_ERv6" colab={"base_uri": "https://localhost:8080/"} outputId="6e67fad2-7e02-471b-bc34-e505b9927ba7"
# seed  
tf.random.set_seed(42)

# Fit the model
history_4 = model_4.fit(train_data,
                        epochs=5,
                        steps_per_epoch=len(train_data),
                        validation_data=test_data,
                        validation_steps=len(test_data))

# + [markdown] id="eVjDvuJl1NDa"
# ### Model 5

# + id="shbNSuEFQFGc"
# Build a CNN model 
model_5 = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(filters=16,
                           kernel_size=3,
                           activation="relu",
                           padding="same",
                           input_shape=(224, 224, 3)),
    tf.keras.layers.Conv2D(16, 3, activation="relu", padding="same"),
    tf.keras.layers.MaxPool2D(pool_size=2),
    tf.keras.layers.SeparableConv2D(32, 3, activation="relu", padding="same"),
    tf.keras.layers.SeparableConv2D(32, 3, activation="relu", padding="same"),
    tf.keras.layers.MaxPool2D(2),
    tf.keras.layers.SeparableConv2D(64, 3, activation="relu", padding="same"),
    tf.keras.layers.SeparableConv2D(64, 3, activation="relu", padding="same"),
    tf.keras.layers.MaxPool2D(2),
    tf.keras.layers.SeparableConv2D(128, 3, activation="relu", padding="same"),
    tf.keras.layers.SeparableConv2D(128, 3, activation="relu", padding="same"),
    tf.keras.layers.MaxPool2D(2),
    tf.keras.layers.Dropout(rate=0.2),
    tf.keras.layers.SeparableConv2D(256, 3, activation="relu", padding="same"),
    tf.keras.layers.SeparableConv2D(256, 3, activation="relu", padding="same"),
    tf.keras.layers.MaxPool2D(2),
    tf.keras.layers.Dropout(rate=0.2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512, activation="relu"),
    tf.keras.layers.Dropout(rate=0.7),
    tf.keras.layers.Dense(128, activation="relu"),
    tf.keras.layers.Dropout(rate=0.5),
    tf.keras.layers.Dense(64, activation="relu"),
    tf.keras.layers.Dropout(rate=0.3),
    tf.keras.layers.Dense(1, activation="sigmoid")
])

# + id="JuCnbQex6LI-"
# Compiling the model
model_5.compile(loss="binary_crossentropy",
                optimizer=tf.keras.optimizers.Adam(),
                metrics=["accuracy"])

# + id="eE98QH-5hgEw" colab={"base_uri": "https://localhost:8080/"} outputId="d6542493-4723-4410-9454-6e6fdf31aa0c"
# seed  
tf.random.set_seed(42)

# Fit the model
history_5 = model_5.fit(train_data,
                        epochs=5,
                        steps_per_epoch=len(train_data),
                        validation_data=test_data,
                        validation_steps=len(test_data))

# + [markdown] id="HKUXBG5z1X5Y"
# ### Model 6

# + id="cW31a1T_i1b-"
# Build a CNN model 
model_6 = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(filters=16,
                           kernel_size=3,
                           activation="relu",
                           padding="same",
                           input_shape=(224, 224, 3)),
    tf.keras.layers.Conv2D(16, 3, activation="relu", padding="same"),
    tf.keras.layers.MaxPool2D(pool_size=2),
    tf.keras.layers.SeparableConv2D(32, 3, activation="relu", padding="same"),
    tf.keras.layers.SeparableConv2D(32, 3, activation="relu", padding="same"),
    tf.keras.layers.MaxPool2D(2),
    tf.keras.layers.SeparableConv2D(64, 3, activation="relu", padding="same"),
    tf.keras.layers.SeparableConv2D(64, 3, activation="relu", padding="same"),
    tf.keras.layers.MaxPool2D(2),
    tf.keras.layers.SeparableConv2D(128, 3, activation="relu", padding="same"),
    tf.keras.layers.SeparableConv2D(128, 3, activation="relu", padding="same"),
    tf.keras.layers.MaxPool2D(2),
    tf.keras.layers.Dropout(rate=0.2),
    tf.keras.layers.SeparableConv2D(256, 3, activation="relu", padding="same"),
    tf.keras.layers.SeparableConv2D(256, 3, activation="relu", padding="same"),
    tf.keras.layers.MaxPool2D(2),
    tf.keras.layers.Dropout(rate=0.2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512, activation="relu"),
    tf.keras.layers.Dropout(rate=0.7),
    tf.keras.layers.Dense(128, activation="relu"),
    tf.keras.layers.Dropout(rate=0.5),
    tf.keras.layers.Dense(64, activation="relu"),
    tf.keras.layers.Dropout(rate=0.3),
    tf.keras.layers.Dense(1, activation="sigmoid")
])

# + id="M4ssuhKDuVAV"
# Compiling the model
model_6.compile(loss="binary_crossentropy",
                optimizer=tf.keras.optimizers.Adam(),
                metrics=["accuracy"])

# + id="H5gZWeFKuY7B" colab={"base_uri": "https://localhost:8080/"} outputId="e9b5c388-6681-474d-f67a-60450ee63a06"
# seed  
tf.random.set_seed(42)

# Fit the model
history_6 = model_6.fit_generator(train_data,
                                  epochs=5,
                                  steps_per_epoch=len(train_data),
                                  validation_data=test_data,
                                  validation_steps=len(test_data))

# + [markdown] id="loUC66JQ1Imc"
# ## Transfer learnt Models

# + id="P3MhgmfdzzDY"
# Models from tensorflow_hub
resnet_url = "https://tfhub.dev/google/imagenet/resnet_v2_50/classification/5"
efficientnet_url = "https://tfhub.dev/tensorflow/efficientnet/b0/classification/1"
mobilenet_url = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/4"
efficientnetb1_url = "https://tfhub.dev/tensorflow/efficientnet/b1/classification/1"


# + id="eLnUL5Ld0Ahp"
# Create a model function to create a model from url
def create_model(model_url, num_classes=10):
  """
  Takes a tensorflow Hub url and creates a Keras Sequential model with it.

  Args:
    model_url (str) : A tensorflow hub feature extraction URL.
    num_classes (int) : Number of output neurons in the output layers.

  Returns:
    An uncompiled Keras Sequential model with model_url as feature extractor layer and Dense output layer with num_classes output neurons. 
  """
  
  # Download the pretrained model and save it as a Keras layer
  feature_extractor_layer = hub.KerasLayer(model_url,
                                           trainable=True, # freezing already learnt patterns 
                                           name="feature_extraction_layer",
                                           input_shape=IMAGE_SHAPE+(None,))
  # Create our own model 
  model = tf.keras.Sequential([
     feature_extractor_layer,
     layers.Dense(1, activation="sigmoid", name="output_layer")                          
  ])
  
  return model


# + id="35qRTcky0BMZ"
# To get rid of 3 byte image not processed error
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

# + [markdown] id="pwypLJpK0RD7"
# ### Resnet Model

# + id="a2vbRF-rN1bt"
resnet_model = create_model(resnet_url)

# + id="SjYXGuRLOKGN"
# Compiling our model
resnet_model.compile(loss="binary_crossentropy",
                     optimizer=tf.keras.optimizers.Adam(),
                     metrics=["accuracy"])

# + id="UAzfZ17DOlwy" colab={"base_uri": "https://localhost:8080/"} outputId="6c9f531a-86e3-4f37-ca9f-776daeba5e3f"
tf.random.set_seed(42)

# Fitting our model
resnet_history = resnet_model.fit(train_data,
                                  epochs=5,
                                  steps_per_epoch=len(train_data),
                                  validation_data = test_data,
                                  validation_steps = len(test_data))

# + [markdown] id="IgCSyM2i0fLL"
# ### Efficientnet

# + id="K-8V4qbReEn4"
efficientnet_model = create_model(efficientnet_url)

# + id="nh-8iNpghi4z"
# Compiling our model
efficientnet_model.compile(loss="binary_crossentropy",
                           optimizer=tf.keras.optimizers.Adam(),
                           metrics=["accuracy"])

# + id="OwoIuWtchwv5" colab={"base_uri": "https://localhost:8080/"} outputId="6bbcce21-a9f2-443e-a01a-ffa06b7cc58a"
tf.random.set_seed(42)

# Fitting our model
efficientnet_history = efficientnet_model.fit(train_data,
                                              epochs=5,
                                              steps_per_epoch=len(train_data),
                                              validation_data = test_data,
                                              validation_steps = len(test_data))

# + [markdown] id="Z_SNQWnz0mPR"
# ### Mobilenet

# + id="ZB_klm3Kh-IF"
mobilenet_model = create_model(mobilenet_url)

# + id="7FW9M3QaiZbu"
# Compiling our model
mobilenet_model.compile(loss="binary_crossentropy",
                        optimizer=tf.keras.optimizers.Adam(),
                        metrics=["accuracy"])

# + id="zr9XK3HfiZY3" colab={"base_uri": "https://localhost:8080/"} outputId="c5d74912-6185-4bc5-f64d-492b320b7e86"
tf.random.set_seed(42)

# Fitting our model
mobilenet_history = mobilenet_model.fit(train_data,
                                        epochs=5,
                                        steps_per_epoch=len(train_data),
                                        validation_data = test_data,
                                        validation_steps = len(test_data))

# + [markdown] id="NOIpezDF0wFp"
# ### Efficientnetb1

# + id="sGlgk2Lv0u6b"
efficientnetb1_model = create_model(efficientnetb1_url)

# + id="m_Dt35surhRg"
# Compiling our model
efficientnetb1_model.compile(loss="binary_crossentropy",
                             optimizer=tf.keras.optimizers.Adam(),
                             metrics=["accuracy"])

# + id="0yqHcDTErhPE" colab={"base_uri": "https://localhost:8080/"} outputId="91e7806f-e000-4e47-c9da-ed9c9852ff11"
tf.random.set_seed(42)

# Fitting our model
efficientnetb1_history = efficientnetb1_model.fit(train_data,
                                                  epochs=5,
                                                  steps_per_epoch=len(train_data),
                                                  validation_data = test_data,
                                                  validation_steps = len(test_data))


# + [markdown] id="1n_AfMY-2JPZ"
# ## Visualizing loss and accuracy curves for all the models

# + id="3ArQez0i1_VZ"
# Plotting the valildation and training curves seperately
def plot_loss_curves(history):
  """
  Returns seperate loss curves for training and validation metrics.
  """

  loss = history.history["loss"]
  val_loss = history.history["val_loss"]

  accuracy = history.history["accuracy"]
  val_accuracy = history.history["val_accuracy"]

  epochs = range(len(history.history["loss"]))

  # Plot loss 
  plt.plot(epochs, loss, label="training_loss")
  plt.plot(epochs, val_loss, label="val_loss")
  plt.title("loss")
  plt.xlabel("epochs")
  plt.legend()

  # Plot accuracy
  plt.figure()
  plt.plot(epochs, accuracy, label="accuracy")
  plt.plot(epochs, val_accuracy, label="val_accuracy")
  plt.title("accuracy")
  plt.xlabel("epochs")
  plt.legend()


# + id="LFya1cb72XVp" colab={"base_uri": "https://localhost:8080/", "height": 573} outputId="ae464ce3-50d8-4189-f30e-b1a293755681"
plot_loss_curves(history_1)

# + id="lWuf_xDa2mLT" colab={"base_uri": "https://localhost:8080/", "height": 573} outputId="8162b0bb-a016-4a4d-8436-6a8c5bba5cdc"
plot_loss_curves(history_2)

# + id="_dOk0KRr2mI4" colab={"base_uri": "https://localhost:8080/", "height": 573} outputId="00b553d0-e868-45c5-87bb-baa85f7a9a7f"
plot_loss_curves(history_3)

# + id="kKyhrH2X2mGL" colab={"base_uri": "https://localhost:8080/", "height": 573} outputId="82b6be50-fe6e-444b-900e-c891ac336ccc"
plot_loss_curves(history_4)

# + id="hIj2UAtN2mDU" colab={"base_uri": "https://localhost:8080/", "height": 573} outputId="45efa3b8-5ffd-44b2-ed6a-3813b843c672"
plot_loss_curves(history_5)

# + id="W2Va6eEd2mAq" colab={"base_uri": "https://localhost:8080/", "height": 573} outputId="acd10cd4-0b31-443e-dbf3-52748c2c461c"
plot_loss_curves(history_6)

# + id="QRKhoFtR2l4n" colab={"base_uri": "https://localhost:8080/", "height": 573} outputId="38f03c72-2b22-4c27-d63b-036edf92c5c2"
plot_loss_curves(resnet_history)

# + id="qmPkZNcw2uk0" colab={"base_uri": "https://localhost:8080/", "height": 573} outputId="d3718d72-a2ef-43c5-9e90-b87bfce2d47e"
plot_loss_curves(efficientnet_history)

# + id="mcyKwc5U2uKR" colab={"base_uri": "https://localhost:8080/", "height": 573} outputId="c4f5ed22-a885-4b63-9580-95e8bc203e69"
plot_loss_curves(mobilenet_history)

# + id="jeerurjB2t1w" colab={"base_uri": "https://localhost:8080/", "height": 573} outputId="ccdb0a00-3948-43bb-a50b-52fd8c492117"
plot_loss_curves(efficientnetb1_history)

# + [markdown] id="PAuCe8mh3CsD"
# ## Evaluating accuracy for all the models

# + id="-uqrvQ6e3GG_" colab={"base_uri": "https://localhost:8080/"} outputId="c51ff515-e1c7-45a0-f5aa-c32b907c7352"
model_1.evaluate(test_data)

# + id="fLpNP7FZ3Xti" colab={"base_uri": "https://localhost:8080/"} outputId="8e53b10e-50cc-4b6f-abf3-fdb555156295"
model_2.evaluate(test_data)

# + id="Y99hAWZp3X1o" colab={"base_uri": "https://localhost:8080/"} outputId="8a28801b-cea8-4273-87b6-81cd9f1d1798"
model_3.evaluate(test_data)

# + id="0bLeOPTM3X5T" colab={"base_uri": "https://localhost:8080/"} outputId="0c0ebb86-1b25-4c10-e592-f7df63d3671e"
model_4.evaluate(test_data)

# + id="gkVczdqZ3X_U" colab={"base_uri": "https://localhost:8080/"} outputId="1f74cbf5-67f8-478e-9b5e-86c8678810ec"
model_5.evaluate(test_data)

# + id="x9gIjSOI3ZVr" colab={"base_uri": "https://localhost:8080/"} outputId="aed7fd86-631c-43a9-8a10-c5d1aeb5a485"
model_6.evaluate(test_data)

# + id="YL-4i5hJ3Za0" colab={"base_uri": "https://localhost:8080/"} outputId="340cbc7b-11e7-4cdd-d25f-62917533fc75"
resnet_model.evaluate(test_data)

# + id="3OCsHI_g3ZhU" colab={"base_uri": "https://localhost:8080/"} outputId="9f25a6de-2eac-4d77-fa5a-b21f170efadd"
efficientnet_model.evaluate(test_data)

# + id="xgGx80mB3ZjZ" colab={"base_uri": "https://localhost:8080/"} outputId="816a33f5-1f19-4144-e3f6-29f511996288"
mobilenet_model.evaluate(test_data)

# + id="O9Dw-cNT3ZlO" colab={"base_uri": "https://localhost:8080/"} outputId="85cca83e-81f5-469e-954c-7be1ffc9e587"
efficientnetb1_model.evaluate(test_data)

# + [markdown] id="U4qTedkW3vdI"
# ## Conclusions 
#
# With all the model building, testing and tuning we come to the conclusion that our `efficientnetb1_model` is the best with `90%` accuracyon test data.
#
# ![Image](https://i.ibb.co/cCBsJ8P/accuracy.jpg)

# + id="L1rdaOAC4d_2"
# Custom layer for our pre-trained model
feature_extractor_layer = hub.KerasLayer(efficientnetb1_url,
                                         trainable=True, # freezing already learnt patterns 
                                         name="feature_extraction_layer",
                                         input_shape=IMAGE_SHAPE+(None,))

# + id="xKWMa_SM4GdP"
Xray_model = tf.keras.models.load_model("/content/drive/MyDrive/efficientnetb1_model.h5", custom_objects={'KerasLayer': feature_extractor_layer})

# + id="YAG2CNXZ4PbZ" colab={"base_uri": "https://localhost:8080/"} outputId="877b0598-f9a5-495a-f0eb-2664f4b9a57c"
Xray_model.evaluate(test_data)

# + id="TmoTksTi_o8f" colab={"base_uri": "https://localhost:8080/"} outputId="3efd8aa1-dc89-41cf-a49f-2d6d93ba2271"
Xray_model.summary()


# + [markdown] id="0yOXYYOj6irM"
# ## Predictions on custom data

# + id="yVPPVY0U6oBw"
# function to import image and resize 
def load_and_prep_image(filename, img_shape=224):
  """
  Reads an image from filename, turns it into a tensor and reshapes it to (img_shape, img_shape,, color_channels)
  """
  # Read in the image
  img = tf.io.read_file(filename)
  # Decode the read file into a tensor
  img = tf.image.decode_image(img)
  # Resize the image  
  img = tf.image.resize(img, size=IMAGE_SHAPE)
  #Grayscale
  img = tf.image.grayscale_to_rgb(img)
  # Rescale the image (getting all values between 0 & 1)
  img = img/255

  return img


# + id="yahfWjhR6rFL"
def pred_and_plot(model, filename, class_names=["NORMAL","PNEUMONIA"]):
  """
  Imports an image located at filename, makes a prediction with model and plots the image with the predicted class as the title.
  """
  # Import target image and preprocess it 
  img = load_and_prep_image(filename)

  # Make a prediction 
  pred = model.predict(tf.expand_dims(img, axis=0))
  # pred = model.predict(tf.squeeze(img))

  # Get the predicted class 
  pred_class = class_names[int(tf.round(pred))]

  # Plot image and predicted class
  plt.imshow(img)
  plt.title(f"Prediction : {pred_class}")
  plt.axis(False);


# + id="ANpCHF3Y63M7" colab={"base_uri": "https://localhost:8080/", "height": 264} outputId="bdc98ce6-6a34-41f3-f961-a47ac6973669"
pred_and_plot(Xray_model,"/content/norm1.jpeg" )

# + id="wpxxA3yN9Q91" colab={"base_uri": "https://localhost:8080/", "height": 264} outputId="739661e2-dd96-4ab2-fe6a-899ddfdf4e99"
pred_and_plot(Xray_model,"/content/pneu1.jpeg" )

# + id="jEOdmr8hD9Zc" colab={"base_uri": "https://localhost:8080/", "height": 264} outputId="2fe7e1a2-9e97-4a67-90b1-287d39d857fd"
pred_and_plot(Xray_model,"/content/norm2.jpeg" )

# + id="Vpkcm3gWD935" colab={"base_uri": "https://localhost:8080/", "height": 264} outputId="0ecf80bb-e69f-4db4-cdd4-1f48654a9809"
pred_and_plot(Xray_model,"/content/pneu2.jpeg" )

# + id="QPFDEljLEGtG"


StarcoderdataJupyterScriptsDedupFiltered