-
Notifications
You must be signed in to change notification settings - Fork 1
/
KneeClassificationTransferC4.py
134 lines (108 loc) · 3.72 KB
/
KneeClassificationTransferC4.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
import io
from contextlib import redirect_stdout
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import StratifiedGroupKFold
import io
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
from tensorflow.keras.models import load_model
from custom_utils import formatKneeDf, plot_to_image
dfNoOther = formatKneeDf(
"/home/msa-project/Dataset_joints/data_apollo/knee_annotations.csv")
# Input Images Variables
TARGET_SIZE = (256,256)
INPUT_SHAPE = TARGET_SIZE + (1,)
RESCALE_FACTOR = 1.0/255
# Architechture Varibales
NUM_CLASSES = 4
DENSE_UNITS = 128
NUM_TOTAL_CONVS = 4
# Processing Variables
BATCH_SIZE = 32
EPOCHS = 200
k_results = []
num = 0
arch_write_flag = True
cv = StratifiedGroupKFold(n_splits=5)
cv1 = StratifiedGroupKFold(n_splits=4)
learn_idxs, test_idxs = next(cv.split(dfNoOther["Filename"], dfNoOther["ml_class"], dfNoOther["patient"]))
learn = dfNoOther.iloc[learn_idxs]
test = dfNoOther.iloc[test_idxs]
train_idxs, val_idxs = next(cv1.split(learn["Filename"], learn["ml_class"], learn["patient"]))
train = learn.iloc[train_idxs]
val = learn.iloc[val_idxs]
gen_train = ImageDataGenerator(
rescale=RESCALE_FACTOR,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.1,
zoom_range=0.1,
horizontal_flip=True)
gen_val = ImageDataGenerator(rescale=RESCALE_FACTOR)
gen_test = ImageDataGenerator(rescale = RESCALE_FACTOR)
set_train = gen_train.flow_from_dataframe(
train,
directory="/home/msa-project/cropped",
x_col='Filename',
y_col='ml_class',
target_size=TARGET_SIZE,
batch_size=BATCH_SIZE,
class_mode="categorical",
color_mode="grayscale"
)
set_val = gen_val.flow_from_dataframe(
val,
directory="/home/msa-project/cropped",
x_col='Filename',
y_col='ml_class',
target_size=TARGET_SIZE,
batch_size=BATCH_SIZE,
class_mode="categorical",
color_mode="grayscale"
)
set_test = gen_test.flow_from_dataframe(
test,
directory="/home/msa-project/cropped",
x_col='Filename',
y_col='ml_class',
target_size=TARGET_SIZE,
batch_size=BATCH_SIZE,
class_mode="categorical",
color_mode="grayscale",
shuffle = False
)
classifier = load_model('currentModel/C4')
for layer in classifier.layers[:-3]:
layer.trainable = False
classifier.compile(optimizer="adam",loss="categorical_crossentropy",metrics=["accuracy"])
log_dir = 'tensorboard_logsPart2/noOtherTransferC'+str(NUM_TOTAL_CONVS)
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
# Fit Kth Model
fitted_model = classifier.fit(
set_train,
epochs=EPOCHS,
validation_data=set_val,
callbacks=[tensorboard_callback]
)
results = classifier.evaluate(set_test)
result_text = "Loss: "+str(results[0])+" Accuracy: " + str(results[1])
file_writer = tf.summary.create_file_writer(log_dir)
with file_writer.as_default():
with tf.name_scope("Test_Metrics"):
tf.summary.text("num"+str(NUM_TOTAL_CONVS),result_text,step=num)
set_test.reset()
y_pred = classifier.predict(set_test, set_test.n // BATCH_SIZE+1)
class_pred = np.argmax(y_pred, axis=1)
labels = set_test.class_indices
cm = confusion_matrix(set_test.classes, class_pred)
cm_disp = ConfusionMatrixDisplay(confusion_matrix = cm, display_labels = list(labels.keys()))
cm_disp.plot()
cm_image = plot_to_image(plt.gcf())
file_writer2 = tf.summary.create_file_writer(log_dir)
with file_writer2.as_default():
with tf.name_scope("Test_Confusion_Matrix"):
tf.summary.image("num"+str(NUM_TOTAL_CONVS), cm_image,step=num)