-
Notifications
You must be signed in to change notification settings - Fork 25
/
pneumonia_test.py
164 lines (127 loc) · 5.36 KB
/
pneumonia_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
### Pneumonia Test
import os
import random
import numpy as np
import cv2
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import models
import matplotlib.pyplot as plt
# Dataset Chest_Xray_Pnenumonia
test_dir = 'datasets/chest_xray/test'
target_size = (224,224) #for CNN
#target_size = (299,299) #for Inception-V3
# Data Generator
rescale = 1./255
test_datagen = ImageDataGenerator(
rescale=rescale,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_generator = test_datagen.flow_from_directory(
test_dir,
target_size=target_size,
class_mode='categorical',
batch_size=32,
color_mode="grayscale", #for CNN
shuffle=True)
# Load Model
model = models.load_model('models/pneumonia_cnn.h5')
# Evaluate Model
result = model.evaluate(test_generator, steps=len(test_generator))
print("%s%.2f " % ("Loss : ", result[0]))
print("%s%.2f%s" % ("Accuracy : ", result[1]*100, "%"))
# Predict
y_pred = model.predict(test_generator, steps=len(test_generator), verbose=1)
y_pred = y_pred.argmax(axis=-1)
y_true=test_generator.classes
numofbatch = len(test_generator)
batch_no = random.randint(0, numofbatch-1)
y_img_batch, y_true_batch = test_generator[batch_no]
y_true_batch = y_true_batch.argmax(axis=-1)
y_pred_batch = model.predict(y_img_batch)
y_pred_batch = y_pred_batch.argmax(axis=-1)
sizeofbatch = len(y_true_batch)
print("-"*35)
print("%s%d"% ("Selected Batch No : ", batch_no))
print("-"*35)
print("%s%d"% ("Batch Size : ", len(y_pred_batch)))
print("-"*35)
print("%s%.2f%s"% ("Accuracy : ", np.mean(y_true==y_pred)*100, "%"))
print("-"*35)
# show predictions
def get_fig_axs(subplot_params):
fig, axs = plt.subplots(
nrows=subplot_params["nrows"], ncols=subplot_params["ncols"],
figsize=(subplot_params["figsize_col"], subplot_params["figsize_row"]),
dpi=subplot_params["dpi"], facecolor=subplot_params["facecolor"],
edgecolor=subplot_params["edgecolor"], subplot_kw=subplot_params["subplot_kw"])
return fig, axs
def show_predictions(y_img_batch, y_true, y_pred, subplot_params, plot_params, class_map, testing_dir, image_file_name, count=8, sample=True):
fig, axs = get_fig_axs(subplot_params)
plt.rcParams.update({'axes.titlesize': plot_params["axes.titlesize"]})
plt.subplots_adjust(hspace=subplot_params["hspace"], wspace=subplot_params["wspace"])
file_names = test_generator.filenames
m = {}
length = len(y_true)
for i in range(0, count):
num = i
if sample:
num = random.randint(0, length-1)
while num in m:
num = int(random.randint(0, length-1))
m[num]=1
plt.subplot(subplot_params["nrows"], subplot_params["ncols"], i+1)
img = cv2.imread(testing_dir+"\\"+ file_names[num], 1)
plt.imshow(img)
plt.xticks([])
plt.yticks([])
original = class_map[y_true[num]]
predicted = class_map[y_pred[num]]
title_text = ("%s%s%s%s%s"%("True: ", original, "\n", "Pred: ", predicted))
if original==predicted:
plt.title(title_text)
else:
plt.title(title_text, color='red')
if plot_params["update_image"] and os.path.exists(image_file_name):
os.remove(image_file_name)
fig.savefig(image_file_name, dpi=subplot_params["dpi"])
plt.tight_layout()
plt.show()
def get_reset_subplot_params(nrows, ncols, dpi):
subplot_params = {}
subplot_params["nrows"] = nrows
subplot_params["ncols"] = ncols
subplot_params["figsize_col"] = subplot_params["ncols"]*2.5
subplot_params["figsize_row"] = subplot_params["nrows"]*2.5
subplot_params["dpi"] = dpi
subplot_params["facecolor"] = 'w'
subplot_params["edgecolor"] = 'k'
subplot_params["subplot_kw"] = {'xticks': [], 'yticks': []}
subplot_params["axes.titlesize"] = 'small'
subplot_params["hspace"] = 0.5
subplot_params["wspace"] = 0.3
return subplot_params
def get_reset_plot_params(figsize=(15, 5), title="", xlabel ="", ylabel="", legends=[], title_fontsize = 18, label_fontsize = 14, image_file_name="", save = False, dpi=100, update_image=True):
plot_params = {}
plot_params["figsize"] = figsize
plot_params["title"] = title
plot_params["xlabel"] = xlabel
plot_params["ylabel"] = ylabel
plot_params["legends"] = legends
plot_params["title_fontsize"] = title_fontsize
plot_params["axes.titlesize"] = "small"
plot_params["label_fontsize"] = label_fontsize
plot_params["image_file_name"] = image_file_name
plot_params["save"] = save
plot_params["update_image"] = update_image
plot_params["subplot"] = None
return plot_params
ncols = 4
nrows = 2
count = ncols*nrows
dpi = 100
class_map = {v: k for k, v in test_generator.class_indices.items()}
subplot_params = get_reset_subplot_params(nrows, ncols, dpi)
plot_params = get_reset_plot_params()
image_file_name_sample = 'out/pneumonia_result.jpg'
show_predictions(y_img_batch, y_true_batch, y_pred_batch, subplot_params, plot_params, class_map, test_dir, image_file_name_sample, count=count, sample=True)