import numpy as np
import pandas as pd
import matplotlib .pyplot as plt
import seaborn as sns
% matplotlib inline
import os
from matplotlib .image import imread
'C:\\Users\\HP\\Desktop\\Kaggle_Challenges\\Chest_XRay\\chest_xray'
my_data_dir = 'C:\\ Users\\ HP\\ Desktop\\ Kaggle_Challenges\\ Chest_XRay\\ chest_xray\\ chest_xray\\ '
train_path = my_data_dir + "train"
test_path = my_data_dir + "test"
val_path = my_data_dir + "val"
os .listdir (train_path + "\\ NORMAL" )[0 ]
norm_1 = imread (train_path + "\\ NORMAL" + "\\ IM-0115-0001.jpeg" )
plt .imshow (norm_1 ,cmap = 'gray' )
<matplotlib.image.AxesImage at 0x19832effe50>
os .listdir (train_path + "\\ PNEUMONIA" )[0 ]
'person1000_bacteria_2931.jpeg'
pne_1 = imread (train_path + "\\ PNEUMONIA" + '\\ person1000_bacteria_2931.jpeg' )
plt .imshow (pne_1 ,cmap = 'gray' )
<matplotlib.image.AxesImage at 0x198330bdc10>
from tensorflow .keras .preprocessing .image import ImageDataGenerator
image_gen = ImageDataGenerator (rotation_range = 20 , width_shift_range = 0.10 , height_shift_range = 0.10 ,
rescale = 1 / 255 , shear_range = 0.1 , zoom_range = 0.1 , horizontal_flip = True , fill_mode = 'nearest' )
from tensorflow .keras .models import Sequential
from tensorflow .keras .layers import Activation , Dropout , Flatten , Dense , Conv2D , MaxPooling2D
model = Sequential ()
model .add (Conv2D (filters = 64 , kernel_size = (3 ,3 ),input_shape = img_shape , activation = 'relu' ))
model .add (MaxPooling2D (pool_size = (2 , 2 )))
model .add (Conv2D (filters = 64 , kernel_size = (3 ,3 ),input_shape = img_shape , activation = 'relu' ))
model .add (MaxPooling2D (pool_size = (2 , 2 )))
model .add (Conv2D (filters = 64 , kernel_size = (3 ,3 ),input_shape = img_shape , activation = 'relu' ))
model .add (MaxPooling2D (pool_size = (2 , 2 )))
model .add (Flatten ())
model .add (Dense (128 ,activation = 'relu' ))
model .add (Dropout (0.5 ))
model .add (Dense (1 ,activation = 'sigmoid' ))
model .compile (loss = 'binary_crossentropy' ,optimizer = 'adam' ,metrics = ['accuracy' ])
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_3 (Conv2D) (None, 148, 148, 64) 1792
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 74, 74, 64) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 72, 72, 64) 36928
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 36, 36, 64) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 34, 34, 64) 36928
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 17, 17, 64) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 18496) 0
_________________________________________________________________
dense_1 (Dense) (None, 128) 2367616
_________________________________________________________________
dropout (Dropout) (None, 128) 0
_________________________________________________________________
dense_2 (Dense) (None, 1) 129
=================================================================
Total params: 2,443,393
Trainable params: 2,443,393
Non-trainable params: 0
_________________________________________________________________
from tensorflow .keras .callbacks import EarlyStopping
early_stop = EarlyStopping (monitor = 'val_loss' ,patience = 2 ,restore_best_weights = True )
train_image_gen = image_gen .flow_from_directory (train_path ,target_size = img_shape [:2 ],color_mode = 'rgb' , batch_size = batch_size ,
class_mode = 'binary' , shuffle = True )
Found 5216 images belonging to 2 classes.
val_image_gen = image_gen .flow_from_directory (val_path ,target_size = img_shape [:2 ],color_mode = 'rgb' ,batch_size = batch_size ,
class_mode = 'binary' ,shuffle = False )
Found 16 images belonging to 2 classes.
test_image_gen = image_gen .flow_from_directory (test_path ,target_size = img_shape [:2 ],batch_size = batch_size ,
class_mode = 'binary' ,shuffle = False )
Found 624 images belonging to 2 classes.
train_image_gen .class_indices
{'NORMAL': 0, 'PNEUMONIA': 1}
results = model .fit_generator (train_image_gen ,epochs = 10 , validation_data = val_image_gen ,callbacks = [early_stop ])
C:\Users\HP\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:1940: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
warnings.warn('`Model.fit_generator` is deprecated and '
Epoch 1/10
326/326 [==============================] - 219s 670ms/step - loss: 0.4142 - accuracy: 0.8196 - val_loss: 1.0482 - val_accuracy: 0.7500
Epoch 2/10
326/326 [==============================] - 220s 675ms/step - loss: 0.2923 - accuracy: 0.8727 - val_loss: 1.4154 - val_accuracy: 0.6250
Epoch 3/10
326/326 [==============================] - 231s 709ms/step - loss: 0.2291 - accuracy: 0.9009 - val_loss: 1.4887 - val_accuracy: 0.7500
model .save ('cxr_model_2.h5' )
model_df = pd .DataFrame (model .history .history )
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
loss
accuracy
val_loss
val_accuracy
0
0.414183
0.819594
1.048191
0.750
1
0.292295
0.872699
1.415353
0.625
2
0.229100
0.900882
1.488686
0.750
cnn_pred = model .predict_generator (test_image_gen )
C:\Users\HP\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:2001: UserWarning: `Model.predict_generator` is deprecated and will be removed in a future version. Please use `Model.predict`, which supports generators.
warnings.warn('`Model.predict_generator` is deprecated and '
predictions = cnn_pred > 0.5
from sklearn .metrics import confusion_matrix ,classification_report , accuracy_score
print (classification_report (test_image_gen .classes , predictions ))
precision recall f1-score support
0 0.84 0.51 0.63 234
1 0.76 0.94 0.84 390
accuracy 0.78 624
macro avg 0.80 0.72 0.74 624
weighted avg 0.79 0.78 0.76 624
print (confusion_matrix (test_image_gen .classes ,predictions ))
d = []
for i in x :
prediction = cnn_pred > i
d .append (accuracy_score (test_image_gen .classes ,prediction ))
the_df = pd .DataFrame ({'threshold' :list (x ) , 'accuracy' : d })
the_df [the_df ['accuracy' ]== the_df ['accuracy' ].max ()]
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
threshold
accuracy
64
0.64
0.804487
65
0.65
0.804487
hence the threshold should be 0.64
prediction = cnn_pred > threshold
print (confusion_matrix (test_image_gen .classes ,predictions ))
print ('\n ' )
print (classification_report (test_image_gen .classes ,predictions ))
[[119 115]
[ 23 367]]
precision recall f1-score support
0 0.84 0.51 0.63 234
1 0.76 0.94 0.84 390
accuracy 0.78 624
macro avg 0.80 0.72 0.74 624
weighted avg 0.79 0.78 0.76 624
prediction on a single image
from tensorflow .keras .preprocessing import image
sample_image_1 = imread (test_path + "\\ NORMAL\\ " + "IM-0029-0001.jpeg" )
plt .imshow (imread (test_path + "\\ NORMAL\\ " + "IM-0029-0001.jpeg" ),cmap = 'gray' )
<matplotlib.image.AxesImage at 0x1983e9dd250>
sample_image_1 = image .load_img (test_path + "\\ NORMAL\\ " + "IM-0029-0001.jpeg" ,target_size = (150 ,150 ))
sample_image_1 = image .img_to_array (sample_image_1 )
sample_image_1 = np .expand_dims (sample_image_1 ,axis = 0 )
sample_image_1 = sample_image_1 / 255
prediction_prob = model .predict (sample_image_1 )
# function to implement whether the predictied probabilty is close to NORMAL or PNEUMONIA
def cxr (x ):
if x >= threshold :
return ('PNEUMONIA' )
else :
return ('NORMAL' )
print (cxr (prediction_prob ))
NOW LET'S CHECK FOR PNEUMONIA CHEST XRAY
sample_image_2 = image .load_img (test_path + "\\ PNEUMONIA\\ " + "person1_virus_11.jpeg" ,target_size = (150 ,150 ))
sample_image_2 = image .img_to_array (sample_image_2 )
sample_image_2 = np .expand_dims (sample_image_2 ,axis = 0 )
sample_image_2 = sample_image_2 / 255
plt .imshow (imread (test_path + "\\ PNEUMONIA\\ " + "person1_virus_11.jpeg" ),cmap = 'gray' )
<matplotlib.image.AxesImage at 0x1983ea27eb0>
pred = model .predict (sample_image_2 )
OUR MODEL PREDCICTED CORRECTLY