<jupyter_start><jupyter_text>Import Libraries<jupyter_code>from tensorflow.keras.models 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 numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Clone & Explore dataset<jupyter_code>! git clone https://github.com/education454/datasets.git
import os
main_dir = "/content/datasets/Data"
train_dir = os.path.join(main_dir,'train')
test_dir = os.path.join(main_dir,'test')
train_covid_dir = os.path.join(train_dir,'COVID19')
train_normal_dir = os.path.join(train_dir,'NORMAL')
test_covid_dir = os.path.join(test_dir,'COVID19')
test_normal_dir = os.path.join(test_dir,'NORMAL')
train_covid_names = os.listdir(train_covid_dir)
print(train_covid_names[:10])

train_normal_names = os.listdir(train_normal_dir)
print(train_normal_names[:10])

test_covid_names = os.listdir(test_covid_dir)
print(test_covid_names[:10])

test_normal_names = os.listdir(test_normal_dir)
print(test_normal_names[:10])
print("Total images present in the training set:", len(train_covid_names+train_normal_names))
print("Total images present in the testing set:", len(test_covid_names+test_normal_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
rows = 4
cols = 4
fig = plt.gcf()
fig.set_size_inches(12,12)
covid_pic = [os.path.join(train_covid_dir,filename)for filename in train_covid_names[0:8]]
normal_pic = [os.path.join(train_normal_dir,filename)for filename in train_normal_names[0:8]]
print(covid_pic)
print(normal_pic)
merged_list = covid_pic+normal_pic
for i , img_path in enumerate(merged_list):
  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>['/content/datasets/Data/train/COVID19/COVID-19 (12).jpg', '/content/datasets/Data/train/COVID19/COVID19(296).jpg', '/content/datasets/Data/train/COVID19/COVID-19 (162).jpg', '/content/datasets/Data/train/COVID19/COVID19(545).jpg', '/content/datasets/Data/train/COVID19/COVID19(522).jpg', '/content/datasets/Data/train/COVID19/COVID-19 (20).jpeg', '/content/datasets/Data/train/COVID19/COVID-19 (342).jpg', '/content/datasets/Data/train/COVID19/COVID19(145).jpg']
['/content/datasets/Data/train/NORMAL/NORMAL(337).jpg', '/content/datasets/Data/train/NORMAL/NORMAL(758).jpg', '/content/datasets/Data/train/NORMAL/NORMAL(210).jpg', '/content/datasets/Data/train/NORMAL/NORMAL(829).jpg', '/content/datasets/Data/train/NORMAL/NORMAL(182).jpg', '/content/datasets/Data/train/NORMAL/NORMAL(588).jpg', '/content/datasets/Data/train/NORMAL/NORMAL(636).jpg', '/content/datasets/Data/train/NORMAL/NORMAL(830).jpg']<jupyter_text>Data Preprocessing & Augmentation<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>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>model.compile(Adam(lr=0.001),loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit(train_generator,
                    epochs = 30,
                    validation_data = validation_generator)<jupyter_output>Epoch 1/30
46/46 [==============================] - 78s 1s/step - loss: 1.9437 - accuracy: 0.6712 - val_loss: 0.4285 - val_accuracy: 0.8950
Epoch 2/30
46/46 [==============================] - 47s 1s/step - loss: 0.2643 - accuracy: 0.8874 - val_loss: 0.1610 - val_accuracy: 0.9475
Epoch 3/30
46/46 [==============================] - 48s 1s/step - loss: 0.2021 - accuracy: 0.9294 - val_loss: 0.1930 - val_accuracy: 0.9448
Epoch 4/30
46/46 [==============================] - 48s 1s/step - loss: 0.1833 - accuracy: 0.9413 - val_loss: 0.1651 - val_accuracy: 0.9448
Epoch 5/30
46/46 [==============================] - 48s 1s/step - loss: 0.1526 - accuracy: 0.9490 - val_loss: 0.2300 - val_accuracy: 0.9392
Epoch 6/30
46/46 [==============================] - 47s 1s/step - loss: 0.1652 - accuracy: 0.9556 - val_loss: 0.1178 - val_accuracy: 0.9586
Epoch 7/30
46/46 [==============================] - 48s 1s/step - loss: 0.1409 - accuracy: 0.9509 - val_loss: 0.1117 - val_accuracy: 0.9530
Epoch 8/30
46/46 [==[...]<jupyter_text>Performance Evaluation<jupyter_code>history.history.keys()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.legend(['Training','Validation'])
plt.title('Training and validation losses')
plt.xlabel('epoch')
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.legend(['Training','Validation'])
plt.title('Training and validation accuracy')
plt.xlabel('epoch')
test_loss , test_acc = model.evaluate(test_generator)
print('test loss :{} test acc:{}'.format(test_loss,test_acc))<jupyter_output>16/16 [==============================] - 10s 651ms/step - loss: 0.0731 - accuracy: 0.9690
test loss :0.0730881616473198 test acc:0.9690082669258118<jupyter_text>Prediction On New Data<jupyter_code>from google.colab import files
from keras.preprocessing import image
uploaded = files.upload()
for filename in uploaded.keys():
  img_path = '/content/'+filename
  img = image.load_img(img_path,target_size=(150,150))
  images = image.img_to_array(img)
  images = np.expand_dims(images,axis=0)
  prediction = model.predict(images)
  print(filename)

  if prediction == 0:
    print("covid detected")
  else:
    print("your report is normal")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15378522122892385, 'fraction_numerical': 0.0608803892723775, 'mean_word_length': 3.8163487738419617, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 1, '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': '168352', 'license': 'MIT', 'n_tokens_mistral': 3116, 'n_tokens_neox': 2752, 'n_words': 491, 'path': 'MajorProject.ipynb', 'repo_name': 'Amulya312/MajorProject', 'size': '8837'}