# ---
# 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="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/bhanuteja2001/COVID-19-XrayDetection/blob/main/Datathon.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + colab={"base_uri": "https://localhost:8080/"} id="NwQJ4WRyGTwQ" outputId="c5eb20f8-37c6-4e2b-c89c-e9344b6fc8de"
from google.colab import drive
drive.mount('/content/drive')

# + id="8MKXEeJQHC2O"
# !unzip "/content/drive/My Drive/covid_image_data.zip" -d "/content/drive/My Drive/"

# + colab={"base_uri": "https://localhost:8080/"} id="_60nQPD6HRun" outputId="b4cae110-c8c5-431e-fd97-8d7621b90e0f"
import tensorflow as tf
print(tf.__version__)

# + id="KHWUjLfSKJ55"
import pandas as pd
import numpy as np

# + colab={"base_uri": "https://localhost:8080/", "height": 195} id="XTA-X74bKXVr" outputId="0b52417f-2fcb-4066-c057-f281fe32a35b"
df_training = pd.read_csv('/content/drive/MyDrive/covid_image_data/Training_set_covid.csv')
df_training.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 195} id="slVgztY-KnJu" outputId="0b5c39ca-3f4e-411b-fa2e-a72d2cfba533"
df_testing = pd.read_csv('/content/drive/MyDrive/covid_image_data/Testing_set_covid.csv')
df_testing.head()

# + id="IEXRlGwOK8qW"
training_file_path = ['/content/drive/MyDrive/covid_image_data/train/' + fname[:-4] +  '.jpg' for fname in df_training['filename']]
training_file_path

# + id="2tfG6wfPP_lo"
testing_file_path = ['/content/drive/MyDrive/covid_image_data/test/' + fname[:-4] +  '.jpg' for fname in df_testing['filename']]
testing_file_path

# + colab={"base_uri": "https://localhost:8080/"} id="0yj9kkOoQRBn" outputId="a718cef5-eb82-4d8d-97e0-f82f419577f8"
df_training.label.value_counts()

# + colab={"base_uri": "https://localhost:8080/"} id="4KfiXAACQhgR" outputId="cda5617a-e793-4cab-c119-51e92b8f5ff8"
# Confirm if number of images is same as number of labels given
if len(df_training) == len(training_file_path):
    print('Number of labels i.e. ', len(df_training), 'matches the number of filenames i.e. ', len(training_file_path))
else:
    print('Number of labels doesnot matches the number of filenames')

# + colab={"base_uri": "https://localhost:8080/", "height": 402} id="GTf-JLTGQ0bj" outputId="a8af9213-e94c-47bf-f870-1773f6c17ada"
images_training = [[fname, '/content/drive/MyDrive/covid_image_data/train/' + fname[:-4] + '.jpg'] for fname in df_training['filename']]
images_training = pd.DataFrame(images_training)
images_training.columns = ['filename', 'file']
images_training

# + id="BN3lht4s9QN_" outputId="12f2ab06-fb5e-4c78-a31a-6818f836a387" colab={"base_uri": "https://localhost:8080/", "height": 195}
df_training.head()

# + id="e-eF26Nm-ppk" outputId="53504500-3414-40df-c213-f2848719020d" colab={"base_uri": "https://localhost:8080/", "height": 402}
X = pd.merge(df_training, images_training, left_index=True, right_index=True).drop(columns=['filename_x'])
X.rename(columns = {'filename_y':'filename'}, inplace = True)
X

# + id="tM_JKRpH_LRk"
value_one = X[X['label'] == 1]

# + id="p9RRFBH7BqPd" outputId="7fbe1474-5cc8-445f-e92b-f51469ddb3cc" colab={"base_uri": "https://localhost:8080/", "height": 402}
value_one

# + id="fNpW8CdoROH9"
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout,Flatten
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import SparseCategoricalCrossentropy
from tensorflow.keras.callbacks import TensorBoard,EarlyStopping
# %load_ext tensorboard

# + id="w9VoQJ_nTD5N"
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
from sklearn.model_selection import train_test_split
from tqdm import tqdm
import matplotlib.pyplot as plt

# + id="Sw_qKZwS_aIH"
datagen = ImageDataGenerator(
        rescale=1./255,
        horizontal_flip=True,
        vertical_flip=True)

# + id="8LOSOG2-C51N" outputId="1f5feccd-b05e-4e41-e53b-ad249e0f235f" colab={"base_uri": "https://localhost:8080/", "height": 856}
from PIL import Image  
  
# creating a object  
im = Image.open(r"/content/drive/MyDrive/covid_image_data/train/Image_1.jpg")  
  
im

