-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmutils.py
322 lines (260 loc) · 9.26 KB
/
mutils.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
310
311
312
313
314
315
316
317
318
319
320
321
322
"""Utility functions."""
import json
import torch
from typing import List, Optional
import numpy as np
from sklearn.metrics import (
classification_report,
f1_score,
precision_score,
recall_score,
mean_squared_error,
accuracy_score,
)
from tqdm import tqdm
def compute_metrics(eval_pred):
"""Compute metrics."""
logits, labels = eval_pred
logits = torch.FloatTensor(logits)
preds = (torch.sigmoid(logits) >= 0.5).float().numpy()
f1_micro = f1_score(labels, preds, average="micro")
f1_macro = f1_score(labels, preds, average="macro")
p_macro = precision_score(labels, preds, average="macro")
p_micro = precision_score(labels, preds, average="micro")
r_macro = recall_score(labels, preds, average="macro")
r_micro = recall_score(labels, preds, average="micro")
metrics = {
"f1_micro": f1_micro,
"f1_macro": f1_macro,
"p_macro": p_macro,
"p_micro": p_micro,
"r_macro": r_macro,
"r_micro": r_micro,
}
return metrics
def compute_metrics_binary(eval_pred, pos_id=0):
"""Compute metrics."""
logits, labels = eval_pred
preds = logits.argmax(axis=1)
acc = accuracy_score(labels, preds)
f1 = f1_score(labels, preds, average="binary", pos_label=pos_id)
p = precision_score(labels, preds, average="binary", pos_label=pos_id)
r = recall_score(labels, preds, average="binary", pos_label=pos_id)
metrics = {
"acc": acc,
"f1": f1,
"p": p,
"r": r,
}
return metrics
def compute_metrics_soft(eval_pred):
"""Compute metrics."""
logits, labels = eval_pred
# convert labels
labels = torch.FloatTensor(labels)
labels = (labels >= 0.5).float().numpy()
# convert logits
logits = torch.FloatTensor(logits)
preds = (torch.sigmoid(logits) >= 0.5).float().numpy()
# calculate metrics
mse = mean_squared_error(labels, logits)
f1_micro = f1_score(labels, preds, average="micro")
f1_macro = f1_score(labels, preds, average="macro")
p_macro = precision_score(labels, preds, average="macro")
p_micro = precision_score(labels, preds, average="micro")
r_macro = recall_score(labels, preds, average="macro")
r_micro = recall_score(labels, preds, average="micro")
metrics = {
"mse": mse,
"f1_micro": f1_micro,
"f1_macro": f1_macro,
"p_macro": p_macro,
"p_micro": p_micro,
"r_macro": r_macro,
"r_micro": r_micro,
}
return metrics
def compute_metrics_with_logits_multiclass(eval_pred):
"""Compute metrics."""
logits, labels = eval_pred
preds = logits.argmax(axis=1)
f1_micro = f1_score(labels, preds, average="micro")
f1_macro = f1_score(labels, preds, average="macro")
p_macro = precision_score(labels, preds, average="macro")
p_micro = precision_score(labels, preds, average="micro")
r_macro = recall_score(labels, preds, average="macro")
r_micro = recall_score(labels, preds, average="micro")
metrics = {
"f1_micro": f1_micro,
"f1_macro": f1_macro,
"p_macro": p_macro,
"p_micro": p_micro,
"r_macro": r_macro,
"r_micro": r_micro,
}
return metrics
def compute_metrics_debug(eval_pred, mlb):
logits, labels = eval_pred
logits = torch.FloatTensor(logits)
preds = (torch.sigmoid(logits) >= 0.5).float().numpy()
sample_labels = labels[0:4]
sample_labels = mlb.inverse_transform(sample_labels)
sample_preds = preds[0:4]
sample_preds = mlb.inverse_transform(sample_preds)
print(f"Sample labels: {sample_labels}")
print(f"Sample preds: {sample_preds}")
f1_micro = f1_score(labels, preds, average="micro")
f1_macro = f1_score(labels, preds, average="macro")
p_macro = precision_score(labels, preds, average="macro")
p_micro = precision_score(labels, preds, average="micro")
r_macro = recall_score(labels, preds, average="macro")
r_micro = recall_score(labels, preds, average="micro")
metrics = {
"f1_micro": f1_micro,
"f1_macro": f1_macro,
"p_macro": p_macro,
"p_micro": p_micro,
"r_macro": r_macro,
"r_micro": r_micro,
}
return metrics
def write_metrics(
y_true,
y_pred,
output_file,
target_names: Optional[List[str]] = None,
expand_neutral: Optional[str] = None,
):
"""Compute and write final metrics.
Expects softmax or sigmoid outputs.
"""
if expand_neutral:
if target_names:
target_names = target_names + [expand_neutral]
# expand y_true and y_pred to have a neutral class
mask = ~np.any(y_true >= 0.5, axis=1)
y_true = np.c_[y_true, np.zeros(y_true.shape[0])]
y_true[mask, -1] = 1
mask = ~np.any(y_pred >= 0.5, axis=1)
y_pred = np.c_[y_pred, np.zeros(y_pred.shape[0])]
y_pred[mask, -1] = 1
f1_micro = f1_score(y_true, y_pred, average="micro")
f1_macro = f1_score(y_true, y_pred, average="macro")
p_macro = precision_score(y_true, y_pred, average="macro")
p_micro = precision_score(y_true, y_pred, average="micro")
r_macro = recall_score(y_true, y_pred, average="macro")
r_micro = recall_score(y_true, y_pred, average="micro")
metrics = {
"f1_micro": f1_micro,
"f1_macro": f1_macro,
"p_macro": p_macro,
"p_micro": p_micro,
"r_macro": r_macro,
"r_micro": r_micro,
}
report = classification_report(
y_true, y_pred, target_names=target_names, digits=4,
)
print(report)
print(f"Results recorded in {output_file}")
with open(output_file, "w") as fout:
print(json.dumps(metrics), file=fout)
print(report, file=fout)
return metrics
def write_run_metrics(
y_true,
y_pred,
output_file,
target_names: Optional[List[str]] = None,
expand_neutral: Optional[str] = None,
):
"""Compute and write final metrics.
Expects softmax or sigmoid outputs.
"""
if expand_neutral:
if target_names:
target_names = target_names + [expand_neutral]
# expand y_true and y_pred to have a neutral class
mask = ~np.any(y_true >= 0.5, axis=1)
y_true = np.c_[y_true, np.zeros(y_true.shape[0])]
y_true[mask, -1] = 1
mask = ~np.any(y_pred >= 0.5, axis=1)
y_pred = np.c_[y_pred, np.zeros(y_pred.shape[0])]
y_pred[mask, -1] = 1
f1_macro = f1_score(y_true, y_pred, average="macro")
metrics = {
"f1_macro": f1_macro,
}
print(f"Results recorded in {output_file}")
with open(output_file, "a") as fout:
print(json.dumps(metrics), file=fout)
return metrics
def compute_metrics_with_logits_multilabel(eval_pred):
"""Compute metrics."""
logits, labels = eval_pred
logits = torch.FloatTensor(logits)
preds = (torch.sigmoid(logits) >= 0.5).float().numpy()
f1_micro = f1_score(labels, preds, average="micro")
f1_macro = f1_score(labels, preds, average="macro")
p_macro = precision_score(labels, preds, average="macro")
p_micro = precision_score(labels, preds, average="micro")
r_macro = recall_score(labels, preds, average="macro")
r_micro = recall_score(labels, preds, average="micro")
metrics = {
"f1_micro": f1_micro,
"f1_macro": f1_macro,
"p_macro": p_macro,
"p_micro": p_micro,
"r_macro": r_macro,
"r_micro": r_micro,
}
return metrics
def perform_inference(
model, dataset, batch_size: int = 128, multilabel: bool = True,
):
"""Perform inference on a multilabel dataset.
Args:
model (torch.nn.Module): The model to use for inference.
dataset (torch.utils.data.Dataset): The dataset to perform inference on.
batch_size (int, optional): The batch size to use for inference.
"""
# Set the model to evaluation mode
model.eval()
device = next(model.parameters()).device
# collate_fn = DataCollatorWithPadding(tokenizer=tokenizer)
# Create an empty list to store the predictions
all_logits = []
# Loop through the dataset in batches
for i in tqdm(range(0, len(dataset), batch_size), dynamic_ncols=True):
inputs = dataset[i : i + batch_size]
# don't pass labels
inputs.pop("labels", None)
# send to same device as model
inputs = {k: v.to(device) for k, v in inputs.items()}
# Pass the inputs through the model
with torch.no_grad():
outputs = model(**inputs, return_dict=True)
# Extract the predicted labels from the output
logits = outputs.logits.detach().float().cpu()
all_logits.append(logits)
all_logits = torch.vstack(all_logits)
# Handle multilabel
if multilabel:
preds = (torch.sigmoid(all_logits) >= 0.5).float().numpy()
return preds
# Handle multiclass
preds = torch.argmax(all_logits, dim=1).numpy()
return preds
def write_metrics_simple(y_true, y_pred, output_file):
"""Compute and write final metrics.
"""
f1_micro = f1_score(y_true, y_pred, average="micro")
f1_macro = f1_score(y_true, y_pred, average="macro")
metrics = {
"f1_micro": f1_micro,
"f1_macro": f1_macro,
}
print(f"Results recorded in {output_file}")
with open(output_file, "w") as fout:
print(json.dumps(metrics), file=fout)
return metrics