-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexperimentation.py
273 lines (222 loc) · 9.04 KB
/
experimentation.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import os
import gc
from typing import List
import pandas as pd
from tensorflow import keras
from networks.models import build_models
from networks.train import train_and_test_model
from networks.test import evaluate_model
from scenarios.folds_creation import (
get_folds_filenames,
get_datafold_filenames,
check_and_retrive_vocabulary,
)
from scenarios.config import MODEL_FROM
from late_fusion.predict import evaluate_multimodal_transcription
######################################################################## STAND-ALONE EVALUATION:
# Utility function for performing a k-fold cross-validation
# experiment on a single dataset (SCENARIOS A, B, and D)
def k_fold_experiment(
*, task: str, scenario_name: str, epochs: int = 150, batch_size: int = 16
):
keras.backend.clear_session()
gc.collect()
# ---------- PRINT EXPERIMENT DETAILS
print(f"5-fold cross-validation experiment for scenario {scenario_name}")
print(f"\tTask: {task}")
print(f"\tEpochs: {epochs}")
print(f"\tBatch size: {batch_size}")
# ---------- FOLDS COLLECTION
folds = get_folds_filenames(scenario_name)
# ---------- 5-FOLD EVALUATION
for id, (train_fold, val_fold, test_fold) in enumerate(
zip(folds["train"], folds["val"], folds["test"])
):
# With 'clear_session()' called at the beginning,
# Keras starts with a blank state at each iteration
# and memory consumption is constant over time
keras.backend.clear_session()
gc.collect()
print(f"Fold {id}")
# Get the current fold data
train_images, train_labels = get_datafold_filenames(
task=task, fold_filename=train_fold
)
val_images, val_labels = get_datafold_filenames(
task=task, fold_filename=val_fold
)
test_images, test_labels = get_datafold_filenames(
task=task, fold_filename=test_fold
)
print(f"Train size: {len(train_images)}")
print(f"Validation size: {len(val_images)}")
print(f"Test size: {len(test_images)}")
# Check and retrieve vocabulary
w2i, i2w = check_and_retrive_vocabulary(fold_id=id)
# Build the models
model, prediction_model = build_models(task=task, num_labels=len(w2i))
# Set filepaths outputs
output_dir = f"results/scenario{scenario_name}/fold{id}"
os.makedirs(output_dir, exist_ok=True)
pred_model_filepath = os.path.join(output_dir, f"best_{task}_model.keras")
log_path = os.path.join(output_dir, f"{task}_logs.csv")
# Train, validate, and test models
# Save logs in CSV file
train_and_test_model(
task=task,
data=(
train_images,
train_labels,
val_images,
val_labels,
test_images,
test_labels,
),
vocabularies=(w2i, i2w),
epochs=epochs,
batch_size=batch_size,
model=model,
prediction_model=prediction_model,
pred_model_filepath=pred_model_filepath,
log_path=log_path,
)
# Clear memory
del train_images, train_labels, val_images, val_labels, test_images, test_labels
del model, prediction_model
# Utility function for performing a k-fold test partition experiment
# using previously trained models (SCENARIO C)
def k_fold_experiment_scenario_c(*, task: str):
keras.backend.clear_session()
gc.collect()
# ---------- PRINT EXPERIMENT DETAILS
print("5-fold cross-validation experiment for scenario C")
print(f"\tTask: {task}")
# ---------- DATA COLLECTION
folds = get_folds_filenames("C")
# ---------- K-FOLD EVALUATION
for id, test_fold in enumerate(folds["test"]):
# With 'clear_session()' called at the beginning,
# Keras starts with a blank state at each iteration
# and memory consumption is constant over time.
keras.backend.clear_session()
gc.collect()
print(f"Fold {id}")
# Get the current fold data
test_images, test_labels = get_datafold_filenames(
task=task, fold_filename=test_fold
)
print(f"Test size: {len(test_images)}")
# Check and retrieve vocabulary
i2w = check_and_retrive_vocabulary(fold_id=id)[1]
# Test the best validation model
print("Evaluating best validation model over test data")
pred_model_filepath = f"results/scenario{MODEL_FROM[task]['C']}/fold{id}"
pred_model_filepath = os.path.join(
pred_model_filepath, f"best_{task}_model.keras"
)
assert os.path.exists(pred_model_filepath), "Model not found!"
prediction_model = keras.models.load_model(pred_model_filepath)
test_symer, test_seqer = evaluate_model(
task=task,
model=prediction_model,
images_files=test_images,
labels_files=test_labels,
i2w=i2w,
)
# Save fold logs
output_dir = f"results/scenarioC/fold{id}"
os.makedirs(output_dir, exist_ok=True)
log_path = os.path.join(output_dir, f"{task}_logs.csv")
logs = {"test_symer": [test_symer], "test_seqer": [test_seqer]}
logs = pd.DataFrame.from_dict(logs)
logs.to_csv(log_path, index=False)
# Clear memory
del test_images, test_labels
del prediction_model
######################################################################## MULTIMODAL EVALUATION:
# Utility function for performing a k-fold cross-validation multimodal experiment on a single dataset
def k_fold_multimodal_experiment(
*,
scenario_name: str,
match: List[int] = [2],
mismatch: List[int] = [-1],
gap_penalty: List[int] = [-1],
):
keras.backend.clear_session()
gc.collect()
# ---------- PRINT EXPERIMENT DETAILS
print(f"5-fold multimodal cross-validation experiment for scenario {scenario_name}")
print(f"\tMatch values: {match}")
print(f"\tMismatch values: {mismatch}")
print(f"\tGap penalty values: {gap_penalty}")
# ---------- DATA COLLECTION
folds = get_folds_filenames(scenario_name)
# ---------- K-FOLD EVALUATION
for id, test_fold in enumerate(folds["test"]):
# With 'clear_session()' called at the beginning,
# Keras starts with a blank state at each iteration
# and memory consumption is constant over time.
keras.backend.clear_session()
gc.collect()
print(f"Fold {id}")
# Get the current fold data
omr_test_images, test_labels = get_datafold_filenames(
task="omr", fold_filename=test_fold
)
amt_test_images, _ = get_datafold_filenames(task="amt", fold_filename=test_fold)
assert len(omr_test_images) == len(
amt_test_images
), "Different number of files in OMR and AMT test sets!"
print(f"Test size: {len(omr_test_images)}")
# Check and retrieve vocabulary
i2w = check_and_retrive_vocabulary(fold_id=id)[1]
# Load the models
omr_pred_model_filepath = os.path.join(
"results", f"scenario{MODEL_FROM['omr'][scenario_name]}"
)
omr_pred_model_filepath = os.path.join(
omr_pred_model_filepath, f"fold{id}", "best_omr_model.keras"
)
omr_prediction_model = keras.models.load_model(omr_pred_model_filepath)
amt_pred_model_filepath = os.path.join(
"results", f"scenario{MODEL_FROM['amt'][scenario_name]}"
)
amt_pred_model_filepath = os.path.join(
amt_pred_model_filepath, f"fold{id}", "best_amt_model.keras"
)
amt_prediction_model = keras.models.load_model(amt_pred_model_filepath)
# Set filepaths outputs
output_dir = f"results/scenario{scenario_name}/fold{id}"
os.makedirs(output_dir, exist_ok=True)
log_path = os.path.join(output_dir, "multimodal_logs.csv")
symer_acc = []
seqer_acc = []
# Iterate over the different match, mismatch and gap_penalty values
for m, mism, gp in zip(match, mismatch, gap_penalty):
# Multimodal transcription evaluation
symer, seqer = evaluate_multimodal_transcription(
omr_model=omr_prediction_model,
amt_model=amt_prediction_model,
omr_image_files=omr_test_images,
amt_image_files=amt_test_images,
labels_files=test_labels,
i2w=i2w,
match=m,
mismatch=mism,
gap_penalty=gp,
)
symer_acc.append(symer)
seqer_acc.append(seqer)
# Save fold logs
logs = {
"match": match,
"mismatch": mismatch,
"gap_penalty": gap_penalty,
"symer": symer_acc,
"seqer": seqer_acc,
}
logs = pd.DataFrame.from_dict(logs)
logs.to_csv(log_path, index=False)
# Clear memory
del omr_test_images, amt_test_images, test_labels
del omr_prediction_model, amt_prediction_model