File size: 4,677 Bytes
945de56 | 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 | """
Evaluate a trained BERT or DistilBERT model on the test split.
Uses Trainer.predict() for inference and sklearn for detailed metrics.
Usage:
python -m src.models.evaluate --model_dir checkpoints/distilbert/best
python -m src.models.evaluate --model_dir checkpoints/bert/best --split val
"""
import argparse
import json
import logging
from pathlib import Path
import numpy as np
import torch
from sklearn.metrics import classification_report, confusion_matrix, f1_score, matthews_corrcoef
from transformers import (
AutoModelForSequenceClassification,
AutoTokenizer,
Trainer,
TrainingArguments,
)
from src.datasets.combined_pairs_dataset import (
CombinedPairsDataset,
CombinedPairsConfig,
ID2LABEL,
)
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger(__name__)
def main() -> None:
parser = argparse.ArgumentParser(description="Evaluate sentence-pair boundary classifier.")
parser.add_argument("--model_dir", required=True, help="Path to saved model directory")
parser.add_argument("--split", choices=["val", "test"], default="test")
parser.add_argument("--data_root", default="data")
parser.add_argument("--batch_size", type=int, default=32)
parser.add_argument("--max_length", type=int, default=512)
parser.add_argument("--seed", type=int, default=42)
args = parser.parse_args()
model_dir = Path(args.model_dir)
# ββ load model + tokenizer ββββββββββββββββββββββββββββββββββββββββββ
log.info(f"Loading model from {model_dir}")
model = AutoModelForSequenceClassification.from_pretrained(str(model_dir))
tokenizer = AutoTokenizer.from_pretrained(str(model_dir), use_fast=True)
# ββ load data βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
cfg = CombinedPairsConfig(
data_root=args.data_root,
seed=args.seed,
max_length=args.max_length,
)
builder = CombinedPairsDataset(cfg)
dd = builder.build_hf_dataset_dict(tokenizer)
ds = dd[args.split]
log.info(f"Evaluating on {args.split} split ({len(ds):,} samples)")
# ββ predict via Trainer βββββββββββββββββββββββββββββββββββββββββββββ
eval_args = TrainingArguments(
output_dir="/tmp/eval_output",
per_device_eval_batch_size=args.batch_size,
report_to="none",
seed=args.seed,
)
trainer = Trainer(
model=model,
args=eval_args,
)
predictions = trainer.predict(ds)
preds = np.argmax(predictions.predictions, axis=-1)
labels = predictions.label_ids
# ββ report ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
target_names = [ID2LABEL[i] for i in range(3)]
weighted_f1 = f1_score(labels, preds, average="weighted")
macro_f1 = f1_score(labels, preds, average="macro")
mcc = matthews_corrcoef(labels, preds)
print(f"\n{'='*60}")
print(f" Evaluation on {args.split} split ({len(ds):,} samples)")
print(f"{'='*60}\n")
print(f" Weighted F1: {weighted_f1:.4f}")
print(f" Macro F1: {macro_f1:.4f}")
print(f" MCC: {mcc:.4f}\n")
report = classification_report(
labels, preds,
target_names=target_names,
digits=4,
)
print(report)
print("Confusion matrix:")
cm = confusion_matrix(labels, preds, labels=[0, 1, 2])
header = " " + " ".join(f"{n:>14s}" for n in target_names)
print(header)
for i, row in enumerate(cm):
row_str = " ".join(f"{v:>14,}" for v in row)
print(f" {target_names[i]:>14s} {row_str}")
# ββ save metrics ββββββββββββββββββββββββββββββββββββββββββββββββββββ
report_dict = classification_report(
labels, preds,
target_names=target_names,
output_dict=True,
)
report_dict["weighted_f1"] = weighted_f1
report_dict["macro_f1"] = macro_f1
report_dict["mcc"] = mcc
out_path = model_dir / f"{args.split}_metrics.json"
with open(out_path, "w") as f:
json.dump(report_dict, f, indent=2)
print(f"\nMetrics saved to {out_path}")
if __name__ == "__main__":
main()
|