# + id="8LmcQWYgBnzL"


# + id="gCDXTzycBxP3" outputId="5369c3c7-82a3-42c3-bbb5-51e9a617d3e7" colab={"base_uri": "https://localhost:8080/"}
pic_array.shape # Image dimension

# + id="Zl2EJ8iYB8cd" outputId="d24a131e-8215-45ba-e437-4aea173318bd" colab={"base_uri": "https://localhost:8080/"}
pic = pic_array.reshape((1,) + pic_array.shape)
pic.shape

# + id="SISmrHC0HdMW"
for i in value_one['file']:
  pic = load_img(i)
  pic_array = img_to_array(pic)
  pic = pic_array.reshape((1,) + pic_array.shape)
  count = 1
  for batch in datagen.flow(pic, batch_size=1,save_to_dir=os.path.dirname("/content/drive/MyDrive/covid_image_data/generated_image/"), save_prefix='Image', save_format='jpg'):
    count += 1
    if count > 3:
      break

# + id="udrgYBaIONLK"
path, dirs, files = next(os.walk("/content/drive/MyDrive/covid_image_data/generated_image/"))
file_count = len(files)

# + id="APeiTTOcKLGH" outputId="700e8c8b-5c4e-463e-9acd-66926d321fc3" colab={"base_uri": "https://localhost:8080/"}
file_count

# + id="_AlCDlJgFhPQ"
import os
path = os.chdir("/content/drive/MyDrive/covid_image_data/train/")
k=1
for file in os.listdir(path):
    if file[-3:] == 'jpg':
        new_name="Image_ex_{}.jpg".format(k)
        k+=1
        os.rename(file,new_name)
        print("done")

# + id="-HI8nZFdCe0u"
for i in os.listdir(os.chdir("/content/drive/MyDrive/covid_image_data/train/")):
  print(i)

# + id="fr9FjKCuUOqA"
for i in value_one['file']:
  os.remove(i)

# + id="IOD23vx4Ui1g" outputId="434c46ee-e277-477e-c42f-10b49b109fde" colab={"base_uri": "https://localhost:8080/", "height": 402}
#X = X[X.label != 1]
X

# + id="Q8OiPn3NVSI3"
images_training = [[fname, '/content/drive/MyDrive/covid_image_data/train/' + fname[:-4] + '.jpg'] for fname in os.listdir(os.chdir("/content/drive/MyDrive/covid_image_data/train/"))]
images_training = pd.DataFrame(images_training)
images_training.columns = ['filename', 'file']
images_training['label'] = 0
#images_training.drop(images_training.tail(1).index,inplace=True)

# + id="eRLlEXrMghkF" outputId="ca86170b-1549-41f9-ee73-cf936dbdeb68" colab={"base_uri": "https://localhost:8080/", "height": 195}
images_training_2.tail()

# + id="AT1pJpfqYGmp"
images_training_2 = [[fname, '/content/drive/MyDrive/covid_image_data/generated_image/' + fname[:-4] + '.jpg'] for fname in os.listdir(os.chdir("/content/drive/MyDrive/covid_image_data/generated_image/"))]
images_training_2 = pd.DataFrame(images_training_2)
images_training_2.columns = ['filename', 'file']
images_training_2['label'] = 1
images_training_2.drop(images_training_2.tail(1).index,inplace=True)

# + id="t0Rr1IWTYwez"
Train = images_training.append(images_training_2, ignore_index=True)

# + id="8V64DZKxf5CT" outputId="38b8e1f3-bdd7-4b78-bdee-9825f9602e90" colab={"base_uri": "https://localhost:8080/", "height": 195}
Train.tail()

# + id="jJClkloBZHda" outputId="83f0a1fa-2286-40d3-c487-f73ea8980311" colab={"base_uri": "https://localhost:8080/", "height": 17}
from google.colab import files
Train.to_csv('filetrain.csv')         
files.download('filetrain.csv')

# + id="Ah3Zs_7Jvfox"
Train = Train.drop(columns=['filename'])

# + id="7zFjlrBOvkUw"


# + id="Ws-iApu8T4jK"
y = np.array(Train['label'])

# + colab={"base_uri": "https://localhost:8080/", "height": 195} id="nw2oho6NToYI" outputId="7db0f922-4d61-4987-c58b-8c8e4eae49ce"
Train = pd.merge(df_training, images_training, on='filename')
Train.drop(columns = ['filename'], inplace = True)
Train.head()

# + colab={"base_uri": "https://localhost:8080/"} id="nO0rh-EcTJSY" outputId="b278895f-4b5f-44fc-89f9-237d0095b59b"
train_image = []
for i in tqdm(range(Train.shape[0])):
    img = image.load_img(Train['file'][i], target_size=(28,28,1), grayscale=False)
    img = image.img_to_array(img)
    img = img/255
    train_image.append(img)
