-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·122 lines (99 loc) · 3.53 KB
/
main.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 17 02:54:13 2021
@author: buzun & BKaralii
"""
#%% Imports
from subprocesses import preProcess
from models import BasicModel, CNN
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import tensorflow as tf
import csv
import pandas as pd
#%% GPU initialize
physical_devices = tf.config.experimental.list_physical_devices('GPU')
if len(physical_devices) > 0:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
#%% PreProcessing
datasetName = "Dataset"
data, labels, dataset = preProcess(datasetName)
xTrainData, xTestData, yTrainData, yTestData = train_test_split(data, labels, test_size=0.25, random_state=2)
print('\nNumber of xTrainData pairs: ', len(xTrainData))
print('\nNumber of xTestData pairs: ', len(xTestData))
print('\nNumber of yTrainData pairs: ', len(yTrainData))
print('\nNumber of yTestData pairs: ', len(yTestData))
#%% Model build and fit
model = CNN()
history = model.fit(xTrainData, yTrainData,
batch_size = 16,
epochs = 20,
validation_split= 0.25,
verbose = 2,
)
# model_test
testEvaluate = model.evaluate(xTestData, yTestData, verbose=0)
print("loss: " + str(testEvaluate[0]) + "\t accuracy: " + str(testEvaluate[1]))
#%% Save weights
model.save("my_h5_model.h5")
model.save_weights("covid19_weights.h5")
#%% Load weights
model.load_weights("my_h5_model.h5")
#%% Plotting
print(history.history.keys())
#Accuracy and Loss
accuracy = history.history['accuracy']
val_accuracy = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(accuracy))
plt.plot(epochs, accuracy, 'b', label='Training accuracy', color="green")
plt.plot(epochs, val_accuracy, 'b', label='Validation accuracy', color="blue")
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'b', label='Training loss', color="green")
plt.plot(epochs, val_loss, 'b', label='Validation loss', color="blue")
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
# Matrix presentation
fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(10, 10),
subplot_kw={'xticks': [], 'yticks': []})
from PIL import Image
import random
for i, ax in enumerate(axes.flat):
i = random.randint(0,len(dataset))
img = Image.fromarray(dataset[i][0])
ax.imshow(img)
ax.set_title(dataset[i][1])
plt.tight_layout()
plt.show()
#%% Prediction
predictions = model.predict_classes(xTestData)
labelTestData = []
for i in yTestData[:,1:2]:
labelTestData.append('Non-Covid') if (i == 1) else labelTestData.append('Covid')
predictionsList = []
for i in predictions:
predictionsList.append('Non-Covid') if (i == 1) else predictionsList.append('Covid')
# Write CSV file
results=pd.DataFrame({"Label" :labelTestData, "Prediction":predictionsList})
results.to_csv("prediction_results.csv",index=False)
# Matrix presentation
fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(10, 10),
subplot_kw={'xticks': [], 'yticks': []})
from PIL import Image
import random
for i, ax in enumerate(axes.flat):
i = random.randint(0,len(predictions))
img = Image.fromarray(np.reshape(xTestData[i], (xTestData[i].shape[0],xTestData[i].shape[1])))
ax.imshow(img)
ax.set_title("GT : " + labelTestData[i] + '\n' + "Pred : " + predictionsList[i])
plt.tight_layout()
plt.show()