# ---
# 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
# ---

# + id="MgGI0-9nOoCW"
COVID_EPOCHS = 20
PNEUMONIA_EPOCHS = 15
PNEUMOTHORAX_EPOCHS = 10
COVID_SEED = 123
PNEUMONIA_SEED = 123
PNEUMOTHORAX_SEED = 123

# + id="h4jGIC4UOs-t"
import tensorflow as tf
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
from keras.models import Sequential
from tensorflow.keras.applications import ResNet50
import os
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.optimizers import Adam, SGD, Adadelta
import pathlib
import matplotlib.pyplot as plt
import numpy as np

# + colab={"base_uri": "https://localhost:8080/"} id="BgOoFyzsOu_G" outputId="70b63986-b9dd-4406-fc59-f1a25d6ee3c0"
from google.colab import drive
drive.mount("/content/gdrive")

# + id="VFh_ddriOvcp"
# Dataset Parameters - CHANGE HERE
MODE = 'folder' # or 'file', if you choose a plain text file (see above).
#DATASET_PATH = '/content/gdrive/MyDrive/CSC522Project/CovidDataset/Dataset/' # the dataset file or root folder path.
DATASET_PATH = '/content/gdrive/MyDrive/CSC522Project/CovidDataset/StratifiedDataset/' # the dataset file or root folder path.

# + id="pHIKhMAtdR8h"
# # # Begin Predict COVID-19 # # #

# + colab={"base_uri": "https://localhost:8080/"} id="B0EkEFwWOxRC" outputId="2c3f6cee-38d8-4812-cd9d-74c7c85b30b0"
# train_ds = tf.keras.utils.image_dataset_from_directory(
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    DATASET_PATH+"Covid/",
    labels="inferred",
    batch_size=32,
    image_size=(256, 256),
    shuffle=True,
    seed=COVID_SEED,
    validation_split=0.2, 
    subset="training",
)

val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    DATASET_PATH+"Covid/",
    labels="inferred",
    batch_size=32,
    image_size=(256, 256),
    shuffle=True,
    seed=COVID_SEED,
    validation_split=0.2, 
    subset="validation",
)

# + colab={"base_uri": "https://localhost:8080/"} id="aGkcTzQrNp1g" outputId="2733be0a-07b6-4f5b-d437-ba428f75f9ff"
class_names = train_ds.class_names
print(class_names)

# + colab={"base_uri": "https://localhost:8080/", "height": 591} id="vG42kvc5Nqhb" outputId="64d1d2a6-dce7-4514-d8ba-d7c88e21e98f"
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")

# + id="tJNfWY56OzbR"
train_X = list()
train_y = list()
test_X = list()
test_y = list()

for image_batch_train, labels_batch_train in train_ds:
  for index in range(0, len(image_batch_train)):
    train_X.append(image_batch_train[index])
    train_y.append(labels_batch_train[index])

for image_batch, labels_batch in val_ds:
  for index in range(0, len(image_batch)):
    test_X.append(image_batch[index])
    test_y.append(labels_batch[index])

# + id="4FNMfud1BDLZ"
# Conv_Base = ResNet50(weights=None, input_shape=(256, 256, 3), classes=2)
Conv_Base = ResNet50(weights=None, input_shape=(256, 256, 3), include_top=False)

for layer in Conv_Base.layers[:-8]:
    layer.trainable = False

layer = tf.keras.layers.Normalization(
    axis=None)

model = Sequential()
model.add(tf.keras.layers.Rescaling(1./255))
model.add(Conv_Base)
model.add(Flatten())
model.add(Dense(units = 256, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(units = 1, activation = 'sigmoid'))

# + colab={"base_uri": "https://localhost:8080/"} id="bT_zoqu29OOA" outputId="873967c6-4796-4baa-83d7-469f29eeaffa"
opt = Adadelta(learning_rate=0.1)
model.compile(optimizer = opt, loss = 'BinaryCrossentropy', metrics=[
        tf.keras.metrics.BinaryAccuracy(name='accuracy'),
        tf.keras.metrics.Precision(name='precision'),
        tf.keras.metrics.Recall(name='recall')
    ])
# try to add class weights to make it predict 0, since we currently only predict class label 1
class_weight = {0: 1.,
                1: 1.}


r=model.fit(x = train_ds, validation_data = val_ds, epochs = COVID_EPOCHS, class_weight=class_weight)

# + colab={"base_uri": "https://localhost:8080/"} id="1tSQOk74lxUu" outputId="f7fafb11-5bc4-4cbf-94a1-cc44532db16b"
predictions = model.predict(val_ds)
predictions = np.ndarray.flatten(predictions)
predictions = np.where(predictions < 0.5, 0, 1) # Convert to 0 and 1.

np.set_printoptions(threshold=np.inf)
# print(predictions)

# + colab={"base_uri": "https://localhost:8080/"} id="DamZG0YJaR73" outputId="c392235e-1f3c-4898-dcb5-5a6076b7bbc2"
model.save('CovidNolan')
# !zip -r /content/CovidNolan.zip /content/CovidNolan/

# + id="yZGTECqAdMuE"
# # # Begin Predict Pneumonia # # #

# + colab={"base_uri": "https://localhost:8080/"} id="lG0zFjPHa2hb" outputId="4dfda53e-4c1a-42e9-ef7e-eabe61dd79aa"
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    DATASET_PATH+"Pneumonia/",
    labels="inferred",
    batch_size=32,
    image_size=(256, 256),
    shuffle=True,
    seed=COVID_SEED,
    validation_split=0.2, 
    subset="training",
)

