-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.py
More file actions
140 lines (112 loc) · 4.13 KB
/
eval.py
File metadata and controls
140 lines (112 loc) · 4.13 KB
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
import pandas as pd
from pathlib import Path
import torch
from utilities.eval_metrics import (
class_report_df,
metrics_summary,
plot_confusion_matrix,
)
from utilities.hf_pipeline import inf_predictions
from utilities.stats_tools import class_model_comparison
DEFAULT_MAPPING = {
0: "sadness",
1: "joy",
2: "love",
3: "anger",
4: "fear",
5: "surprise",
}
def build_runtime_args(data_dir: str | Path, device: str) -> dict:
"""
Build all required kwargs for inference, metrics, and model comparison.
:param data_dir: Path to the data directory
:param device: spcufied whether to use CPU or GPU
:return: A dictionary of all required kwargs
:rtype: dict
"""
"""Build all required kwargs for inference, metrics, and model comparison."""
plot_dir = Path("results/plots")
metrics_dir = Path("results/metrics")
plot_dir.mkdir(parents=True, exist_ok=True)
metrics_dir.mkdir(parents=True, exist_ok=True)
data_dir = Path(data_dir)
mapping_dict = DEFAULT_MAPPING.copy()
return {
"data_dir": data_dir,
"plot_dir": plot_dir,
"metrics_dir": metrics_dir,
"mapping_dict": mapping_dict,
"num_labels": len(mapping_dict),
"id2label": mapping_dict,
"label2id": {v: k for k, v in mapping_dict.items()},
"device": device,
"quantized": False,
"max_length": 128,
"batch": 32,
"rounds": 1000,
"outdir": str(plot_dir),
"size": None,
"show": True,
"model_name": "qlora_model",
"models_list": [],
"seed": 1234,
}
def run_test(adapter_dir, x_test, y_test, **args):
out, model, m_name = inf_predictions(adapter_dir, x_test, y_test, **args)
if "/" in m_name:
m_name = m_name.split("/")[-1]
lbs, preds = out[0], out[1]
cls_report = class_report_df(lbs, preds, mapping_dict=args['mapping_dict'],
output_dict=True, m_name=m_name,
save_path=args['metrics_dir'])
print(cls_report)
classes = list(args['mapping_dict'].values())
plot_confusion_matrix(
lbs,
preds,
classes=classes,
image_name=f"cm_{m_name}",
outdir=args["plot_dir"],
)
print("")
bootstrap_metrics = metrics_summary(
predictions=preds,
labels=lbs,
rounds=args["rounds"],
model_name=m_name,
seed=11,
)
print(bootstrap_metrics)
# Save predictions for comparison for individual model evaluations
bootstrap_metrics.to_csv(args['metrics_dir'] / f"bs-qlora-{m_name}.csv", index=False)
return lbs, preds, m_name
def ensure_list(obj):
"""Standardizes input to be a list."""
return obj if isinstance(obj, list) else [obj]
if __name__ == "__main__":
# Can be one of: Path("local/dir"), "hf/repo", or a [list] of either.
adapt_dir = ["Wb-az/peft-roberta-base", "Wb-az/peft-opt-350m", "Wb-az/peft-modernbert-base"]
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
args = build_runtime_args(data_dir=Path("dataset"), device=device)
# Data Loading
test = pd.read_csv(Path(args["data_dir"]) / "test_dataset.csv")
x_test, y_test = test["text"].values, test["label"].values
# Test execution
binary_results = {}
for adapter in ensure_list(adapt_dir):
print(f"\n>>> Running Evaluation: {adapter}")
# The run_test extracts 'm_name' internally
lbs, preds, m_name = run_test(adapter, x_test, y_test, **args)
# Store results for the comparison table
binary_results[m_name] = (preds == lbs).astype(int)
# Triggers comparison if more than one adapter is provided
if len(binary_results) > 1:
print("\n--- Running Group Comparison ---")
binary_res_df = pd.DataFrame(binary_results).astype(int)
comparison = class_model_comparison(binary_res_df, plot=True, **args)
if comparison:
print("\nComparison Matrix:\n", comparison[0])
print("")
print("\nModels Direction:\n", comparison[1])
else:
print("\nSingle model evaluation complete.")