X = np.array(train_image)

# + id="4Xb5Hf1HTkLx"
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.2)

# + id="fey3DjJ6fWzz"
y_train = y_train.reshape((-1,1))
y_test = y_test.reshape((-1,1))

# + id="GKD7y6FizJqz"


# + [markdown] id="zZPo08jGzKeY"
# ## MobileNet V2

# + id="U4rQTQcOfc0G"
from tensorflow.keras.applications import MobileNetV2

# + colab={"base_uri": "https://localhost:8080/"} id="2apMSaIKlCLc" outputId="2e069808-5676-461c-a602-3c301aeb8a70"
model2 = Sequential()
model2.add(MobileNetV2(include_top = False, weights="imagenet", input_shape=(200, 200, 3)))
model2.add(tf.keras.layers.GlobalAveragePooling2D())
model2.add(Dense(1, activation = 'sigmoid'))
model2.layers[0].trainable = False

# + id="pn3z3KPzlUC8"
from tensorflow.keras.optimizers import RMSprop
# model.compile(loss='binary_crossentropy',
#               optimizer=RMSprop(lr=0.001),
#               metrics=['accuracy', tf.keras.metrics.AUC()])

from tensorflow.keras import optimizers


model2.compile(optimizer=RMSprop(lr=0.01), loss = 'binary_crossentropy', metrics = 'accuracy')

# + colab={"base_uri": "https://localhost:8080/"} id="pDWt9RiAlWMb" outputId="cc40417c-7e4a-4466-86a2-e747654c8588"
train_image_2 = []
for i in tqdm(range(Train.shape[0])):
    img = load_img(Train['file'][i], target_size=(200,200,3), grayscale=False)
    img = img_to_array(img)
    img = img/255
    train_image_2.append(img)
X = np.array(train_image_2)

# + id="3momp8Cllyfw"
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.2)
y_train = y_train.reshape((-1,1))
y_test = y_test.reshape((-1,1))

# + colab={"base_uri": "https://localhost:8080/"} id="q4TOAiJHmE4k" outputId="46d9a122-7e85-4ddf-d095-f3eccd2d41be"
EPOCHS = 100

es_callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=2, 
                                               restore_best_weights=True,
                                               verbose=1)


history = model2.fit(X_train, y_train, epochs=EPOCHS,callbacks=[es_callback], validation_data=(X_test, y_test))

# + [markdown] id="Qu5qCqCiruCT"
# ## Image Testing

# + colab={"base_uri": "https://localhost:8080/", "height": 402} id="x7EJ1Ya9rn0C" outputId="08d5d724-b567-452f-f4ff-17c430501775"
images_testing = [[fname, '/content/drive/MyDrive/covid_image_data/test/' + fname[:-4] + '.jpg'] for fname in df_testing['filename']]
images_testing = pd.DataFrame(images_testing)
images_testing.columns = ['filename', 'file']
images_testing

# + colab={"base_uri": "https://localhost:8080/"} id="prx1B3_Sr4cd" outputId="bcb6b419-1c21-4233-e9a8-f0500eb4778c"
test_image = []
for i in tqdm(range(images_testing.shape[0])):
    img = load_img(images_testing['file'][i], target_size=(200, 200, 3), grayscale=False)
    img = img_to_array(img)
    img = img/255
    test_image.append(img)
X = np.array(test_image)

# + id="WR8gHuYssn53" outputId="adea3386-f519-496a-b23f-abc416c12827" colab={"base_uri": "https://localhost:8080/"}
prediction = model2.predict_classes(X)

# + colab={"base_uri": "https://localhost:8080/", "height": 17} id="eTcfOisdtFnm" outputId="d27cd88a-a27e-4906-93bc-f9f800c367db"

# To create Dataframe of predicted value with particular respective index
res = pd.DataFrame(prediction) #preditcions are nothing but the final predictions of your model on input features of your new unseen test data
res.index = res.index # its important for comparison. Here "test_new" is your new test dataset
res.columns = ["prediction"]

# To download the csv file locally
from google.colab import files
res.to_csv('prediction_results.csv')         
files.download('prediction_results.csv')

# + [markdown] id="WDNOG4-1vPnz"
# ### Using Movile Net V2 73.97081967213115
#

# + colab={"base_uri": "https://localhost:8080/"} id="EXSLucfquKS4" outputId="39de0f3f-0ac8-43b1-b1ae-90fb0ca023b6"


StarcoderdataJupyterScriptsDedupFiltered