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

# + [markdown] id="3PMty2EdFDwL"
# ## IMPORTING Covid19-radiography-database DATASET

# + colab={"base_uri": "https://localhost:8080/"} id="djIvdmgWlKgT" outputId="27c4a584-7aef-4b43-ccf5-6eb00f4be5eb"
# !pip install opendatasets --upgrade

# + id="0gTkJTbilydb"
# # !cp -r "/content/covid19-radiography-database" "/content/drive/MyDrive/Covid"

# + colab={"base_uri": "https://localhost:8080/"} id="iSUSy24mnZ_1" outputId="03560161-b9f7-499e-b267-9b1c99cf0e45"
# !pip install pyyaml h5py  # Required to save models in HDF5 format

# + id="iJpiGD06mfNb"
from keras.models import Sequential
from keras.layers import Convolution2D as Conv2D
from keras.layers import MaxPooling2D 
from keras.layers import Flatten
from keras.layers import Dense
from keras.callbacks import ModelCheckpoint
from keras.preprocessing.image import ImageDataGenerator

# + id="iOoC5V6VoSl2"
checkpoint =ModelCheckpoint(filepath="Custom_CNN__ternary_model.h5", 
                            monitor='val_accuracy',
                            verbose=1,
                            save_best_only=True, 
                            save_weights_only=False, 
                            mode='auto',
                            save_freq='epoch')

callbacks = [checkpoint]

# + [markdown] id="gspcjMNIFPXf"
# ## *CREATING CNN_TERNARY MODEL*

# + id="7dZtXnZWmzTE"
model1 = Sequential()
model1.add(Conv2D(32, (3, 3), input_shape = (224, 224, 3), activation = 'relu'))
model1.add(MaxPooling2D(pool_size = (2, 2)))
 
model1.add(Conv2D(64, (3, 3),activation = 'relu'))
model1.add(MaxPooling2D(pool_size = (2, 2)))
 
model1.add(Conv2D(128, (3, 3),activation = 'relu'))
model1.add(MaxPooling2D(pool_size = (2, 2)))
 
model1.add(Conv2D(256, (3, 3),activation = 'relu'))
model1.add(MaxPooling2D(pool_size = (2, 2)))
 
model1.add(Conv2D(512, (3, 3),activation = 'relu'))
model1.add(MaxPooling2D(pool_size = (2, 2)))
 
model1.add(Flatten())
model1.add(Dense(128, activation = 'relu'))
model1.add(Dense(64, activation = 'relu'))
model1.add(Dense(32, activation = 'relu'))
model1.add(Dense(3, activation='softmax'))
 
model1.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

# + colab={"base_uri": "https://localhost:8080/"} id="bzSgTLgXo0vQ" outputId="e111619f-866a-47e0-decf-5e32777585cc"
model1.summary()

# + colab={"base_uri": "https://localhost:8080/"} id="Xwfq7hmdo7VA" outputId="429d8e47-3a72-4c3c-9c0d-bb3ebb5a1c92"

base_dir =r'/content/Final'
 
datagen= ImageDataGenerator(rescale=1./255,validation_split=0.2)
 
 
train_generator = datagen.flow_from_directory(
              base_dir,  # This is the source directory for training images
              target_size=(224, 224),  
              batch_size=32,
              subset='training',
              class_mode='categorical')
 
 
validation_generator = datagen.flow_from_directory(
              base_dir,
              target_size=(224,224),
              batch_size=32,
              subset='validation',
              shuffle=False,
              class_mode='categorical')

