<jupyter_start><jupyter_text>Covid 19 Prediction using Lung CT Scan (using CNN) Author : <NAME>  Importing required Libraries<jupyter_code>import os
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<jupyter_output><empty_output><jupyter_text>Importing required data<jupyter_code>main_dir = "D:/Statistics/M.Sc Statistics/Internship/ShapeAI/Final Project/Data"

train_dir = os.path.join("D:/Statistics/M.Sc Statistics/Internship/ShapeAI/Final Project/Data/train")
test_dir  = os.path.join("D:/Statistics/M.Sc Statistics/Internship/ShapeAI/Final Project/Data/test")

train_covid_dir    = os.path.join("D:/Statistics/M.Sc Statistics/Internship/ShapeAI/Final Project/Data/train/CT_covid")
train_noncovid_dir = os.path.join("D:/Statistics/M.Sc Statistics/Internship/ShapeAI/Final Project/Data/train/CT_noncovid")

test_covid_dir    = os.path.join("D:/Statistics/M.Sc Statistics/Internship/ShapeAI/Final Project/Data/test/CT_covid")
test_noncovid_dir = os.path.join("D:/Statistics/M.Sc Statistics/Internship/ShapeAI/Final Project/Data/test/CT_noncovid")
# 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>import matplotlib.image as mpimg

#figure size
fig = plt.gcf()
fig.set_size_inches(12,12)

#get the file names 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]]

#merge the covid and normal pics

mergerd_list = covid_pic + noncovid_pic
for i, img_path in enumerate(covid_pic + noncovid_pic):
    data = img_path.split('/',6)[6]
    sp = plt.subplot(4, 4, 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><empty_output><jupyter_text>Data Preprocessing and Augumentation<jupyter_code>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')
train_generator.class_indices
train_generator.image_shape<jupyter_output><empty_output><jupyter_text>Building 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"))

#polling layer
model.add(MaxPooling2D(pool_size = (2,2)))

#place a dropout layer
model.add(Dropout(0.5))

#Flattenlayer
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 eachupdate
model.add(Dropout(0.5))
model.add(Dense(1,activation='sigmoid'))
model.summary()<jupyter_output>Model: "sequential_4"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_3 (Conv2D)            (None, 150, 150, 32)      2432      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 75, 75, 32)        0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 75, 75, 32)        0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 75, 75, 64)        51264     
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 37, 37, 64)        0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 37, 37, 64)        0         
______________________________________________________[...]<jupyter_text>Compile and train the model<jupyter_code>#compile the model

model.compile(Adam(learning_rate=0.001),loss='binary_crossentropy',metrics=['accuracy'])
#training the model

history = model.fit(train_generator,
                   epochs = 30,
                   validation_data = validation_generator)<jupyter_output>Epoch 1/30
46/46 [==============================] - 123s 3s/step - loss: 0.3375 - accuracy: 0.8682 - val_loss: 0.2375 - val_accuracy: 0.9448
Epoch 2/30
46/46 [==============================] - 106s 2s/step - loss: 0.2105 - accuracy: 0.9234 - val_loss: 0.2106 - val_accuracy: 0.9586
Epoch 3/30
46/46 [==============================] - 146s 3s/step - loss: 0.1411 - accuracy: 0.9469 - val_loss: 0.2473 - val_accuracy: 0.9365
Epoch 4/30
46/46 [==============================] - 142s 3s/step - loss: 0.1796 - accuracy: 0.9351 - val_loss: 0.2743 - val_accuracy: 0.9530
Epoch 5/30
46/46 [==============================] - 144s 3s/step - loss: 0.1618 - accuracy: 0.9455 - val_loss: 0.1338 - val_accuracy: 0.9724
Epoch 6/30
46/46 [==============================] - 136s 3s/step - loss: 0.1177 - accuracy: 0.9565 - val_loss: 0.1230 - val_accuracy: 0.9751
Epoch 7/30
46/46 [==============================] - 131s 3s/step - loss: 0.1564 - accuracy: 0.9524 - val_loss: 0.1517 - val_accuracy: 0.9669
Epoch 8/30
46[...]<jupyter_text>Performance Evaluation<jupyter_code># get the keys of the history object

history.history.keys()
# Plot the graph betweeen the 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 the graph betweeen the training and validation accuracy

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 accuracy and loss

test_loss, test_acc = model.evaluate(test_generator)
print("Test loss: ", test_loss)
print("Test Accuracy: ",test_acc)<jupyter_output>Test loss:  0.06465120613574982
Test Accuracy:  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")<jupyter_output>D:/Statistics/M.Sc Statistics/Internship/ShapeAI/Final Project/Data/train/CT_covid/COVID-19 (4).jpg
{'fraction_non_alphanumeric': 0.1333871339635693, 'fraction_numerical': 0.05084159557297671, 'mean_word_length': 3.6464916979110873, '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': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '124381', 'license': 'MIT', 'n_tokens_mistral': 2974, 'n_tokens_neox': 2645, 'n_words': 573, 'path': 'Shape AI - Project 1.ipynb', 'repo_name': 'TenzingJoseph/Shape-AI-Internship', 'size': '8677'}