# Development of Pneumonia Disease Detection Model Based on Deep Learning Algorithm

Authors: Dalya S. Al-Dulaimi; Aseel Ghazi Mahmoud; Nadia Moqbel Hassan; Ahmed Alkhayyat; Sayf A. Majeed
Journal: Wireless Communications and Mobile Computing
 (2022)

---

## Abstract

Pneumonia represents a life-endangering and deadly disease that results from a viral or bacterial infection in the human lungs. The earlier pneumonia’s diagnosing is an essential aspect in the processes of successful treatment. Recently, the developed methods of deep learning that include several layers of processing to comprehend the stratified data representation have obtained the best results in various domains, especially in the identification and classification of human diseases. Therefore, for improving the systems’ performance for detecting pneumonia disease, there is a requirement for implementing automatic models based on deep learning models that have the ability to diagnose the images of chest X-rays and to facilitate the detection process of pneumonia novices and experts. A convolutional neural network (CNN) model is developed in this paper for detecting pneumonia via utilizing the images of chest X-rays. The proposed framework encompasses two main stages: the stage of image preprocessing and the stage of extracting features and image classification. The proposed CNN model provides high results of precision, recall, F1-score, and accuracy by 98%, 98%, 97%, and 99.82%, respectively. Regarding the obtained results, the proposed CNN model-based pneumonia detection has achieved a better result of consistency and accuracy, and it has outperformed the other pretrained deep learning models such as residual networks (ResNet 50) and VGG16. Furthermore, it exceeds the recently existing models presented in the literature. Thus, the significant performance of the proposed CNN model-based pneumonia detection in all measures of performance can provide effective services of patient care and decrease the rates of mortality.


---

## Body

## 1. Introduction

Pneumonia represents inflammation of lung parenchyma that can result from chemical and physical factors, immunologic injury, pathogenic microorganisms, and other improper pharmaceuticals [1]. Pneumonia can be categorized into noninfectious and infectious relying on various pathogeneses in which noninfectious pneumonia can be categorized into aspiration pneumonia and immune-related pneumonia, while infectious pneumonia can be categorized into the virus, bacteria, chlamydial, mycoplasmas, etc. [2]. The accelerated detection of pneumonia and the subsequent implementation of proper medicine can assist considerably to avoid patients’ condition deterioration that ultimately may lead to death [3].In the last decades, various technologies have appeared like genomics and imaging that offer a complicated and enormous amount of health care data [4]. Chest X-ray images represent the preferable technology in diagnosis pneumonia; however, these images are somewhat not obvious and sometimes misclassified to benign abnormalities or other diseases by the expert radiologists, which lead to giving the wrong medicine to the patients and consequently worsening the patients’ condition [5]. There is a requirement for an automatic and intelligent model to assist radiologists in diagnosing various kinds of pneumonia from the images of chest X-rays [6].Deep learning represents a subdomain of machine learning regarding algorithms motivated via the structure and function of the brain [7]. The recently developed algorithms of deep learning promote the quantification, identification, and classification of patterns within medical images. Deep learning algorithms are capable of learning features simply from data, rather than hand-designing features depending on field-specific knowledge. The recently utilized model of developed deep learning is the convolutional neural network (CNN). This model of layers works on processing images and coming up with extracting low levels of features (like edges) within images. The layers of CNN can successfully capture the temporal and spatial dependencies in images with the assistance of filters. In contrary to the layers of normal feed-forward, the CNN layers include considerably fewer parameters and utilized a technique of weight sharing, consequently, decreasing the effort of computation. Thus, this developed model helps medical practitioners in diagnosing and classifying specific medical conditions effectively [8].The main contribution of this work is to provide an effective deep learning framework for detecting pneumonia using the images of chest X-rays with a balanced performance in accuracy and complexity terms, furthermore providing medical and radiologist experts with a lower cost tool. The attended objectives are as follows:(i)
Firstly, applying CNN model to detect pneumonia from the images of chest X-rays as feature extraction and classification scheme(ii)
Secondly, investigating the performance of CNN and other deep learning models in classifying pneumonia(iii)
Finally, developing a model capable of detecting normal and abnormal (pneumonia) imagesThe residual of this paper is organized as follows; an examination of related works is stated in Section2. The proposed deep learning model for detecting pneumonia is explained in Section 3, and its efficiency is shown in Section 4. Finally, Conclusions of this paper are highlighted in Section 6.


