Detecting Covid-19 with Chest X-ray

by admin

COVID-19 pandemic is likely one of the largest challenges for the healthcare system proper now. It’s a respiratory illness that impacts our lungs and might trigger lasting harm to the lungs that led to signs equivalent to problem in respiration and in some circumstances pneumonia and respiratory failure. On this article, we’ll use X-ray information of lungs regular and COVID optimistic sufferers and prepare a mannequin to distinguish between them.

Dataset and Fashions Used:

The dataset used on this submit is the winner of the Kaggle group award. The dataset is collected by researchers from Qatar and Bangladesh. This dataset accommodates Three varieties of photographs:

  • COVID-19 optimistic (219 photographs)
  • Viral Pneumonia (1341 photographs)
  • Regular X-ray (1345 photographs)

These photographs have the dimensions (1024, 1024) and three coloration channels. The authors of the dataset additionally skilled a ResNet -34 mannequin and achieved an accuracy of 98they obtained 98.5% accuracy on the ResNet-34 mannequin.

Implementation:

  • On this article, we’ll use the Xception mannequin with the assistance of Keras API. This mannequin obtained an ImageNet top-1 accuracy of 79% and a top-5 accuracy of 95%.
  • First, we have to import the mandatory modules.

Python3

import numpy as np

  

import matplotlib.pyplot as plt

  

import tensorflow as tf

from tensorflow.keras import Sequential

from keras.preprocessing.picture import ImageDataGenerator

from tensorflow.keras.purposes import InceptionResNetV2

from tensorflow.keras.preprocessing.picture import ImageDataGenerator

from tensorflow.keras.purposes.xception import Xception

from tensorflow.keras.layers import Dense,Flatten,  Enter, Dropout

  • Now, we’ll use Kaggle API to obtain the dataset to the system. First, we would require the API key, to do that simply navigate to the profile part on Kaggle and obtain a JSON file containing our particulars for API, after that simply add this to colab or find within the native jupyter surroundings.

Python3

!pip set up kaggle

  

from google.colab import information

information.add()

! mkdir ~/.kaggle

! cp kaggle.json ~/.kaggle/

! chmod 600 ~/.kaggle/kaggle.json

 

! kaggle datasets obtain -d tawsifurrahman/covid19-radiography-database

  • Now, we unzip the dataset into the specified folder.

Python3

! unzip covid19-radiography-database.zip -d /content material/information

  • Now we preprocessed the dataset, we scale back the picture measurement from (1024, 1024) to (299,299) [the maximum size accepted by the Xception model], and we divide it right into a batch measurement of 16.

Python3

base = Xception(weights="imagenet", input_shape =(299,299,3),include_top= False)

for layers in base.layers:

    layers.trainable=False

  

base.abstract()

