<jupyter_start><jupyter_text>Covid 19 Prediction using Lung CT Scan Author: <NAME>  Importing the required Libraries<jupyter_code>import numpy as np
import matplotlib.pyplot as plt

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import os<jupyter_output><empty_output><jupyter_text>Clone and Explore dataset<jupyter_code>main_dir = "D:/Internship/Project_1/datasets-master/datasets-master/Data"
train_dir = os.path.join("D:/Internship/Project_1/datasets-master/datasets-master/Data/train")
test_dir = os.path.join("D:/Internship/Project_1/datasets-master/datasets-master/Data/test")

train_covid_dir = os.path.join("D:/Internship/Project_1/datasets-master/datasets-master/Data/train/COVID19")
train_noncovid_dir = os.path.join("D:/Internship/Project_1/datasets-master/datasets-master/Data/train/NORMAL")

test_covid_dir = os.path.join("D:/Internship/Project_1/datasets-master/datasets-master/Data/test/COVID19")
test_noncovid_dir = os.path.join("D:/Internship/Project_1/datasets-master/datasets-master/Data/test/NORMAL")
# print file name

train_covid_names = os.listdir(train_covid_dir)
train_noncovid_names = os.listdir(train_noncovid_dir)

test_covid_names = os.listdir(test_covid_dir)
test_noncovid_names = os.listdir(test_noncovid_dir)
# Total images in train and test

print("Total images present in the training set: ", len(train_covid_names + train_noncovid_names))
print("Total images present in the testing set: ", len(test_covid_names + test_noncovid_names))<jupyter_output>Total images present in the training set:  1811
Total images present in the testing set:  484<jupyter_text>Data Visualization<jupyter_code># Plot a grid of 16 images (8 images of Covid19 and 8 images of Normal)
import matplotlib.image as mpimg
# Set the number of columns and rows
rows = 4
cols = 4
# Set the figure size 
fig = plt.gcf()
fig.set_size_inches(12,12)
# Get the filenames from the Covid & Normal dir of the train dataset
covid_pic = [os.path.join(train_covid_dir,filename)for filename in train_covid_names[0:8]]
noncovid_pic = [os.path.join(train_noncovid_dir,filename)for filename in train_noncovid_names[0:8]]
# Print the list
print(covid_pic)
print(noncovid_pic)
# Merge the Covid and Normal List
merged_list = covid_pic+noncovid_pic
for i, img_path in enumerate(covid_pic+noncovid_pic):
    data = img_path.split('/',6)[6]
    sp = plt.subplot(rows,cols,i+1)
    sp.axis('Off')
    img = mpimg.imread(img_path)
    sp.set_title(data,fontsize=10)
    plt.imshow(img,cmap='gray')
    
plt.show()<jupyter_output>['D:/Internship/Project_1/datasets-master/datasets-master/Data/train/COVID19\\COVID-19 (1).jpeg', 'D:/Internship/Project_1/datasets-master/datasets-master/Data/train/COVID19\\COVID-19 (1).png', 'D:/Internship/Project_1/datasets-master/datasets-master/Data/train/COVID19\\COVID-19 (10).jpeg', 'D:/Internship/Project_1/datasets-master/datasets-master/Data/train/COVID19\\COVID-19 (101).jpg', 'D:/Internship/Project_1/datasets-master/datasets-master/Data/train/COVID19\\COVID-19 (107).jpg', 'D:/Internship/Project_1/datasets-master/datasets-master/Data/train/COVID19\\COVID-19 (11).jpeg', 'D:/Internship/Project_1/datasets-master/datasets-master/Data/train/COVID19\\COVID-19 (11).jpg', 'D:/Internship/Project_1/datasets-master/datasets-master/Data/train/COVID19\\COVID-19 (12).jpeg']
['D:/Internship/Project_1/datasets-master/datasets-master/Data/train/NORMAL\\NORMAL(0).jpg', 'D:/Internship/Project_1/datasets-master/datasets-master/Data/train/NORMAL\\NORMAL(1).jpg', 'D:/Internship/Project_1/datasets-[...]<jupyter_text>Data Preprocessing & Augmentation<jupyter_code># generate training,testing and validation batches 
dgen_train = ImageDataGenerator(rescale = 1./255,
                                validation_split=0.2,
                                zoom_range=0.2,
                                horizontal_flip = True)
dgen_validation = ImageDataGenerator(rescale = 1./255)
dgen_test = ImageDataGenerator(rescale=1./255)

train_generator = dgen_train.flow_from_directory(train_dir,
                                                target_size=(150,150),
                                                subset='training',
                                                batch_size = 32,
                                                class_mode = 'binary')
validation_generator = dgen_train.flow_from_directory(train_dir,
                                                target_size=(150,150),
                                                subset='validation',
                                                batch_size = 32,
                                                class_mode = 'binary')
test_generator = dgen_test.flow_from_directory(test_dir,
                                                target_size=(150,150),
                                                batch_size = 32,
                                                class_mode = 'binary')

#get the class indices
train_generator.class_indices
#get the image shape
train_generator.image_shape<jupyter_output><empty_output><jupyter_text>Build Convolutional Neural Network Model<jupyter_code>model = Sequential()
# add the convolutional layer
# filters, size of filters,padding,activation_function,input_shape
model.add(Conv2D(32,(5,5),padding='SAME',activation='relu',input_shape=(150,150,3)))
# pooling layer
model.add(MaxPooling2D(pool_size=(2,2)))
# place a dropout layer
model.add(Dropout(0.5))
# add another convolutional layer
model.add(Conv2D(64,(5,5),padding='SAME',activation='relu'))
# pooling layer
model.add(MaxPooling2D(pool_size=(2,2)))
# place a dropout layer
model.add(Dropout(0.5))
# Flatten layer
model.add(Flatten())
# add a dense layer : amount of nodes, activation
model.add(Dense(256,activation='relu'))
# place a dropout layer
# 0.5 drop out rate is recommended, half input nodes will be dropped at each update
model.add(Dropout(0.5))
model.add(Dense(1,activation='sigmoid'))
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 150, 150, 32)      2432      
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 75, 75, 32)        0         
_________________________________________________________________
dropout (Dropout)            (None, 75, 75, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 75, 75, 64)        51264     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 37, 37, 64)        0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 37, 37, 64)        0         
________________________________________________________[...]<jupyter_text>Compile & Train the Model<jupyter_code>#compile the model
model.compile(Adam(lr=0.001),loss='binary_crossentropy',metrics=['accuracy'])
#train the model
history=model.fit(train_generator,
                 epochs=30,
                 validation_data=validation_generator)<jupyter_output>Epoch 1/30
