Pneumonia Detection with Python

In this article, I will introduce you to a machine learning project on pneumonia detection with the Python programming language. Pneumonia is an inflammatory condition of the lung mainly affecting the small air sacs called alveoli.

Introduction to Pneumonia Detection

Pneumonia is an infectious and fatal respiratory disease caused by bacteria, fungi, or a virus that infects human lung air sacs with a load full of fluid or pus.

Also, Read – 100+ Machine Learning Projects Solved and Explained.

Chest x-rays are the common method used to diagnose pneumonia and it takes a medical expert to assess the result of the x-ray. The troublesome method of detecting pneumonia leads to loss of life due to improper diagnosis and treatment.

With the emerging computing power, the development of an automatic pneumonia detection and disease treatment system is now possible, especially if the patient is in a remote area and medical services are limited.

Machine Learning Project on Pneumonia Detection with Python

In this section, I will take you through a Machine Learning Project on Pneumonia Detection with Python programming language. I will use the Fastai library in Python for the task of Pneumonia Detection.

Now let’s get started with this task by importing the necessary Python libraries:

Now we need to set up the path for training dataset as the dataset includes images only:

x  = 'Path'
path = Path(x)

Now let’s load the data for training or Machine Learning model for the task of Pneumonia Detection with Python:

data = ImageDataBunch.from_folder(path, train = '.', valid_pct=0.2,
                                  ds_tfms=get_transforms(), size=224,

Data Exploration

The dataset I’m using here is stored as .jpg files in 2 different folders, each folder with the model name of the images in the folder.

We need to use the ImageDataBunch.from_folder() function to load the images and assign tags to the images based on the name of the folder from which they are read:

data.show_batch(rows=3, figsize=(7,6),recompute_scale_factor=True)
pneumonia dataset

Using Machine Learning for Pneumonia Detection with Python

Now, I will use a pre-trained model known as ResNet50, which is a type of Convolutional Neural network in Machine Learning. Now let’s see how to use this model:

learn = cnn_learner(data, models.resnet50, metrics=[accuracy], model_dir = Path('Path'),path = Path("."))

Now let’s have a look at the learning rate of the model:

pneumonia learning rate

Training and Testing the Model 

In the above section, we loaded the model. Now I will train the model on our dataset:

Now let’s test the model:


So this is how we can use Machine Learning to detect Pneumonia. I hope you liked this article on Machine Learning project on Pneumonia Detection with Python. Feel free to ask your valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1537

Leave a Reply