Sitemap
Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Identifying Pneumonia in Chest X-rays Using Ludwig Deep Learning and Python

9 min readOct 4, 2020

--

A s a programming enthusiast I love to get my hands dirty with all types of technologies, trying to leave no stones unturned. From creating websites with web frameworks to creating GUIs to experimenting with multiple APIs, I have loved to push my programming skills to new horizons. However the one area in the computer science field, I have strayed away from… Machine Learning! I have always been discouraged in a certain way from touching upon this topic due to one simple reason, Machine Learning is complex. It requires creating algorithms, comprehending advanced mathematical concepts, using certain classifications, and understanding the vernacular of the field. To say the least, there is a large learning curve involved in jumping into creating, training, and predicting with models.

However there is an answer to bridging the gap between the absolute novice and the complexities of Machine Learning. The answer: Ludwig.

Ludwig is an Uber open source software that allows users to train and test deep learning models without much code, that has been built on top of TensorFlow. The beauty of Ludwig is its ability to scale its complexity. To the veteran there are an innumerable amount of parameters that can be changed to create pristine models and to the novice student there are defaults and railings that allow for a smooth, easy, and quick ways to set up Machine Learning. On top of that the software can be used to accomplish an endless amount of use cases due to the variety of data types that Ludwig can handle living up to their principle of generality.

As it states in their Github, the available datatypes in Ludwig are:

  • binary
  • numerical
  • category
  • set
  • bag
  • sequence
  • text
  • timeseries
  • image
  • audio
  • date
  • h3
  • vector

You only have to specify the input datatype and the output datatype and Ludwig will handle the rest. In my example, the input is an image and the output is text, thus creating an image classifier.

The software has two methods in terms of how to utilize it, through the command line or as an API in python.

Due to my own bias of loving to create python scripts, I will be showcasing the Ludwig API and how I integrated it into my code. By the end of this guide, you should have a jumpstart on creating models for any use cases you have in mind.

Why I Chose My Use Case?

Before I get into the actual technical process, I wanted to take a moment and explain my decision making for the purpose of my model. The model I have created is able to predict at around a 70% success rate whether a chest x-ray is indicative of Pneumonia. As a beginner with Machine Learning, I wanted to start small with the project idea, build experience, but also have some type of practical use. Originally the idea of a sentiment analyzer sounded interesting, however the idea of having a computer tell me the difference between images simply astonished me. Furthermore as a big believer in technology revolutionizing the medical industry, I set my sights upon a dataset related to health and medicine. After a short amount of searching(proving how there are a million use cases), I found a dataset of Pneumonia chest x-rays and began my journey through Ludwig.

Installing Ludwig

The way I installed Ludwig was by first creating a virtual environment using python. The simply using pip install to download the basic libraries for Ludwig into my environment.

However this only installs just the basic amount of libraries needed for Ludwig, without datatype specified libraries, Ludwig cannot take in the datatypes and create models. For my specific case, I required to download the image and text libraries since those were my input and output types, respectively. I also chose to download the viz library so I could view graphs on my model’s training progress(this helped me figure out certain settings to tweak, to make my model more accurate). It is worth noting how well Ludwig integrates the Matplotlib into their software to create models. As a user who has in the past used Matplotlib to graph trends in the stock market, I attest to the fact that there is a lot of setup involved, but Ludwig handles the whole process seamlessly and with a high level of abstraction.

Finding the Data

The first part of any Machine Learning project is gathering the data, without or with too little, will result in poor models! Luckily, finding data when you have a broad category of ideas is definitely not too difficult, in fact a great place to find data and where in fact I found my chest x-ray dataset is Kaggle, a website that allows users to post and find datasets to work with.

Preprocessing the Data

The next part included preprocessing the dataset in order to feed the information into the Ludwig API. The dataset needs to be transformed from a file of images into a CSV(comma-separated value) file so that Ludwig can read through the file. Furthermore, the data needs to be distributed into three CSV files, one for training: to teach the model, one for validation: to identify the accuracy of the model, and one for predictions: to see how the model handles predicting the classification of the image.

