<jupyter_start><jupyter_text>**Importing the packages and libraries**<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import VGG16,DenseNet201,ResNet50,VGG19,Xception,InceptionResNetV2
from tensorflow.keras.layers import AveragePooling2D,MaxPooling2D,GlobalAveragePooling2D
from tensorflow.keras.layers import Dropout
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
import os
if tf.test.gpu_device_name():
  print('Default GPU Device:{}'.format(tf.test.gpu_device_name()))
else:
  print("Please install GPU version of TF")
train_path='/content/drive/My Drive/covid-chestxray-dataset-master/covid-chestxray-dataset-master/DATA/train'
test_path='/content/drive/My Drive/covid-chestxray-dataset-master/covid-chestxray-dataset-master/DATA/test'
BS = 2
train_datagen=ImageDataGenerator(rescale=1./255,fill_mode='nearest')
train_generator = train_datagen.flow_from_directory(directory=train_path,target_size=(224,224),class_mode="categorical",batch_size=BS,shuffle=True,seed=42)
# val_generator=train_datagen.flow_from_directory(directory=train_path,target_size=(224,224),class_mode="categorical",batch_size=BS,subset='validation')
test_batches=ImageDataGenerator(rescale=1./255,fill_mode='nearest').flow_from_directory(directory=test_path,target_size=(224,224),class_mode="categorical",batch_size=BS,seed=42,shuffle=False)
#RANDOM INITIALIZATION OF WEIGHTS
# class_weight = {
#     0:0.85,
#     1:0.15
# }
imgs,labels=next(train_generator)
def plot_img(img_arr):
  fig,axes=plt.subplots(1,2,figsize=(10,10))
  axes=axes.flatten()
  for img,ax in zip(img_arr,axes):
    ax.imshow(img)
    ax.axis('off')
  plt.tight_layout()
  plt.show()
plot_img(imgs)
print(labels)<jupyter_output><empty_output><jupyter_text>ResNet50<jupyter_code>INIT_LR = 1e-5
EPOCHS = 30
BS = 2
k_fold=3
cv_scores, model_history = list(), list()
for i in range(k_fold):
  print("K-FOLD:",i+1)
  baseModel = ResNet50(include_top=False,weights='imagenet',input_tensor=Input(shape=(224, 224, 3)))

  headModel = baseModel.output

  headModel=GlobalAveragePooling2D()(headModel)

  headModel=Dense(1024,activation='relu')(headModel)
  # headModel=Dense(256,activation='relu')(headModel)
  headModel = Dense(2, activation="softmax")(headModel)

  model = Model(inputs=baseModel.input, outputs=headModel)

  print("[INFO] compiling model...")
  opt = Adam(lr=INIT_LR)
  model.compile(loss="categorical_crossentropy", optimizer=opt,metrics=["accuracy"])


  print("[INFO] training head...")
  H = model.fit(x=train_generator,batch_size=BS,epochs=EPOCHS,verbose=2)
  predictions=model.predict(test_batches)
  predIdxs = np.argmax(predictions, axis=1)
  cm=confusion_matrix(y_true=test_batches.classes,y_pred=predIdxs)
  print(cm)
  print(classification_report(test_batches.classes,predIdxs))
  total = sum(sum(cm))
  acc = (cm[0, 0] + cm[1, 1]) / total
  sensitivity = cm[0, 0] / (cm[0, 0] + cm[0, 1])
  specificity = cm[1, 1] / (cm[1, 0] + cm[1, 1])
  # show the confusion matrix, accuracy, sensitivity, and specificity
  print(cm)
  print("acc: {:.4f}".format(acc))
  print("sensitivity: {:.4f}".format(sensitivity))
  print("specificity: {:.4f}".format(specificity))

  cv_scores.append(acc)
  model_history.append(H.history)

