Covid-19 detection analyzing X-Ray images using a simple Convolutional Neural Net architecture
Saikat Chakraborty
Managing Director @ Accenture | Enterprise AI Value Strategy Executive
Just like all my articles, the opinions expressed in this article are of mine only as per my understanding and in no way represents anything that has to do with the organization I work for.
As several references mention,Chest X-Ray scans may be helpful to diagnose COVID-19 in individuals with a high clinical suspicion of infection. This small article is a demonstration of this using a simple deep learning architecture with a pretty reasonable accuracy, which is not perfect but makes a definite point in proving the effectiveness of AI as it may evolve in future. It is amazing to observe that with only a few lines of code, great results can be achieved!
The data that has been used for this experimentation, is publicly available in here:
https://github.com/shervinmin/DeepCovid/tree/master/data
Let's start by importing the required libraries as usual.
Created on Sat Nov 28 10:37:45 2020 @author: chakr """ import tensorflow as tf from keras.preprocessing.image import ImageDataGenerator import numpy as np import glob
Next we need to preprocess the data.The ImageDataGenerator class is particularly useful and I would request the readers to try with different options to make the prediction even more accurate.
#data preprocessing traingen = ImageDataGenerator(rescale = 1./255,shear_range=0.2,zoom_range=[0.2,1.8],horizontal_flip=True,vertical_flip=True) training_set = traingen.flow_from_directory('covid19\\train',target_size = (256,256),batch_size=32,class_mode='binary') testgen = ImageDataGenerator(rescale = 1./255) test_set = traingen.flow_from_directory('covid19\\test',target_size = (256,256),batch_size=32,class_mode='binary')
Now, simply build the CNN model and train it with the training set.
cnn = tf.keras.models.Sequential() cnn.add(tf.keras.layers.Conv2D(filters =32,kernel_size=3,activation='relu',input_shape = [256,256,3])) cnn.add(tf.keras.layers.MaxPool2D(pool_size=(2,2),strides=2)) cnn.add(tf.keras.layers.Conv2D(filters =32,kernel_size=3,activation='relu')) cnn.add(tf.keras.layers.MaxPool2D(pool_size=(2,2),strides=2)) cnn.add(tf.keras.layers.Flatten()) cnn.add(tf.keras.layers.Dense(units = 256,activation = 'relu')) cnn.add(tf.keras.layers.Dense(units = 1,activation = 'sigmoid')) cnn.compile(optimizer = 'adam',loss = 'binary_crossentropy',metrics=['accuracy']) cnn.fit(x = training_set,validation_data=test_set,epochs=100)
Note the number of epochs in the snippet above. It usually takes 60 minutes to 90 minutes in my computer to finish the entire training for a 100 epoch run. So babysit the training in the beginning and based on the learning, you may adjust the same.
In my case, this is how the metrics look at the end of the run...
.... .... Epoch 99/100 21/21 [==============================] - 66s 3s/step - loss: 0.0645 - accuracy: 0.9759 - val_loss: 0.2242 - val_accuracy: 0.9381 Epoch 100/100 21/21 [==============================] - 67s 3s/step - loss: 0.0770 - accuracy: 0.9699 - val_loss: 0.0830 - val_accuracy: 0.9810
Now all that remains is the prediction for your data. You may feed single or multiple images in the model and collec predictions and check accuracy.
k = training_set.class_indices['covid'] def imageconverter(picture): import numpy as np from keras.preprocessing import image chk_img = image.load_img(picture,target_size = (256,256)) chk_img = np.expand_dims(image.img_to_array(chk_img),axis = 0) return chk_img imagelist = glob.glob('covid19\\val\\*.jpg') chkimages = [imageconverter(i) for i in imagelist] collector = [] for j in [imageconverter(i) for i in imagelist]: predictions = cnn.predict(x = j) if predictions[0][0] == k: predictions = "Covid-19" else: predictions = "Normal" collector.append(predictions)
In my case, just without any optimization, in the first run, 88% accuracy was obtained in the validation images. I would encourage the readers to further optimize and share the results in the comments below.
Senior Principal Engineer
4 年On the onset accuracy is good...still can work on hyperparameters of CNN...Image size is 256*256...we can add more conv layers to have a better receptive field...stride of 2 may make the image blur can try with stride 1..also we can increase the filters on every layer added to extract more features from the image....will work on dataset and let you the results.