## 2. Related Works

Ayan and Ünver [9] compared two CNN models (VGG16 and Xception) for diagnosing pneumonia. In these models, transfer learning and fine-tuning are utilized in the stage of training. The obtained results demonstrated that the Vgg16 model exceeds the Xception model in several metrics: accuracy, specificity, precision, and f-score, by 0.87%, 0.91%, 0.91%, and 0.90%, respectively, while the Xception model exceeds the VGG16 model in sensitivity metric by 0.85%. Furthermore, the Xception model is more effective than the other model in detecting cases of pneumonia, while the VGG16 model is more effective in detecting normal cases. But these models require to be ensemble (combine the strengths) for achieving more effective results in diagnosing pneumonia. Hashmi et al. [10] proposed a model for pneumonia detection in chest X-ray images. In this model, firstly, the dataset was augmented; then, predictions from the pretrained deep learning models (Xception, Inception V3, ResNet 18, MobileNetV3, and DenseNet121) were combined, by utilizing a weighted classifier for computing the final prediction. This proposed model outperforms the other individual models and achieves an accuracy of 98.43%. But the utilized models had highly complex constructions; therefore, it is necessary to provide a model in which the weights according to various models are estimated effectively. Jain et al. [11] presented six CNN models for classifying chest X-ray images into pneumonia and nonpneumonia. These models have differed in the number of utilized parameters, hyperparameters, and convolutional layers. The first and second models include three convolutional layers and provide 85.26% and 92.31% of accuracy, respectively, while the other models are pretrained models (VGG16, VGG19, Inception V3, and ResNet 50) which provide 87.28%, 88.46%, 70.99%, and 77.56% of accuracy, respectively. These presented models are focused on the recall metric as a performance evaluator for minimizing the number of false negatives. The best-obtained recall was achieved by the second model which was 98%. However, these models require to improve the accuracy of classification via fine-tuning each parameter and hyperparameter. Al Mamlook et al. [12] presented seven models for detecting and classifying pneumonia from the images of chest X-rays; these models are random forest, decision tree, K-nearest neighbor, adaptive boosting, gradient boost, XGBboost, and CNN. Particularly, all these models were compared using f-score and accuracy score. The CNN model exceeds the other machine learning models with a small margin, and the obtained score of accuracy was 98.46%. Surprisingly, the random forest model achieved well and the score of accuracy was 97.61%. However, these presented models need to construct a large database for training and testing to provide better results. Wu et al. [13] proposed an improved median filtering based on the CNN model using a random forest algorithm. In this model, the adaptive median filter was firstly utilized for removing noises within the images of chest X-ray to be more easily recognizable; then, the architecture of CNN was established depend on dropout for extracting the features of deep activation from each image. Finally, a random forest dependent on GridSearchCV class was employed as a classifier for the features of deep activation in the CNN model. This proposed model not only works on avoiding the overfitting phenomenon in data training, however improving the image classification accuracy as well. The achieved score of accuracy was 96.9% for 64×64×3 image size and 93.8% for 224×224×3 image size. Regarding the achieved accuracy, the achieved scores for precision, recall, and F1-score were 90%, 95%, and 97.7%, respectively. But this model requires improving the CNN model’s performance to be more efficient with no further preprocessing work. Chouhan et al. [14] presented a deep learning approach to detect pneumonia based on transfer learning. In this framework, firstly, the chest X-ray images were resized to 224×224×3; then, the augmentation techniques (random horizontal flip, random resized crop, and a varying intensity) were utilized. After that, the features were extracted from images via utilizing several pretrained models on the dataset (AlexNet, Inception V3, DenseNet121, ResNet 18, and GoogLeNet) to be passed to the classifier for prediction. Finally, an ensemble model was employed that utilized the pretrained models and exceeded individual models. The obtained scores of recall and accuracy were 99.62% and 96.4%, respectively. But the performance of this framework requires further improvement either by increasing the size of the dataset or utilizing hand-crafted features. Zhang et al. [15] presented a model based on CNN for diagnosing pneumonia. In this model, the technique of dynamic histogram equalization was firstly utilized to improve the contrast of chest X-ray images; then, the VGG-based CNN model was designed for features extraction and classification. This presented model can classify abnormal and normal chest X-ray images with a 94.41% precision rate and 96.07% accuracy rate. But this model requires exploring a more accurate architecture of classification for diagnosing pneumonia. Manickam et al. [16] preprocessed the input images of chest X-rays for identifying the existence of pneumonia via utilizing the architecture of U-Net based segmentation and classified pneumonia as abnormal and normal via utilizing pretrained models (Inception V3, ResNet 50, and Inception-ResNet V2). The ResNet 50 model exceeds the other models with 96.78% recall, 88.97% precision, 92.71% F1-score, and 93.06% accuracy.Most of these related works are based on the utilization of a pretrained structure of neural network or finding a specific structure with a specific number of layers. When the number of layers is increased besides the complexity of the features to be detected via the neural network for achieving the required task, the complexity of computations will be increased with no implying any accuracy improvement. Furthermore, the utilization of a few layers can decrease the neural network’s accuracy, since the features at the needed level of complexity are not detected. Therefore, the main aim of this work is to implement an effective deep CNN model on a publicly accessible dataset for detecting pneumonia by achieving a balanced performance in accuracy and complexity terms.


