-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.py
150 lines (119 loc) · 5.04 KB
/
validate.py
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
import torch
from models.tasks.pnr import PNRTask
from utils.meters import BaseMeter
from torch_geometric.loader.dataloader import DataLoader
from models.tasks import RecognitionTask, OSCCTask, LTATask
from typing import List
import logging
logger = logging.getLogger(__name__)
@torch.no_grad()
def validate(
epoch,
temporal_graph_model: torch.nn.Module,
dataloader: DataLoader,
meter: BaseMeter,
primary_task: RecognitionTask | OSCCTask,
other_tasks: List[RecognitionTask | LTATask | OSCCTask | PNRTask] = [],
graphone=None, late_fusion: bool = True,
device: str = "cuda"
):
temporal_graph_model.eval()
# Set all tasks in evaluation mode
for task in [primary_task, *other_tasks]:
task.eval()
# Set the graphone in evaluation mode, if it exists
if graphone is not None:
graphone.eval()
for data in dataloader:
data = data.to(device)
feat = temporal_graph_model(data) # Perform a single forward pass.
feat_primary = primary_task.forward_features(feat)
if graphone is not None:
feat_secondary = {task.name: task.forward_features(feat) for task in other_tasks}
feat_secondary, *_ = graphone.interact(feat_secondary)
feat = torch.stack([feat_primary, *feat_secondary.values()], dim=1)
if late_fusion:
logits = primary_task.forward_logits(features=feat_primary, batch=data.batch, aux_features=feat_secondary)
else:
feat = torch.stack([feat_primary, *feat_secondary.values()], dim=1).max(1).values
logits = primary_task.forward_logits(feat, data.batch)
else:
feat = feat_primary
logits = primary_task.forward_logits(feat, data.batch)
if len(data.x.shape) == 3:
pre_features, post_features = data.x.mean(1), feat
else:
pre_features, post_features = data.x, feat
loss = primary_task.compute_loss(logits, data.y).mean()
meter.update(logits, data.y, loss, pre_features, post_features)
@torch.no_grad()
def validate_lta(
temporal_graph_model: torch.nn.Module,
dataloader: DataLoader,
meter: BaseMeter,
primary_task: LTATask,
other_tasks: List[RecognitionTask | LTATask | OSCCTask | PNRTask] = [],
graphone=None,
late_fusion: bool = False,
device: str = "cuda"
):
temporal_graph_model.eval()
# Set all tasks in evaluation mode
for task in [primary_task, *other_tasks]:
task.eval()
# Set the graphone in evaluation mode, if it exists
if graphone is not None:
graphone.eval()
for data in dataloader:
data = data.to(device)
feat = temporal_graph_model(data)
feat_primary = primary_task.forward_features(feat)
if graphone is not None:
feat_secondary = {task.name: task.forward_features(feat) for task in other_tasks}
feat_secondary, *_ = graphone.interact(feat_secondary)
if late_fusion:
logits = primary_task.forward_logits(features=feat_primary, batch=data.batch, aux_features=feat_secondary)
else:
feat = torch.stack([feat_primary, *feat_secondary.values()], dim=1).max(1).values
logits = primary_task.forward_logits(feat, data.batch)
else:
feat = feat_primary
logits = primary_task.forward_logits(feat, data.batch)
predictions, logits = primary_task.generate_from_logits(logits)
loss = primary_task.compute_loss(logits, data.y).mean()
meter.update(logits, data.y, predictions, loss)
@torch.no_grad()
def validate_pnr(
temporal_graph_model: torch.nn.Module,
dataloader: DataLoader,
meter: BaseMeter,
primary_task: PNRTask,
other_tasks: List[RecognitionTask | LTATask | OSCCTask | PNRTask] = [],
graphone=None,
late_fusion: bool = False,
device: str = "cuda"
):
temporal_graph_model.eval()
# Set all tasks in evaluation mode
for task in [primary_task, *other_tasks]:
task.eval()
# Set the graphone in evaluation mode, if it exists
if graphone is not None:
graphone.eval()
for data in dataloader:
data = data.to(device)
feat = temporal_graph_model(data)
feat_primary = primary_task.forward_features(feat)
if graphone is not None:
feat_secondary = {task.name: task.forward_features(feat) for task in other_tasks}
feat_secondary, *_ = graphone.interact(feat_secondary)
if late_fusion:
logits = primary_task.forward_logits(features=feat_primary, batch=data.batch, aux_features=feat_secondary)
else:
feat = torch.stack([feat_primary, *feat_secondary.values()], dim=1).max(1).values
logits = primary_task.forward_logits(feat)
else:
feat = feat_primary
logits = primary_task.forward_logits(feat)
loss = primary_task.compute_loss(logits, data.y.float())
meter.update(logits, data.y, data.batch, data.start_frame, data.end_frame, data.pnr_frame, loss)