Learn data science and machine learning by building real-world projects on Jovian

Predict Noval Corona or Pneumonia or Normal

Using a limited sized dataset to predict weather a person is having Noval Corona or Pneumonia Or he is normal by analyzing his upper abdomen X-Ray scans

Got doubts/ have suggestions
馃憠 Contact me through LinkedIn @Snehit Vaddi

Importing Modules

In [1]:
import matplotlib.pyplot as plt
import cv2
import glob
import os
import random
import jovian

Note :

My model did not perform as I expected, If anybody tries this and got better metrics than me please feel free to share your knowledge!! After all, Knowledge is Sharing. right!!

Dataset :

You can download dataset from here https://drive.google.com/open?id=1wwXhV2pNJjGdC4dsMz3NQxfhojthUbeB

Data cleaning, naming and Data set Organising

  • The below step will be executed only if dataset in unstructured data.
  • Here data set is already organized, so there is no need of running this cell
In [ ]:
## 
paths = glob.glob("C:\\Users\\vsneh\\Udemy-notebooks\\covid-19\\dataset\\test\\covid\\*")
i=0
for filename in os.listdir("C:\\Users\\vsneh\\Udemy-notebooks\\covid-19\\dataset\\train\\covid\\"): 
        dst ="train-covid" + str(i) + ".jpg"
        src = "C:\\Users\\vsneh\\Udemy-notebooks\\covid-19\\NovelCorona-Pneumonia-Healthy-Prediction\\dataset\\train\\covid\\" + filename 
        dst = "C:\\Users\\vsneh\\Udemy-notebooks\\covid-19\\NovelCorona-Pneumonia-Healthy-Prediction\\dataset\\train\\covid\\" + dst 
          
        # rename() function will 
        # rename all the files 
        os.rename(src, dst) 
        i += 1
In [2]:
os.listdir("C:\\Users\\vsneh\\Udemy-notebooks\\covid-19\\NovelCorona-Pneumonia-Healthy-Prediction\\dataset\\test\\covid\\")
Out[2]:
['test-covid0.jpg',
 'test-covid10.jpg',
 'test-covid11.jpg',
 'test-covid12.jpg',
 'test-covid2.jpg',
 'test-covid3.jpg',
 'test-covid4.jpg',
 'test-covid5.jpg',
 'test-covid6.jpg',
 'test-covid7.jpg',
 'test-covid8.jpg',
 'test-covid9.jpg']
In [3]:
img1 = cv2.imread('C:\\Users\\vsneh\\Udemy-notebooks\\covid-19\\NovelCorona-Pneumonia-Healthy-Prediction\\dataset\\train\\covid\\train-covid10.jpg')
plt.imshow(img1)
Out[3]:
<matplotlib.image.AxesImage at 0x20e14ff9588>
Notebook Image
In [4]:
img2 = cv2.imread('C:\\Users\\vsneh\\Udemy-notebooks\\covid-19\\NovelCorona-Pneumonia-Healthy-Prediction\\dataset\\train\\normal\\train-normal53.jpg')
plt.imshow(img2)
Out[4]:
<matplotlib.image.AxesImage at 0x20e150ffe48>
Notebook Image
In [5]:
img3 = cv2.imread('C:\\Users\\vsneh\\Udemy-notebooks\\covid-19\\NovelCorona-Pneumonia-Healthy-Prediction\\dataset\\train\\pneumonia\\train-pneumonia13.jpg')
plt.imshow(img3)
Out[5]:
<matplotlib.image.AxesImage at 0x20e15192fc8>
Notebook Image
In [6]:
src_path = "C:\\Users\\vsneh\\Udemy-notebooks\\covid-19\\NovelCorona-Pneumonia-Healthy-Prediction\\dataset\\train\\"
sub_class = os.listdir(src_path)

fig = plt.figure(figsize=(10,5))
path = os.path.join(src_path,sub_class[0])

# v = random.sample(range(1, 15), 4)
for i in range(4):
    plt.subplot(240 + 1 + i)
    img = plt.imread(os.path.join(path,'train-covid'+str(i)+'.jpg'))
    plt.imshow(img, cmap=plt.get_cmap('gray'))
    plt.title('Covid-19')

# path = os.path.join(src_path,sub_class[1])
# for i in range(4):
#     plt.subplot(240 + 1 + i)
#     img = plt.imread(os.path.join(path,'train-normal'+str(i)+'.jpg'))
#     plt.imshow(img, cmap=plt.get_cmap('gray'))
    
