Machine Learning for the detection of COVID-19
COVID-19 is the disease caused by the new coronavirus known as SARS-CoV-2. The WHO first learned of the existence of this new virus on December 31, 2019, when it was informed of a group of cases...
A learning project shared by the community. The article documents its team, methods and results; it does not establish that the prototype is ready for real-world use.

This archive includes automatic translations. Audio, video and text inside images remain in their original language.
Editorial note: this article documents an educational project and its results on the data used. It does not establish clinical validation or authorisation to diagnose or treat patients. Proposed healthcare applications are retained as the team’s goals, not demonstrated capabilities.
COVID-19 is the disease caused by the new coronavirus known as SARS-CoV-2. The WHO first learned of the existence of this new virus on December 31, 2019, when it was informed of a group of cases of "viral pneumonia" that had been declared in Wuhan (People's Republic of China). In this article we will try to describe how to apply Machine Learning to detect Covid-19.
It is called SARS-CoV-2, for the acronym:
- “SARS” because it can cause “Severe Acute Respiratory Syndrome” (SARS).
- “CoV” because it is a coronavirus.
- “2” because a similar virus already existed in 2002–2003 that also caused SARS.
WHAT TESTS ARE USED TO DIAGNOSE COVID-19?
PCR
PCR (acronym in English for “Polymerase Chain Reaction”) are a type of diagnostic tests that have been used for years in different public health crises related to infectious diseases. These tests have been used since the first days of the outbreak of the coronavirus pandemic in Spain. However, rapid tests have been incorporated recently and, as their name suggests, they are faster and easier. Both are used to check whether a person is infected or not by Covid-19.
ANTIGEN
Antigen test. This COVID-19 test detects certain proteins on the virus. A swab is used to take a sample of fluid from the nose, and antigen tests can give results in minutes.

CHEST X-RAY
Scans or x-rays produce an image of the organs and structures (heart, lungs, and airways) in the chest. They can detect blockages, inflammation and excess fluid.
- X-rays use a small amount of radiation to produce a two-dimensional image. They are usually done by a radiologist in the hospital using stationary equipment, but they can also be done with a portable machine.
- Computed tomography (CT) uses a computer to merge several x-rays taken from different angles to produce a two-dimensional image that can be converted to a three-dimensional image. It requires very specialized equipment and is performed in the hospital by a specialist radiologist.
They can be done in a hospital or other healthcare facilities, such as a doctor's office or clinic.
PROBLEM
Since COVID-19 test kits are of limited access to the general population, we must rely on other diagnostic measures.
X-RAY IMAGES
X-rays and CT scans are frequently used in the medical field to diagnose pneumonia, lung inflammation, abscesses, and/or enlarged lymph nodes. Since COVID-19 attacks the epithelial cells that line our respiratory tract, we can use x-rays to analyze the health of a patient's lungs.
With a vast majority of hospitals having X-ray imaging machines, the question arises: How could COVID-19 be detected in X-ray images? without dedicated testing kits.
OBJECTIVES
- Compile model inputs into datasets for training, testing, and validation.
- Develop a model for the diagnosis of the covid through X-ray images using deep learning, with an acceptable reliability percentage.
- Evaluate the results of the model through the confusion matrix.
MODEL DEVELOPMENT
To develop the model, a dataset from the Kaggle repository has been used that has a total of 5,856 images. X-rays of patients who had pneumonia have been used because these patients have a high probability of having covid-19.
https://www.kaggle.com/paultimothymooney/chest-xray-pneumonia
MODEL SELECTION AND IMPLEMENTED TECHNIQUES
To build the Machine Learning model to detect Covid-19, Convolutional Neural Networks were used, because they are neural networks designed and widely used to work with images.
Convolutional networks contain several hidden layers, which are responsible for detecting lines and curves and thus with convolutions it will be possible to detect more complex shapes such as silhouettes, faces, etc.

The tools used are: Tensorflow and keras. Tensorflow is an open source platform used for machine learning made up of a set of tools, libraries and resources that facilitate the work in the development and implementation of solutions with artificial intelligence (AI). Keras is a library, currently it is a high-level API that provides interfaces that simplify the work in developing applications with AI. Starting with version 2.0, Keras is already integrated into Tensorflow.

PROJECT DEVELOPMENT
Because it is a small proof of concept of image classification for an introductory course to Deep Learning, the dataset images have been uploaded to a Google Drive folder and Google's colab.research services were used to develop the Machine Learning model to detect Covid-19.
The images were adjusted to a size of 500×500, to be able to train. The following image shows an x-ray of a normal patient.

With the integration of Keras with Tensorflow, there are new classes such as “ImageDataGenerator” that facilitate the loading of images:

The images were divided into 3 groups: training, testing and validation.

The classification model can be seen in the following graph:

MODEL EVALUATION
To carry out the evaluation, the confusion matrix was used:

Where you can see that the model has identified:
- For people who were healthy and who the model predicted as healthy people there were 175 cases of true negatives (VN).
- For people who were sick and who the model predicted as sick people, there were 384 cases of true positives (TP).
- For people who were sick and who the model predicted as healthy people, there were 59 cases of false negatives (FN).
- For people who were healthy and who the model predicted as sick people, there were 6 cases of false positives (FP).
With this data we can calculate the following indicators:
Accuracy = (VP + VN) / (VP + VN + FN + FP)
Accuracy = (175 + 384) / (175 + 384 + 59 + 6)
Accuracy = 0.8958
Accuracy is the number of positive predictions that were correct and reached a value of 89.58%.
Precision = VP / (VP + FP)
Accuracy = 384 / (384 + 6)
Precision = 0.9846
Precision is the percentage of positive cases detected, reaching a value of 98.46%
Sensitivity = VP / (VP + FN)
Sensitivity = 384 / (384 + 59)
Sensitivity = 0.8668
Sensitivity is the proportion of positive cases correctly identified, reaching a value of 86.68%.
Specificity = VN / (VN + FN)
Specificity = 175 / (175 + 59)
Specificity = 0.7478
Specificity refers to the number of negative cases correctly identified, reaching a value of 74.78%.
ANALYSIS OF RESULTS
From the model development process, according to the Keras and Tensorflow libraries, we were able to reach an accuracy of 89.59%.

With the results obtained we can see in the figure that the precision value remained above 80%, the loss value was less than 20%.

CONCLUSION
According to the results obtained, we have:
- The reliability value of the model is acceptable, represented by 89%.
- The covid diagnosis model through X-ray images using Machine Learning could be applied in our environment as another diagnostic alternative.
BIBLIOGRAPHY
How do PCR and rapid coronavirus tests work and how are they different?
https://bootcampai.medium.com/redes-neuronales-convolucionales-5e0ce960caf8
Detecting COVID-19 in X-ray images with Keras, TensorFlow, and Deep Learning
https://www.kaggle.com/ankitachoudhury01/covid-patients-chest-xray
https://www.juanbarrios.com/la-matriz-de-confusion-y-sus-metricas/Saturdays.AI
Saturdays.AIFollow
WRITTEN BY
Bladimir Calcina
Saturdays.AI
Saturdays.AI is an impact-focused organization on a mission to empower diverse individuals to learn Artificial Intelligence in a collaborative and project-based way, beyond the conventional path of traditional education.
A history of the Saturdays.AI community. Originally published on Jan 2, 2022.
← More community stories



