-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraining.py
153 lines (127 loc) · 6.61 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
import utils
import simclr_utitlities
import csv
import model
import numpy as np
import tensorflow as tf
from sklearn.utils import class_weight
def downStreamPipeline(fineTuneData,fineTuneLabel,valData,valLabel,testData,testLabel,base_save_path,evaluation_dir, initWeightDir,intermediate_model, finetune_epoch = 50,finetune_batch_size = 64, output_shape = 10, feature_extraction_layer = 218, load_ini_weights = True):
macro_f1_list = []
intermediate_model.load_weights(base_save_path)
# No Tune
tag = "no_tune"
no_tuneEvaluationResult = simclr_utitlities.evaluate_model_simple(intermediate_model.predict(testData, verbose = 0), testLabel, return_dict=True)
macro_f1_list.append(no_tuneEvaluationResult['F1 Macro'])
with open(evaluation_dir +'notune_evaluation_results.csv','w') as f:
w = csv.writer(f)
w.writerows(no_tuneEvaluationResult.items())
# Linear Model
tag = "linear_eval"
linear_evaluation_model = model.create_linear_model_from_base_model(intermediate_model,
output_shape)
linear_eval_best_model_file_name = evaluation_dir+"linearModelCheckPoint.h5"
best_model_callback = tf.keras.callbacks.ModelCheckpoint(linear_eval_best_model_file_name,
monitor='val_accuracy', mode='max', save_best_only=True, save_weights_only=True, verbose=0
)
training_history = linear_evaluation_model.fit(
x = fineTuneData,
y = fineTuneLabel,
batch_size=finetune_batch_size,
shuffle=True,
epochs=finetune_epoch,
# class_weight=class_weights,
callbacks=[best_model_callback],
verbose=2,
validation_data = (valData,valLabel),
)
linear_evaluation_model.load_weights(linear_eval_best_model_file_name)
linearEvaluationResult = simclr_utitlities.evaluate_model_simple(linear_evaluation_model.predict(testData, verbose = 0), testLabel, return_dict=True)
macro_f1_list.append(linearEvaluationResult['F1 Macro'])
with open(evaluation_dir +'linear_evaluation_results.csv','w') as f:
w = csv.writer(f)
w.writerows(linearEvaluationResult.items())
utils.plot_learningCurve(training_history,finetune_epoch,evaluation_dir,'frozen_linear_')
# FULL HAR MODEL
tag = "full_eval"
full_evaluation_model = model.create_full_classification_model_from_base_model(intermediate_model, output_shape, freeze_fe = True)
full_eval_best_model_file_name = evaluation_dir+"fullModelCheckPoint.h5"
best_model_callback = tf.keras.callbacks.ModelCheckpoint(full_eval_best_model_file_name,
monitor='val_accuracy', mode='max', save_best_only=True, save_weights_only=True, verbose=0
)
training_history = full_evaluation_model.fit(
x = fineTuneData,
y = fineTuneLabel,
batch_size=finetune_batch_size,
shuffle=True,
epochs=finetune_epoch,
# class_weight=class_weights,
callbacks=[best_model_callback],
verbose=2,
validation_data=(valData,valLabel)
)
full_evaluation_model.load_weights(full_eval_best_model_file_name)
# print("Model with lowest validation Acc:")
fullEvaluationResult = simclr_utitlities.evaluate_model_simple(full_evaluation_model.predict(testData, verbose = 0), testLabel, return_dict=True)
macro_f1_list.append(fullEvaluationResult['F1 Macro'])
# print(fullEvaluationResult, flush= True)
with open(evaluation_dir +'full_evaluation_results.csv','w') as f:
w = csv.writer(f)
w.writerows(fullEvaluationResult.items())
utils.plot_learningCurve(training_history,finetune_epoch,evaluation_dir,'frozen_full_')
# Full HAR Model Unfrozen
tag = "full_eval_unfrozen"
full_evaluation_model = model.create_full_classification_model_from_base_model(intermediate_model, output_shape, freeze_fe = False)
full_eval_best_model_file_name = evaluation_dir+"fullModelUnfreezeCheckPoint.h5"
best_model_callback = tf.keras.callbacks.ModelCheckpoint(full_eval_best_model_file_name,
monitor='val_accuracy', mode='max', save_best_only=True, save_weights_only=True, verbose=1
)
training_history = full_evaluation_model.fit(
x = fineTuneData,
y = fineTuneLabel,
batch_size=finetune_batch_size,
shuffle=True,
epochs=finetune_epoch,
# class_weight=class_weights,
callbacks=[best_model_callback],
verbose=2,
validation_data=(valData,valLabel)
)
full_evaluation_model.load_weights(full_eval_best_model_file_name)
# print("Model with lowest validation Acc:")
fullEvaluationResult = simclr_utitlities.evaluate_model_simple(full_evaluation_model.predict(testData, verbose = 0), testLabel, return_dict=True)
# print(fullEvaluationResult, flush= True)
macro_f1_list.append(fullEvaluationResult['F1 Macro'])
with open(evaluation_dir +'unfrozen_full_evaluation_results.csv','w') as f:
w = csv.writer(f)
w.writerows(fullEvaluationResult.items())
utils.plot_learningCurve(training_history,finetune_epoch,evaluation_dir,'unfrozen_full_')
### Full HAR Random Model unfrozen
tag = "full_eval_unfrozen_random"
if(load_ini_weights):
intermediate_model.load_weights(initWeightDir)
full_evaluation_model = model.create_full_classification_model_from_base_model(intermediate_model, output_shape, freeze_fe = False)
full_eval_best_model_file_name = evaluation_dir+"fullModelUnfreezeRandomCheckPoint.h5"
best_model_callback = tf.keras.callbacks.ModelCheckpoint(full_eval_best_model_file_name,
monitor='val_accuracy', mode='max', save_best_only=True, save_weights_only=True, verbose=1
)
training_history = full_evaluation_model.fit(
x = fineTuneData,
y = fineTuneLabel,
batch_size=finetune_batch_size,
shuffle=True,
epochs=finetune_epoch,
# class_weight=class_weights,
callbacks=[best_model_callback],
verbose=2,
validation_data=(valData,valLabel)
)
full_evaluation_model.load_weights(full_eval_best_model_file_name)
# print("Model with lowest validation Acc:")
fullEvaluationResult = simclr_utitlities.evaluate_model_simple(full_evaluation_model.predict(testData, verbose = 0), testLabel, return_dict=True)
macro_f1_list.append(fullEvaluationResult['F1 Macro'])
# print(fullEvaluationResult, flush= True)
with open(evaluation_dir +'unfrozen_random_full_evaluation_results.csv','w') as f:
w = csv.writer(f)
w.writerows(fullEvaluationResult.items())
utils.plot_learningCurve(training_history,finetune_epoch,evaluation_dir,'unfrozen_rand_full_')
return macro_f1_list