My Training CSV File(left), first part is the image path which allows Ludwig to identify where the image file is located and the second part is the classification. The code I utilized to transform my files from a folder to a CSV. This code is not my own, it has been written by Gilbert Tanner.
Images from the dataset that the model works with. All contain Pneumonia

Using Ludwig API

Finally, now that we have set up our data sets we can get to my favorite part, creating the model. Here we will teach the computer to understand the difference between an image of a chest that has Pneumonia versus one that does not. The lucky part of this aspect of the project, is that due to the great documentation provided on Ludwig’s GitHub and relatively little prior experience needed to get started, the coding aspect is neither long nor difficult. I’ll be breaking down by code part by part.

The first step in creating our script is loading all of our setup resources, including importing the Ludwig libraries to use the API and the pandas library to handle the CSV files that I have created. The panda library allows us to store the CSVs into these three variables, each a portion in creating the model. One important point if you are using this blog as guide to making your own models, make sure that your CSV files are in the same place as your python script otherwise you must provide the file path to them.

The model definition is the next part of the code where we inform Ludwig of all the information and domains about the model we want. Ludwig uses this information and then constructs the model accordingly. The model definition can have many different types of values and parameters however I will be explaining the parameters I used in my model(the basic ones). There are two ways in providing this information, as I did here, you can create a dictionary variable and structure it similarly or import a yaml file with all the necessary information.

The input features includes the name of the first column of the CSV file, the datatype, a default encoder, and an optional preprocessor. There are many customizations to this that can be employed however for my project I did not seek other features. The encoder in simple terms, encodes our input datatype and then decodes into our target output datatype. The preprocessor makes sure all the images are the same height and width, making it easier for the model to compare images.

The output features are the output name and the datatype the model should output.

Finally, the training portion is to inform Ludwig how to train the model. Epochs and Batch Size, tell Ludwig how many times to run through the dataset you have provided. I found myself playing with Epoch, and chose to stop at 7 epochs. The larger the epoch and batch size is, the longer the model will train and thus take a lot longer to process.

Next we begin the creation and training of our model. The first line of code creates the model for us by passing in our model definition as a parameter to Ludwig. After this we begin to train the model using the train function and passing in both our validation and train variables which contain the CSV files. This will be the longest portion of the runtime on your machine. Furthermore the commented out portion details how to load in a previously saved model, so that you don’t waste time creating a new model when all you want to do is predict. Make sure to specify the right file path in the model_path variable.

The last part of the program is where we get our predictions based on our test dataset. Choose your own save path to access the CSV of your predictions.

Here are a look at my predictions!

Predictions my model made(left), Corresponding test dataset(right)

After looking at the generated statistics file that Ludwig generates, my predictions are about 70% accurate in identifying whether there is Pneumonia or not in a chest x-ray. As this is my first hands on project with Machine Learning, my accuracy is a somewhat low. With better configurations using Ludwig, then the accuracy will definitely increase.

There you have it, here is my complete and final code:

Visualizing the Model

A both visual and vital part of the project or any Machine learning project is seeing how the machine learned. Luckily, Ludwig handles this whole process for us with a simple command.

And here are the resulting graphs:

All showing consistent improvement as the model went through each epoch!

Conclusion

The Ludwig Deep Learning Open Source Software from Uber is a powerful tool in bringing Machine Learning to a multitude of users. It’s ability to be extremely elastic and able to be plugged into any scripts is vital to manufacturing programs in a streamline process. Due to it being open source, it has a rapid ability to grow and expand even larger than it is currently, offering more and refined functionalities in the future. Its core principles of extensibility, generality, and flexibility are all ingrained.

As this is my first project with Machine Learning, the success in integrating a model into a script only motivates me further to rely on this technology for future endeavors as-well as contributing to Ludwig’s open source software.

Resources

Ludwig Github: https://github.com/uber/ludwig

Thanks to Robert Walid’s article on Ludwig: Introduction to Ludwig and how to deploy a Deep Learning model via Flask

--

--

Analytics Vidhya
Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Kalyan Sunkara
Kalyan Sunkara

Second-year Computer Science Student at University of California, Riverside. Lover of change, innovation, and everything technology!

No responses yet