This repository was archived by the owner on May 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforward_strategy.py
More file actions
245 lines (196 loc) · 8.09 KB
/
forward_strategy.py
File metadata and controls
245 lines (196 loc) · 8.09 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import gc
from typing import Dict, Any, Callable, Tuple, List
import torch
from tqdm import tqdm
from torch import GradScaler, Tensor
from torch.utils.data import DataLoader
from torch.optim.optimizer import Optimizer
from torch.optim.lr_scheduler import LRScheduler
from torch.autograd.grad_mode import set_grad_enabled, inference_mode
import AI.src.utils.BatchForwarder as BatchForwarder # Circular dependency
from .DotDict import DotDict
from ..losses import LossWrapper
from ..data.model import BatchOutput
from ..runner import Trainer, Tester
from ..utils import VideoCache
from ..utils.inference_ops import dispatch_infer
from ..utils.runner_utils.trainer import find_initial_total
__all__ = ["FORWARD_STRATEGIES"]
def v1(instance: Trainer,
grad_ctx: set_grad_enabled | inference_mode,
dataloader: DataLoader,
amp_cfg: Dict[str, Any],
grad_scaler: GradScaler = None,
loss: LossWrapper = None,
optim: Optimizer = None,
scheduler: LRScheduler = None
) -> None:
"""
Teacher training
"""
phase = instance.state.phase
device: str = instance.config.Global.get("device", "cpu")
initial, _ = find_initial_total(instance, dataloader)
instance.callback(f"on_{phase}_epoch_begin")
for step, (inps, _) in enumerate(dataloader):
inps: Tensor
lr: None | float = None
batch_loss: None | torch.Tensor = None
if phase == "train" and optim is not None:
# safer than optimizer.zero_grad() in case of num of optimizer > 1
instance.model.zero_grad()
instance.callback("on_step_begin")
with grad_ctx, torch.amp.autocast(**amp_cfg):
anomaly, normal = torch.chunk(inps, 2, 1)
anomaly_preds: Tensor = instance.model(anomaly.to(device)).preds # (B, S)
normal_preds: Tensor = instance.model(normal.to(device)).preds # (B, S)
if loss is not None:
batch_loss: Tensor = loss.compute_batch_loss([anomaly_preds, normal_preds])
# Exits the context manager before backward
if phase == "train":
if grad_scaler is not None:
grad_scaler.scale(batch_loss).backward()
# torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm) add later on
grad_scaler.step(optim)
grad_scaler.update()
else:
batch_loss.backward()
optim.step()
if scheduler is not None:
scheduler.step(instance.state.step)
lr = optim.param_groups[-1]["lr"] if instance.scheduler is None else scheduler.get_last_lr()[-1]
# Per step logging
batch_output: Dict[str, Any] = {
"phase": phase,
"epoch": instance.state.epoch,
"step": initial+step,
"lr": lr,
"loss": batch_loss.item() if batch_loss is not None else batch_loss,
}
instance.state.batch_output = BatchOutput(**batch_output)
instance.callback(f"on_step_end")
if instance.control.should_evaluate:
BatchForwarder.BatchForwarder(
instance.config.Data[phase].forward_strategy,
instance,
**{
"overridden_args": instance.config.Data[phase].get("overridden_args", DotDict({})).get_dict()
}
)()
instance.callback(f"on_{phase}_epoch_end")
def v2(instance: Trainer,
grad_ctx: set_grad_enabled | inference_mode,
dataloader: DataLoader,
amp_cfg: Dict[str, Any],
grad_scaler: GradScaler = None,
loss: LossWrapper = None,
optim: Optimizer = None,
scheduler: LRScheduler = None
) -> None:
"""
Student training
"""
phase = instance.state.phase
device: str = instance.config.Global.get("device", "cpu")
initial, _ = find_initial_total(instance, dataloader)
instance.callback(f"on_{phase}_epoch_begin")
for step, (inps, _) in enumerate(dataloader):
inps: Tensor
lr: None | float = None
batch_loss: None | torch.Tensor = None
if phase == "train" and optim is not None:
# safer than optimizer.zero_grad() in case of num of optimizer > 1
instance.model.zero_grad()
instance.callback("on_step_begin")
with grad_ctx, torch.amp.autocast(**amp_cfg):
anomaly, normal = torch.chunk(inps, 2, 1)
student_outs, teacher_outs = instance.model(anomaly.to(device), normal.to(device))
if loss is not None:
batch_loss: Tensor = loss.compute_batch_loss(student_outs, teacher_outs)
# Exits the context manager before backward
if phase == "train":
if grad_scaler is not None:
grad_scaler.scale(batch_loss).backward()
# torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm) add later on
grad_scaler.step(optim)
grad_scaler.update()
else:
batch_loss.backward()
optim.step()
if scheduler is not None:
scheduler.step(instance.state.step)
lr = optim.param_groups[-1]["lr"] if instance.scheduler is None else scheduler.get_last_lr()[-1]
# Per step logging
batch_output: Dict[str, Any] = {
"phase": phase,
"epoch": instance.state.epoch,
"step": initial+step,
"lr": lr,
"loss": batch_loss.item() if batch_loss is not None else batch_loss,
}
instance.state.batch_output = BatchOutput(**batch_output)
instance.callback(f"on_step_end")
if instance.control.should_evaluate:
BatchForwarder.BatchForwarder(
instance.config.Data[phase].forward_strategy,
instance,
**{
"overridden_args": instance.config.Data[phase].get("overridden_args", DotDict({})).get_dict()
}
)()
instance.callback(f"on_{phase}_epoch_end")
def v3(instance: Tester,
grad_ctx: set_grad_enabled | inference_mode,
dataloader: DataLoader,
amp_cfg: Dict[str, Any],
# metric: MetricWrapper,
T_max: int = 30,
overlap_ratio: float = 0.5,
) -> None:
"""
Test VAD model
"""
device: str = instance.config.Global.get("device", "cpu")
batch_thres = instance.config.Global.get("batch_thres", None)
batch_worker = instance.config.Global.get("batch_worker", None)
phase: str = instance.state.phase
video_cache = VideoCache(batch_thres, batch_worker)
for idx, inp, label in tqdm(dataloader, total=len(dataloader), desc=f"Forward v3, Phase: {phase}"):
idx: torch.Tensor
inp: Tuple[str]
idx: int = idx.item()
inp: str = inp[0]
video_cache.cache(label.squeeze(0).shape[0], ["inp", "label", "idx"], [inp, label, idx])
cache: Dict[str, Any] | None = video_cache.get_cache()
if cache is not None:
print("Batch:", cache["batch_worker"])
result: Tuple[List[float], List[int]] = dispatch_infer(
cache, instance.model, device,
T_max, overlap_ratio, True,
amp_cfg, grad_ctx
)
for i in range(len(result)):
instance.state.step_info = f"{result[i][0]}; {result[i][1]}; {cache['idx'][i]}"
instance.callback(f"on_step_end")
gc.collect()
torch.cuda.empty_cache()
# Remaining
print("Run leftovers")
for cache in video_cache.get_remains():
print("Batch:", cache["batch_worker"])
result: Tuple[List[float], List[int]] = dispatch_infer(
cache, instance.model, device,
T_max, overlap_ratio, True,
amp_cfg, grad_ctx
)
for i in range(len(result)):
instance.state.step_info = f"{result[i][0]},{result[i][1]},{cache['idx'][i]}"
instance.callback(f"on_step_end")
gc.collect()
torch.cuda.empty_cache()
return None
FORWARD_STRATEGIES: Dict[str, Callable] = {
"v1": v1,
"v2": v2,
"v3": v3
}