-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·190 lines (148 loc) · 7.61 KB
/
test.py
File metadata and controls
executable file
·190 lines (148 loc) · 7.61 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
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
import argparse
import os
import re
import lightning
import torch
from tqdm import tqdm
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
from dataset.dialogue_graph_datamodule import SubDialogueDataModule
from model.GiBERTino import GiBERTino
from utils.constants import RELATIONS
from utils.metrics import Metrics
from utils.utils import get_device
def test_model(args): # noqa
lightning.seed_everything(args.seed)
os.makedirs(args.eval_dir, exist_ok=True)
device = get_device()
print(f"Loading model from checkpoint: {args.checkpoint_path}")
try:
checkpoint = torch.load(args.checkpoint_path, map_location=device, weights_only=False)
model_config = checkpoint.get("hyper_parameters", {})
in_channels = model_config.get("in_channels", 770)
hidden_channels = model_config.get("hidden_channels", 10)
num_layers = model_config.get("num_layers", 3)
print(f"Model parameters loaded from checkpoint: "
f"in_channels={in_channels}, hidden_channels={hidden_channels}, num_layers={num_layers}")
model = GiBERTino.load_from_checkpoint(args.checkpoint_path, map_location=device)
model.eval()
print("Loading test data...")
data_module = SubDialogueDataModule(args.data_path, num_workers=0)
data_module.setup(stage="test")
test_loader = data_module.test_dataloader()
metrics = Metrics(num_classes=len(RELATIONS['UNIFIED']), log_dir=args.eval_dir)
all_metrics = []
all_rel_preds = []
all_rel_labels = []
print("Running classification...")
with torch.no_grad():
for batch in tqdm(test_loader, desc="Processing test data"):
link_logits, rel_probs = model(batch)
link_labels = batch[('edu', 'to', 'edu')].get('link_labels', None)
rel_labels = batch[('edu', 'to', 'edu')].get('rel_labels', None)
link_metrics = metrics.compute_metrics(link_logits, link_labels, 'link', 'test', 0)
rel_metrics = metrics.compute_metrics(rel_probs, rel_labels, 'rel', 'test', 0)
batch_metrics = {
'link_accuracy': link_metrics['link_accuracy'],
'link_precision': link_metrics['link_precision'],
'link_recall': link_metrics['link_recall'],
'link_f1': link_metrics['link_f1'],
'link_roc': link_metrics['link_roc'],
'rel_accuracy': rel_metrics['rel_accuracy'],
'rel_precision': rel_metrics['rel_precision'],
'rel_recall': rel_metrics['rel_recall'],
'rel_f1': rel_metrics['rel_f1'],
'rel_roc': rel_metrics['rel_roc']
}
metrics.log(batch_metrics, 'test', 0)
all_metrics.append(batch_metrics)
if rel_labels is not None:
preds = rel_probs.argmax(dim=-1).cpu().numpy()
labels = rel_labels.cpu().numpy()
all_rel_preds.extend(preds.flatten())
all_rel_labels.extend(labels.flatten())
aggregated_metrics = metrics.aggregate_metrics()
print("Test results:")
for key, value in aggregated_metrics.items():
print(f"{key}: {value:.4f}")
print("\nAggregate metrics:")
print(f"Link Prediction Accuracy: {aggregated_metrics['link_accuracy']:.4f}")
print(f"Relation Classification Accuracy: {aggregated_metrics['rel_accuracy']:.4f}")
eval_path = os.path.join(args.eval_dir, args.eval_file)
header = "Model;Dataset;" + ";".join(aggregated_metrics.keys()) + "\n"
if os.path.exists(eval_path):
with open(eval_path, "r", encoding="utf-8") as f:
first_line = f.readline().strip()
else:
first_line = ""
with open(eval_path, "a", encoding="utf-8") as f:
if first_line != header.strip():
f.write(header)
f.write(
f"{args.model_name};{args.dataset_name};" +
";".join(f"{c:.5f}" for c in aggregated_metrics.values()) + "\n"
)
print(f"Metrics saved to {eval_path}")
# Confusion matrix for relation prediction
if all_rel_preds and all_rel_labels:
cm = confusion_matrix(all_rel_labels, all_rel_preds, labels=range(len(RELATIONS['UNIFIED'])))
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=RELATIONS['UNIFIED'])
fig, ax = plt.subplots(figsize=(12, 10))
disp.plot(ax=ax, xticks_rotation='vertical', cmap='Blues', values_format='d')
cm_path = os.path.join(args.eval_dir, f"confusion_matrix_{args.model_name}_{args.dataset_name}.png")
plt.title(f"Relation Confusion Matrix: {args.model_name} on {args.dataset_name}")
plt.savefig(cm_path, bbox_inches='tight')
plt.close()
print(f"Confusion matrix saved to {cm_path}")
return aggregated_metrics
except Exception as e:
print(f"An error occurred: {e}")
return None
def find_checkpoints(root_dir):
"""
Recursively finds .ckpt files in a 'checkpoints' subdirectory.
Returns a list of tuples: (checkpoint_path, model_name, dataset_name)
Expected filename format: giBERTino-<model_name>-<dataset_name>-epoch=xx-val_loss=xx.ckpt
"""
pattern = re.compile(r"giBERTino-(.+)-(.+?)-epoch=\d+-val_loss=[\d\.]+\.ckpt$")
found = []
for dirpath, dirnames, filenames in os.walk(root_dir):
if "checkpoints" in dirpath:
for filename in filenames:
if filename.endswith(".ckpt"):
match = pattern.match(filename)
if match:
model_name, dataset_name = match.groups()
full_path = os.path.join(dirpath, filename)
found.append((full_path, model_name + "-" + dataset_name, dataset_name))
return found
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--seed", type=int, default=42)
parser.add_argument("--eval_dir", type=str, default=os.path.join(os.getcwd(), "eval_results"))
parser.add_argument("--eval_file", type=str, default="scores.csv")
parser.add_argument("--root_dir", type=str, required=True, help="Root directory to search for checkpoints")
parser.add_argument("--data_root", type=str, required=True, help="Root directory where datasets are stored")
args = parser.parse_args()
checkpoint_infos = find_checkpoints(args.root_dir)
datasets = ["STAC", "MOLWENI", "MINECRAFT", "BALANCED"]
if not checkpoint_infos:
print("No valid checkpoints found.")
else:
for ckpt_path, model_name, _ in checkpoint_infos: # Ignore dataset_name from filename
for dataset_name in datasets:
dataset_path = os.path.join(args.data_root, dataset_name, "alibaba-graphs")
if not os.path.exists(dataset_path):
print(f"Dataset path does not exist for {dataset_name}: {dataset_path}")
continue
print(f"\n--- Evaluating {ckpt_path} on {dataset_name} ---")
test_args = argparse.Namespace(
seed=args.seed,
eval_dir=args.eval_dir,
eval_file=args.eval_file,
checkpoint_path=ckpt_path,
data_path=dataset_path,
model_name=model_name,
dataset_name=dataset_name
)
test_model(test_args)