# path = os.path.join(src_path,sub_class[2])
# for i in range(4):
#     plt.subplot(240 + 1 + i)
#     img = plt.imread(os.path.join(path,'train-pneumonia'+str(i)+'.jpg'))
#     plt.imshow(img, cmap=plt.get_cmap('gray'))
Notebook Image
In [7]:
from keras.preprocessing.image import ImageDataGenerator
Using TensorFlow backend.
In [8]:
image_gen = ImageDataGenerator(rotation_range=30, # rotate the image 30 degrees
                               width_shift_range=0.1, # Shift the pic width by a max of 10%
                               height_shift_range=0.1, # Shift the pic height by a max of 10%
                               rescale=1/255, # Rescale the image by normalzing it.
                               shear_range=0.2, # Shear means cutting away part of the image (max 20%)
                               zoom_range=0.2, # Zoom in by 20% max
                               horizontal_flip=True, # Allo horizontal flipping
                               fill_mode='nearest' # Fill in missing pixels with the nearest filled value
                              )
In [9]:
plt.imshow(image_gen.random_transform(img1))
Out[9]:
<matplotlib.image.AxesImage at 0x20e2225f648>
Notebook Image
In [10]:
image_gen.flow_from_directory('C:\\Users\\vsneh\\Udemy-notebooks\\covid-19\\NovelCorona-Pneumonia-Healthy-Prediction\\dataset\\train')
Found 231 images belonging to 3 classes.
Out[10]:
<keras.preprocessing.image.DirectoryIterator at 0x20e22261e88>
In [11]:
image_gen.flow_from_directory('C:\\Users\\vsneh\\Udemy-notebooks\\covid-19\\NovelCorona-Pneumonia-Healthy-Prediction\\dataset\\test')
Found 52 images belonging to 3 classes.
Out[11]:
<keras.preprocessing.image.DirectoryIterator at 0x20e2259cb48>
In [12]:
image_shape = (150,150,3)
In [13]:
from keras.models import Sequential
from keras.layers import Activation, Dropout, Flatten, Dense, Conv2D, MaxPool2D
In [14]:
model = Sequential()
model.add(Conv2D(filters=32,kernel_size=(3,3),input_shape=(150,150,3),activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))

model.add(Conv2D(filters=64,kernel_size=(3,3),input_shape=(150,150,3),activation='relu'))
model.add(MaxPool2D(pool_size=(2,2)))

model.add(Flatten())

model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(3))
model.add(Activation('softmax'))

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


In [15]:
model.summary()
Model: "sequential_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_1 (Conv2D) (None, 148, 148, 32) 896 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 74, 74, 32) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 72, 72, 64) 18496 _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 36, 36, 64) 0 _________________________________________________________________ flatten_1 (Flatten) (None, 82944) 0 _________________________________________________________________ dense_1 (Dense) (None, 128) 10616960 _________________________________________________________________ activation_1 (Activation) (None, 128) 0 _________________________________________________________________ dropout_1 (Dropout) (None, 128) 0 _________________________________________________________________ dense_2 (Dense) (None, 3) 387 _________________________________________________________________ activation_2 (Activation) (None, 3) 0 ================================================================= Total params: 10,636,739 Trainable params: 10,636,739 Non-trainable params: 0 _________________________________________________________________
In [16]:
batch_size = 16

train_image_gen = image_gen.flow_from_directory('C:\\Users\\vsneh\\Udemy-notebooks\\covid-19\\NovelCorona-Pneumonia-Healthy-Prediction\\dataset\\train',
                                               target_size=(150,150),
                                               batch_size=batch_size,
                                               class_mode='categorical')
Found 231 images belonging to 3 classes.
In [17]:
test_image_gen = image_gen.flow_from_directory('C:\\Users\\vsneh\\Udemy-notebooks\\covid-19\\NovelCorona-Pneumonia-Healthy-Prediction\\dataset\\test',
                                               target_size=(150,150),
                                               batch_size=batch_size,
                                               class_mode='categorical')
Found 52 images belonging to 3 classes.
In [18]:
train_image_gen.class_indices
Out[18]:
{'covid': 0, 'normal': 1, 'pneumonia': 2}
In [19]:
import warnings
import PIL.Image
warnings.filterwarnings('ignore')
In [20]:
results = model.fit_generator(train_image_gen,epochs=15,
                              steps_per_epoch=75,
                              validation_data=test_image_gen,validation_steps=10)