print('Estimated Accuracy %.3f (%.3f)' % (np.mean(cv_scores), np.std(cv_scores)))
N = EPOCHS
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, N), H.history["loss"], label="train_loss")
# plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, N), H.history["accuracy"], label="train_acc")
# plt.plot(np.arange(0, N), H.history["val_accuracy"], label="val_acc")
plt.title("Training Loss and Accuracy on COVID-19 Dataset")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")<jupyter_output><empty_output><jupyter_text>VGG19<jupyter_code>INIT_LR = 1e-5
EPOCHS = 30
BS = 2
k_fold=3
cv_scores, model_history = list(), list()
for i in range(k_fold):
  print("K-FOLD:",i+1)
  baseModel = VGG19(include_top=False,weights='imagenet',input_tensor=Input(shape=(224, 224, 3)))

 

  headModel = baseModel.output
  headModel=GlobalAveragePooling2D()(headModel)
  headModel=Dense(1024,activation='relu')(headModel)
  headModel = Dense(2, activation="softmax")(headModel)

  model = Model(inputs=baseModel.input, outputs=headModel)

  print("[INFO] compiling model...")
  opt = Adam(lr=INIT_LR)
  model.compile(loss="categorical_crossentropy", optimizer=opt,metrics=["accuracy"])


  print("[INFO] training head...")
  H = model.fit(x=train_generator,batch_size=BS,epochs=EPOCHS,verbose=2)
  predictions=model.predict(test_batches)
  predIdxs = np.argmax(predictions, axis=1)
  cm=confusion_matrix(y_true=test_batches.classes,y_pred=predIdxs)
  print(cm)
  print(classification_report(test_batches.classes,predIdxs))
  total = sum(sum(cm))
  acc = (cm[0, 0] + cm[1, 1]) / total
  sensitivity = cm[0, 0] / (cm[0, 0] + cm[0, 1])
  specificity = cm[1, 1] / (cm[1, 0] + cm[1, 1])
  # show the confusion matrix, accuracy, sensitivity, and specificity
  print(cm)
  print("acc: {:.4f}".format(acc))
  print("sensitivity: {:.4f}".format(sensitivity))
  print("specificity: {:.4f}".format(specificity))
  cv_scores.append(acc)
  model_history.append(H.history)

print('Estimated Accuracy %.3f (%.3f)' % (np.mean(cv_scores), np.std(cv_scores)))
N = EPOCHS
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, N), H.history["loss"], label="train_loss")
# plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, N), H.history["accuracy"], label="train_acc")
# plt.plot(np.arange(0, N), H.history["val_accuracy"], label="val_acc")
plt.title("Training Loss and Accuracy on COVID-19 Dataset")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")<jupyter_output><empty_output><jupyter_text>VGG16<jupyter_code>INIT_LR = 1e-5
EPOCHS = 30
BS = 2
k_fold=3
cv_scores, model_history = list(), list()
for i in range(k_fold):
  print("K-FOLD:",i+1)
  baseModel = VGG16(include_top=False,weights='imagenet',input_tensor=Input(shape=(224, 224, 3)))
  
 

  headModel = baseModel.output

  headModel=GlobalAveragePooling2D()(headModel)
  
  headModel=Dense(1024,activation='relu')(headModel)
  headModel = Dense(2, activation="softmax")(headModel)

  model = Model(inputs=baseModel.input, outputs=headModel)

  print("[INFO] compiling model...")
  opt = Adam(lr=INIT_LR)
  model.compile(loss="categorical_crossentropy", optimizer=opt,metrics=["accuracy"])


  print("[INFO] training head...")
  H = model.fit(x=train_generator,batch_size=BS,epochs=EPOCHS,verbose=2)
  predictions=model.predict(test_batches)
  predIdxs = np.argmax(predictions, axis=1)
  cm=confusion_matrix(y_true=test_batches.classes,y_pred=predIdxs)
  print(cm)
  print(classification_report(test_batches.classes,predIdxs))
  total = sum(sum(cm))
  acc = (cm[0, 0] + cm[1, 1]) / total
  sensitivity = cm[0, 0] / (cm[0, 0] + cm[0, 1])
  specificity = cm[1, 1] / (cm[1, 0] + cm[1, 1])
  # show the confusion matrix, accuracy, sensitivity, and specificity
  print(cm)
  print("acc: {:.4f}".format(acc))
  print("sensitivity: {:.4f}".format(sensitivity))
  print("specificity: {:.4f}".format(specificity))
  cv_scores.append(acc)
  model_history.append(H.history)

