What is pneumonia?
Pneumonia is an infection in one or both lungs. It can be caused by bacteria, viruses, or fungi. Bacterial pneumonia is the most common type in adults.Pneumonia causes inflammation in the air sacs in your lungs, which are called alveoli. The alveoli fill with fluid or pus, making it difficult to breathe. According to National Institutes of Health (NIH), chest x ray is the best test for pneumonia diagnosis. However, reading x ray images can be tricky and requires domain expertise and experience. It would be nice if we can just ask a computer to read the images and tell us the results.
In this story, we will use transfer learning to train an AI algorithm that analyzes chest x ray images and detects pneumonia using Deep Learning Studio
If you are not familiar with how to use Deep Learning Studio take a look at this :)
About Deep Learning Studiotowardsdatascience.com
Is Deep Learning without Programming Possible?towardsdatascience.com
If you want to win cash prizes worth upto $2500 do participate in their upcoming competition and to know more about it have a look at this article or check out their competition page here.
Is Deep Learning without Programming Possible?medium.com
In this story we are going to use Chest X-Ray Images (Pneumonia) available on kaggle here.
Figure 1. Illustrative Examples of Chest X-Rays in Patients with Pneumonia, Related to Figure 6 The normal chest X-ray (left panel) depicts clear lungs without any areas of abnormal opacification in the image. Bacterial pneumonia (middle) typically exhibits a focal lobar consolidation, in this case in the right upper lobe (white arrows), whereas viral pneumonia (right) manifests with a more diffuse ‘‘interstitial’’ pattern in both lungs.
The dataset is organized into 3 folders (train, test, val) and contains subfolders for each image category (Pneumonia/Normal). There are 5,863 X-Ray images (JPEG) and 2 categories (Pneumonia/Normal).
Chest X-ray images (anterior-posterior) were selected from retrospective cohorts of pediatric patients of one to five years old from Guangzhou Women and Children’s Medical Center, Guangzhou. All chest X-ray imaging was performed as part of patients’ routine clinical care.
For the analysis of chest x-ray images, all chest radiographs were initially screened for quality control by removing all low quality or unreadable scans. The diagnoses for the images were then graded by two expert physicians before being cleared for training the AI system. In order to account for any grading errors, the evaluation set was also checked by a third expert.
“Information: The Chest X-Rays Dataset csv file which I prepared according to Deep Learning Studio is available at my GitHub repository so all of you can download the csv file from there along with the model I used”
What is Transfer Learning?
The ability of a system to recognize and apply knowledge and skills learned in previous tasks to novel tasks (or new domains). So basically it is a machine learning technique where a model trained on one task is re-purposed on a second related task.
Transfer learning is popular in deep learning given the enormous resources required to train deep learning models or the large and challenging datasets on which deep learning models are trained.Transfer learning only works in deep learning if the model features learned from the first task are general.
- Higher start. The initial skill (before refining the model) on the source model is higher than it otherwise would be.
- Higher slope. The rate of improvement of skill during training of the source model is steeper than it otherwise would be.
- Higher asymptote. The converged skill of the trained model is better than it otherwise would be.
Why Transfer Learning?
- In practice a very few people train a Convolution network from scratch (random initialization) because it is rare to get enough dataset. So, using pre-trained network weights as initializations or a fixed feature extractor helps in solving most of the problems in hand.
- Very Deep Networks are expensive to train. The most complex models take weeks to train using hundreds of machines equipped with expensive GPUs.
- Determining the topology/training method/hyper parameters for deep learning is a black art with not much theory to guide you.
Why it works
In a neural network, neurons are organized in layers. Different layers may perform different kinds of transformations on their inputs. Signals travel from the first layer (input), to the last one (output), possibly after traversing the layers multiple times. As the last hidden layer, the “bottleneck” has enough summarized information to provide the next layer which does the actual classification task.
So, When you look at what these Deep Learning networks learn, they try to detect edges in the earlier layers, Shapes in the middle layer and some high level data specific features in the later layers. These trained networks are generally helpful in solving other computer vision problems.
Transfer Learning using Inception V3
Inception-v3 is trained for the ImageNet Large Visual Recognition Challenge using the data from 2012. This is a standard task in computer vision, where models try to classify entire images into 1000 classes, like “Zebra”, “Dalmatian”, and “Dishwasher”.
Inception v3 is a widely-used image recognition model that has been shown to attain greater than 78.1% accuracy on the ImageNet dataset. The model is the culmination of many ideas developed by multiple researchers over the years. It is based on the original paper: “Rethinking the Inception Architecture for Computer Vision” by Szegedy, et. al.
The model itself is made up of symmetric and asymmetric building blocks, including convolutions, average pooling, max pooling, concats, dropouts, and fully connected layers. Batchnorm is used extensively throughout the model and applied to activation inputs. Loss is computed via Softmax.
Now we will see how to build this model step by step
1) Project Creation:
After you log in to Deep Learning Studio that is either running locally or in cloud click on + button to create a new project.
2) Dataset Preprocessing
Since inceptionV3 model requires RGB image as an input image we need to convert our Grayscale image into RGB
So convert image into RGB or either download the custom dataset from here.
Next we need to generate custom CSV for DLS which store address of the image in the 1st column and label in the next.
3) Upload dataset:
You need to upload dataset and for that
- Download the dataset from Kaggle and custom csv for training data only from my github account here.
- Add data and csv file to a zip file
or Download Zip file for DLS from here.
- Go to my Datasets
- Click on the “upload dataset” option
- After selecting the zip file simply upload dataset file.
4) Dataset Intake:
We then setup dataset for this project in “Data” tab. Usually 80% — 20% is a good split between training and validation but you can use other setting if you prefer. Also don’t forget to set Load Dataset in Memory to “Full dataset” if your machine has enough RAM to load full dataset in RAM.
I have use resize to resize image to 150X150 . Feel free to change
5) Create the Neural Network
You can create a neural network as shown below by dragging and dropping the layers.
Don’t forget to make inceptionV3 100% trainable
Configuration of the network:
6) Hyperparameter and Training:
Hyperparameters that I have used are shown below. Feel free to change and experiment with them.
Finally, you can start the training from Training Tab and monitor the progress with training dashboard.
Once you complete your training you can check the results in results tab. I have achieved around 68% accuracy on the Validation dataset.
With Deep Learning Studio you can easily check the inference on validation and test dataset at different layers of the network.
Just go to inference tab and click on start inference.
So, the main purpose of this article is to build a simple deep learning model to demonstrate transfer learning using Deep Learning Studio.
- Dataset download link.
- Custom dataset for DLS download link.
- The DLS Model and custom CSV file can be fetched from my repo here.
- If you want to know more about Pneumonia check here
- To know the benefits of using Deep Learning Studio check here.
- If you want to learn more about transfer learning check here.
- Deep Cognition Website
If you like my articles, you can always support me with some money https://www.paypal.me/rajat2712/usd50
Thank you for your attention
You are using your time to read my work means the world to me. I fully mean that.
If you liked this story, go crazy with the applaud(👏) button! It will help other people to find my work and also share it with your friends!
You can also checkout my professional portfolio website
Source: Deep Learning on Medium