# + colab={"base_uri": "https://localhost:8080/"} id="MQiKKd_lrdn5" outputId="d87a7f92-55c2-45b6-9d64-6fb9c11c867b"
hist = model1.fit_generator(train_generator,
                         steps_per_epoch = (train_generator.samples//32),
                         epochs = 5,
                         validation_data = validation_generator,
                         validation_steps = (validation_generator.samples/32),
                         callbacks=[checkpoint],
                         verbose = 1 )

# + [markdown] id="J1M18Y19DXzl"
# ## Accuracy of Ternary Classification using Custom CNN

# + colab={"base_uri": "https://localhost:8080/"} id="DEKleUE62XIz" outputId="4ed6269b-6784-4e3f-ac6e-1fb9bd702081"
ans = model1.evaluate(validation_generator,verbose=1)
print('Accuracy: %.2f' % (ans[1]*100))

# + id="xwjVLcf_3xX5"
# !cp -r "/content/Custom_CNN__ternary_model.h5" "/content/drive/MyDrive/Model"

# + [markdown] id="Ii_6OUXIoiNS"
# # **Prediction using X_RAY Scan Image**

# + colab={"base_uri": "https://localhost:8080/", "height": 340} id="YrWpZX4YhQ2N" outputId="7e08b70e-7bd4-4b9d-94ab-75d1a4fa2472"
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
import matplotlib.pyplot as plt
import numpy as np

def load_image(filename):
  # load the image
  img = load_img(filename, target_size=(225, 225))
  # convert to array
  img = img_to_array(img)
  plt.imshow(img/255.)
  # reshape into a single sample with 3 channels
  img = img.reshape(1, 225, 225, 3)
  return img

def predict(img):
  result = model1.predict_classes(img)
  if(result[0]==0):
    print("YOU ARE A COVID-19 PATIENT")
  elif(result[0]==1):
    print("YOU ARE  NORMAL")
  else:
    print("YOU ARE A VIRAL PNEUMONIA PATIENT")

img1 = load_image('/content/drive/MyDrive/Covid/COVID-19 Radiography Database/COVID/COVID (1006).png')
predict(img1)

# + colab={"base_uri": "https://localhost:8080/", "height": 340} id="th6ucomoir1V" outputId="76717126-600c-4f41-fb3a-9e2e2b0c0fb5"
img2 = load_image('/content/drive/MyDrive/Covid/COVID-19 Radiography Database/NORMAL/NORMAL (104).png')
predict(img2)

# + colab={"base_uri": "https://localhost:8080/", "height": 340} id="pQIMgsASoMZN" outputId="e1fd253e-ea5c-44e4-d319-f3edf377b1ca"
img3 = load_image('/content/drive/MyDrive/Covid/COVID-19 Radiography Database/Viral Pneumonia/Viral Pneumonia (1).png')
predict(img3)

# + [markdown] id="WV1sCOCeFsbL"
# ### ACCURACY GRAPH

# + colab={"base_uri": "https://localhost:8080/", "height": 281} id="EL7C0LI5h4rB" outputId="ac474fb9-f061-4204-ffcf-420084970640"
from matplotlib import pyplot

# pyplot.subplot(211)
# pyplot.title('Cross Entropy Loss')
# pyplot.plot(hist.history['loss'], color='blue', label='train')
# pyplot.plot(hist.history['val_loss'], color='orange', label='test')
# # plot accuracy
#pyplot.subplot(212)
pyplot.title('Classification Accuracy')
pyplot.plot(hist.history['accuracy'], color='blue', label='train')
pyplot.plot(hist.history['val_accuracy'], color='orange', label='test')
pyplot.savefig("accuracy1.png",dpi=300)

# + id="5gcc9I7wp029"
# !cp -r "/content/drive/MyDrive/Model"

# + [markdown] id="9-aa2Aa83Iac"
# ## Visualizing Feature Map

# + colab={"base_uri": "https://localhost:8080/"} id="KtCXc7yv_lcR" outputId="9c29ad13-557c-4ba8-999e-6982e490cb21"
#from keras.Input import Model
import keras
feature = keras.Model(model1.input,model1.layers[1].output)
feature.summary()

# + colab={"base_uri": "https://localhost:8080/"} id="_d_SaKtyCBvh" outputId="9dcdc8db-178e-4e15-de59-4212572e742b"
import cv2
img= cv2.imread("/content/drive/MyDrive/Covid/COVID-19 Radiography Database/COVID/COVID (1).png")
img2= cv2.resize(img,(224,224))
img2= np.expand_dims(img2,axis=0)
print(img2.shape)
feature_maps = feature.predict(img2)
feature_maps.shape

# + colab={"base_uri": "https://localhost:8080/", "height": 252} id="OK_uIOGiDZky" outputId="2ba290b4-3d67-4861-dcb7-c45ca45a9b42"
#square = 16
ix = 1
for _ in range(8):
	for _ in range(4):
		# specify subplot and turn of axis
		ax = pyplot.subplot(8, 4, ix)
		ax.set_xticks([])
		ax.set_yticks([])
		# plot filter channel in grayscale
		pyplot.imshow(feature_maps[0, :, :, ix-1])
		ix += 1
# show the figure
pyplot.savefig('feature.png',dpi=1000)
pyplot.show()

# + [markdown] id="TQiDB2ihk_Cm"
# ## CREATING VGG MODEL

# + colab={"base_uri": "https://localhost:8080/"} id="oO7HBJkUkYQl" outputId="fb75f28f-1054-44b7-8dcf-e183abcaaf0b"
from keras.layers import Dropout
from keras.applications import VGG16
VGG = VGG16(input_shape=(224,224,3),include_top=False,weights = 'imagenet')
VGG.trainable = False

modelV1 = Sequential()
modelV1.add(VGG)
modelV1.add(Flatten())
modelV1.add(Dense(128, activation = 'relu'))
modelV1.add(Dense(64, activation = 'relu'))
modelV1.add(Dropout(0.3))
modelV1.add(Dense(32, activation = 'relu'))
modelV1.add(Dense(3, activation='softmax'))

modelV1.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

# + colab={"base_uri": "https://localhost:8080/"} id="r6MAg7Q2lLA0" outputId="70e09824-14ea-4a97-a776-ca674545ceb3"
modelV1.summary()

# + id="rjN--LMwlOgj"
checkpoint =ModelCheckpoint(filepath="VGG_CNN__ternary_model.h5", 
                            monitor='val_accuracy',
                            verbose=1,
                            save_best_only=True, 
                            save_weights_only=False, 
                            mode='auto',
                            save_freq='epoch')

callbacks = [checkpoint]

# + colab={"base_uri": "https://localhost:8080/"} id="iBFSQyGlpkQA" outputId="cfee99d9-cdb5-4cd9-ad1d-02ed3a952873"
history = modelV1.fit_generator(train_generator,
                         steps_per_epoch = (train_generator.samples//32),
                         epochs = 5,
                         validation_data = validation_generator,
                         validation_steps = (validation_generator.samples/32),
                         callbacks=[checkpoint],
                         verbose = 1)

# + [markdown] id="oZAsENgYpwpL"
#
# ## Accuracy of VGG_TERNARY Model

# + colab={"base_uri": "https://localhost:8080/"} id="2-N2QjvLpsW6" outputId="0d9a5b14-ac0c-47ae-a454-3eb631a1631b"
accuracy = modelV1.evaluate(validation_generator,verbose=1)
print('Accuracy: %.2f' % (accuracy[1]*100))

# + id="bvgutF8ceI11"
# !cp -r "/content/VGG_CNN__ternary_model.h5" "/content/drive/MyDrive/Model"

# + [markdown] id="_uYZn9csGS7-"
# ### VGG_ACCURACY GRAPH

# + colab={"base_uri": "https://localhost:8080/", "height": 281} id="PUUZyXDHwbIg" outputId="88b510cb-f12e-4b20-83ce-56cca95b0756"

pyplot.title('Classification Accuracy')
pyplot.plot(history.history['accuracy'], color='blue', label='train')
pyplot.plot(history.history['val_accuracy'], color='orange', label='test')
pyplot.legend()
pyplot.savefig('accuracy2.png',dpi=300)

# + [markdown] id="eTjrT4IcGYSh"
# ## COMPARISON OF VGG AND CUSTOM MODEL ACCURACY

# + colab={"base_uri": "https://localhost:8080/", "height": 265} id="Ft22ZJwy2hTS" outputId="e47ef2fb-0a30-4735-e7a2-78ad4466fb31"
pyplot.plot(history.history['val_accuracy'], color='orange', label='VGG_test')
pyplot.plot(hist.history['val_accuracy'], color='green', label='Custom_test')
pyplot.legend()
pyplot.savefig('accuracy_together.png',dpi=300)

# + id="wvpgFfJcrkAi"
# !cp -r "/content/accuracy_together.png" "/content/drive/MyDrive/Model"

# + [markdown] id="L5l8svGk4FcO"
# ## Creating Binary Model

# + id="7kkiIOMcEnjA"
checkpoint =ModelCheckpoint(filepath="Custom_CNN__binary_model.h5", 
                            monitor='val_accuracy',
                            verbose=1,
                            save_best_only=True, 
                            save_weights_only=False, 
                            mode='auto',
                            save_freq='epoch')

callbacks = [checkpoint]

# + id="fufsptdg3nWp"
model2 = Sequential()
model2.add(Conv2D(32, (3, 3), input_shape = (224, 224, 3), activation = 'relu'))
model2.add(MaxPooling2D(pool_size = (2, 2)))
 
model2.add(Conv2D(64, (3, 3),activation = 'relu'))
model2.add(MaxPooling2D(pool_size = (2, 2)))
 
model2.add(Conv2D(128, (3, 3),activation = 'relu'))
model2.add(MaxPooling2D(pool_size = (2, 2)))
 
model2.add(Conv2D(256, (3, 3),activation = 'relu'))
model2.add(MaxPooling2D(pool_size = (2, 2)))
 
model2.add(Conv2D(512, (3, 3),activation = 'relu'))
model2.add(MaxPooling2D(pool_size = (2, 2)))
 
model2.add(Flatten())
model2.add(Dense(128, activation = 'relu'))
model2.add(Dense(64, activation = 'relu'))
model2.add(Dense(32, activation = 'relu'))
model2.add(Dense(1, activation='sigmoid'))
 
model2.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])