Epoch 1/15 75/75 [==============================] - 44s 592ms/step - loss: 0.9655 - accuracy: 0.5446 - val_loss: 0.4967 - val_accuracy: 0.7426 Epoch 2/15 75/75 [==============================] - 45s 602ms/step - loss: 0.6067 - accuracy: 0.7654 - val_loss: 0.2727 - val_accuracy: 0.8952 Epoch 3/15 75/75 [==============================] - 43s 573ms/step - loss: 0.5449 - accuracy: 0.7896 - val_loss: 0.2510 - val_accuracy: 0.8971 Epoch 4/15 75/75 [==============================] - 46s 607ms/step - loss: 0.4999 - accuracy: 0.7922 - val_loss: 0.2665 - val_accuracy: 0.9355 Epoch 5/15 75/75 [==============================] - 44s 593ms/step - loss: 0.4381 - accuracy: 0.8286 - val_loss: 0.2399 - val_accuracy: 0.9338 Epoch 6/15 75/75 [==============================] - 41s 543ms/step - loss: 0.4682 - accuracy: 0.8286 - val_loss: 0.3019 - val_accuracy: 0.9516 Epoch 7/15 75/75 [==============================] - 57s 758ms/step - loss: 0.4247 - accuracy: 0.8303 - val_loss: 0.2739 - val_accuracy: 0.9118 Epoch 8/15 75/75 [==============================] - 99s 1s/step - loss: 0.4539 - accuracy: 0.8312 - val_loss: 0.0778 - val_accuracy: 0.9355 Epoch 9/15 75/75 [==============================] - 54s 715ms/step - loss: 0.4099 - accuracy: 0.8381 - val_loss: 0.3763 - val_accuracy: 0.8824 Epoch 10/15 75/75 [==============================] - 46s 612ms/step - loss: 0.4145 - accuracy: 0.8485 - val_loss: 0.1567 - val_accuracy: 0.9839 Epoch 11/15 75/75 [==============================] - 43s 567ms/step - loss: 0.3832 - accuracy: 0.8580 - val_loss: 0.1634 - val_accuracy: 0.9118 Epoch 12/15 75/75 [==============================] - 48s 634ms/step - loss: 0.3866 - accuracy: 0.8468 - val_loss: 0.1450 - val_accuracy: 0.9516 Epoch 13/15 75/75 [==============================] - 50s 661ms/step - loss: 0.3576 - accuracy: 0.8563 - val_loss: 0.1322 - val_accuracy: 0.9706 Epoch 14/15 75/75 [==============================] - 68s 911ms/step - loss: 0.3575 - accuracy: 0.8589 - val_loss: 0.0635 - val_accuracy: 0.9516 Epoch 15/15 75/75 [==============================] - 71s 940ms/step - loss: 0.3369 - accuracy: 0.8589 - val_loss: 0.2159 - val_accuracy: 0.9559
In [21]:
results.history['accuracy']
Out[21]:
[0.54458874,
 0.765368,
 0.7896104,
 0.7922078,
 0.82857144,
 0.82857144,
 0.830303,
 0.83116883,
 0.83809525,
 0.8484849,
 0.8580087,
 0.84675324,
 0.85627705,
 0.85887444,
 0.85887444]
In [22]:
plt.plot(results.history['loss'])
Out[22]:
[<matplotlib.lines.Line2D at 0x20e228a0a08>]
Notebook Image
In [23]:
plt.plot(results.history['accuracy'])
plt.plot(results.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
Notebook Image
In [24]:
plt.plot(results.history['loss'])
plt.plot(results.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
Notebook Image

External Image prediction

In [25]:
train_image_gen.class_indices
Out[25]:
{'covid': 0, 'normal': 1, 'pneumonia': 2}
In [27]:
import numpy as np
from keras.preprocessing import image
img = 'C:\\Users\\vsneh\\Udemy-notebooks\\covid-19\\NovelCorona-Pneumonia-Healthy-Prediction\\dataset\\test-covid9.jpg'
img = image.load_img(img,target_size=(150,150))
img = image.img_to_array(img)
img = img/255
In [28]:
prediction = model.predict_classes(img.reshape(1,150,150,3))
In [29]:
prediction
Out[29]:
array([0], dtype=int64)
In [39]:
import numpy as np
from keras.preprocessing import image
coropos = 'C:\\Users\\vsneh\\Udemy-notebooks\\covid-19\\NovelCorona-Pneumonia-Healthy-Prediction\\dataset\\test\\normal\\test-normal8.jpg'
img = image.load_img(coropos,target_size=(150,150))
img = image.img_to_array(img)
img = img/255
prediction = model.predict_classes(img.reshape(1,150,150,3))
print(prediction)
[1]
In [38]:
import numpy as np
from keras.preprocessing import image
coropos = 'C:\\Users\\vsneh\\Udemy-notebooks\\covid-19\\NovelCorona-Pneumonia-Healthy-Prediction\\dataset\\test\\pneumonia\\test-pneumonia2.jpg'
img = image.load_img(coropos,target_size=(150,150))
img = image.img_to_array(img)
img = img/255
prediction = model.predict_classes(img.reshape(1,150,150,3))
print(prediction)
[2]
In [58]:
jovian.configure()
[jovian] It looks like Jovian is already configured ( check ~/.jovian/credentials.json ). Do you want to overwrite the existing configuration? [y/N]: y [jovian] Removing existing configuration.. [jovian] If you're a jovian-pro user please enter your company's organization ID on Jovian (otherwise leave it blank). Organization ID: [jovian] Please enter your API key ( from https://jovian.ml/ ): API KEY: 路路路路路路路路 [jovian] Configuration complete!
In [ ]:
jovian.commit(project="v-snehith999/corona-pneumonia-normal-keras",environment='none')
[jovian] Attempting to save notebook..
In [ ]: