Virus Xray Image Classification with Tensorflow Keras Python and Apache Spark Scala
Disclaimer:
This writing is exclusively and entirely for educational purpose in the field of computer science. Only government medical board-certified radiologist can and should perform diagnosis from an Xray image.
Introduction
Can average person tell the difference from a picture of cat or dog? Probably yes. Can average person tell the difference from looking at an Xray photo and tells the difference between normal or virus caused pneumonia? Not unless that person is a board-certified medical professional.
For educational purpose in computer science on machine learning, can a computer after it is trained by a given dataset (labeled Xray pictures) that are empirically true to differentiate an Xray photo and tells the difference between normal and virus caused pneumonia from Xray images? That needs to be found out.
Data Preparation
To begin with, I downloaded the Xray image dataset from Kaggle (Coronahack chest Xray dataset)
https://www.kaggle.com/praveengovi/coronahack-chest-xraydataset
and build a neural network with Tensorflow Keras train the machine.
Generally, dataset to be used in image recognition is usually stored the following way, because the dataset is not a single file, with features and label, but many image files such as jpegs and a csv file telling the label and file name for each image file.
For image classification, common practice would be creating a folder, name the folder with label name, and place all the image files belong to that label inside that folder.
Therefore, I placed the files in below directory structure:
Train:
./
βββ normal
βββ virus
Validation:
./
βββ normal
βββ virus
Data Preprocessing
Apache Spark SQL API Image Read API Scala code to explore the image size
First, determine the image size by the following Scala code invoking Apache Spark Image read API:
Scala code to resize the jpeg image
The images are large, around 2500*2500, about 6 MP. This means, each pixel is a feature, or a column, this is like a table that has 6 million columns.
Therefore, I need to downsize to smaller image. I wrote the following Scala code to resize the image from about 2500*2500 to about 300*350, about one MP.
After resizing images to 300*350, the new location of the image files are in /mnt/common/20200510
Algorithm Selection
Image classification is typically by convolutional neural network. I use Tensorflow/Keras. Now I need to switch language from Scala to Python to invoke Keras APIs.
Original Xray image
This is the example of the image before resizing:

Resized Xray Image
This is the example of resized image that is label as normal

This is the example of resized image that is labeled as pneumonia by virus

Xray CNN image classification by Keras
Following is the code to train the machine to classify Xray Images whether normal or pneumonia by virus by convolutional neural network with Keras and Tensorflow on the background
Output below
Save model
Hardware Used:
By the way, the machine that runs this exercise is equipped with Intel 8700 8th gen CPU with 6 cores/12 threads, 64GB RAM and a nvidia GTX 1060 GPU with 6GB GPU memory. Both Tensorflow and Keras are GPU enabled version.
Summary
With not many lines of Python code and a few minutes of processing time, deep learning by CNN (Convolutional Neural Network) using Tensorflow/Keras yield training/validation accuracy of about 93%, which means, out of 100 Xray images, the machine tell whether normal or pneumonia by virus correctly on 93 images and wrong on 7 images.
Disclaimer again
This writing is exclusively and entirely for educational purpose in the field of computer science. Only government medical board-certified radiologist can and should perform diagnosis from an Xray image.
Last updated
Was this helpful?