-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_model.py
More file actions
136 lines (91 loc) · 3.22 KB
/
Copy pathevaluate_model.py
File metadata and controls
136 lines (91 loc) · 3.22 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
import os
import numpy as np
from sklearn.metrics import (
roc_auc_score,
average_precision_score,
precision_score,
recall_score,
f1_score
)
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
DATASET_DIR = os.path.join(PROJECT_ROOT, "datasets", "OEP database")
GRAPH_DIR = os.path.join(PROJECT_ROOT, "datasets", "graph_dataset")
BAG_INDEX_PATH = os.path.join(PROJECT_ROOT, "datasets", "bag_index.npy")
CLIP_LEN = 16
# ------------------------------------------------
# Load ground truth from gt.txt
# ------------------------------------------------
def load_gt(video_path, total_frames):
subject_dir = os.path.dirname(video_path)
gt_file = os.path.join(subject_dir, "gt.txt")
gt = np.zeros(total_frames)
if not os.path.exists(gt_file):
return gt
with open(gt_file, "r") as f:
for line in f:
start, end, label = line.strip().split()
start = int(start)
end = int(end)
gt[start:end] = 1
return gt
# ------------------------------------------------
# Convert clip scores → frame scores
# ------------------------------------------------
def clip_to_frame(scores):
return np.repeat(scores, CLIP_LEN)
# ------------------------------------------------
# Evaluate metrics
# ------------------------------------------------
def compute_metrics(gt, pred):
roc = roc_auc_score(gt, pred)
ap = average_precision_score(gt, pred)
pred_bin = (pred > 0.5).astype(int)
precision = precision_score(gt, pred_bin)
recall = recall_score(gt, pred_bin)
f1 = f1_score(gt, pred_bin)
return roc, ap, precision, recall, f1
# ------------------------------------------------
# Main evaluation
# ------------------------------------------------
bag_index = np.load(BAG_INDEX_PATH, allow_pickle=True)
mil_scores_all = []
gnn_scores_all = []
gt_all = []
for i in range(len(bag_index)):
bag_meta = bag_index[i]
graph_file = os.path.join(GRAPH_DIR, f"bag_{i:05d}.npz")
if not os.path.exists(graph_file):
continue
data = np.load(graph_file)
print(dat)
mil_scores = data["scores"]
gnn_scores = data["gnn_pred"]
frame_mil = clip_to_frame(mil_scores)
frame_gnn = clip_to_frame(gnn_scores)
video_path = bag_meta["video"]
total_frames = len(frame_mil)
gt = load_gt(video_path, total_frames)
mil_scores_all.extend(frame_mil)
gnn_scores_all.extend(frame_gnn)
gt_all.extend(gt)
mil_scores_all = np.array(mil_scores_all)
gnn_scores_all = np.array(gnn_scores_all)
gt_all = np.array(gt_all)
# ------------------------------------------------
# Compute metrics
# ------------------------------------------------
mil_metrics = compute_metrics(gt_all, mil_scores_all)
gnn_metrics = compute_metrics(gt_all, gnn_scores_all)
print("\n========== Evaluation ==========\n")
print("MIL Label Generator")
print("ROC-AUC:", mil_metrics[0])
print("Average Precision:", mil_metrics[1])
print("Precision:", mil_metrics[2])
print("Recall:", mil_metrics[3])
print("F1 Score:", mil_metrics[4])
print("\nGNN Model")
print("ROC-AUC:", gnn_metrics[0])
print("Average Precision:", gnn_metrics[1])
print("Precision:", gnn_metrics[2])
print("Recall:", gnn_metrics[3])
print("F1 Score:", gnn_metrics[4])