Detection of Pneumonia using ML & DL in Python

Pneumonia is form of a respiratory infection that affects the lungs. In these acute respiratory diseases, human lungs which are made up of small sacs called alveoli which in air in normal and healthy people but in pneumonia these alveoli get filled with fluid or “pus” one of the major step of phenomena detection and treatment is getting the chest X-ray of the (CXR). Chest X-ray is a major tool in treating pneumonia, as well as many decisions taken by doctor are dependent on the chest X-ray. Our project is about detection of Pneumonia by chest X-ray using Convolutional Neural Network. This paper written by us is an efficient approach towards classifying chest X- rays into pneumonia and no pneumonia X-rays. We have taken this approach as the most used radiography method produces errors. So, we have used CNN and batch normalization from keras to develop this model, and calculated accuracy using confusion matrix. We were successful in doing so with the help of “Python” and “OpenCV”, both of which are freely available and are open source tools and can be used by anyone. Pneumonia day, states that by the year 2030, 11 million children who are under the age of 5 year.


Introduction
7% of the population or 450 million people worldwide are suffering from Pneumonia which results in the death of 4 million people every year [1].
Pneumonia is an acute lower respiratory disease with traces of infection usually because of some micro-organism, bacteria or virus. World health organization (WHO) states that the pneumonia is the leading reason for the child dearth in the world. It had killed approximately 1.2 million children under the age of five. And south Asia is leading the table. South Asian countries like Indian Pakistan, Bangladesh, Sri Lanka, Indonesia etc. have the most no. children having pneumonia disease. Not only is this death rate of pneumonia greater than death rate of other dangerous disease like AIDS, malaria and tuberculosis combined [2]. Among children younger than five years, pneumonia is considered as one of the deadliest diseases [3]. Only India has encountered 158,176 deaths in the year 2016, and still India continue to show the significant no.in the infant deaths because of pneumonia. According to WHO report published on World would be consumed by this communicable disease [4]. In today's time the most effective Pneumonia detection method accepted by the doctors is radiograph. Although, some studies tell us that discrepancies are usual in the interpretation of the chest radiographs via radiologist. This drawback of human based observation has provided enough motivation to technology making interference in this field where machine will classify between a normal X-ray and abnormal Chest X-ray. This provides accuracy and IOP Publishing doi:10.1088/1757-899X/1022/1/012066 2 speed to the diagnosis process. [5] Our problem for this project is the "binary Classification" of the input chest X-ray images and then classifies them between the two classes of pneumonia or nonpneumonia. This project emphasizes upon the extensive use of neural networks in detection of pneumonia in chest X-rays.

Literature Review
After analyzing and reading various datasets available on various platforms and websites, pneumonia dataset was found to be best fit for performing on and making a model to detect it using image dataset of chest X-rays of patients [9]. World health organization (WHO) states that the pneumonia is the leading reason for the child dearth in the world. It had killed approximately 1.2 million children under the age of five. And south Asia is leading the table. South Asian countries like Indian Pakistan, Bangladesh, Sri Lanka, Indonesia etc. have the most no. children having pneumonia disease. Not only is this death rate of pneumonia greater than death rate of other dangerous disease like AIDS, malaria and tuberculosis combined [2]. Pneumonia is one of the gravest illnesses among children younger than 5 years of age [3]. This was motivation enough to work on this dataset and produce a model with accuracy good enough to successfully detect pneumonia by reading chest X-rays While examining for pneumonia in the patient's X-ray, the radiologist looks in it for spots, specifically white ones, within the lungs termed as "infiltrates" which are helpful in identifying the infection. Pneumonia chest x-ray can be observed in TB, severe case of bronchitis as well. Complete Blood Count (CBC), Chest Computed Tomography (CT) and sputum test etc. are further conducted to reach a conclusion about the infection. Therefore, in this attempt to solve the problem we have only tried to detect whether a chest x-ray conclude that a person is ill with pneumonia or normal patients and do so by searching for any cloudy pattern in the X-ray. Conclusive detection will therefore, depend on pathological tests only [16]. Today many diseases are detected using Artificial Intelligence based solution. Some of these diseases are breast cancer, brain tumor etc. based solutions [15]. This Artificial Intelligence based detection by Convolutional Neural Network have played a great part and shown great promises in classification and this AI based classification in trusted by doctors all over the world [17]. When we talk about low-cost imaging methods and easy use, Deep and machine learning methods are gaining popularity when it comes to examining chest X-rays. Also, the fact that there is ample of data available for training of various machine learning models. Among all the papers studied by us, the highest accuracy was obtained by [15] as 98%.
Thus, we chose CNN for operating on our dataset and took the help of this deep learning approach to obtain accuracy better than other models using other deep learning approaches.

