Diagnosing Pneumonia Using CNN
Diagnosing Pneumonia Using CNN
by
SATHYABAMA
INSTITUTE OF SCIENCE AND TECHNOLOGY
(DEEMED TO BE UNIVERSITY)
Accredited with Grade “A” by NAAC
JEPPIAAR NAGAR, RAJIV GANDHI SALAI
CHENNAI - 600 119
APRIL – 2022
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
BONAFIDE CERTIFICATE
This is to certify that this Project Report is the bonafide work of CHANDAVARAPU
SAI SARAN(38110096),PURAM AJITH(38110434) who carried out the project
entitled ‖DIAGNOSING PNEUMONIA USING CNN‖ under my supervision
from NOV 2021 to APRIL 2022.
Internal Guide
Mrs.N.S.Usha,B.E.,M.E.,(Ph.D)
I C.SAI SARAN (Reg No:38110096) and P. AJITH(Reg No: 38110434) hereby declare
that the Project Report entitled ―DIAGNOSING PNEUMONIA USING CNN‖ done by us under
the guidance of Mrs. N.S. USHA is submitted in partial fulfillment of the requirements for
the award of Bachelor of Engineering degree in 2018-2022.
DATE:
3
ACKNOWLEDGEMENT
I would like to express my sincere and a deep sense of gratitude to my Project Guide
Mrs.N.S.Usha,B.E,M.E,(Ph.D),CSE for her valuable guidance, suggestions and
constant encouragement paved way for the successful completion of my project
work.
I wish to express my thanks to all Teaching and Non-teaching staff members of the
Department of Computer Science and Engineering who were helpful in many
ways for the completion of the project.
4
ABSTRACT
Pneumonia is one of the serious diseases that is caused by a bacterial or viral infection
of the lungs and has the potential to result in severe consequences within a short
period. Therefore, early diagnosis is a key factor in terms of the successful treatment
process. Thus, there is a need for an intelligent and automatic system that has the
capability of diagnosing chest X-rays and to simplify the pneumonia detection process
for experts as well as for novices. This study aims to develop a model that will help with
the classification of chest x-ray medical images into normal(healthy) vs abnormal(sick).
To achieve this, seven existing state-of-the-art machine learning techniques and well-
known convolutional neural network models have been employed to increase efficiency
and accuracy. In this study, we propose our machine learning for the classification task,
which is trained with modified images, through multiple steps of preprocessing.
Experimentally, it demonstrated that the machine learning technique for the
classification task performs the best, compared to the other seven machine learning
techniques. In this study, we successfully classified chest infection in chest predict
using on CNN with an overall accuracy of 98.46%. It achieved a more successful result
in detecting pneumonia cases. Machine learning models. In this study, we propose a
method for image-based diagnosis for Pneumonia leveraging deep learning techniques
and interpretability of explanation models such as Local Interpretable Model-agnostic
Explanations and Saliency maps. We experiment on a variety of sizes and
Convolutional neural network architecture to evaluate the efficiency of the proposed
method on the set of Chest x-ray images. The work is expected to provide an approach
to distinguish between healthy individuals and patients who are affected by Pneumonia
as well as differentiate between viral Pneumonia and bacteria Pneumonia by providing
signals supporting image-based disease diagnosis approaches. . This study aims to
develop a model that will help with the classification of chest x-ray medical images into
normal(healthy) vs abnormal(sick). To achieve this, seven existing state-of-the-art
machine learning techniques and well-known convolutional neural network models have
been employed to increase efficiency and accuracy. In this study, we propose our deep
learning for the classification task, which is trained with modified images, through
multiple steps of preprocessing. Experimentally, it demonstrated that the deep learning
technique for the classification task performs the best, compared to the other seven
5
machine learning techniques. In this study, we successfully classified chest infection in
chest Xray images using deep leaning based on CNN with an overall accuracy of
98.46%. It achieved a more successful result in detecting pneumonia cases.
6
TABLE OF CONTENT
7
4.6.4 PADDING 30
4.6.5 POOLING LAYER 30
4.6.6 MAX POOLING 30
4.6.7 AVERAGE POOLING 31
4.6.8 FULLY CONNECTED LAYER 31
5 RESULTS & DISCUSSION 32
6 SUMMARY & CONCLUSION 33
A.REFERENCES 34
B.OUTPUT SCREENSHOTS 36
8
LIST OF FIGURES
9
CHAPTER 1
INTRODUCTION
10
certain influence on image processing in general and medicine in particular. The
diagnosis process performs with Machine Learning or Deep Learning can help
physicians investigate the medical images conveniently and reduce the analysis time.
Several studies have resolved the challenging tasks such as medical image
classification , skin cancer detection using images , or 3D image biomedical
segmentation.
Moreover, Deep Learning-based technologies have successfully demonstrated in
clinical practice including clinical decision support systems (CDSS), diagnosis
prediction, and predicting the invasiveness of lung adenocarcinoma manifesting based
on radionics and clinical features. Though, there are still several challenges with
Machine Learning. Selecting a dataset, creating a predictive model, and evaluating and
refining the model, the most important thing is data. The implementations of Machine
Learning or Deep Learning in health care are influenced by the accuracy of medical
data. Specifically, the annotation progress in the medical image is based on medical
professional knowledge, medical industry standard, and medical system Pneumonia is
one of the serious diseases which cause most of the deaths in adults globally.
According to Health Metrics and Evaluation (IHME), the highest pneumonia mortality
rates in 2017 were among people aged 70 and older. More than 1.13 million
pneumonia-related deaths are reported every year were in this age group . Pneumonia
is a disease of infectious origin that causes inflammation in the air sacs or alveoli of one
or both lungs The air sacs get filled with fluid which can cause difficulty in breathing
and an over-generation of mucus and sputum as shown in . Pneumonia is most
commonly caused by viruses or bacteria and, less commonly, other microorganisms].
Nowadays, chest X-ray (CXR) imaging is used commonly for health intensive care and
analysis of many lung diseases such as pneumonia, cancer because of relatively low
prices. Therefore, Chest X-rays are currently the best available tool for diagnosing
pneumonia which has played a huge role in clinical care and epidemiological research
However, detecting pneumonia in chest X-rays is a challenging task that relies on the
availability of expert radiologists. Accurate image analysis and image interpretation are
very crucial for a better diagnosis. Though image interpretation by conventional
machine learning algorithms depends mostly on expertly crafted features, computer
vision is a very effective machine learning application. However, not all doctors have
good diagnosing tools to diagnose patients. As a result, sometimes their diagnosing is
not very accurate. It is also much more difficult to judge pneumonia just by looking at
11
the chest X-rays images.
Therefore, more specific diagnostic tools must be developed which are cheap
and accurate in diagnosing pneumonia. With the advancements of information
technology, machine learning algorithms have been successfully applied to many
healthcare problems and have explained complex relationships, and improved clinical
predictions In recent years, several researchers have proposed different artificial
intelligence (AI)- based solutions for different medical problems. techniques based on
Convolutional neural networks (CNNs) have allowed researchers to obtain successful
results in wide medical applications, including classifying skin cancer from skin
photographs, detecting metastasis on pathological images and disease classification in
X-ray images all of which demonstrated expert-level diagnostic accuracy. As a result of
this trend, numerous studies have emerged that analyzes the influence of deep learning
and convolutional neural networks in the field of health and imaging medical diagnosis.
Specifi- cally, due to successes in other fields especially in medical problems, deep
learning algorithms such as Convolutional neural networks (CNNs) have recently been
applied for image classification tasks. However, algorithms in those studies have not yet
beenfully validated in various data sets, limiting the generalization of the results.
Classification methods are among the most commonly used techniques in medical
imaging, where the goal is building classifiers that are capable of predicting whether x-
ray images are normal or show the presence of Pneumonia. Especially, this study tries
to answer the following research question: Could machine learning algorithms be used
to assist in the diagnosis of whether a patient has pneumonia or not by looking at chest
X-ray images? This inspires us to use machine learning algorithms to predict
pneumonia based on the x-ray images. Moreover, it would provide doctors with
immediate information about the patient‘s condition and risk level to recommend more
diagnostic tests without delay. Thus, the investigative and predictive methods, such as
machine learning algorithms, are vital to make smart decisions that will help doctors
and radiologists to get more information to prevent themselves from misdiagnosing a
patient. The primary objective of this study was to examine and evaluate the
performance of machine learning techniques and convolutional neural network (CNN)
for the classification of pneumonia, which is based on Chest X-ray Images to achieve
high accuracy. Other objective is to provide radiologists and medical experts with a tool
which is a lower cost .This tools will Be helping radiologists and medical experts to
identify the slit and slow changes among.
12
13
CHAPTER 2
Fawole et. al and ResNet-50 have concentrated inside and out X-X imaging and
changed undetectable articles like patient age, vision, and perceivability. also the vector
related with the picture. Up to 15 percent of beneficial things require one look.
.Ruuskanen O, et. al The report incorporates 247 radiograms from the Japan Society for
Radiology (JSRT). The first picture was diminished to 96 96 and the Log channel was
utilized prior to preparing or testing. The presentation of the inside and out preparing
technique is surveyed in light of the ID of the knobs on the radiograph, and the
outcomes are analyzed.
Juan G.; et. al. Recent work has shown that a round network is profound, finished, and
proficient in preparing assuming it contains a short circle between the inbound and
outbound lines. In this article, we will investigate the DenseNet network that associates
every classification to another level. Conventional L-broken pipes have a L
association between each stage and the following stage, while our organization has a
straight line (L + 1)/2. For each stage, the guide of the multitude of past areas is utilized
as an info, and the information picture is utilized as an info. DenseNets has various
14
significant benefits: it works with the absence of slope issues, fortifies execution
conveyance, upholds execution improvement, and altogether lessens execution. We
are assessing our development for four capacities (CIFAR- 10, CIFAR-100, SVHN, and
ImageNet) to make the data more exact. DenseNets requires a ton of memory and
figuring to accomplish superior execution while accomplishing extraordinary turn of
events and exceptional innovation across numerous gadgets.
Jilani,. et. al Magnetic reverberation imaging (MRI) will in general recognize a wide
scope of articles from various points. Now and again, the outcomes can be mistaken for
pathology. What's more, present day MR recuperation calculations have a repetitive
nature that builds recuperation time. As of late, a top to bottom review technique has
been utilized in MRI reproduction to give excellent rapid imaging. Inside and out study is
generally subject to learning data, so the nature of learning ought not be neglected. This
article talks about what noisy pictures in media practice mean for the construction of MR
reproduction. The normal technique utilizes a weighty misfortune to change the capacity
of the line to make the picture more appealing. This page utilizes a similar blunder free
stretch; however, this technique can be reached out to other misfortune making
exercises. Utilizing fixed heart MRI, this technique is contrasted with the current
strategy for eliminating clamor delicate exercise data or overlooking the positive
distinction. Indeed, even essential systems will work on the nature of profound gaining
from this methodology.
Hlemper et. al First of all, we expect that the normal technique for reproducing each 2D
picture autonomously is superior to the cutting edge strategy for disentangling the 2D
picture, in particular by concentrating on the jargon in view of the MR picture of the
design, by mistake and blunder .. Second, When we reproduce a progression of edges
together, CNN shows that we can actually learn space-time designs by organizing ends
and trading data. We show that the arranged technique is more present day than the
current one and that the physical construction can be dependably kept up with for as
15
long as 11 days. Also, the recuperation is extremely quick: each full casing can be
reproduced in under 10 seconds, and in 2-D, each picture can be reproduced in ms 23,
which helps continuously use.
16
CHAPTER 3
17
CHAPTER 4
Data Processing. In 2018, the Radiological Society of North America (RSNA) [b20]
released a dataset on the detection and localization of pneumonia in chest X-rays .
In the Det Net network, the first four phases of the backbone are consistent with the
original ResNet50 phase,
To enhance the pictorial information output of the Det Net network, this paper
combines FPN and Det Net to enhance the feature-extraction mode of the network.
4.1.1Disadvantages
We introduce in detail our proposed Deep Conv-Dilated Net method, including the
data processing, the architecture of our network, and the effective enhancement
effect of Soft-NMS.
The Chest X-Ray images which include normal chest X-Ray and Pneumonia chest
X-Ray. Here, we have taken 5,856 sample images of the dataset to use later on for
18
recognition and classification of pneumonia.1,583 of which are normal X-Ray lung
images, and 4,273 of which are X-Ray lung images with Pneumonia from patients.
The most important part of machine learning is data and data must be clean for
models to process it. When it comes to image data, there are some preparation
methods. Various types of pre-processing tasks such as dimension reduction,
image resize, and image cropping is applied.
Then it will process CNN algorithm to identify the pneumonia.
4.2.1Advantage
19
The process flow carried out by the proposed model is given in Figure 4.2
4.4 MODULES
1. Gathering data
2. Data pre-processing
3. Researching the model that will be best for the type of data
5. Evaluation
The process of gathering data depends on the type of project we desire to make,
if we want to make an ML project that uses real-time data, then we can build an IoT
system that using different sensors data. The data set can be collected from various
sources such as a file, database, sensor and many other such sources but the collected
20
data cannot be used directly for performing the analysis process as there might be a lot
of missing data, extremely large values, unorganized text data or noisy data. Therefore,
to solve this problem Data Preparation is done. Some free data sets which are present
on the internet are Kaggle and UCI Machine learning Repository , the repositories that
are used the most for making Machine learning models. Kaggle is one of the most visited
websites that is used for practicing machine learning algorithms, they also host
competitions in which people can participate and get to test their knowledge of machine
learning.
Data pre-processing is one of the most important steps in machine learning. It is the
most important step that helps in building machine learning models more accurately. In
machine learning, there is an 80/20 rule. Every data scientist should spend 80% time for
data per-processing and 20% time to actually perform the analysis.
Data Preprocessing
Data pre-processing is a process of cleaning the raw data i.e. the data is collected
in the real world and is converted to a clean data set. In other words, whenever the data
is gathered from different sources it is collected in a raw format and it cannot be
analysed. Therefore, certain steps are executed to convert the data into a small clean
data set, this part of the process is called as data pre-processing.
As we know that data pre-processing is a process of cleaning the raw data into
clean data, so that can be used to train the model. So, we definitely need data pre-
processing to achieve good results from the applied model in machine learning and deep
learning projects. Most of the real-world data is messy, some of these types of data are:
1. Missing data: Missing data can be found when it is not continuously created or due to
technical issues in the application (IOT system).
21
2. Noisy data: This type of data is also called outliners, this can occur due to human
errors (human manually gathering the data) or some technical problem of the device at
the time of collection of data.
3. Inconsistent data: This type of data might be collected due to human errors
(mistakes with the name or values) or duplication of data.
These are some of the basic pre — processing techniques that can be used to convert
raw data.
1. Conversion of data: As we know that Machine Learning models can only handle
numeric features, hence categorical and ordinal data must be somehow converted into
numeric features.
2. Ignoring the missing values: Whenever we encounter missing data in the data set
then we can remove the row or column of data depending on our need. This method is
known to be efficient but it shouldn‘t be performed if there are a lot of missing values in
the dataset.
3. Filling the missing values: Whenever we encounter missing data in the data set
then we can fill the missing data manually, most commonly the mean, median or highest
frequency value is used.
4. Machine learning: If we have some missing data then we can predict what data shall
be present at the empty position by using the existing data.
22
5. Outliers detection: There are some error data that might be present in our data set
that deviates drastically from other observations in a data set. [Example: human weight =
800 Kg; due to mistyping of extra 0]
4.4.3 Researching the model that will be best for the type of data
Our main goal is to train the best performing model possible, using the pre-processed
data.
Classification
Classification problem is when the target variable is categorical (i.e. the output
could be classified into classes — it belongs to either Class A or B or something else).
A classification problem is when the output variable is a category, such as ―red‖ or ―blue‖
, ―disease‖ or ―no disease‖ or ―spam‖ or ―not spam‖.
23
Fig 4.4 Classification of data given in dataset
As shown in the above representation, we have 2 classes which are plotted on the graph
i.e. red and blue which can be represented as ‗setosa flower‘ and ‗versicolor flower‘, we
can image the X-axis as ther ‗Sepal Width‘ and the Y-axis as the ‗Sepal Length‘, so we
try to create the best fit line that separates both classes of flowers.
K-Nearest Neighbor
Naive Bayes
Decision Trees/Random Forest
Support Vector Machine
Logistic Regression
Regression
While a Regression problem is when the target variable is continuous (i.e. the output is
numeric).
24
Fig 4.5 Comparison of gradient search
As shown in the above representation, we can imagine that the graph‘s X-axis is the
‗Test scores‘ and the Y-axis represents ‗IQ‘. So we try to create the best fit line in the
given graph so that we can use that line to predict any approximate IQ that isn‘t present
in the given data.
Linear Regression
Support Vector Regression
Decision Tress/Random Forest
Gaussian Progresses Regression
Ensemble Methods
Unsupervised Learning
The unsupervised learning is categorized into 2 other categories which are “Clustering”
and “Association”.
25
Clustering
A set of inputs is to be divided into groups. Unlike in classification, the groups are not
known beforehand, making this typically an unsupervised task.
Gaussian mixtures
K-Means Clustering
Boosting
Hierarchical Clustering
K-Means Clustering
Spectral Clustering
26
Overview of models under categories
For training a model we initially split the model into 3 three sections which are ‗Training
data‟ ,„Validation data‟ and „Testing data‟. train the classifier using „training data set‟,
tune the parameters using ‗validation set‟ and then test the performance of your classifier
on unseen ‗test data set‟. An important point to note is that during training the classifier
only the training and/or validation set is available. The test data set must not be used
during training the classifier. The test set will only be available during testing the
classifier.
27
Training set
The training set is the material through which the computer learns how to process
information. Machine learning uses algorithms to perform the training part. A set of data
used for learning, that is to fit the parameters of the classifier.
Validation set
Once the data is divided into the 3 given segments we can start the training process.
In a data set, a training set is implemented to build up a model, while a test (or
validation) set is to validate the model built. Data points in the training set are excluded
from the test (validation) set. Usually, a data set is divided into a training set, a validation
set (some people use ‗test set‘ instead) in each iteration, or divided into a training set, a
validation set and a test set in each iteration.
28
The model uses any one of the models that we had chosen in step 3/ point 3. Once the
model is trained we can use the same trained model to predict using the testing data i.e.
the unseen data. Once this is done we can develop a confusion matrix, this tells us how
well our model is trained. A confusion matrix has 4 parameters, which are ‗True
positives‟, „True Negatives‟, „False Positives‟ and ‗False Negative‟. We prefer that
we get more values in the True negatives and true positives to get a more accurate
model. The size of the Confusion matrix completely depends upon the number of
classes.
Table 4.1 Prediction table
True positives : These are cases in which we predicted TRUE and our predicted
output is correct.
False positives : We predicted TRUE, but the actual predicted output is FALSE.
False negatives : We predicted FALSE, but the actual predicted output is TRUE.
We can also find out the accuracy of the model using the confusion matrix.
29
Accuracy = (100 + 50) / 165 = 0.9090 (90.9% accuracy)
4.4.5 Evaluation
To improve the model we might tune the hyper-parameters of the model and try to
improve the accuracy and also looking at the confusion matrix to try to increase the
number of true positives and true negatives.
CNN takes an image as input, which is classified and process under a certain category
such as dog, cat, lion, tiger, etc. The computer sees an image as an array of pixels and
depends on the resolution of the image. Based on image resolution, it will see as h * w *
d, where h= height w= width and d= dimension. For example, An RGB image is 6 * 6 * 3
array of the matrix, and the grayscale image is 4 * 4 * 1 array of the matrix.
In CNN, each input image will pass through a sequence of convolution layers along with
pooling, fully connected layers, filters (Also known as kernels). After that, we will apply
the Soft-max function to classify an object with probabilistic values 0 and 1.
30
Fig 4.10 Classification using CNN
4.5.2 Convolution Layer
Convolution layer is the first layer to extract features from an input image. By learning
image features using a small square of input data, the convolutional layer preserves the
relationship between pixels. It is a mathematical operation which takes two inputs such
as image matrix and a kernel or filter.
4.5.3 Strides
Stride is the number of pixels which are shift over the input matrix. When the stride is
equaled to 1, then we move the filters to 1 pixel at a time and similarly, if the stride is
equaled to 2, then we move the filters to 2 pixels at a time. The following figure shows
that the convolution would work with a stride of 2.
4.5.4 Padding
Padding plays a crucial role in building the convolutional neural network. If the image
will get shrink and if we will take a neural network with 100's of layers on it, it will give us
a small image after filtered in the end.
31
reduces the number of parameters when the images are too large. Pooling is
"downscaling" of the image obtained from the previous layers. It can be compared to
shrinking an image to reduce its pixel density. Spatial pooling is also called
downsampling or subsampling, which reduces the dimensionality of each map but
retains the important information. There are the following types of spatial pooling:
Max pooling is done by applying a max filter to non-overlapping sub-regions of the initial
representation.
4.5.7 Average Pooling
Down-scaling will perform through average pooling by dividing the input into rectangular
pooling regions and computing the average values of each region.
32
CHAPTER 5
Our proposed model is designed and development to detect and classify pneumonia
from chest X-ray images. It contains both image processing and convolutional neural
network.We developed a model The algorithm begins by transforming chest X-ray
images into sizes smaller than the original. The next step involves the identification and
classification of images by the convectional neural network framework, which extracts
features from the images and classify them. This work has presented the X-Ray images
for Pneumonia detection based on convolutional neural networks and different machine
learning. By training a set of strong CNNs on a large scale dataset, we built a model
that can accurately predict Pneumonia.During each epoch data is trained over and over
again to learn the feature of data. The performance evaluation of the model is estimated
by using classification accuracy and cross-validation. We performed 5-fold and 10- fold
cross-validation and presented the results in terms of mean and standard deviation(SD).
Performances of seven Machine learning classification models are presented in The
results presented in Figure 9, show that accuracy of Pneumonia Image Classification. It
can be seen that all machine-learning models, except the decision tree. The highest
accuracy was 98.46% to deep learning . On the other hand, the decision tree classifier
achieved the lowest performance of 85.64%. An F-test carried out over these results
confirms that the performance differences between predictive models are statistically
significant. As shown in Figure 10, F-score of Pneumonia Image Classification.The
average F- score of the classifier is all-more than 90%, which is all an excellent result.
Therefore, in these results of the f-score, the classification accuracy of Deep Learning
(DNN) with Feed Forward and Random Forest (RF) is approximately 98.95%, which is
large as compared to other classifiers.
33
APPENDICES
A SOURCE CODE:
Pneumonia Detection:
import os
import numpy as np
import cv2
from numpy import random
from sklearn.metrics import accuracy_score, confusion_matrix
from tensorflow.keras import layers, models
import matplotlib.pyplot as plt
from tqdm import tqdm
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
from tensorflow.keras.models import Model
from matplotlib import pyplot
import matplotlib.patheffects as path_effects
input_path = './chest_xray/'
#defined some constants for later usage
img_dims = 160
epochs = 15
batch_size = 32
class_names = ['/NORMAL/', '/PNEUMONIA/']
34
val_datagen = ImageDataGenerator(rescale=1. / 255)
train_gen = train_datagen.flow_from_directory(
directory=input_path + 'train',
target_size=(img_dims, img_dims),
batch_size=batch_size,
class_mode='binary',
shuffle=True)
val_gen = val_datagen.flow_from_directory(
directory=input_path + 'val',
target_size=(img_dims, img_dims),
batch_size=batch_size,
class_mode='binary',
shuffle=True)
# I will be making predictions off of the test set in one batch size
# This is useful to be able to get the confusion matrix
test_data = []
test_labels = []
label = 0
for cond in ['/NORMAL/', '/PNEUMONIA/']:
for img in tqdm(os.listdir(input_path + 'test' + cond)):
img = plt.imread(input_path + 'test' + cond + img)
img = cv2.resize(img, (img_dims, img_dims))
if len(img.shape) == 1:
img = np.dstack([img, img, img])
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img.astype(np.float32) / 255
if cond == '/NORMAL/':
label = 0
35
elif cond == '/PNEUMONIA/':
label = 1
test_data.append(img)
test_labels.append(label)
test_data = np.array(test_data)
test_labels = np.array(test_labels)
model.add(layers.Conv2D(16,(3,3),activation = 'relu',
padding='same',input_shape=(img_dims,img_dims,3)))
model.add(layers.MaxPooling2D((2,2)))
36
model.add(layers.Conv2D(256,(3,3),activation = 'relu', padding='same'))
model.add(layers.BatchNormalization())
model.add(layers.MaxPooling2D((2,2)))
# model.add(layers.Dropout(0.3))
model.add(layers.Flatten())
# Creating 1 Dense Layer
model.add(layers.Dense(units=128,activation='relu'))
model.add(layers.Dropout(0.2))
model.add(layers.Dense(units=1, activation='sigmoid'))
#model.load_weights('best_weights.hdf5')
##Predictions
predictions = model.predict(test_data)
37
predictions = np.array(predictions).reshape(-1)
##Accuracy
print("Untrained model, accuracy: {:5.2f}%".format(100 * test_acc))
38
plt.show()
plt.imshow(img, cmap=plt.cm.binary)
39
if predicted_label == int(true_label):
thisplot[int(true_label)].set_color('blue')
else:
thisplot[predicted_label].set_color('red')
def data_test_opt(pred):
pred[pred > 0.5] = int(1)
pred[pred <= 0.5] = int(0)
return np.array(pred).reshape(-1)
######################################################################
##########################################
num_rows = 5
num_cols = 3
num_images = num_rows * num_cols
plt.figure(figsize=(2 * 2 * num_cols, 2 * num_rows))
ind = random.randint(len(predictions), size=(num_images))
40
for i in range(num_images):
plt.subplot(num_rows, 2 * num_cols, 2 * i + 1)
plot_image(ind[i], predictions, test_labels, test_data)
plt.subplot(num_rows, 2 * num_cols, 2 * i + 2)
plot_value_array(ind[i], predictions, test_labels)
plt.tight_layout()
plt.show()
##print the names of the layers from Feature map and from the filters
def showFiltersBlocks():
indexFilter = []
for index in range(len(model.layers)):
layer = model.layers[index]
# check for convolutional layer
if 'conv' not in layer.name:
continue
# get filter weights
filters, biases = layer.get_weights()
indexFilter.append(index)
print(layer.name, filters.shape)
print(indexFilter)
return indexFilter
41
# get feature map for first hidden layer
feature_maps = model2.predict(test_data[1:6])
# plot the output from each block
# plot all 64 maps in an 8x8 squares
plt.figure(figsize=(15, 15))
j=0
for fmap in feature_maps:
square = 4
ix = 1
fig = plt.figure(figsize=(5, 5))
filters, biases = model.layers[indexs[j]].get_weights()
print(model.layers[indexs[j]].name)
text = fig.text(0.5, 1., str(model.layers[indexs[j]].name) + " " + str(filters.shape),
ha='center', va='center', size=20)
text.set_path_effects([path_effects.Normal()])
for _ in range(square):
for _ in range(square):
# specify subplot and turn of axis
ax = plt.subplot(square, square, ix)
ax.set_xticks([])
ax.set_yticks([])
# plot filter channel in grayscale
plt.imshow(fmap[0, :, :, ix - 1])
ix += 1
j += 1
# show the figure
plt.show()
showFeatureMap(showFiltersBlocks())
42
print("_____________________Visualize Filters Start_____________________")
##Visualize Filters
def showFilters(indexs):
for index in indexs:
fig = plt.figure(figsize=(5, 5))
filters, biases = model.layers[index].get_weights()
text = fig.text(0.5, 1., str(model.layers[index].name) + " " + str(filters.shape),
ha='center', va='center', size=20)
text.set_path_effects([path_effects.Normal()])
# normalize filter values to 0-1 so we can visualize them
f_min, f_max = filters.min(), filters.max()
filters = (filters - f_min) / (f_max - f_min)
# plot first few filters
n_filters, ix = 6, 1
for i in range(n_filters):
# get the filter
f = filters[:, :, :, i]
# plot each channel separately
for j in range(3):
# specify subplot and turn of axis
ax = pyplot.subplot(n_filters, 3, ix)
ax.set_xticks([])
ax.set_yticks([])
# plot filter channel in grayscale
pyplot.imshow(f[:, :, j])
ix += 1
# show the figure
pyplot.show()
43
showFilters(showFiltersBlocks())
Test App:
from flask import Flask, render_template, request
from tensorflow.keras.models import load_model
import cv2 as cv
import numpy as np
import os
app = Flask(__name__)
model = load_model('model.h5')
Dict = {0: 'Normal', 1: "Pneumonia"}
def predict_label(imgPath):
imgSize = (224, 224)
img = cv.imread(imgPath)
img = cv.resize(img, imgSize)
img = img.astype(np.float32)/255.
finalImg = np.expand_dims(img, axis=0)
result = np.argmax(model.predict(finalImg), axis=-1)[0]
re = Dict[result]
if re == "Pneumonia":
return "Pneumonia Positive"
return re
@app.route('/', methods=['GET'])
44
def index():
return render_template('index.html')
@app.route('/submit', methods=['POST'])
def get_output():
if request.method == 'POST':
img = request.files['my_image']
img_path = "static/" + img.filename
img.save(img_path)
p = predict_label(img_path)
os.remove(img_path)
return render_template("index.html", prediction=p)
Screen Shots:
45
Fig B.1 Pneumonia positive Fig B.2 Normal
CHAPTER 6
46
SUMMARY AND CONCLUSIONS
In this project, we offered a model for detecting and classifying pneumonia from chest
X-ray images using machine learning methods based on Convolutional Neural Networks
(CNN) .In particular, comparing all seven models based on test accuracy score, F-score
and ROC curve, it appears that CNN outperforms all other models by a small margin
with test accuracy score 98.46%. Random forest performs surprisingly well with test
accuracy score 97.61%. It should note that the CNN model was run with only 100
epochs due to its long running and waiting time. We believe, with proper parameter
tuning and giving a little more time, the CNN model could achieve an even better result.
The result of our model indicates that our model outperforms other methods, which use
no extra training data on chest radiography and shows that image training may be
sufficient for general medical image recognition tasks. Moreover, the cooperation
between a machine learning based medical system and the detection of Pneumonia will
improve the outcomes and bring benefits to clinicians and their patients. This study may
use in similar research which will help to develop a diagnostic tool such as COVID-19
diagnostic. future, we will collect X-ray images from hospitals with big data to train and
test the system to predict better results. Also, we have plan to work with more complex
medical data.
REFERENCES:
47
1 M. Roser, H. Ritchie, and B. Dadonaite, ―Child & infant mortality,‖ Our World in Data,
2013.
6. Ronneberger, O.; Fischer, P.; Brox, T. U- net: Convolutional networks for biomedical
image segmentation. In Medical Image Computing and Computer-Assisted
Intervention—MICCAI 2015; Springer International Publishing: Cham, Switzerland,
2015; pp. 234–241.
7. Milletari, F.; Navab, N.; Ahmadi, S.A. V- Net: Fully Convolutional Neural Networks for
Volumetric Medical Image Segmentation. In Proceedings of the 2016 Fourth
International Conference on 3D Vision (3DV), Stanford, CA, USA, 25–28 October 2016;
pp. 565–571.
8. Jeelani, H.; Martin, J.; Vasquez, F.; Salerno, M.; Weller, D. Image quality affects
deep learning reconstruction of MRI. In Proceedings of the 2018 IEEE 15th
International Symposium on Biomedical Imaging (ISBI 2018), Washington, DC, USA,
4–7 April 2018; pp. 357–360.
9. Chlemper, J.S.; Caballero, J.; Hajnal, J.; Price, A.N.; Rueckert, D. A deep cascade
of convolutional neural networks for dynamic MR image reconstruction. IEEE Trans.
Med. Imaging 2017, 37, 491–503. [CrossRef] [PubMed]
10. Rajpurkar, P.; Irvin, J.; Zhu, K.; Yang, B.; Mehta, H.; Duan, T.; Ding, D.; Bagul, A.;
Langlotz, C.; Shpanskaya, K.; et al. Chexnet: Radiologist-level pneumonia detection
on chest x-rays with deep learning. arXiv 2017, arXiv:1711.05225.
Huang, G.; Liu, Z.; Van Der Maaten, L.; Weinberger, K.Q. Densely Connected
Convolutional Networks. In Proceedings of the 2017 IEEE Conference on Computer
Vision and Pattern Recognition (CVPR), Honolulu, HI, USA, 21–26 July 2017; pp.
2261–2269.
48
11. He, K.; Zhang, X.; Ren, S.; Sun, J. Deep residual learning for image recognition. In
Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition,
Las Vegas, NV, USA, 27–30 June 2016; pp. 770–778.
12. Yu, F.; Koltun, V.; Funkhouser, T. Dilated Residual Networks. In Proceedings of
the 2017 IEEE Conference on Computer Vision and Pattern Recognition (CVPR),
Honolulu, HI, USA, 25–30 June 2017; pp. 636–644.
13. Liang, G.; Zheng, L. A transfer learning method with deep residual network for
pediatric pneumonia diagnosis. Comput. Methods Programs Biomed. 2020, 187,
104964. [CrossRef] [PubMed]
14. Jain, R.; Nagrath, P.; Kataria, G.; Kaushik, V.S.; Hemanth, D.J. Pneumonia
detection in chest X-ray images using convolutional neural networks and transfer
learning. Measurment 2020, 165. [CrossRef]
Conference Certificate
49
50
51
52