File size: 1,662 Bytes
8bbb872
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Load saved XGBoost checkpoint and print test accuracy, F1, AUC."""
import os
import sys

import numpy as np
from sklearn.metrics import f1_score, roc_auc_score
from xgboost import XGBClassifier

# run from repo root: python -m models.xgboost.eval_accuracy
REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
if REPO_ROOT not in sys.path:
    sys.path.insert(0, REPO_ROOT)

from data_preparation.prepare_dataset import get_numpy_splits

MODEL_NAME = "face_orientation"
CKPT_DIR = os.path.join(REPO_ROOT, "checkpoints")
MODEL_PATH = os.path.join(CKPT_DIR, f"xgboost_{MODEL_NAME}_best.json")


def main():
    if not os.path.isfile(MODEL_PATH):
        print(f"No checkpoint at {MODEL_PATH}. Train first: python -m models.xgboost.train")
        return

    splits, num_features, num_classes, _ = get_numpy_splits(
        model_name=MODEL_NAME,
        split_ratios=(0.7, 0.15, 0.15),
        seed=42,
        scale=False,
    )
    X_test = splits["X_test"]
    y_test = splits["y_test"]

    model = XGBClassifier()
    model.load_model(MODEL_PATH)

    preds = model.predict(X_test)
    probs = model.predict_proba(X_test)
    acc = float(np.mean(preds == y_test))
    f1 = float(f1_score(y_test, preds, average="weighted"))
    if num_classes > 2:
        auc = float(roc_auc_score(y_test, probs, multi_class="ovr", average="weighted"))
    else:
        auc = float(roc_auc_score(y_test, probs[:, 1]))

    print("XGBoost (face_orientation) — test set")
    print("  Accuracy: {:.2%}".format(acc))
    print("  F1:       {:.4f}".format(f1))
    print("  ROC-AUC:  {:.4f}".format(auc))


if __name__ == "__main__":
    main()