Theory: CNN:
Convolutional Neural Network or CNN, due to its better than ever efficiency in Image classification, is becoming quite popular now-a-days. Features such as spatial and temporal in an image are easily extracted using CNN. Each layer has some specific weight as CNN use weight- sharing technique as it reduces the computation efforts [6][7] Architecture wise, Convolutional Neural Network are simply forward feed artificial neural Network (ANN) with only two limitations. To reduce the total number of parameters of the model, firstly the weights should be shared and secondly neurons which are in the same layers are connected to local paths only. There are three basic building blocks in CNN to preserve the spatial structure:  A convolution layer.  A max-pooling layer to reduce the Dimension of the map  A fully connected layer to classify the between the kinds [8]. Below in figure 2, the architectural explanation of CNN is given.
Normalization: Normalization has been a full of life research field of deep learning normalization is used to reduce the training time by a lot of factors, allow us to show a number of the advantages of using Normalization.  Sometimes some features have a very high value as compared to the feature surrounding it, so in this process we normalize each and every feature is maintained. By doing so, we're making our network unbiased.  Normalization decreases the internal covariate shift. Covariate shift is the change within the distribution of activation network .and because of the alteration in the network parameters during training of the model. To boost the training, we need to scale back the Internal Covariate Shift  As stated in point no. 1 normalization maintains the features but the sharp feature is lost because of normalization  Normalization paces up the Optimization as normalization do not allow weights to explode and it also restrict them to a specific range  Normalization also helps CNN in regularization which is one unintended benefit of it (only slightly, not significantly though) [10]. The normalization technique used in our model was Batch Normalization.
Batch Normalization: Batch normalization is one of the many methods that normalize activations in a network across the mini-batch of definite size. For each feature, batch normalization computes the mean and variance of that feature in the mini batch. It then subtracts the mean and divides the feature by its mini-batch standard deviation.
Max Pooling: Pooling is one the most common feature imbibed in the Convolutional neural Network. The main idea behind the pooling layer is to "accumulate of collect" feature of the normalized feature map created by convoluting a filter over the picture. The Primary job is to continually decrease the featured map's spatial size and decrease the number computation efforts of the network. Max pooling is the most used type of pooling. [11]

Data and Software (Modules and IDE) implemented
After analyzing and reading various datasets available on various platforms and websites, pneumonia the programming language that we are using is Python version 3.6. We have chosen to work in Juypter Notebook with various imported libraries. Dataset was obtained by Kaggle link is in the reference [10]. The X-ray images that we are using as a dataset were of size greater than 1000 pixels per dimension and the total dataset was tagged large and has a space of 1.2 GB. For cropping images, we wrote some algorithms in order to remove extra features of the images which useless to user and to the algorithm as well.

of case stored in train and test of pneumonia and non-pneumonia
Thus, we chose CNN for operating on our dataset and took the help of this deep learning approach to obtain accuracy better than other models using other deep learning approaches.

Methodology:
The methodology and approach we've used has been describes step by step ahead. The task has been performed as per the following step. We have pre-processed the data and all the X-ray images were cropped to the ideal dimensions for computational needs. Next, we uploaded the data in the memory. Then we created a callbacks function and model checkpoint. Next, we reshaped the data and created the model through CNN. And last we did model fitting and found the accuracy and value loss using confusion matrix. Pre-processing: In this we have resized the images for performing efficient operation on them. The images are originally 1000 pixels per dimension and they were resized to a compatible size for better computation. We also created a function in which pneumonia file in training dataset is given label = 0 and normal label = 1, else = 2. Two arrays, 'X' and 'Y' are created where 'X' stores pre-processed (resized image) data and 'Y' stores the label which is again stored back in the file we use the function 'resize' of OpenCV, and uploading the images in new h5py file whose main function is to store the data in binary format, means images of chest X-ray are converted to array of numbers as in figure 8.
Uploading data in memory: Now, as the data has been pre-processed, we considered uploading it in the memory. We create a function to do so, it loads the training dataset and also the test dataset along with their labels. Afterwards, the shape of test and train datasets is obtained and the X-rays are displayed of both, with pneumonia and without pneumonia using "matplotlib".

Figure 7: Sample chest X-ray
The above image of the chest X-ray above is changed in an array like given figure 8. Callbacks and model checkpoint: We have imported "ModelCheckpoint" from the "callbacks" library of "keras". We have done so as to reduce learning rate timely after monitoring a quantity. Model Checkpoint call back is utilised in addition with the training using model.fit() function to save some the model or weights (in checkpoint file) at some interval, model/weights are hence loaded afterwards to continue with the previously saved model making checkpoints which helps in timely check and save the best model performance till last and also avoid further validation accuracy drop due to over fitting.
Model fitting and finding the accuracy: Next and final step in the completion of the project is fitting the training data set in the model using .fit() function in which the arguments are X_train and y_train and callback function to reduce the learning rate and the value of epochs (in our project the value epochs is set to be 10). As now our model is trained and fit to go and final readings we got after fitting the dataset are:  val_accuracy is found to be 0.8410  val_loss is found to be 0.8395  Accuracy is found to be 0.9806  Loss is found to be 0.0742 (after 10 epochs)

Performance
Performance of a CNN model over the testing data set is calculate after the completion of the training of the model and the performance is calculated in the following six metrics: accuracy, precision (PPV), sensitivity or recall, F1 score, the area under the curve (AUC), and specificity. In the above equations we classify the chest X-rays in 4 major section: Here false negative are those X-rays which are normal x-rays and have been detected wrongly by the machine, false positive are those X-rays which are normal x-rays and have been detected correctly by the machine, True Negative are those cases which are pneumonia X-rays but have been detected as The value we got after the successful compilation of our model is given in the figure below: Here 0 means non-Pneumonia and 1 means Pneumonia.

Future Work
In the data set there are small boxes indicating the presence of pneumonia in future one can use these bounding boxes to train the CNN to not only classify image with pneumonia but also to identify where is pneumonia in a person's chest. One might explore the DenseNet, SqueezeNet, ResNet, AlexNet activations using Class Activation map to better understand its behaviour and other feature of the dataset. This might inform us why it achieves such high ACCURACY as compared to similar work.

Conclusion
Detection of diseases with the assistance of computers from various Machine and Deep learning techniques are very beneficial in such places where there is shortage of people who are skilled in techniques like radiology. Especially in south Asian countries and African countries where of 60% -70% people live in rural places. Such tool are very low cost and instrument requirement is low hence it very easy to deploy in rural areas. And, additionally these tools will be very helpful in automatically differentiating between who need urgent medical care and who can be made to wait. Out project successfully provides with a CNN based approach for detection of pneumonia automatically.