46/46 [==============================] - 168s 4s/step - loss: 0.9518 - accuracy: 0.7605 - val_loss: 0.5124 - val_accuracy: 0.8564
Epoch 2/30
46/46 [==============================] - 110s 2s/step - loss: 0.2558 - accuracy: 0.9034 - val_loss: 0.1804 - val_accuracy: 0.9530
Epoch 3/30
46/46 [==============================] - 105s 2s/step - loss: 0.1632 - accuracy: 0.9413 - val_loss: 0.1129 - val_accuracy: 0.9751
Epoch 4/30
46/46 [==============================] - 109s 2s/step - loss: 0.1493 - accuracy: 0.9517 - val_loss: 0.1337 - val_accuracy: 0.9696
Epoch 5/30
46/46 [==============================] - 109s 2s/step - loss: 0.1343 - accuracy: 0.9531 - val_loss: 0.1551 - val_accuracy: 0.9586
Epoch 6/30
46/46 [==============================] - 109s 2s/step - loss: 0.1201 - accuracy: 0.9572 - val_loss: 0.0939 - val_accuracy: 0.9696
Epoch 7/30
46/46 [==============================] - 106s 2s/step - loss: 0.1140 - accuracy: 0.9593 - val_loss: 0.0947 - val_accuracy: 0.9724
Epoch 8/30
46[...]<jupyter_text>Performance Evaluation<jupyter_code>#get the keys of history object
history.history.keys()
#plot graph between training and validation loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.legend(['Training','Validation'])
plt.title('Training and Validation losses')
plt.xlabel('epoch')
#plot graph between training and validation accuarcy
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.legend(['Training','Validation'])
plt.title('Training and Validation accuracy')
plt.xlabel('epoch')
# get the test acuarcy and loss
test_loss , test_acc = model.evaluate(test_generator)
print('test loss: {} test acc: {}'.format(test_loss,test_acc))<jupyter_output>16/16 [==============================] - 38s 2s/step - loss: 0.0507 - accuracy: 0.9814
test loss: 0.050737008452415466 test acc: 0.9814049601554871<jupyter_text>Prediction on New Data<jupyter_code>from PIL import Image  
from tensorflow.keras.preprocessing import image
img_path = input()
plt.imshow(img)
plt.show()
img = image.load_img(img_path, target_size=(150,150))
img_array = image.img_to_array(img)
img_batch = np.expand_dims(img_array, axis=0)

prediction = model.predict(img_batch)

if prediction ==0:
     print("Covid is detected")
else:
    print("Covid is not detected")
from PIL import Image  
from tensorflow.keras.preprocessing import image
img_path = input()
plt.imshow(img)
plt.show()
img = image.load_img(img_path, target_size=(150,150))
img_array = image.img_to_array(img)
img_batch = np.expand_dims(img_array, axis=0)

prediction = model.predict(img_batch)

if prediction ==0:
     print("Covid is detected")
else:
    print("Covid is not detected")<jupyter_output>D:/Internship/Project_1/datasets-master/datasets-master/Data/test/NORMAL/NORMAL(49).jpg
{'fraction_non_alphanumeric': 0.14298297122724604, 'fraction_numerical': 0.05294578195341554, 'mean_word_length': 3.9873108833577353, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '301860', 'license': 'MIT', 'n_tokens_mistral': 3592, 'n_tokens_neox': 3210, 'n_words': 620, 'path': 'Shape AI - Project 1 Jupyter Notebook.ipynb', 'repo_name': 'RenoNoel/Shape-AI-Internship', 'size': '10223'}