Diagnosing Covid-19 Faster & Accurately with Machine Learning
In the battle against the coronavirus pandemic, medical professionals are now utilizing artificial intelligence and Python programming to detect COVID-19 from a patient’s lung x-ray. By utilizing machine learning, doctors can now diagnose patients quicker and more accurately, allowing for a more efficient response and treatment plan. Motivated to further my understanding of machine learning, I decided to undertake a research sprint and create as many projects as possible. One of the projects I tackled was the segmentation of COVID-19 in lungs.
Before we go further, if you aren’t familiar with the technology behind this project, I recommend reading this article to gain an understanding:
CNNs
The process begins with the collection of images. These images are then used to train a Convolutional Neural Network (CNN). A CNN is a type of neural network used for image classification.
Like I mentioned in my previous article, composed of an input layer, hidden layers, and an output layer. During the training phase, the CNN is fed a set of labeled images, and uses its layers to recognize patterns and classify the images.
The diagram shows the basic structure of a CNN. It consists of an input layer, multiple convolutional and pooling layers, and an output layer. The input layer takes in an image, which is then processed by the convolutional layers. In these layers, a set of filters are applied to the image to extract features such as edges and shapes.
These features are then passed to the pooling layers, which reduce the dimensionality of the feature maps. The output layer then takes the processed features and uses them to classify the image.
The machine learning algorithm I built can be used to help hospitals diagnose and treat patients with Covid-19 more quickly and accurately. Using this algorithm, doctors can more easily and quickly identify patients who are infected with the virus and get them into treatment as soon as possible.
This helps to reduce the spread of the virus and allows for more effective treatments for those who are infected. Additionally, it can help reduce the burden on hospital staff, allowing them to focus on more pressing matters. Although we’re nearing the end of this pandemic, this would’ve been a significantly valuable tool.
COVID-19 Segmentation
After having an understanding of how neural networks work, I now worked on a project to create an AI model that can accurately detect COVID in lungs.
Tools Used
Numpy: Numpy is a powerful library of multi-dimensional array objects and a collection of routines for processing those arrays. It is most commonly used to manipulate data in order to create mathematical models to detect COVID in lungs. It can be used to preprocess X-ray and CT scan data and also helps to create visualizations of the data that can be used to identify COVID infections.
Pandas: Pandas is a library that enables easy manipulation and analysis of tabular data. It is commonly used to read, write, and modify data sets related to COVID in lungs. It can also be used to manipulate data in order to create models for detection and classification of COVID infections.
Matplotlib: Matplotlib is a library for creating visualizations of data. It is often used to create plots and charts of data related to COVID in lungs. These visualizations can be used to gain insights into the data and to identify patterns and anomalies that may be indicative of an infection.
TensorFlow is a powerful open source software library created by Google that is used for data flow programming and deep learning. It is used to build and train neural networks that can be used to detect patterns in data. TensorFlow has been used to develop applications in computer vision, natural language processing, speech recognition, and many other areas.
In the fight against COVID-19, TensorFlow can be used to detect COVID-19 in lung X-ray images. By training a deep learning model with images of both healthy and infected lungs, the model can learn to distinguish between the two. The model can then be used to detect COVID-19 in new X-ray images.
Preparing the Model
train_datagen = image.ImageDataGenerator(rescale=1/25, horizontal_flip=True, zoom_range=0.2)
train_data = train_datagen.flow_from_directory(directory = '/content/CovidDataset/Train', target_size=(256,256), batch_size=16, class_mode='binary')
val_datagen = image.ImageDataGenerator(rescale=1/25)
val_data = train_datagen.flow_from_directory(directory = '/content/CovidDataset/Val', target_size=(256,256), batch_size=16, class_mode='binary')
The above code is used to create two image data generators which will be used to flow the training and validation data into the model. The train_datagen generator is used to rescale the data by 1/25 and to apply image augmentation techniques such as horizontal flipping and zoom range of 0.2. This is done to increase the number of images available for training and to increase the robustness of the model.
The validation generator is used to rescale the data by 1/25. It is also used to train the model on the validation set and to ensure that the model is generalizing well. The flow_from_directory functions are used to retrieve the training and validation images from the respective directories.
The target size is set to 256x256 and the batch size is set to 16. The batch size is the number of images that will be processed at once by the model. The target size is the size of the images that will be used as input to the model.
Creating the CNN with Code
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu', input_shape=(256,256,3)))
model.add(Conv2D(filters=64, kernel_size=(3,3), activation='relu'))
model.add(MaxPool2D())
model.add(Dropout(0.25))
model.add(Conv2D(filters=64, kernel_size=(3,3), activation='relu'))
model.add(MaxPool2D())
model.add(Dropout(0.25))
model.add(Conv2D(filters=128, kernel_size=(3,3), activation='relu'))
model.add(MaxPool2D())
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
The above code is a simple sequential model for binary classification. The model consists of convolutional layers with various filters, max pooling layers, dropout layers, flatten layer, and a dense layer.
Conv2D is a 2D convolutional layer which takes a 3D input and applies a set of 2D filters to the input to extract features. It helps to identify patterns in the input data which can be used for binary classification.
MaxPool2D is a 2D max pooling layer which reduces the size of the image by taking the maximum value of each filter window and downsizing the image. This helps to reduce the amount of data the model needs to process and can improve performance.
Dropout is a regularization technique which randomly sets a fraction of the input units to 0 at each update during training. This helps to reduce overfitting and improve the model’s generalization.
Dense is a fully connected layer which takes the output from the convolutional layers and computes the probability of each class. This is necessary for the model to make predictions based on the data it has seen.
In the last line I’m using a sigmoid activation function; it’s a type of convolutional neural network (CNN) architecture which uses sigmoid activation functions instead of the more commonly used ReLU activation functions. It is mainly used for tasks such as image similarity, image retrieval, and object recognition.
Training my Neural Network
The last step in a CNN project is to train the model. This is done with the model.fit() function, which uses the prepared data and neural network to update the model’s parameters and improve its performance.
#train model
model.fit_generator(
train_data,
steps_per_epoch=8,
epochs=20,
validation_data = val_data,
)
The model is trained for 20 epochs, with 8 steps per epoch.
Epochs refer to the number of times the model is trained over the training data. The steps per epoch indicate the number of batches of data the model is trained on during each epoch. The model is also evaluated using the validation_data as well (We need both in order for the training to actually work the way we intended).
Results
In the screenshot above we see three cells. One that loads up an image from a dataset to test our model, one that detects and predicts the output (COVID or no COVID), and one that shows us what the output really means.
We see in the output of the second cell that the array is outputting a 0. This is a correct prediction of our image because as we can see in the next cell output, a 0 array value represents a COVID image.
I downloaded a massive dataset filled with images of healthy and Covid-infected lungs to test this and the first line of code in that screenshot is just a method of inputting an image of a COVID x-ray for the model to train.
The use of machine learning to detect Covid-19 in the lungs is a project that has demonstrated great potential and has the ability to redefine how medical professionals detect and diagnose the virus in patients.
This project has the potential to revolutionize the field of medical imaging and create a more efficient way to diagnose and treat the virus or even viruses in the future. The research and development put into this project has been invaluable, and the results thus far have been incredibly promising.
Through the use of this machine learning technology, we are on a path towards a more accurate and efficient approach to diagnosing diseases in the lungs, which could have significant implications for the global healthcare system.