print('Estimated Accuracy %.3f (%.3f)' % (np.mean(cv_scores), np.std(cv_scores)))
N = EPOCHS
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, N), H.history["loss"], label="train_loss")
# plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, N), H.history["accuracy"], label="train_acc")
# plt.plot(np.arange(0, N), H.history["val_accuracy"], label="val_acc")
plt.title("Training Loss and Accuracy on COVID-19 Dataset")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")<jupyter_output><empty_output><jupyter_text>Xception<jupyter_code>INIT_LR = 1e-5
EPOCHS = 30
BS = 2
k_fold=3
cv_scores, model_history = list(), list()
for i in range(k_fold):
  print("K-FOLD:",i+1)
  baseModel = Xception(include_top=False,weights='imagenet',input_tensor=Input(shape=(224, 224, 3)))
  
 

  headModel = baseModel.output
 
  headModel=GlobalAveragePooling2D()(headModel)
  
  headModel=Dense(1024,activation='relu')(headModel)
  headModel = Dense(2, activation="softmax")(headModel)

  model = Model(inputs=baseModel.input, outputs=headModel)

  print("[INFO] compiling model...")
  opt = Adam(lr=INIT_LR)
  model.compile(loss="categorical_crossentropy", optimizer=opt,metrics=["accuracy"])


  print("[INFO] training head...")
  H = model.fit(x=train_generator,batch_size=BS,epochs=EPOCHS,verbose=2)
  predictions=model.predict(test_batches)
  predIdxs = np.argmax(predictions, axis=1)
  cm=confusion_matrix(y_true=test_batches.classes,y_pred=predIdxs)
  print(cm)
  print(classification_report(test_batches.classes,predIdxs))
  total = sum(sum(cm))
  acc = (cm[0, 0] + cm[1, 1]) / total
  sensitivity = cm[0, 0] / (cm[0, 0] + cm[0, 1])
  specificity = cm[1, 1] / (cm[1, 0] + cm[1, 1])
  # show the confusion matrix, accuracy, sensitivity, and specificity
  print(cm)
  print("acc: {:.4f}".format(acc))
  print("sensitivity: {:.4f}".format(sensitivity))
  print("specificity: {:.4f}".format(specificity))
  cv_scores.append(acc)
  model_history.append(H.history)

print('Estimated Accuracy %.3f (%.3f)' % (np.mean(cv_scores), np.std(cv_scores)))
N = EPOCHS
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, N), H.history["loss"], label="train_loss")
# plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, N), H.history["accuracy"], label="train_acc")
# plt.plot(np.arange(0, N), H.history["val_accuracy"], label="val_acc")
plt.title("Training Loss and Accuracy on COVID-19 Dataset")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")<jupyter_output><empty_output><jupyter_text>DenseNet201<jupyter_code>INIT_LR = 1e-5
EPOCHS = 30
BS = 2
k_fold=3
cv_scores, model_history = list(), list()
for i in range(k_fold):
  print("K-FOLD:",i+1)
  baseModel = DenseNet201(include_top=False,weights='imagenet',input_tensor=Input(shape=(224, 224, 3)))
  


  headModel = baseModel.output
  
  headModel=GlobalAveragePooling2D()(headModel)
  
  headModel=Dense(1024,activation='relu')(headModel)
  headModel = Dense(2, activation="softmax")(headModel)

  model = Model(inputs=baseModel.input, outputs=headModel)

  print("[INFO] compiling model...")
  opt = Adam(lr=INIT_LR)
  model.compile(loss="categorical_crossentropy", optimizer=opt,metrics=["accuracy"])


  print("[INFO] training head...")
  H = model.fit(x=train_generator,batch_size=BS,epochs=EPOCHS,verbose=2)
  predictions=model.predict(test_batches)
  predIdxs = np.argmax(predictions, axis=1)
  cm=confusion_matrix(y_true=test_batches.classes,y_pred=predIdxs)
  print(cm)
  print(classification_report(test_batches.classes,predIdxs))
  total = sum(sum(cm))
  acc = (cm[0, 0] + cm[1, 1]) / total
  sensitivity = cm[0, 0] / (cm[0, 0] + cm[0, 1])
  specificity = cm[1, 1] / (cm[1, 0] + cm[1, 1])
  # show the confusion matrix, accuracy, sensitivity, and specificity
  print(cm)
  print("acc: {:.4f}".format(acc))
  print("sensitivity: {:.4f}".format(sensitivity))
  print("specificity: {:.4f}".format(specificity))
  cv_scores.append(acc)
  model_history.append(H.history)