## 3. Proposed Methodology

The proposed deep learning framework has been constructed and trained many times with various parameters for choosing the preferable hyperparameters and providing a balanced-performing architecture. Generally, it encompasses two main stages. The first stage involves several image preprocesses; firstly, the process of image resizing which is implemented to obtain224∗224∗3 image size and, secondly, rescaling the value of the image’s pixel to [0,1] interval, while the second stage represents extracting features and image classification utilizing the proposed CNN models.The proposed CNN model is applied to detect pneumonia from the images of chest X-rays as feature extraction and classification scheme. Figure1 shows the general structure of the proposed model for detecting pneumonia disease. This structure includes three main parts. The input layer in the CNN model represents the first part (layer) of the CNN structure that passes the input chest X-ray image of size 224∗224∗3 to the next parts.Figure 1
The proposed deep learning framework for detecting pneumonia disease.The feature extraction represents the second part of the CNN structure which includes three blocks, and each block includes the convolution layer, maximum pooling layer, and dropout layer. In the convolutional layer, the input images are converted into matrix forms. The operation convolution is implemented within the input matrix and a feature kernel of3×3 dimension, and the outcome represents a feature map. This operation works on reducing the image dimensions to make it easier to be processed.In order to achieve better performance, all the convolutional layers are followed by the Rectified Linear Unit (ReLU). ReLU is the widely utilized activation function that thresholds the inputs (converts the input to 0 if it is valued less than 0) and creates a nonlinear output.After that, the max pooling layer is utilized for recognizing the prominent features within the image and reducing the image’s dimensions and the parameters, consequently decreasing the computational complexity. The max pooling layer with a2×2 dimension operates on every feature map and scales its dimensions by utilizing the function “MA” which works on selecting the highest value of a pixel from the image window. The dropout is added to the max pooling layers for preventing overfitting.The output of the second part is subsequently passed to the third part which is the classification part. This part includes several layers; firstly, the flattened layer that is utilized for changing the shape of the data to a one-dimensional vector, secondly, three dense layers each of which is followed by the dropout layer and, finally, the dense layer of sigmoid activation function that works on classifying output image into normal or abnormal. The number of parameters that are utilized in the CNN model for detecting pneumonia disease is demonstrated in Table1.Table 1
The parameters of the CNN model layers.
Layers (types)Shape_outputParameterslayer1 (convolutional 2D)None, “224, 224, 64”1792layer2 (convolutional 2D)None, “224, 224, 64”36928layer3 (max pooling 2D)None, “112, 112, 64”0(Dropout)None, “112, 112, 64”0layer4 (convolutional 2D)None, “112, 112, 128”73856layer5 (convolutional 2D)None, “112, 112, 128”147584layer6 (max pooling 2D)None, “56, 56, 128”0(Dropout)None, “56, 56, 128”0layer7 (convolutional 2D)None, “56, 56, 256”295168layer8 (convolutional 2D)None, “56, 56, 256”590080layer9 (max pooling 2D)None, “28, 28, 256”0(Dropout)None, “28, 28, 256”0(Flatten)None, “200704”0layer10 (dense)None, “512”102760960(Dropout)None, “512”0layer11 (dense)None, “128”65664(Dropout)None, “128”0layer12 (dense)None, “64”8256(Dropout)None, “64”0(Dense)None, “2”130Total of parameters: 103,980,418Trainable parameters: 103,980,418Nontrainable parameters: 0As demonstrated in Table1, the proposed CNN model encompasses twelve layers (six convolutional layers, three max pooling, and three dense layers), and the number of trainable parameters was 103,980,418.


