-
Notifications
You must be signed in to change notification settings - Fork 0
/
modelo.py
291 lines (221 loc) · 7.87 KB
/
modelo.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python
import os
os.environ["CUDA_VISIBLE_DEVICES"]="1"
import tensorflow as tf
#config = tf.compat.v1.ConfigProto()
#config.gpu_options.allow_growth = True
#tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config))
path = '.'
import psutil
import humanize
#import os
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import VGG16, VGG19
from tensorflow.keras.layers import AveragePooling2D, SeparableConv2D, Dropout, Flatten, Dense, Input, MaxPooling2D, Activation, BatchNormalization
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam, Adagrad
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras import backend as K
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
from imutils import paths
import seaborn as sns
import pandas as pd
import numpy as np
import cv2
def show_image(img):
'''
Quick display of image in grayscale
'''
plt.imshow(img, cmap = 'gray')
plt.title('Example X-Ray scan')
plt.grid(False)
plt.axis('off')
plt.show()
def plot_cm(labels, predictions):
'''
Plot the confusion matrix
'''
print(classification_report(labels, predictions,
target_names=lb.classes_))
cm = confusion_matrix(labels, predictions)
total = sum(sum(cm))
acc = (cm[0, 0] + cm[1, 1]) / total
sensitivity = cm[0, 0] / (cm[0, 0] + cm[0, 1])
specificity = cm[1, 1] / (cm[1, 0] + cm[1, 1])
print("accuracy: {:.4f}".format(acc))
print("sensitivity: {:.4f}".format(sensitivity))
print("specificity: {:.4f}".format(specificity))
print()
print('Correct Healthy Patient Detection (True Negatives): ', cm[0][0])
print('Incorrect Covid-19 Detection (False Positives): ', cm[0][1])
print('Incorrect Healthy Patient Detection (False Negatives): ', cm[1][0])
print('Correct Covid-19 Detection (True Positives): ', cm[1][1])
print('Total Patietns with Covid-19: ', np.sum(cm[1]))
print()
plt.figure(figsize=(7,7))
sns.heatmap(cm, annot=True, fmt="d")
plt.title('Confusion matrix')
plt.ylabel('Actual label')
plt.xlabel('Predicted label')
plt.show()
# In[19]:
df = pd.read_csv(path+'/metadata.csv')
df.head() # Show samples from Covid-19 dataset
# In[20]:
df[['finding','view','modality','location']]
# In[21]:
# Initialize the variables for training
dataset_path = path+'/dataset'
init_lr = 1e-3
epochs = 100
batch_size = 5
# Read all files from path
print("[INFO] loading images...")
imagePaths = list(paths.list_images(dataset_path))
data = []
labels_ = []
# loop over the image paths
for imagePath in imagePaths:
#print(imagePath)
# extract the class label from the filename
label = imagePath.split(os.path.sep)[-2]
#print(label)
# load the image, swap color channels, and resize it to be a fixed
# 224x224 pixels while ignoring aspect ratio
image = cv2.imread(imagePath)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
#print(image.shape)
image = cv2.resize(image, (256, 256))
# update the data and labels lists, respectively
data.append(image)
labels_.append(label)
# Normalize images to range [0,1]
data = np.array(data) / 255.0
labels_ = np.array(labels_)
# Plot example patient scan
print('Number of training images: ', len(data))
print()
show_image(data[0])
# perform one-hot encoding on the labels
print('Example scan label:\t ', labels_[0])
lb = LabelBinarizer()
labels = lb.fit_transform(labels_)
labels = to_categorical(labels)
print('One-hot encoded label: ', labels[0])
# In[22]:
# split training and test data
(trainX, testX, trainY, testY) = train_test_split(data, labels,
test_size=0.20, stratify=labels, random_state=3, shuffle=True)
(trainX, validX, trainY, validY) = train_test_split(trainX, trainY,
test_size=0.20, stratify=trainY, random_state=3, shuffle=True)
print('Number of training pairs: ', len(trainX))
print('Number of validation pairs: ', len(validX))
print('Number of testing pairs: ', len(testX))
# # initialize the training data augmentation object
trainAug = ImageDataGenerator(
# rescale=1 / 255.0,
rotation_range=10,
# zoom_range=0.05,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.05,
# horizontal_flip=True,
# vertical_flip=True,
fill_mode="nearest")
# Load the VGG16 network
baseModel = VGG16(weights="imagenet", include_top=False,
input_tensor=Input(shape=(256, 256, 3)))
# Make all pre-trained layers from VGG19 non-trainable
for layer in baseModel.layers[:-3]:
layer.trainable = False
# Add trainable fully-connected (FC) layers for predictions
newModel = baseModel.output
newModel = AveragePooling2D(pool_size=(4, 4))(newModel)
newModel = Flatten(name="flatten")(newModel)
newModel = Dense(64, activation="relu")(newModel)
newModel = Dropout(0.5)(newModel)
newModel = Dense(2, activation="softmax")(newModel)
# Stack the FC layers on top of VGG19 model
model = Model(inputs=baseModel.input, outputs=newModel, name='Covid19_Detector')
# compile our model
print("\n[INFO] compiling model...")
opt = Adam(learning_rate=init_lr, decay=init_lr / epochs)
model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"])
# In[23]:
idx = np.random.randint(0,len(data))
print('idx: ', idx)
print()
print(labels_[idx])
print(labels[idx])
# In[24]:
class_weights = {0 : 1, 1 : 2.01}
# In[25]:
early_stopping = tf.keras.callbacks.EarlyStopping(
monitor='val_accuracy',
verbose=1,
patience=15,
mode='max',
restore_best_weights=True)
# In[26]:
# train the model
print("[INFO] training new model ...")
results = model.fit_generator(
trainAug.flow(trainX, trainY),
# steps_per_epoch=len(trainX) // batch_size,
validation_data=(validX, validY),
# validation_steps=len(testX) // batch_size,
callbacks = [early_stopping],
epochs=epochs,
# class_weight = class_weights
)
# Analyse Results from the Test Set
# In[27]:
# Make predictions on the testing set
print("[INFO] evaluating network...")
predIdxs = model.predict(testX, batch_size=batch_size)
print('\nNumber of test scans: ', len(testX))
print('Predicted class probabilities:')
print(predIdxs)
# Find the predicted labels
predIdxs = np.argmax(predIdxs, axis=1)
print('\nPredicted outcome (Covid=1, Normal=0):')
print(predIdxs)
print('Ground-truth outcome:')
# print(testY)
trueIdxs = np.argmax(testY, axis=1)
print(trueIdxs)
# Loss/Accuracy Curve
# In[28]:
N = len(results.history["loss"])
plt.style.use("ggplot")
plt.figure(figsize = (20,10))
plt.plot(np.arange(0, N), results.history["loss"], label="train_loss", color = 'firebrick')
plt.plot(np.arange(0, N), results.history["val_loss"], label="val_loss", color = 'salmon')
plt.plot(np.arange(0, N), results.history["accuracy"], label="train_acc", color = 'teal')
plt.plot(np.arange(0, N), results.history["val_accuracy"], label="val_acc",color = 'cadetblue')
plt.plot( np.argmin(results.history["val_loss"]), np.min(results.history["val_loss"]), marker="x", color="r", label="best model")
plt.title("Training Loss and Accuracy on COVID-19 Dataset")
plt.xlabel("Epochs")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="upper right")
plt.savefig('loss_curve.pdf', format='pdf')
plt.show()
# compute the confusion matrix and and use it to derive the raw accuracy, sensitivity, and specificity
# In[29]:
predicted_metrics = model.evaluate(testX, testY,
batch_size=batch_size, verbose=0)
for name, value in zip(model.metrics_names, predicted_metrics):
print(name, ': ', value)
print()
plot_cm(trueIdxs, predIdxs)
model_json = model.to_json()
with open("modelo.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("modelo.h5")
print("Saved model to disk")