val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    DATASET_PATH+"Pneumonia/",
    labels="inferred",
    batch_size=32,
    image_size=(256, 256),
    shuffle=True,
    seed=COVID_SEED,
    validation_split=0.2, 
    subset="validation",
)

# + colab={"base_uri": "https://localhost:8080/"} id="UPUzbgeaeSJB" outputId="a845af3d-eb66-4b89-e095-7ed536d9e7ee"
class_names = train_ds.class_names
print(class_names)

# + colab={"base_uri": "https://localhost:8080/", "height": 591} id="RCPoyhsHby1m" outputId="4e519d21-8a27-4d01-f0e8-454476a83f8c"
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")

# + id="sykbSP0CcMDL"
Conv_Base = ResNet50(weights=None, input_shape=(256, 256, 3), include_top=False)

for layer in Conv_Base.layers[:-8]:
    layer.trainable = False

model = Sequential()
model.add(tf.keras.layers.Rescaling(1./255))
model.add(Conv_Base)
model.add(Flatten())
model.add(Dense(units = 256, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(units = 1, activation = 'sigmoid'))

# + colab={"base_uri": "https://localhost:8080/"} id="a4fdzC9wcZSS" outputId="ee55f69f-ac18-4f44-90b7-4edab6f355eb"
opt = Adadelta(learning_rate=0.25)
model.compile(optimizer = opt, loss = 'BinaryCrossentropy', metrics=[
        tf.keras.metrics.BinaryAccuracy(name='accuracy'),
        tf.keras.metrics.Precision(name='precision'),
        tf.keras.metrics.Recall(name='recall')
    ])
# try to add class weights to make it predict 0, since we currently only predict class label 1
class_weight = {0: 1.,
                1: 1.}


r=model.fit(x = train_ds, validation_data = val_ds, epochs = PNEUMONIA_EPOCHS, class_weight=class_weight, shuffle=True)

# + colab={"base_uri": "https://localhost:8080/"} id="BD1z_SINcfSh" outputId="f366c899-29c4-40ce-b27c-590c0711c4e5"
predictions = model.predict(val_ds)
predictions = np.ndarray.flatten(predictions)
predictions = np.where(predictions < 0.5, 0, 1) # Convert to 0 and 1.

np.set_printoptions(threshold=np.inf)
# print(predictions)

# + id="y8xFtI33dGxz"
# # # Begin Predict Pneumothorax # # #

# + colab={"base_uri": "https://localhost:8080/"} id="1t5-Z58yqnwK" outputId="17788b70-9539-4188-d14f-592dd37b94c4"
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    DATASET_PATH+"Pneumothorax/",
    labels="inferred",
    batch_size=32,
    image_size=(256, 256),
    shuffle=True,
    seed=PNEUMOTHORAX_SEED,
    validation_split=0.2, 
    subset="training",
)

val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    DATASET_PATH+"Pneumothorax/",
    labels="inferred",
    batch_size=32,
    image_size=(256, 256),
    shuffle=True,
    seed=PNEUMOTHORAX_SEED,
    validation_split=0.2, 
    subset="validation",
)

# + colab={"base_uri": "https://localhost:8080/"} id="YrdovkH2qoEC" outputId="4a55379a-973d-4fd2-a6e8-fc3bacc821ab"
class_names = train_ds.class_names
print(class_names)

# + colab={"base_uri": "https://localhost:8080/", "height": 591} id="WfOkUX7RrBUi" outputId="75d64732-e0a1-4d81-b488-af44b2b03079"
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")

# + id="nUmVN6ubrH23"
Conv_Base = ResNet50(weights=None, input_shape=(256, 256, 3), include_top=False)

for layer in Conv_Base.layers[:-8]:
    layer.trainable = False

model = Sequential()
model.add(tf.keras.layers.Rescaling(1./255))
model.add(Conv_Base)
model.add(Flatten())
model.add(Dense(units = 256, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(units = 1, activation = 'sigmoid'))

# + colab={"base_uri": "https://localhost:8080/"} id="tnx6mBNgrNXg" outputId="9ec14092-41f8-4205-e823-b1066901446f"
opt = Adadelta(learning_rate=0.25)
model.compile(optimizer = opt, loss = 'BinaryCrossentropy', metrics=[
        tf.keras.metrics.BinaryAccuracy(name='accuracy'),
        tf.keras.metrics.Precision(name='precision'),
        tf.keras.metrics.Recall(name='recall')
    ])
# try to add class weights to make it predict 0, since we currently only predict class label 1
class_weight = {0: 1.,
                1: 1.}


r=model.fit(x = train_ds, validation_data = val_ds, epochs = PNEUMOTHORAX_EPOCHS, class_weight=class_weight, shuffle=True)

# + id="Co_PgwhlrRIq"
predictions = model.predict(val_ds)
predictions = np.ndarray.flatten(predictions)
predictions = np.where(predictions < 0.5, 0, 1) # Convert to 0 and 1.

np.set_printoptions(threshold=np.inf)
# print(predictions)

StarcoderdataJupyterScriptsDedupFiltered