-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_LCL_GCL.py
213 lines (173 loc) · 7.18 KB
/
test_LCL_GCL.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
from LCL_GCL_module import global_enc_proj, local_enc_proj
from sklearn.model_selection import KFold, StratifiedKFold
from sklearn.metrics import confusion_matrix, roc_curve
from skimage import data
from sklearn.model_selection import train_test_split
from sklearn.metrics import ConfusionMatrixDisplay
from Evaluation import evaluate_multiclass
from LCOL_GCOL_loader import DCMDataFrameIterator
############### GLobal Contrastive Learning Module #######################
save_dir = '/FastData/'
##### Encoder Projector weights ###
def get_model_nameA(k):
return 'SupCon_ord'+str(k)+'.h5'
def get_model_nameC(k):
return 'Local_SupCon+dist_' + str(k) + '.h5'
### Regression Network/ To check individual performance of Glbobal Module######
def get_model_nameB(k):
return 'SupCon_ord_Class+_'+str(k)+'.h5'
def get_model_nameD(k):
return 'Local_SupCon+dist_Class' + str(k) + '.h5'
# augmentation parameters
train_augmentation_parameters = dict(
rotation_range=15,
shear_range=0.05,
width_shift_range=0.05,
height_shift_range=0.05,
fill_mode='constant',
cval=0)
test_augmentation_parameters = dict(
rescale=0.0,
)
# training parameters
BATCH_SIZE = 16
CLASS_MODE = 'raw'
COLOR_MODE = 'rgb'
TARGET_SIZE = (320, 320)
SEED = 7
train_consts = {
'seed': SEED,
'batch_size': BATCH_SIZE,
'class_mode': CLASS_MODE,
'color_mode': COLOR_MODE,
'target_size': TARGET_SIZE,
'subset': 'training'
}
test_consts = {
'batch_size': 1, # should be 1 in testing
'class_mode': CLASS_MODE,
'color_mode': COLOR_MODE,
'target_size': TARGET_SIZE, # resize input images
'shuffle': False
}
########################## For testing GCL ################################
fold_var = 1
Kkfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=3)
for train_index, val_index in Kkfold.split(np.zeros(1914),Y):
train_df = df.iloc[train_index]
test_df = df.iloc[val_index]
train_df, valid_df = train_test_split(train_df, test_size=0.2)
train_data_generator = DCMDataFrameIterator(dataframe=train_df,
x_col='fileName',
y_col='labels',
image_data_generator=train_augmenter,
**train_consts, shuffle = True)
valid_data_generator = DCMDataFrameIterator(dataframe=valid_df,
x_col='fileName',
y_col='labels',
image_data_generator=test_augmenter,
**test_consts)
test_generator = DCMDataFrameIterator(dataframe=test_df,
x_col='fileName',
y_col='labels',
image_data_generator=test_augmenter,
**test_consts)
filenames = test_generator.filenames
nb_samples = len(filenames)
################## Encoder_projector Network #########################
model = global_enc_proj()
################ Regression Module ##########################
mC = tf.keras.Model(inputs = model.input, outputs = model.layers[-4].output) ### Discard Projection Layers
mC.trainable = False
in_fea = mC.output
features = Dense(1280, activation="relu",)(in_fea)
features = Dropout(0.4)(features)
features = Dense(128, activation="relu",)(features)
features = Dropout(0.2)(features)
outputs = Dense(1, activation="linear")(features)
model1 = keras.Model(inputs=mC.input, outputs=outputs)
model1.load_weights(save_dir+get_model_nameB(k))
filenames = test_generator.filenames
nb_samples = len(filenames)
predict =model1.predict(test_generator,steps = nb_samples)
y_pred = np.round(predict)
y_true = test_df['labels'].astype('float32')
y_true = np.array(y_true)
y_pred[y_pred<2]=0
y_pred[(y_pred>=2)&(y_pred<6)]=1
y_pred[y_pred>=6]=2
y_true[y_true<2]=0
y_true[(y_true>=2)&(y_true<6)]=1
y_true[y_true>=6]=2
matrix = confusion_matrix(y_true, y_pred)
print(matrix)
A = evaluate_multiclass(y_true, y_pred)
print('Accuracy=')
print(A[0],np.mean(A[0]))
print('F1-Score=')
print(A[6],np.mean(A[6]))
import gc
K.clear_session()
gc.collect()
fold_var += 1
######## For Testing LCL #############
fold_var = 1
Kkfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=3)
for train_index, val_index in Kkfold.split(np.zeros(1914), Y):
train_df = df.iloc[train_index]
test_df = df.iloc[val_index]
train_df, valid_df = train_test_split(df, test_size=0.2)
train_data_generator = DCMDataFrameIterator(dataframe=train_df,
x_col='fileName',
y_col='labels',
image_data_generator=train_augmenter,
**train_consts, shuffle=True)
valid_data_generator = DCMDataFrameIterator(dataframe=valid_df,
x_col='fileName',
y_col='labels',
image_data_generator=test_augmenter,
**test_consts)
test_generator = DCMDataFrameIterator(dataframe=test_df,
x_col='fileName',
y_col='labels',
image_data_generator=test_augmenter,
**test_consts)
filenames = test_generator.filenames
model = local_enc_proj()
mC = tf.keras.Model(inputs=model.input, outputs=model.layers[-4].output)
in_fea = mC.output
features = Dense(1280, activation="relu", name='d_1')(in_fea)
features = Dropout(0.4)(features)
features = Dense(128, activation="relu", name='d_2')(features)
features = Dropout(0.2)(features)
outputs = Dense(1, activation="linear", name='final_output')(features)
model1 = keras.Model(inputs=mC.input, outputs=outputs)
model1.load_weights(save_dir+get_model_nameD(fold_var))
filenames = test_generator.filenames
nb_samples = len(filenames)
predict = model1.predict(test_generator, steps=nb_samples)
y_pred = np.round(predict)
y_true = valid_df['labels'].astype('float32')
y_true = np.array(y_true)
y_pred[y_pred < 2] = 0
y_pred[(y_pred >= 2) & (y_pred < 6)] = 1
y_pred[y_pred >= 6] = 2
y_true[y_true < 2] = 0
y_true[(y_true >= 2) & (y_true < 6)] = 1
y_true[y_true >= 6] = 2
matrix = confusion_matrix(y_true, y_pred)
print(matrix)
A = evaluate_multiclass(y_true, y_pred)
print('Accuracy=')
print(A[0], Average(A[0]))
print('PPV=')
print(A[1], Average(A[1]))
print('NPV=')
print(A[2], Average(A[2]))
print('Sensitivity')
print(A[3], Average(A[3]))
print('Specificity')
print(A[4], Average(A[4]))
print('F1-Score')
print(A[6], Average(A[6]))
fold_var += 1