# + colab={"base_uri": "https://localhost:8080/"} id="odxo8ix15qC1" outputId="d1c94793-b5b7-4dba-dca3-41e4c2345879"
model2.summary()

# + colab={"base_uri": "https://localhost:8080/"} id="otBglnb75tPV" outputId="992f549d-e3b2-4b42-ee92-96cf456ea4bd"

base =r'/content/Binary1'

data= ImageDataGenerator(rescale=1./255,validation_split=0.2)


train = data.flow_from_directory(
              base,  # This is the source directory for training images
              target_size=(224, 224),  
              batch_size=32,
              subset='training',
              class_mode='binary')


val = data.flow_from_directory(
              base,
              target_size=(224,224),
              batch_size=32,
              subset='validation',
              shuffle=False,
              class_mode='binary')

# + colab={"base_uri": "https://localhost:8080/"} id="XnSNcHKR6MF1" outputId="13fb35ee-99cd-4613-f414-2034556baba8"
h = model2.fit_generator(train,
                         steps_per_epoch = (train.samples//32),
                         epochs = 5,
                         validation_data = val,
                         validation_steps = (val.samples/32),
                         callbacks=[checkpoint],
                         verbose = 1)

# + [markdown] id="n-2ejlif-Nt9"
# ## Accuarcy of binary classification of Covid and Normal

# + colab={"base_uri": "https://localhost:8080/"} id="VvKmQmXB9IQp" outputId="0fc985aa-776c-4de7-c6f1-e1e459d0b975"
acc = model2.evaluate(val,verbose=1)
print('Accuracy: %.2f' % (acc[1]*100))

# + id="eTYbGjPBF915"
# !cp -r "/content/Custom_CNN__binary_model.h5" "/content/drive/MyDrive/Model"

# + [markdown] id="XeXhKC1pGyzE"
# ## ROC Curve of Binary classification of Covid and Normal

# + colab={"base_uri": "https://localhost:8080/"} id="-pk46qYu03_C" outputId="2e3a69a1-b0a2-4180-9d8d-c34eaff59c97"
import numpy as np
y_pred= model2.predict(val,verbose=1)
pred= np.round(y_pred)
y= val.classes
# y=np.expand_dims(y,axis=1)
print(pred.shape)

# + colab={"base_uri": "https://localhost:8080/"} id="AR68moAUBbCj" outputId="e371a3d6-5f13-46bf-9452-db725d8f12fb"
import sklearn.metrics as metrics
print(metrics.accuracy_score(y,pred))

# + colab={"base_uri": "https://localhost:8080/"} id="BbQVmC4-AdYZ" outputId="7a86ac9f-4d42-42b2-bcca-60c0b20558ec"
import sklearn.metrics as metrics
# calculate the fpr and tpr for all thresholds of the classification
# probs = model.predict_proba(X_test)
# preds = probs[:,1]
fpr, tpr, threshold = metrics.roc_curve(y, pred)
roc_auc = metrics.auc(fpr, tpr)
print(fpr)

# + colab={"base_uri": "https://localhost:8080/", "height": 295} id="77He4gNh005T" outputId="f81a1b92-10e1-436f-917e-6e8358ce2f92"


# method I: plt
import matplotlib.pyplot as plt
# plt.title('Receiver Operating Characteristic')
# plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc)
# plt.legend(loc = 'lower right')
# plt.plot([0, 1], [0, 1],'r--')
# plt.xlim([0, 1])
# plt.ylim([0, 1])
# plt.ylabel('True Positive Rate')
# plt.xlabel('False Positive Rate')
# plt.show()

plt.figure(1)
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(fpr, tpr, label='Keras (area = {:.3f})'.format(roc_auc))
# plt.plot(fpr_rf, tpr_rf, label='RF (area = {:.3f})'.format(auc_rf))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve')
plt.legend(loc='best')
plt.savefig('Custom_binary.png',dpi=300)
plt.show()


# + id="cubQ1FbtH_6j"
# !cp -r "/content/Custom_binary.png" "/content/drive/MyDrive/Model"

# + [markdown] id="B2HcIHGJSHQ4"
# ## VGG Model for Binary Classification

# + id="fqZamUgOStSE"
checkpoint =ModelCheckpoint(filepath="VGG_CNN__binary_model.h5", 
                            monitor='val_accuracy',
                            verbose=1,
                            save_best_only=True, 
                            save_weights_only=False, 
                            mode='auto',
                            save_freq='epoch')

callbacks = [checkpoint]

# + id="a-A6JRaGpqlo"
from keras.layers import Dropout
from keras.applications import VGG16
VGG = VGG16(input_shape=(224,224,3),include_top=False,weights = 'imagenet')
VGG.trainable = False

modelV2 = Sequential()
modelV2.add(VGG)
modelV2.add(Flatten())
modelV2.add(Dense(128, activation = 'relu'))
modelV2.add(Dense(64, activation = 'relu'))
modelV2.add(Dropout(0.3))
modelV2.add(Dense(32, activation = 'relu'))
modelV2.add(Dense(1, activation='sigmoid'))

modelV2.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

# + colab={"base_uri": "https://localhost:8080/"} id="cdqpJMjXscyD" outputId="864aa722-4c88-4696-ffc1-b61a42836fe4"
modelV2.summary()

# + colab={"base_uri": "https://localhost:8080/"} id="EIpdYEj-pLN2" outputId="2ec48432-d987-4f70-d214-1459062656b1"
bi = modelV2.fit_generator(train,
                         steps_per_epoch = (train.samples//32),
                         epochs = 5,
                         validation_data = val,
                         validation_steps = (val.samples/32),
                         callbacks=[checkpoint],
                         verbose = 1)

# + [markdown] id="jA85wTVeqohK"
# ## Accuracy of VGG model in binary classification of Covid and Normal 

# + colab={"base_uri": "https://localhost:8080/"} id="cXJ2XojDSmXf" outputId="b22d3169-3425-4115-a0ba-18f573dca57c"
bi = modelV2.evaluate(val,verbose=1)
print('Accuracy: %.2f' % (bi[1]*100))

# + id="TGk_XINBUsRg"
# !cp -r "/content/VGG_CNN__binary_model.h5" "/content/drive/MyDrive/Model"

# + [markdown] id="MsOo9PN8HH2q"
# ## ROC Curve of VGG model in binary classification of Covid and Normal 

# + colab={"base_uri": "https://localhost:8080/"} id="sOfq0zITfH9z" outputId="c4b350b8-3570-4df0-944a-0ae84c5aa9c9"
y_Vpred= modelV2.predict_proba(val,verbose=1)


# + colab={"base_uri": "https://localhost:8080/"} id="wo282celJr2h" outputId="8c4871aa-9b43-4231-bb11-d35ff53f7598"
Vpred= np.round(y_Vpred)
y_V= val.classes
# y=np.expand_dims(y,axis=1)
print(Vpred.shape)

# + id="XDiaBRgFD5Lp"
# def prediction(pred):
#   y_pred=[]

#   for i in pred:
#     if i>0.5:
#       y_pred.append(1)
#     else:
#       y_pred.append(0)
#   return y_pred
    

# f_pred= prediction(y_Vpred)

# + colab={"base_uri": "https://localhost:8080/"} id="xqMYvkUeDalL" outputId="1a0dea54-792f-4ce7-d2ff-97f453f7d081"
import sklearn.metrics as metrics
print(metrics.accuracy_score(y_V,Vpred))

# + colab={"base_uri": "https://localhost:8080/", "height": 295} id="e6_YyVGPfJNI" outputId="0f1d576f-c2a2-47d1-805c-9a3d6973fcbc"

fprV, tprV, thresholdV = metrics.roc_curve(y_V, Vpred)
roc_auc2 = metrics.auc(fprV, tprV)

# method I: plt

# plt.title('Receiver Operating Characteristic')
# plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc)
# plt.legend(loc = 'lower right')
# plt.plot([0, 1], [0, 1],'r--')
# plt.xlim([0, 1])
# plt.ylim([0, 1])
# plt.ylabel('True Positive Rate')
# plt.xlabel('False Positive Rate')
# plt.show()

plt.figure(1)
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(fprV, tprV, label='Keras (area = {:.3f})'.format(roc_auc2))
# plt.plot(fpr_rf, tpr_rf, label='RF (area = {:.3f})'.format(auc_rf))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve')
plt.legend(loc='best')
plt.savefig('VGG_binary',dpi=300)
plt.show()

# + id="QR31wQujI7wS"
# !cp -r "/content/VGG_binary.png" "/content/drive/MyDrive/Model"

# + [markdown] id="PB9658ePHQuQ"
# ## Comaprison of VGG and Custom binary model in classification of Covid and Normal

# + colab={"base_uri": "https://localhost:8080/", "height": 573} id="tEgvrzDFI_W6" outputId="86978c87-db72-4656-cb4f-eb01b34e785e"
plt.figure(1)
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(fprV, tprV, label='VGG (area = {:.3f})'.format(roc_auc2))
plt.plot(fpr, tpr, label='Custom (area = {:.3f})'.format(roc_auc))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve')
plt.legend(loc='best')
plt.savefig('VGG_Custom_both',dpi=300)
plt.show()

plt.figure(2)
plt.xlim(0, 0.2)
plt.ylim(0.8, 1.03)
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(fprV, tprV, label='VGG (area = {:.3f})'.format(roc_auc2))
plt.plot(fpr, tpr, label='Custom (area = {:.3f})'.format(roc_auc))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve (zoomed in at top left)')
plt.legend(loc='best')
plt.savefig('VGG_Custom_zoomed',dpi=300)
plt.show()

# + id="nZbZn5TuJTvX"
# !cp -r "/content/VGG_Custom_zoomed.png" "/content/drive/MyDrive/Model"

# + [markdown] id="RO_MWEmPHgsz"
# ## Applying Custom Binary Model to Covid and Pneumonia

# + colab={"base_uri": "https://localhost:8080/"} id="pG-SElrR9bdt" outputId="e48c5418-a254-494c-d177-058d7771186a"
b =r'/content/Binary2'

dat= ImageDataGenerator(rescale=1./255,validation_split=0.2)


tr = dat.flow_from_directory(
              b,  # This is the source directory for training images
              target_size=(224, 224),  
              batch_size=32,
              subset='training',
              class_mode='binary')


va = dat.flow_from_directory(
              b,
              target_size=(224,224),
              batch_size=32,
              subset='validation',
              shuffle = False,
              class_mode='binary')

# + id="oZRBDTc5LbjR"
checkpoint =ModelCheckpoint(filepath="Custom_CNN__binary_model_Pneumonia.h5", 
                            monitor='val_accuracy',
                            verbose=1,
                            save_best_only=True, 
                            save_weights_only=False, 
                            mode='auto',
                            save_freq='epoch')

callbacks = [checkpoint]

# + colab={"base_uri": "https://localhost:8080/"} id="wyQO4oTf97Rf" outputId="171abed1-272b-43e3-8c64-3e09486c7ce0"
histo = model2.fit_generator(tr,
                         steps_per_epoch = (tr.samples//32),
                         epochs = 5,
                         validation_data = va,
                         validation_steps = (va.samples/32),
                         callbacks = [checkpoint],
                         verbose=1 )

# + [markdown] id="_7hQnE8gBTaN"
# ## Accuracy of binary classification of Covid and Viral Pneunomia

# + colab={"base_uri": "https://localhost:8080/"} id="bt9dogu-AcPL" outputId="33f9b561-db1d-4296-aa88-bafa1112ed66"
a = model2.evaluate(va,verbose=1)
print('Accuracy: %.2f' % (a[1]*100))

# + id="0gVJBVTCL33K"
# !cp -r "/content/Custom_CNN__binary_model_Pneumonia.h5" "/content/drive/MyDrive/Model"

# + [markdown] id="HIIZgUO-HvFl"
# ## ROC Curve in binary classification of Covid and Viral Pneumonia

# + colab={"base_uri": "https://localhost:8080/"} id="qy_p75AKE2cy" outputId="99ea28c6-9b36-40dd-a51d-867fdbd2e2fc"
import numpy as np
y_pred2= model2.predict(va,verbose=1)


# + colab={"base_uri": "https://localhost:8080/"} id="GASwHilgNSrA" outputId="c6b6b0b2-e78e-4d75-fd40-d37848f00aa5"
pred2= np.round(y_pred2)
y2= va.classes
# y=np.expand_dims(y,axis=1)
print(pred2.shape)

# + colab={"base_uri": "https://localhost:8080/"} id="RPokQ-leLMhb" outputId="98e6e688-2603-41ff-8d1d-fcebb4715f67"
import sklearn.metrics as metrics
print(metrics.accuracy_score(y2,pred2))

# + colab={"base_uri": "https://localhost:8080/"} id="p4naRqQXKatO" outputId="358d4115-6b7d-4b43-d565-cb3d6066bb93"
import sklearn.metrics as metrics
# calculate the fpr and tpr for all thresholds of the classification
# probs = model.predict_proba(X_test)
# preds = probs[:,1]
fpr, tpr, threshold = metrics.roc_curve(y2, pred2)
roc_auc = metrics.auc(fpr, tpr)
print(fpr)

# + colab={"base_uri": "https://localhost:8080/", "height": 295} id="m94y8keVKsPo" outputId="89581c5c-c1cb-43ca-b6d0-45a8935479da"
import matplotlib.pyplot as plt
plt.figure(1)
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(fpr, tpr, label='Keras (area = {:.3f})'.format(roc_auc))
# plt.plot(fpr_rf, tpr_rf, label='RF (area = {:.3f})'.format(auc_rf))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve')
plt.legend(loc='best')
plt.savefig('Custom_binary_Pneumonia.png',dpi=300)
plt.show()

# + id="co65r-94qP-K"
# !cp -r "/content/Custom_binary_Pneumonia.png" "/content/drive/MyDrive/Model"

# + [markdown] id="EJ5wJEb3H5Mo"
# ## Applying VGG Binary model to Covid and Pneumonia

# + id="PUgpJJWCJ8nN"
checkpoint =ModelCheckpoint(filepath="VGG_CNN__binary_model_Pneumonia.h5", 
                            monitor='val_accuracy',
                            verbose=1,
                            save_best_only=True, 
                            save_weights_only=False, 
                            mode='auto',
                            save_freq='epoch')

callbacks = [checkpoint]

# + colab={"base_uri": "https://localhost:8080/"} id="I_KVfClOrIc6" outputId="f985e000-4d9f-4a5a-8462-e51a0ff9f077"
bin = modelV2.fit_generator(tr,
                         steps_per_epoch = (tr.samples//32),
                         epochs = 5,
                         validation_data = va,
                         validation_steps = (va.samples/32),
                         callbacks = [checkpoint],
                         verbose=1)

# + [markdown] id="jMMGG6h4rQ6s"
# ## Accuracy of VGG Model in Binary Classification of Covid and Viral Pneumonia

# + colab={"base_uri": "https://localhost:8080/"} id="9i90y4SOrbxG" outputId="5fb130b7-d557-4655-fce5-b7106127b5bd"
bin_c = modelV2.evaluate(va,verbose=1)
print('Accuracy: %.2f' % (bin_c[1]*100))

# + id="ugkOou0Ho_xn"
# !cp -r "/content/VGG_CNN__binary_model_Pneumonia.h5" "/content/drive/MyDrive/Model1"

# + [markdown] id="Dx4j8CTtIF_h"
# ## ROC Curve of VGG Binary Model

# + colab={"base_uri": "https://localhost:8080/"} id="DKA6s3JmpOhI" outputId="cfd91641-f1da-429d-88af-620cd0d51876"
y_Vpred2= modelV2.predict_proba(va,verbose=1)

# + colab={"base_uri": "https://localhost:8080/"} id="7uBAY5ggpOMe" outputId="563d555d-3281-4be4-b76b-0c653c413d0c"
Vpred2= np.round(y_Vpred2)
y_V2= va.classes
# y=np.expand_dims(y,axis=1)
print(Vpred2.shape)

# + colab={"base_uri": "https://localhost:8080/", "height": 295} id="0SKNJUXJplrB" outputId="0fc297a4-84c7-492b-af85-f9d158ece299"
fprV, tprV, thresholdV = metrics.roc_curve(y_V2, Vpred2)
roc_auc2 = metrics.auc(fprV, tprV)

# method I: plt

# plt.title('Receiver Operating Characteristic')
# plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc)
# plt.legend(loc = 'lower right')
# plt.plot([0, 1], [0, 1],'r--')
# plt.xlim([0, 1])
# plt.ylim([0, 1])
# plt.ylabel('True Positive Rate')
# plt.xlabel('False Positive Rate')
# plt.show()

plt.figure(1)
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(fprV, tprV, label='VGG_Binary (area = {:.3f})'.format(roc_auc2))
# plt.plot(fpr_rf, tpr_rf, label='RF (area = {:.3f})'.format(auc_rf))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve')
plt.legend(loc='best')
plt.savefig('VGG_binary_Pneumonia',dpi=300)
plt.show()

# + id="bfE1p50wqbnc"
# !cp -r "/content/VGG_binary_Pneumonia.png" "/content/drive/MyDrive/Model"

# + [markdown] id="2XXZLQ2tIQKx"
# ## Comparison of VGG and Custom Binary Model in classification of Covid and Viral Penumonia

# + colab={"base_uri": "https://localhost:8080/", "height": 573} id="9FKm7WjYplYW" outputId="f8547255-a6bd-42da-d9a3-dc2874c5053e"
plt.figure(1)
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(fprV, tprV, label='VGG (area = {:.3f})'.format(roc_auc2))
plt.plot(fpr, tpr, label='Custom (area = {:.3f})'.format(roc_auc))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve')
plt.legend(loc='best')
plt.savefig('VGG_Custom_both_Pneumonia',dpi=300)
plt.show()

plt.figure(2)
plt.xlim(0, 0.2)
plt.ylim(0.9, 1.03)
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(fprV, tprV, label='VGG (area = {:.3f})'.format(roc_auc2))
plt.plot(fpr, tpr, label='Custom (area = {:.3f})'.format(roc_auc))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve (zoomed in at top left)')
plt.legend(loc='best')
plt.savefig('VGG_Custom_zoomed_Pneumonia',dpi=300)
plt.show()

# + id="p7DL1CyPqZf1"
# !cp -r "/content/VGG_Custom_zoomed_Pneumonia.png" "/content/drive/MyDrive/Model1"

# + [markdown] id="XMkQBlp_cNfe"
# # **So Clearly VGG Model is better in all 3 classifications. Therefore, we can say using Transfer Learning is more useful and impactful than creating Custom model**

StarcoderdataJupyterScriptsDedupFiltered