Downloading information from 
https://storage.googleapis.com/tensorflow/keras-applications/xception/
xception_weights_tf_dim_ordering_tf_kernels_notop.h5
83689472/83683744 [==============================] - 1s 0us/step
Mannequin: "xception"
__________________________________________________________________________________________________
Layer (kind)                    Output Form         Param #     Related to                     
==================================================================================================
input_1 (InputLayer)            [(None, 299, 299, 3) 0                                            
__________________________________________________________________________________________________
block1_conv1 (Conv2D)           (None, 149, 149, 32) 864         input_1[0][0]                    
__________________________________________________________________________________________________
block1_conv1_bn (BatchNormaliza (None, 149, 149, 32) 128         block1_conv1[0][0]               
__________________________________________________________________________________________________
block1_conv1_act (Activation)   (None, 149, 149, 32) 0           block1_conv1_bn[0][0]            
__________________________________________________________________________________________________
block1_conv2 (Conv2D)           (None, 147, 147, 64) 18432       block1_conv1_act[0][0]           
__________________________________________________________________________________________________
block1_conv2_bn (BatchNormaliza (None, 147, 147, 64) 256         block1_conv2[0][0]               
__________________________________________________________________________________________________
block1_conv2_act (Activation)   (None, 147, 147, 64) 0           block1_conv2_bn[0][0]            
__________________________________________________________________________________________________
block2_sepconv1 (SeparableConv2 (None, 147, 147, 128 8768        block1_conv2_act[0][0]           
__________________________________________________________________________________________________
block2_sepconv1_bn (BatchNormal (None, 147, 147, 128 512         block2_sepconv1[0][0]            
__________________________________________________________________________________________________
block2_sepconv2_act (Activation (None, 147, 147, 128 0           block2_sepconv1_bn[0][0]         
__________________________________________________________________________________________________
block2_sepconv2 (SeparableConv2 (None, 147, 147, 128 17536       block2_sepconv2_act[0][0]        
__________________________________________________________________________________________________
block2_sepconv2_bn (BatchNormal (None, 147, 147, 128 512         block2_sepconv2[0][0]            
__________________________________________________________________________________________________
conv2d (Conv2D)                 (None, 74, 74, 128)  8192        block1_conv2_act[0][0]           
__________________________________________________________________________________________________
block2_pool (MaxPooling2D)      (None, 74, 74, 128)  0           block2_sepconv2_bn[0][0]         
__________________________________________________________________________________________________
batch_normalization (BatchNorma (None, 74, 74, 128)  512         conv2d[0][0]                     
__________________________________________________________________________________________________
add (Add)                       (None, 74, 74, 128)  0           block2_pool[0][0]                
                                                                 batch_normalization[0][0]        
__________________________________________________________________________________________________
block3_sepconv1_act (Activation (None, 74, 74, 128)  0           add[0][0]                        
__________________________________________________________________________________________________
block3_sepconv1 (SeparableConv2 (None, 74, 74, 256)  33920       block3_sepconv1_act[0][0]        
__________________________________________________________________________________________________
block3_sepconv1_bn (BatchNormal (None, 74, 74, 256)  1024        block3_sepconv1[0][0]            
__________________________________________________________________________________________________
block3_sepconv2_act (Activation (None, 74, 74, 256)  0           block3_sepconv1_bn[0][0]         
__________________________________________________________________________________________________
block3_sepconv2 (SeparableConv2 (None, 74, 74, 256)  67840       block3_sepconv2_act[0][0]        
__________________________________________________________________________________________________
block3_sepconv2_bn (BatchNormal (None, 74, 74, 256)  1024        block3_sepconv2[0][0]            
__________________________________________________________________________________________________
conv2d_1 (Conv2D)               (None, 37, 37, 256)  32768       add[0][0]                        
__________________________________________________________________________________________________
block3_pool (MaxPooling2D)      (None, 37, 37, 256)  0           block3_sepconv2_bn[0][0]         
__________________________________________________________________________________________________
batch_normalization_1 (BatchNor (None, 37, 37, 256)  1024        conv2d_1[0][0]                   
__________________________________________________________________________________________________
.............

(Trimmed mannequin Abstract)

==================================================================================================
Whole params: 20,861,480
Trainable params: 0
Non-trainable params: 20,861,480
________________________________________________________________________
  • Now, we apply some information augmentation on the dataset and put together it for coaching. After that, we plot some coaching photographs. We’ll break up the dataset in such a manner that we have now 75% information for coaching and 25% for the take a look at/validation.

Python3

train_datagen = ImageDataGenerator(

        rescale=1./255,

        shear_range=0.2,

        zoom_range=0.2,

        validation_split=0.25,

        horizontal_flip =True

        )

  

prepare =train_datagen.flow_from_directory(

    "information/",

    target_size=(299, 299),

    batch_size=batch_size,

    class_mode='categorical',

    subset='coaching')

val =train_datagen.flow_from_directory(

    "information/",

    target_size=(299, 299),

    batch_size=batch_size,

    class_mode='categorical',

    subset='validation')

class_names=['covid-19','normal','pneumonia']

  

def plotImages(images_arr, labels):

    fig, axes = plt.subplots(12, 4, figsize=(20,80))

    axes = axes.flatten()

    label=0

    for img, ax in zip( images_arr, axes):

        ax.imshow(img)

        ax.set_title(class_names[np.argmax(labels[label])])

        label=label+1

    plt.present()

  

photographs = [train[34][0][0] for i in vary(16)]

photographs = photographs + [train[5][0][0] for i in vary(16)]

photographs = photographs + [train[0][0][0] for i in vary(16)]

  

labels=[]

labels = [train[34][1][0] for i in vary(16)]

labels=  labels + [train[5][1][0] for i in vary(16)]

labels=  labels + [train[0][1][0] for i in vary(16)]

  

plotImages(photographs,labels)

Regular Lungs X-ray

Covid -19 (+) Lungs X-ray

Viral Pneumonia Lungs X-ray

  • Now, we outline our mannequin, First, we’ll import our base mannequin i.e Xception (we use imagenet skilled weights) in our sequential mannequin, flatten the weights from and apply a dense layer (absolutely related layer) and a softmax classification layer. To stop the mannequin from overfitting we additionally add some dropout layers

Python3

mannequin = Sequential()

mannequin.add(Enter(form =(299,299,3)))

mannequin.add(base)

mannequin.add(Dropout(0.2))

mannequin.add(Flatten())

mannequin.add(Dropout(0.2))

mannequin.add(Dense(16))

mannequin.add(Dense(3,activation='softmax'))

mannequin.abstract()

Mannequin: "sequential"
_________________________________________________________________
Layer (kind)                 Output Form              Param #   
=================================================================
xception (Practical)        (None, 10, 10, 2048)      20861480  
_________________________________________________________________
dropout (Dropout)            (None, 10, 10, 2048)      0         
_________________________________________________________________
flatten (Flatten)            (None, 204800)            0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 204800)            0         
_________________________________________________________________
dense (Dense)                (None, 16)                3276816   
_________________________________________________________________
dense_1 (Dense)              (None, 3)                 51        
=================================================================
Whole params: 24,138,347
Trainable params: 3,276,867
Non-trainable params: 20,861,480
_________________________________________________________________
  • Now we’ll compile and prepare the mannequin, we use Adam Optimizer with a 0.001 studying fee. We’ll prepare the mannequin for 30 epochs.

Python3

from tensorflow.keras.optimizers import Adam

mannequin.compile(

    optimizer=Adam(learning_rate=1e-3),

    loss="categorical_crossentropy",

    metrics=["accuracy"],

)

mannequin.fit_generator(prepare,epochs=30,validation_data=val)

  

mannequin.save('epoch_30.h5')

Epoch 1/30
137/137 [==============================] - 121s 886ms/step - 
loss: 5.7757 - accuracy: 0.8528 - val_loss: 3.4022 - val_accuracy: 0.8966
Epoch 2/30
137/137 [==============================] - 119s 867ms/step - 
loss: 3.3137 - accuracy: 0.9028 - val_loss: 2.0748 - val_accuracy: 0.9228
Epoch 3/30
137/137 [==============================] - 119s 866ms/step - 
loss: 2.2811 - accuracy: 0.9161 - val_loss: 2.2661 - val_accuracy: 0.9186
Epoch 4/30
137/137 [==============================] - 119s 867ms/step - 
loss: 1.6122 - accuracy: 0.9339 - val_loss: 3.8654 - val_accuracy: 0.8648
Epoch 5/30
137/137 [==============================] - 120s 877ms/step - 
loss: 1.0704 - accuracy: 0.9440 - val_loss: 1.6559 - val_accuracy: 0.9214
Epoch 6/30
137/137 [==============================] - 120s 875ms/step - 
loss: 0.7675 - accuracy: 0.9509 - val_loss: 1.3920 - val_accuracy: 0.9255
Epoch 7/30
137/137 [==============================] - 120s 872ms/step - 
loss: 0.5744 - accuracy: 0.9509 - val_loss: 1.2669 - val_accuracy: 0.9021
Epoch 8/30
137/137 [==============================] - 119s 872ms/step - 
loss: 0.4065 - accuracy: 0.9528 - val_loss: 1.1800 - val_accuracy: 0.9145
Epoch 9/30
137/137 [==============================] - 118s 864ms/step - 
loss: 0.2160 - accuracy: 0.9638 - val_loss: 0.7624 - val_accuracy: 0.9379
Epoch 10/30
137/137 [==============================] - 119s 865ms/step - 
loss: 0.2552 - accuracy: 0.9606 - val_loss: 0.4897 - val_accuracy: 0.9421
Epoch 11/30
137/137 [==============================] - 118s 864ms/step - 
loss: 0.2015 - accuracy: 0.9651 - val_loss: 0.4510 - val_accuracy: 0.9476
Epoch 12/30
137/137 [==============================] - 121s 880ms/step - 
loss: 0.1473 - accuracy: 0.9725 - val_loss: 0.3458 - val_accuracy: 0.9352
Epoch 13/30
137/137 [==============================] - 121s 880ms/step - 
loss: 0.1534 - accuracy: 0.9656 - val_loss: 0.5945 - val_accuracy: 0.9297
Epoch 14/30
137/137 [==============================] - 120s 876ms/step - 
loss: 0.1315 - accuracy: 0.9734 - val_loss: 0.4655 - val_accuracy: 0.9407
Epoch 15/30
137/137 [==============================] - 121s 882ms/step - 
loss: 0.1127 - accuracy: 0.9661 - val_loss: 0.3728 - val_accuracy: 0.9186
Epoch 16/30
137/137 [==============================] - 121s 882ms/step - 
loss: 0.1198 - accuracy: 0.9716 - val_loss: 0.4312 - val_accuracy: 0.9476
Epoch 17/30
137/137 [==============================] - 120s 875ms/step - 
loss: 0.1046 - accuracy: 0.9771 - val_loss: 0.4035 - val_accuracy: 0.9393
Epoch 18/30
137/137 [==============================] - 119s 870ms/step - 
loss: 0.0872 - accuracy: 0.9761 - val_loss: 0.8248 - val_accuracy: 0.9145
Epoch 19/30
137/137 [==============================] - 120s 874ms/step - 
loss: 0.1116 - accuracy: 0.9752 - val_loss: 0.3309 - val_accuracy: 0.9393
Epoch 20/30
137/137 [==============================] - 120s 877ms/step - 
loss: 0.1261 - accuracy: 0.9729 - val_loss: 0.5384 - val_accuracy: 0.8924
Epoch 21/30
137/137 [==============================] - 119s 869ms/step - 
loss: 0.0840 - accuracy: 0.9748 - val_loss: 0.5690 - val_accuracy: 0.9366
Epoch 22/30
137/137 [==============================] - 119s 868ms/step - 
loss: 0.0942 - accuracy: 0.9761 - val_loss: 0.3517 - val_accuracy: 0.9448
Epoch 23/30
137/137 [==============================] - 120s 876ms/step - 
loss: 0.1207 - accuracy: 0.9656 - val_loss: 0.2871 - val_accuracy: 0.9434
Epoch 24/30
137/137 [==============================] - 118s 864ms/step - 
loss: 0.0959 - accuracy: 0.9729 - val_loss: 0.4589 - val_accuracy: 0.9366
Epoch 25/30
137/137 [==============================] - 119s 867ms/step - 
loss: 0.0945 - accuracy: 0.9748 - val_loss: 0.3964 - val_accuracy: 0.9490
Epoch 26/30
137/137 [==============================] - 119s 871ms/step - 
loss: 0.1039 - accuracy: 0.9761 - val_loss: 0.3048 - val_accuracy: 0.9393
Epoch 27/30
137/137 [==============================] - 119s 866ms/step - 
loss: 0.0905 - accuracy: 0.9739 - val_loss: 0.3308 - val_accuracy: 0.9407
Epoch 28/30
137/137 [==============================] - 120s 873ms/step - 
loss: 0.0757 - accuracy: 0.9766 - val_loss: 0.1871 - val_accuracy: 0.9517
Epoch 29/30
137/137 [==============================] - 119s 871ms/step - 
loss: 0.1012 - accuracy: 0.9688 - val_loss: 0.7361 - val_accuracy: 0.9297
Epoch 30/30
137/137 [==============================] - 120s 874ms/step - 
loss: 0.0713 - accuracy: 0.9780 - val_loss: 0.3497 - val_accuracy: 0.9434

Outcomes and Conclusion:

We’ve obtained an accuracy on 97.8% on coaching and 94.3% with coaching on simply 30 epochs on Xception mannequin, which is near the 98.3% accuracy as reported by the authors of paper.

References:



Should you like GeeksforGeeks and wish to contribute, you can even write an article utilizing contribute.geeksforgeeks.org or mail your article to [email protected] See your article showing on the GeeksforGeeks foremost web page and assist different Geeks.

Please Enhance this text in case you discover something incorrect by clicking on the “Enhance Article” button beneath.

Related Posts

Leave a Comment