-
Notifications
You must be signed in to change notification settings - Fork 1
/
neural_metrics.py
309 lines (268 loc) · 16.2 KB
/
neural_metrics.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import typing
import numpy as np
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, balanced_accuracy_score
import utils
import data_preprocessing
def get_change_str(change: typing.Union[float, str]):
return '' if change == '' else (utils.red_text(f'({round(change, 2)}%)') if change < 0
else utils.green_text(f'(+{round(change, 2)}%)'))
def print_num_inconsistencies(preprocessor: data_preprocessing.FineCoarseDataPreprocessor,
pred_fine_data: np.array,
pred_coarse_data: np.array,
current_num_test_inconsistencies=None,
original_test_inconsistencies=None,
prior: bool = True):
"""
Prints the number of inconsistencies between fine and coarse predictions.
:param current_num_test_inconsistencies:
:param original_test_inconsistencies:
:param preprocessor:
:param pred_fine_data: NumPy array of predictions at the fine granularity.
:param pred_coarse_data: NumPy array of predictions at the coarse granularity.
:param prior:
"""
inconsistencies, _ = preprocessor.get_num_inconsistencies(fine_labels=pred_fine_data,
coarse_labels=pred_coarse_data)
print(f"Total {'prior' if prior else 'post'} inconsistencies "
f"{utils.red_text(inconsistencies)}/{utils.red_text(len(pred_fine_data))} "
f'({utils.red_text(round(inconsistencies / len(pred_fine_data) * 100, 2))}%)\n')
# if current_num_test_inconsistencies is not None and original_test_inconsistencies is not None:
# print(f'Recovered inconsistencies: '
# f'{round(current_num_test_inconsistencies / original_test_inconsistencies[1] * 100, 2)}%'
# )
def get_individual_metrics(pred_data: np.array,
true_data: np.array,
labels: list = None,
binary: bool = False):
accuracy = accuracy_score(y_true=true_data,
y_pred=pred_data)
balanced_accuracy = balanced_accuracy_score(y_true=true_data,
y_pred=pred_data)
f1 = f1_score(y_true=true_data,
y_pred=pred_data,
labels=labels,
average='macro'
)
precision = precision_score(y_true=true_data,
y_pred=pred_data,
labels=labels,
average='macro'
)
recall = recall_score(y_true=true_data,
y_pred=pred_data,
labels=labels,
average='macro'
)
# if len(labels) > 3: for idx in labels: precision_per_class = precision_score(y_true=true_data,
# y_pred=pred_data, labels=labels, average=None)[idx] recall_per_class = recall_score(y_true=true_data,
# y_pred=pred_data, labels=labels, average=None)[idx] print(f'class {idx} has precision {precision_per_class} and
# recall {recall_per_class}')
if not binary:
return accuracy, f1, precision, recall
return accuracy, balanced_accuracy, f1, precision, recall
def get_metrics(preprocessor: data_preprocessing.FineCoarseDataPreprocessor,
pred_fine_data: np.array,
pred_coarse_data: np.array,
true_fine_data: np.array,
true_coarse_data: np.array):
"""
Calculates and returns performance metrics for fine and coarse granularities.
:param preprocessor:
:param pred_fine_data: NumPy array of predictions at the fine granularity.
:param pred_coarse_data: NumPy array of predictions at the coarse granularity.
:param true_fine_data: NumPy array of true labels at the fine granularity.
:param true_coarse_data: NumPy array of true labels at the coarse granularity.
:return: A tuple containing the accuracy, F1, precision, and recall metrics
for both fine and coarse granularities.
"""
fine_accuracy, fine_f1, fine_precision, fine_recall = (
get_individual_metrics(pred_data=pred_fine_data,
true_data=true_fine_data,
labels=list(range(len(preprocessor.fine_grain_classes_str)))))
coarse_accuracy, coarse_f1, coarse_precision, coarse_recall = (
get_individual_metrics(pred_data=pred_coarse_data,
true_data=true_coarse_data,
labels=list(range(len(preprocessor.coarse_grain_classes_str)))))
return (fine_accuracy, fine_f1, fine_precision, fine_recall,
coarse_accuracy, coarse_f1, coarse_precision, coarse_recall)
def get_and_print_binary_metrics(pred_data: np.array,
loss: str,
true_data: np.array,
test: bool,
prior: bool = True,
model_name: str = '',
lr: typing.Union[str, float] = ''):
"""
Calculates, prints, and returns accuracy metrics for fine and coarse granularities.
:param true_data:
:param pred_data:
:param loss: The loss function used during training.
:param test: True for test data, False for training data.
:param prior:
:param model_name: The name of the model (optional).
:param lr: The learning rate used during training (optional).
:return: fine_accuracy, coarse_accuracy
"""
accuracy, f1, precision, recall = get_individual_metrics(pred_data=pred_data,
true_data=true_data,
labels=[0, 1])
prior_str = 'prior' if prior else 'post'
test_str = 'Test' if test else 'Train'
print('#' * 100 + '\n' + (f'Main model name: {utils.blue_text(model_name)} ' if model_name != '' else '') +
f"with {utils.blue_text(loss)} loss on the {utils.blue_text('test' if test else 'train')} dataset\n" +
(f'with lr={utils.blue_text(lr)}\n' if lr != '' else '') +
f'\n{test_str} {prior_str} accuracy: {utils.green_text(round(accuracy * 100, 2))}%'
f' {test_str} {prior_str} macro f1: {utils.green_text(round(f1 * 100, 2))}%'
f'\n {test_str} {prior_str} macro precision: '
f'{utils.green_text(round(precision * 100, 2))}%'
f', {test_str} {prior_str} macro recall: {utils.green_text(round(recall * 100, 2))}%\n')
return accuracy, f1, precision, recall
def get_and_print_metrics(preprocessor: data_preprocessing.FineCoarseDataPreprocessor,
pred_fine_data: np.array,
pred_coarse_data: np.array,
loss: str,
true_fine_data: np.array,
true_coarse_data: np.array,
split: str,
prior: bool = True,
combined: bool = True,
model_name: str = '',
lr: typing.Union[str, float] = '',
print_inconsistencies: bool = True,
current_num_test_inconsistencies=None,
original_test_inconsistencies=None,
original_pred_fine_data: np.array = None,
original_pred_coarse_data: np.array = None):
"""
Calculates, prints, and returns accuracy metrics for fine and coarse granularities.
:param split:
:param current_num_test_inconsistencies:
:param original_test_inconsistencies:
:param preprocessor:
:param original_pred_coarse_data:
:param original_pred_fine_data:
:param print_inconsistencies:
:param pred_fine_data: NumPy array of predictions at the fine granularity.
:param pred_coarse_data: NumPy array of predictions at the coarse granularity.
:param loss: The loss function used during training.
:param true_fine_data: NumPy array of true labels at the fine granularity.
:param true_coarse_data: NumPy array of true labels at the coarse granularity.
:param prior:
:param combined: Whether the model are individual or combine one.
:param model_name: The name of the model (optional).
:param lr: The learning rate used during training (optional).
:return: fine_accuracy, coarse_accuracy
"""
(fine_accuracy, fine_f1, fine_precision, fine_recall,
coarse_accuracy, coarse_f1, coarse_precision, coarse_recall) = get_metrics(preprocessor=preprocessor,
pred_fine_data=pred_fine_data,
pred_coarse_data=pred_coarse_data,
true_fine_data=true_fine_data,
true_coarse_data=true_coarse_data)
prior_str = 'prior' if prior else 'post'
combined_str = 'combined' if combined else 'individual'
fine_accuracy_change_str = ''
fine_f1_change_str = ''
fine_precision_change_str = ''
fine_recall_change_str = ''
coarse_accuracy_change_str = ''
coarse_f1_change_str = ''
coarse_precision_change_str = ''
coarse_recall_change_str = ''
if original_pred_fine_data is not None and original_pred_coarse_data is not None:
(original_fine_accuracy, original_fine_f1, original_fine_precision, original_fine_recall,
original_coarse_accuracy, original_coarse_f1, original_coarse_precision, original_coarse_recall) = get_metrics(
preprocessor=preprocessor,
pred_fine_data=original_pred_fine_data,
pred_coarse_data=original_pred_coarse_data,
true_fine_data=true_fine_data,
true_coarse_data=true_coarse_data)
fine_accuracy_change_str = fine_accuracy - original_fine_accuracy
fine_f1_change_str = fine_f1 - original_fine_f1
fine_precision_change_str = fine_precision - original_fine_precision
fine_recall_change_str = fine_recall - original_fine_recall
coarse_accuracy_change_str = coarse_accuracy - original_coarse_accuracy
coarse_f1_change_str = coarse_f1 - original_coarse_f1
coarse_precision_change_str = coarse_precision - original_coarse_precision
coarse_recall_change_str = coarse_recall - original_coarse_recall
print('#' * 100 + '\n' + (f'Main model name: {utils.blue_text(model_name)} ' if model_name != '' else '') +
f"with {utils.blue_text(loss)} loss on the {split} dataset\n" +
(f'with lr={utils.blue_text(lr)}\n' if lr != '' else '') +
f'\nFine-grain {prior_str} {combined_str} accuracy: {utils.green_text(round(fine_accuracy * 100, 2))}%'
f' {get_change_str(fine_accuracy_change_str)}, '
f'fine-grain {prior_str} {combined_str} macro f1: {utils.green_text(round(fine_f1 * 100, 2))}%'
f' {get_change_str(fine_f1_change_str)}'
f'\nFine-grain {prior_str} {combined_str} macro precision: '
f'{utils.green_text(round(fine_precision * 100, 2))}% {get_change_str(fine_precision_change_str)}'
f', fine-grain {prior_str} {combined_str} macro recall: {utils.green_text(round(fine_recall * 100, 2))}%'
f' {get_change_str(fine_recall_change_str)}\n'
f'\nCoarse-grain {prior_str} {combined_str} accuracy: '
f'{utils.green_text(round(coarse_accuracy * 100, 2))}% {get_change_str(coarse_accuracy_change_str)}'
f', coarse-grain {prior_str} {combined_str} macro f1: '
f'{utils.green_text(round(coarse_f1 * 100, 2))}% {get_change_str(coarse_f1_change_str)}'
f'\nCoarse-grain {prior_str} {combined_str} macro precision: '
f'{utils.green_text(round(coarse_precision * 100, 2))}% {get_change_str(coarse_precision_change_str)}'
f', coarse-grain {prior_str} {combined_str} macro recall: '
f'{utils.green_text(round(coarse_recall * 100, 2))}% {get_change_str(coarse_recall_change_str)}\n'
)
if print_inconsistencies:
print_num_inconsistencies(preprocessor=preprocessor,
pred_fine_data=pred_fine_data,
pred_coarse_data=pred_coarse_data,
current_num_test_inconsistencies=current_num_test_inconsistencies,
original_test_inconsistencies=original_test_inconsistencies,
prior=prior)
return fine_accuracy, coarse_accuracy, fine_f1, coarse_f1
def get_and_print_post_epoch_binary_metrics(epoch: int,
num_epochs: int,
train_ground_truths: np.array,
train_predictions: np.array,
total_running_loss: float):
accuracy, f1, _, _ = get_individual_metrics(pred_data=train_predictions,
true_data=train_ground_truths,
labels=[1])
print(f'\nEpoch {epoch + 1}/{num_epochs} done'
f'\nMean loss across epochs: {round(total_running_loss / (epoch + 1), 2)}'
f'\npost-epoch training accuracy: {round(accuracy * 100, 2)}%'
f', post-epoch f1: {round(f1 * 100, 2)}%\n')
return accuracy, f1
def get_and_print_post_metrics(preprocessor: data_preprocessing.FineCoarseDataPreprocessor,
train_fine_ground_truth: np.array,
train_fine_prediction: np.array,
train_coarse_ground_truth: np.array,
train_coarse_prediction: np.array,
curr_epoch: int = None,
total_num_epochs: int = None,
curr_batch_num: int = None,
total_batch_num: int = None):
training_fine_accuracy = accuracy_score(y_true=train_fine_ground_truth,
y_pred=train_fine_prediction)
training_coarse_accuracy = accuracy_score(y_true=train_coarse_ground_truth,
y_pred=train_coarse_prediction)
training_fine_f1 = f1_score(y_true=train_fine_ground_truth,
y_pred=train_fine_prediction,
labels=range(preprocessor.num_fine_grain_classes),
average='macro')
training_coarse_f1 = f1_score(y_true=train_coarse_ground_truth,
y_pred=train_coarse_prediction,
labels=range(preprocessor.num_coarse_grain_classes),
average='macro')
post_str = f'Epoch {curr_epoch + 1}/{total_num_epochs} done' if curr_epoch is not None \
else f'Batch {curr_batch_num}/{total_batch_num} done'
print(f'\n{post_str}'
f'\nTraining fine accuracy: {round(training_fine_accuracy * 100, 2)}%'
f', training fine f1: {round(training_fine_f1 * 100, 2)}%'
f'\nLast batch training coarse accuracy: {round(training_coarse_accuracy * 100, 2)}%'
f', last batch coarse f1: {round(training_coarse_f1 * 100, 2)}%\n')
return training_fine_accuracy, training_coarse_accuracy
def print_post_batch_binary_metrics(batch_num: int,
num_batches: int,
train_ground_truths: np.array,
train_predictions: np.array,
batch_total_loss: float = None):
if batch_num > 0 and batch_num % 5 == 0:
accuracy, f1, _, _ = get_individual_metrics(pred_data=train_predictions,
true_data=train_ground_truths,
labels=[0, 1])
print(f'\nCompleted batch num {batch_num}/{num_batches}, current accuracy: {accuracy:.2f},'
f'current f1: {f1:.2f}, batch total loss: {batch_total_loss:.2f}')