## 4. Computational Experiments

Python programming language was utilized in the implementation of the proposed deep CNN, ResNet 50, and VGG16 models. Google Colab was utilized for GPU runtime in the stages of training and validation. Each model was fine-tuned for 100 epochs, with 128 batch-size. The NAdam optimizer was utilized for optimizing the function of learning with 0.001 learning rate.Regarding the implemented pretrained CNN models, VGG16 included sixteen layers (thirteen-convolution and three-dense), and the ResNet 50 included fifty layers (forty-eight-convolution, one-max pooling, and one-average-pooling). VGG16 had approximately 134 million trainable parameters, and ResNet 50 had approximately 23 million trainable parameters. The same image preprocessing steps are utilized in the implemented pretrained models, while the feature extraction and classification steps follow the structure of each model.


### 4.1. Dataset Description

The three deep learning models for pneumonia detection are trained and tested on the images of the chest X-ray dataset [17], which includes 5880 samples, 80% (4707 samples) were utilized for training these models, and 20% (1173 samples, 855 abnormal, and 318 normal) were utilized for testing. Figure 2 manifests examples of chest X-ray images in which abnormal-viral pneumonia demonstrates an interstitial pattern in the lungs and abnormal-bacterial pneumonia demonstrates the consolidation of focal lobar, while the normal image demonstrates pure lungs without any abnormal opacification areas in the chest X-ray.Figure 2
Examples of chest X-ray images.
(a)
Abnormal-viral pneumonia(b)
Abnormal-bacterial pneumonia(c)
Normal


### 4.2. Evaluation Metrics

The utilized performance metrics for identifying the best model are precision, recall, F1-score, and accuracy. In order to compute these metrics, True Negative “Tneg,” True Positive “Tpos,” False Negative “Fneg,” and False Positive “Fpos” are provided. The lower the TnegandTpos, the fewer classifiers’ performance that capable of detecting normal and abnormal (pneumonia) images. The higher the FposandFneg, the higher classifiers mistakes that misclassify pneumonia images as normal images and conversely. The specificity metric indicates the ability of classifiers to recognize the normal images; it is assigned by TnegandFpos as in
(1)Specificity=TnegTneg+Fpos.The precision metric works on measuring the real abnormal (pneumonia) image percentage from all predicted abnormal images, as in(2)Precision=TposTpos+Fpos.The recall metric (sensitivity) is a properly classified class from the model of classification, and the sensitivity with a high value will make the model more reliable and robust. This metric is associated withTpos, as in
(3)Recall=TposTpos+Fneg.The accuracy metric is utilized for measuring the classification models’ performance, and in other words, it represents the total classifier performance that is measured by(4)Accuracy=Tneg+TposTneg+Fpos+Tpos+Fneg.And finally, F1-score indicates the classifiers’ ability of classification via utilizing the combination of precision and recall metrics as a single evaluation metric of performance, as in(5)F1−score=2Precision×AccuracyPrecision+Accuracy.