print('Estimated Accuracy %.3f (%.3f)' % (np.mean(cv_scores), np.std(cv_scores)))
N = EPOCHS
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, N), H.history["loss"], label="train_loss")
# plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, N), H.history["accuracy"], label="train_acc")
# plt.plot(np.arange(0, N), H.history["val_accuracy"], label="val_acc")
plt.title("Training Loss and Accuracy on COVID-19 Dataset")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")<jupyter_output><empty_output><jupyter_text>InceptionResNetV2<jupyter_code>INIT_LR = 1e-5
EPOCHS = 30
BS = 2
k_fold=3
cv_scores, model_history = list(), list()
for i in range(k_fold):
  print("K-FOLD:",i+1)
  baseModel = InceptionResNetV2(include_top=False,weights='imagenet',input_tensor=Input(shape=(224, 224, 3)))
  
 

  headModel = baseModel.output
  
  headModel=GlobalAveragePooling2D()(headModel)
  
  headModel=Dense(1024,activation='relu')(headModel)
  headModel = Dense(2, activation="softmax")(headModel)

  model = Model(inputs=baseModel.input, outputs=headModel)

  print("[INFO] compiling model...")
  opt = Adam(lr=INIT_LR)
  model.compile(loss="categorical_crossentropy", optimizer=opt,metrics=["accuracy"])


  print("[INFO] training head...")
  H = model.fit(x=train_generator,batch_size=BS,epochs=EPOCHS,verbose=2)
  predictions=model.predict(test_batches)
  predIdxs = np.argmax(predictions, axis=1)
  cm=confusion_matrix(y_true=test_batches.classes,y_pred=predIdxs)
  print(cm)
  print(classification_report(test_batches.classes,predIdxs))
  total = sum(sum(cm))
  acc = (cm[0, 0] + cm[1, 1]) / total
  sensitivity = cm[0, 0] / (cm[0, 0] + cm[0, 1])
  specificity = cm[1, 1] / (cm[1, 0] + cm[1, 1])
  # show the confusion matrix, accuracy, sensitivity, and specificity
  print(cm)
  print("acc: {:.4f}".format(acc))
  print("sensitivity: {:.4f}".format(sensitivity))
  print("specificity: {:.4f}".format(specificity))
  cv_scores.append(acc)
  model_history.append(H.history)

print('Estimated Accuracy %.3f (%.3f)' % (np.mean(cv_scores), np.std(cv_scores)))
N = EPOCHS
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, N), H.history["loss"], label="train_loss")
# plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, N), H.history["accuracy"], label="train_acc")
# plt.plot(np.arange(0, N), H.history["val_accuracy"], label="val_acc")
plt.title("Training Loss and Accuracy on COVID-19 Dataset")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1710261569416499, 'fraction_numerical': 0.026018178033719558, 'mean_word_length': 6.675186368477103, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '415485', 'license': 'MIT', 'n_tokens_mistral': 5844, 'n_tokens_neox': 5482, 'n_words': 866, 'path': 'Files/Covid_19_Chest_Xray_detection.ipynb', 'repo_name': 'KARTHIKEYANJM/Automatic-Covid-19-Classification-and-Segmentation', 'size': '14413'}