-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
203 lines (161 loc) · 7.71 KB
/
training.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
import utils
import csv
import tensorflow as tf
def supervised_downStreamPipeline(fineTuneData,fineTuneLabel,valData,valLabel,testData,testLabel, evaluation_dir,classification_model, FE_Layers, random_FT_weights,trained_FT_weights, finetune_epoch = 50,finetune_batch_size = 64, FT_LR =5e-4):
macro_f1_list = []
# Feature Extrator Frozen
best_validation_weights_dir = evaluation_dir+"Checkpoint_Frozen_FE.h5"
best_model_callback = tf.keras.callbacks.ModelCheckpoint(best_validation_weights_dir,
monitor='val_accuracy', mode='max', save_best_only=True, save_weights_only=True, verbose=0
)
classification_model.load_weights(trained_FT_weights)
for model_layer in classification_model.layers[:FE_Layers]:
model_layer.trainable = False
classification_model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=FT_LR),
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=["accuracy"]
)
training_history = classification_model.fit(
x = fineTuneData,
y = fineTuneLabel,
batch_size=finetune_batch_size,
shuffle=True,
epochs=finetune_epoch,
callbacks=[best_model_callback],
verbose=2,
validation_data=(valData,valLabel)
)
classification_model.load_weights(best_validation_weights_dir)
macro_f1_list.append(utils.getF1Macro(testLabel,classification_model.predict(testData, verbose = 0)))
utils.plot_learningCurve(training_history,finetune_epoch,evaluation_dir,'Graph_Frozen_FE_')
# Feature Extrator Unfrozen
best_validation_weights_dir = evaluation_dir+"Checkpoint_Unfrozen_FE.h5"
best_model_callback = tf.keras.callbacks.ModelCheckpoint(best_validation_weights_dir,
monitor='val_accuracy', mode='max', save_best_only=True, save_weights_only=True, verbose=0
)
for model_layer in classification_model.layers[:FE_Layers]:
model_layer.trainable = True
classification_model.load_weights(trained_FT_weights)
classification_model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=FT_LR),
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=["accuracy"]
)
training_history = classification_model.fit(
x = fineTuneData,
y = fineTuneLabel,
batch_size=finetune_batch_size,
shuffle=True,
epochs=finetune_epoch,
callbacks=[best_model_callback],
verbose=2,
validation_data=(valData,valLabel)
)
classification_model.load_weights(best_validation_weights_dir)
macro_f1_list.append(utils.getF1Macro(testLabel,classification_model.predict(testData, verbose = 0)))
utils.plot_learningCurve(training_history,finetune_epoch,evaluation_dir,'Graph_Unfrozen_FE_')
# Feature Extrator Randomly Initialized
best_validation_weights_dir = evaluation_dir+"Checkpoint_Random_FE.h5"
best_model_callback = tf.keras.callbacks.ModelCheckpoint(best_validation_weights_dir,
monitor='val_accuracy', mode='max', save_best_only=True, save_weights_only=True, verbose=0
)
classification_model.load_weights(random_FT_weights)
training_history = classification_model.fit(
x = fineTuneData,
y = fineTuneLabel,
batch_size=finetune_batch_size,
shuffle=True,
epochs=finetune_epoch,
callbacks=[best_model_callback],
verbose=2,
validation_data=(valData,valLabel)
)
classification_model.load_weights(best_validation_weights_dir)
macro_f1_list.append(utils.getF1Macro(testLabel,classification_model.predict(testData, verbose = 0)))
utils.plot_learningCurve(training_history,finetune_epoch,evaluation_dir,'Graph_Random_FE_')
with open(evaluation_dir +'Result_Report.csv','w') as f:
w = csv.writer(f)
w.writerow(["Result_Frozen_FE"])
w.writerow([str(macro_f1_list[0])])
w.writerow(['Result_Unfrozen_FE'])
w.writerow([str(macro_f1_list[1])])
w.writerow(['Result_Unfrozen_FE_Random'])
w.writerow([str(macro_f1_list[2])])
return macro_f1_list
def downStreamPipeline(fineTuneData,fineTuneLabel,valData,valLabel,testData,testLabel, evaluation_dir,classification_model, FE_Layers, random_FT_weights,trained_FT_weights, finetune_epoch = 50,finetune_batch_size = 64, FT_LR =5e-4):
macro_f1_list = []
# Feature Extrator Frozen
best_validation_weights_dir = evaluation_dir+"Checkpoint_Frozen_FE.h5"
best_model_callback = tf.keras.callbacks.ModelCheckpoint(best_validation_weights_dir,
monitor='val_accuracy', mode='max', save_best_only=True, save_weights_only=True, verbose=0
)
stop_early = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=15)
callbacksList = []
callbacksList.append(stop_early)
callbacksList.append(best_model_callback)
stop_early.stopped_epoch
classification_model.load_weights(trained_FT_weights)
for model_layer in classification_model.layers[:FE_Layers]:
model_layer.trainable = False
classification_model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=FT_LR),
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=["accuracy"]
)
training_history = classification_model.fit(
x = fineTuneData,
y = fineTuneLabel,
batch_size=finetune_batch_size,
shuffle=True,
epochs=finetune_epoch,
callbacks= callbacksList,
verbose=2,
validation_data=(valData,valLabel)
)
classification_model.load_weights(best_validation_weights_dir)
macro_f1_list.append(utils.getF1Macro(testLabel,classification_model.predict(testData, verbose = 0)))
plotCurveEpoch = finetune_epoch
if(stop_early.stopped_epoch != 0):
plotCurveEpoch = stop_early.stopped_epoch + 1
utils.plot_learningCurve(training_history,plotCurveEpoch,evaluation_dir,'Graph_Frozen_FE_')
# Feature Extrator Unfrozen
best_validation_weights_dir = evaluation_dir+"Checkpoint_Unfrozen_FE.h5"
best_model_callback = tf.keras.callbacks.ModelCheckpoint(best_validation_weights_dir,
monitor='val_accuracy', mode='max', save_best_only=True, save_weights_only=True, verbose=0
)
stop_early = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=15)
callbacksList = []
callbacksList.append(stop_early)
callbacksList.append(best_model_callback)
for model_layer in classification_model.layers[:FE_Layers]:
model_layer.trainable = True
classification_model.load_weights(trained_FT_weights)
classification_model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=FT_LR),
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=["accuracy"]
)
training_history = classification_model.fit(
x = fineTuneData,
y = fineTuneLabel,
batch_size=finetune_batch_size,
shuffle=True,
epochs=finetune_epoch,
callbacks=callbacksList,
verbose=2,
validation_data=(valData,valLabel)
)
classification_model.load_weights(best_validation_weights_dir)
macro_f1_list.append(utils.getF1Macro(testLabel,classification_model.predict(testData, verbose = 0)))
plotCurveEpoch = finetune_epoch
if(stop_early.stopped_epoch != 0):
plotCurveEpoch = stop_early.stopped_epoch + 1
utils.plot_learningCurve(training_history,plotCurveEpoch,evaluation_dir,'Graph_Unfrozen_FE_')
with open(evaluation_dir +'Result_Report.csv','w') as f:
w = csv.writer(f)
w.writerow(["Result_Frozen_FE"])
w.writerow([str(macro_f1_list[0])])
w.writerow(['Result_Unfrozen_FE'])
w.writerow([str(macro_f1_list[1])])
return macro_f1_list