## 4.1. Dataset Description

The three deep learning models for pneumonia detection are trained and tested on the images of the chest X-ray dataset [17], which includes 5880 samples, 80% (4707 samples) were utilized for training these models, and 20% (1173 samples, 855 abnormal, and 318 normal) were utilized for testing. Figure 2 manifests examples of chest X-ray images in which abnormal-viral pneumonia demonstrates an interstitial pattern in the lungs and abnormal-bacterial pneumonia demonstrates the consolidation of focal lobar, while the normal image demonstrates pure lungs without any abnormal opacification areas in the chest X-ray.Figure 2
Examples of chest X-ray images.
(a)
Abnormal-viral pneumonia(b)
Abnormal-bacterial pneumonia(c)
Normal


## 4.2. Evaluation Metrics

The utilized performance metrics for identifying the best model are precision, recall, F1-score, and accuracy. In order to compute these metrics, True Negative “Tneg,” True Positive “Tpos,” False Negative “Fneg,” and False Positive “Fpos” are provided. The lower the TnegandTpos, the fewer classifiers’ performance that capable of detecting normal and abnormal (pneumonia) images. The higher the FposandFneg, the higher classifiers mistakes that misclassify pneumonia images as normal images and conversely. The specificity metric indicates the ability of classifiers to recognize the normal images; it is assigned by TnegandFpos as in
(1)Specificity=TnegTneg+Fpos.The precision metric works on measuring the real abnormal (pneumonia) image percentage from all predicted abnormal images, as in(2)Precision=TposTpos+Fpos.The recall metric (sensitivity) is a properly classified class from the model of classification, and the sensitivity with a high value will make the model more reliable and robust. This metric is associated withTpos, as in
(3)Recall=TposTpos+Fneg.The accuracy metric is utilized for measuring the classification models’ performance, and in other words, it represents the total classifier performance that is measured by(4)Accuracy=Tneg+TposTneg+Fpos+Tpos+Fneg.And finally, F1-score indicates the classifiers’ ability of classification via utilizing the combination of precision and recall metrics as a single evaluation metric of performance, as in(5)F1−score=2Precision×AccuracyPrecision+Accuracy.


## 5. Results and Discussion

