Professional Documents
Culture Documents
Dataset Description:
A dataset of 1.16 GB size has been imported from Kaggle, with a total of 5856 images in
the jpeg format split into Train, Test and Val folders. Each further divided into categories
Pneumonia and Normal. The images are chest X-ray images and have been selected from
pediatric patients ranging from one to five years of age from Guangzhou Women and
Children's Medical centre, Guangzhou.
CNN Model:
CNN stands for Convolutional Neural Network. Its a sub field of deep learning which is
mostly used for the analysis of visual imagery. It is a specific type of artificial neural network
that employs perceptrons. Perceptrons are the building blocks of neural networks. They are
simple models of a biological neuron but in an ANN setting.
Convolutional Layer:
These are the building blocks of the CNN.As we are working with images consisting of our
data, the input image is first converted into a matrix format. Just like how convolution
is carried out in mathematics, a convolution filter is applied to the converted matrix format of
the input image. This filter slides over it, performing an element-wise multiplication and then
storing the sum. This has now created a feature map. 3*3 filters are generally used to create
2D feature maps when the images are black and white. Usually, convolutions are performed
in
3D when the input image is represented as a 3D matrix. The third dimension is represented
by
the RGB colour.
Activation Functions:
The four models mentioned use two different activation functions, namely ReLU and
Softmax functions.
ReLU:
Standing for Rectified Linear Unit, this activation function is broadly used in CNNS.
It handles the problem of vanishing gradients and is useful for increasing the non-linearity of
layers. ReLU has many variants- namely, Noisy ReLUs , Leaky ReLUs and parametric
ReLUs.
The advantage of this particular activation function over other activation functions is
attributed to it's computational simplicity.
Softmax:
This activation function calculates relative probabilities by taking in a vector of
raw outputs of the neural network. It returns a vector of probability scores. The equation of
The softmax function is
where z is the vector of raw outputs from the neural network, value
of e is approximately 2.7and i stands for the i-th entry which can be interpreted as the
predicted probability of the test input belonging to class i. The softmax function is used in all
the four mentioned models. Since it's return type is a vector of probability scores,
this activation function normalizes the input data into a probability distribution.
Pooling layers:
After convolutional layers, pooling layers come into the picture. Pooling layers
are a layer of neural nodes in neural networks that reduce the size of the input feature set.
We employ the max-pooling technique as it assists us in recognizing the salient features of
the image. The max-pooling technique is done after convolution. Since this technique only
stores pixels of the maximum value, it's very easy to downsize an image. That is, we extract
the most important feature in the region. There are many other advantages to max-pooling
other than downsizing an image. It adds invariance. What's the advantage of adding
invariance? Invariance in images is important if we need to know the presence or absence
of the feature as opposed to the knowledge of it's precise location. Here, the max-pooling
layer having a dimension 2*2 selects maximum pixel intensity values.
Reducing Over-fitting:
This is a type of data simplification technique which reduces the overfitting of data such that
it properly fits into the training data set. Substantial overfitting is exhibited by the first
model. The next three exhibited dropout technique. Dropout technique helps with the
problem of vanishing gradients. It makes each neuron form it's own representation of
the input data. Dropout technique is where the technique cuts connections between
neurons in success layers during the training process.
Algorithm:
The number of epochs in these classifier models was fixed at 20. This number was fixed after
seeing that models with higher number of epochs overfit the training datasets. Many
optimizer functions were trained and studied too. Finally, Adam Optimizer function was
finalized as it brought out the best results. The Adam optimizer makes use of an
exponentially decaying average of past gradients.
First Classifier: A Simple classifier model along with a convolutional layer of image set to
64*64, 32 feature maps, ReLU activation function and a fully connected dense layer with 128
perceptrons was trained.
Second Classifier: To improve the performance of the first classifier, another convolutionaly
layer of 64 feature maps was added. This was done for better feature extraction. The number
of perceptrons increased from 128 to 256. This was done so that a better learning could occur.
Third Classifier: Three convolutional layers, 128 feature maps included in the third
convolutional layer, Dense layer kept intact, dropout layer introduced, learning rate of
optimizer reduced to 0.0001. The third convolutional layer was introduced for more detailed
feature extraction. The learning rate of the optimizer was reduced to prevent overfitting.
Fourth Classifier: Four convolutional layers, 256 feature maps in fourth convolutional layer.
Other parameters kept same as the third model.
MODEL :
The proposed work uses Deep learning models like Convolutional Neural Network (CNN)
model to detect the pneumonia disease from a scanned chest x-ray images of a patient.
PROGRAM :
import matplotlib.pyplot as plt
import tensorflow as tf
plt.style.use('fivethirtyeight')
import pickle
import os
import numpy as np
import cv2
# %matplotlib inline
img_size = 200
def get_training_data(data_dir):
data = []
class_num = labels.index(label)
try:
data.append([resized_arr, class_num])
except Exception as e:
print(e)
return np.array(data)
drive.mount('/content/drive')
train = get_training_data('/content/drive/MyDrive/train')
test = get_training_data('/content/drive/MyDrive/test')
val = get_training_data('/content/drive/MyDrive/val')
pnenumonia = 0
normal = 0
for i, j in train:
if j == 0:
pnenumonia+=1
else:
normal+=1
print('Pneumonia:', pnenumonia)
print('Normal:', normal)
print('Pneumonia - Normal:', pnenumonia-normal)
plt.imshow(train[1][0], cmap='gray')
plt.axis('off')
print(labels[train[1][1]])
X = []
y = []
X.append(feature)
y.append(label)
X.append(feature)
y.append(label)
X.append(feature)
y.append(label)
y = np.array(y)
datagen = ImageDataGenerator(
featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=90,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
vertical_flip=True)
datagen.fit(X_train)
model = Sequential()
model.add(Activation('relu'))
model.add(BatchNormalization(axis=1))
model.add(Activation('relu'))
model.add(BatchNormalization(axis=1))
model.add(Activation('relu'))
model.add(BatchNormalization(axis=1))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
adam = Adam(learning_rate=0.0001)
model.compile(loss='binary_crossentropy',optimizer=adam,metrics=['acc'])
model.summary()
model.evaluate(X_test, y_test)
plt.figure(figsize=(16, 9))
plt.plot(history.epoch, history.history['acc'])
plt.title('Model Accuracy')
plt.show()
plt.figure(figsize=(16, 9))
plt.plot(history.epoch, history.history['loss'])
plt.title('Model Loss')
plt.show()
plt.figure(figsize=(16, 9))
plt.plot(history.epoch, history.history['val_acc'])
plt.show()
plt.figure(figsize=(16, 9))
plt.plot(history.epoch, history.history['val_loss'])
plt.show()
pred = model.predict(X_train)
plt.xlabel('Thresholds')
plt.show()
plt.plot(fpr, tpr)
plt.show()
plot_roc(fpr, tpr)
predictions = model.predict(X_test)
binary_predictions = []
for i in predictions:
if i >= threshold:
binary_predictions.append(1)
else:
binary_predictions.append(0)
![](https://silvrback.s3.amazonaws.com/uploads/4ab81a17-4a77-4e9e-b092-
de5fac2afa07/confusionmatrix_large.png)
plt.figure(figsize=(16, 9))
ax= plt.subplot()
ax.xaxis.set_ticklabels(labels)
ax.yaxis.set_ticklabels(labels)
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
if(binary_predictions[i]==y_test[i]):
plt.xlabel(labels[binary_predictions[i]], color='blue')
else:
plt.xlabel(labels[binary_predictions[i]], color='red')
plt.show()
model.save('pneumonia_detection_ai_version_3.h5')
Accuracy and computation time are used as the metric for evaluation.