Tables2–4 show the obtained results of the utilized metrics (precision, recall, F1-score, and accuracy) for the proposed CNN model, ResNet 50, and VGG16 models, respectively.Table 2
The obtained metrics of precision, recall, F1-score, and accuracy for the proposed CNN model.
Pneumonia classesPrecisionRecallF1-scoreNo. of tested images“Abnormal”0.980.960.99855“Normal”0.980.970.98318Accuracy——0.991173Macroaverage0.990.980.971173Weighted average0.980.980.971173As indicated in Table2, the CNN model has the best results of precision, recall, F1-score, and accuracy by 98%, 98%, 97%, and 99.82%, respectively.The obtained results of precision, recall, F1-score, and accuracy for the ResNet 50 model (indicated in Table3) were 95%, 95%, 95%, and 95.37%, respectively.Table 3
The obtained metrics of precision, recall, F1-score, and accuracy for ResNet 50 model.
Pneumonia classesPrecisionRecallF1-scoreNo. of tested images“Abnormal”0.950.980.97855“Normal”0.940.860.90318Accuracy——0.951173Macroaverage0.950.920.931173Weighted average0.950.950.951173The obtained results of precision, recall, F1-score, and accuracy for the VGG16 model (indicated in Table4) were 73%, 73%, 71%, and 73.40%, respectively.Table 4
The obtained metrics of precision, recall, F1-score, and accuracy for VGG16 model.
Pneumonia classesPrecisionRecallF1-scoreNo. of tested images“Abnormal”0.880.730.80855“Normal”0.500.740.62318Accuracy0.731173Macroaverage0.740.730.731173Weighted average0.730.730.711173The confusion matrix offers a perception of the error being obtained via the utilized classifiers. It is utilized for describing the classification performance on the test images for the known true values. Figure3 demonstrates the confusion matrices for the proposed CNN, ResNet 50, and VGG16 models.Figure 3
Confusion matrices: (a) proposed CNN model, (b) ResNet 50 model, and (c) VGG16 model.
(a)(b)(c)The experiments were achieved with 100 epochs, 128 batch size, and “Nadam” optimizer. The accuracy per epoch and loss per epoch for the proposed CNN, ResNet 50, and VGG16 models are illustrated in Figures4–6, respectively.Figure 4
CNN-based pneumonia disease recognition: (a) accuracy per epoch and (b) loss per epoch.
(a)(b)Figure 5
ResNet 50-based pneumonia disease recognition: (a) accuracy per epoch and (b) loss per epoch.
(a)(b)Figure 6
VGG16-based pneumonia disease recognition: (a) accuracy per epoch and (b) loss per epoch.
(a)(b)As demonstrated in Figure4, the obtained curves of training and validation accuracy and training and validation loss for the proposed CNN model with 100 epochs were 99.82%, 96.53%, 0.0110, and 0.2005, respectively.The proposed model has been evaluated by comparing it with the ResNet 50 and VGG16 models and recently presented models using the chest X-ray images as shown in Table5.Table 5
A comparison between the proposed model and other models.
Ref.Learning modelsPrecisionRecallF1-scoreAccuracy[9]Xception86%85%87%82%VGG1691%82%90%87%[10]Weighted classifier based pretrained models——98.63%98.43%[11]Inception V386%84%78%70.99%ResNet 5092%97%84%77.56%VGG1693%96%90%87.18%VGG1994%95%91%88.46%[12]CNN——98.95%98.46%[13]CNN-random forest90%95%97%93.8%[14]Ensemble model93.28%99.62%94.8%96.39%[15]VGG-based CNN model94.41%——96.07%[16]ResNet 5088.97%96.78%92.71%93.06%—ResNet 5095%95%95%95.37—VGG1673%73%71%73.40%—Proposed CNN model98%98%97%99.82%As demonstrated in Table5, the proposed CNN model-based pneumonia detection has achieved a better result of consistency and accuracy, outperforms the ResNet 50 and VGG16 models, and exceeds the other recently existing models presented in the literature.


## 6. Conclusion

A deep learning model is proposed in this paper for pneumonia disease detection from the images of chest X-rays. The number of layers is not constantly lead to improve the accuracy, and increasing the networks’ layers may yield negative performance. Throughout the construction of the CNN model, an indisputable number of layers was attained that provided the best accuracy.The obtained results demonstrated that the proposed CNN model-based pneumonia detection has the best high results of precision, recall, F1-score, and accuracy by 98%, 98%, 97%, and 99.82%, respectively, and this leads to the ability to utilize this model rather than other implemented models as a supplement for radiologists in the process of decision-making. The achieved results support the concept that deep learning models are capable of simplifying the process of diagnosis and improving the management of pneumonia disease and thus leading to improve treatment quality. Furthermore, the proposed CNN model exceeds the other recently existing models presented in the literature. This proposed model can be effectively implemented in diagnosing pneumonia disease and other diseases like COVID-19.



---

*Source: 2951168-2022-06-06.xml*