-
Notifications
You must be signed in to change notification settings - Fork 147
/
train_person_acitivity.py
227 lines (186 loc) · 5.69 KB
/
train_person_acitivity.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
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
import argparse
import os
import subprocess
import time
from unittest.loader import VALID_MODULE_NAME
import torch
import torch.nn as nn
import pytorch_lightning as pl
from duv_person_activity import get_person_dataset
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.callbacks import Callback
from torch_cfc import Cfc
import numpy as np
import sys
class PersonActivityLearner(pl.LightningModule):
def __init__(self, model, hparams):
super().__init__()
self.model = model
self.loss_fn = nn.CrossEntropyLoss()
self._hparams = hparams
def _prepare_batch(self, batch):
_, t, x, mask, y = batch
t_elapsed = t[:, 1:] - t[:, :-1]
t_fill = torch.zeros(t.size(0), 1, device=x.device)
t = torch.cat((t_fill, t_elapsed), dim=1)
t = t * self._hparams["tau"]
# return new_x, t, new_mask, y
return x, t, mask, y
def training_step(self, batch, batch_idx):
x, t, mask, y = self._prepare_batch(batch)
y_hat = self.model.forward(x, t, mask=mask)
enable_signal = torch.sum(y, -1) > 0.0
y_hat = y_hat[enable_signal]
y = y[enable_signal]
y = torch.argmax(y.detach(), dim=-1)
loss = self.loss_fn(y_hat, y)
preds = torch.argmax(y_hat.detach(), dim=-1) # labels are given as one-hot
acc = (preds == y).float().mean()
self.log("train_acc", acc, prog_bar=True)
self.log("train_loss", loss, prog_bar=True)
return {"loss": loss}
def validation_step(self, batch, batch_idx):
x, t, mask, y = self._prepare_batch(batch)
y_hat = self.model.forward(x, t, mask=mask)
enable_signal = torch.sum(y, -1) > 0.0
y_hat = y_hat[enable_signal]
y = y[enable_signal]
y = torch.argmax(y, dim=-1) # labels are given as one-hot
loss = self.loss_fn(y_hat, y)
preds = torch.argmax(y_hat, dim=1)
acc = (preds == y).float().mean()
self.log("val_loss", loss, prog_bar=True)
self.log("val_acc", acc, prog_bar=True)
return loss, acc
def validation_epoch_end(self, validation_step_outputs):
val_acc = torch.stack([l[1] for l in validation_step_outputs])
val_acc = torch.mean(val_acc)
print(f"\nval_acc: {val_acc.item():0.3f}\n")
def test_step(self, batch, batch_idx):
# Here we just reuse the validation_step for testing
return self.validation_step(batch, batch_idx)
def configure_optimizers(self):
optimizer = torch.optim.Adam(
self.model.parameters(),
lr=self._hparams["base_lr"],
weight_decay=self._hparams["weight_decay"],
)
scheduler = torch.optim.lr_scheduler.LambdaLR(
optimizer, lambda epoch: self._hparams["decay_lr"] ** epoch
)
return [optimizer], [scheduler]
class FakeArg:
batch_size = 32
classif = True
extrap = False
sample_tp = None
cut_tp = None
n = 10000
def eval(hparams):
model = Cfc(
in_features=12 * 2,
hidden_size=hparams["hidden_size"],
out_feature=11,
hparams=hparams,
return_sequences=True,
use_mixed=hparams["use_mixed"],
)
learner = PersonActivityLearner(model, hparams)
fake_arg = FakeArg()
fake_arg.batch_size = hparams["batch_size"]
data_obj = get_person_dataset(fake_arg)
train_loader = data_obj["train_dataloader"]
test_loader = data_obj["test_dataloader"]
trainer = pl.Trainer(
max_epochs=hparams["epochs"],
gradient_clip_val=hparams["clipnorm"],
accelerator="gpu",
devices=1,
)
trainer.fit(
learner, train_loader
)
results = trainer.test(learner, test_loader)[0]
return float(results["val_acc"])
CFC = {
"epochs": 100,
"clipnorm": 0,
"hidden_size": 448,
"base_lr": 0.002,
"decay_lr": 0.97,
"backbone_activation": "silu",
"backbone_units": 64,
"backbone_layers": 1,
"backbone_dr": 0.0,
"weight_decay": 0.0001,
"tau": 10,
"batch_size": 64,
"optim": "adamw",
"init": 0.84,
"use_mixed": False,
}
CFC_MIXED = {
"epochs": 100,
"clipnorm": 0,
"hidden_size": 256,
"base_lr": 0.0005,
"decay_lr": 0.99,
"backbone_activation": "gelu",
"backbone_units": 128,
"backbone_layers": 2,
"backbone_dr": 0.5,
"weight_decay": 4e-05,
"tau": 10,
"batch_size": 64,
"optim": "adamw",
"init": 1.35,
"use_mixed": True,
"no_gate": False,
"minimal": False,
}
CFC_NOGATE = {
"epochs": 100,
"clipnorm": 0,
"hidden_size": 64,
"base_lr": 0.005,
"decay_lr": 0.97,
"backbone_activation": "silu",
"backbone_units": 192,
"backbone_layers": 2,
"backbone_dr": 0.2,
"weight_decay": 0.0002,
"tau": 0.5,
"batch_size": 64,
"optim": "adamw",
"init": 0.78,
"use_mixed": False,
"no_gate": True,
"minimal": False,
}
CFC_MINIMAL = {
"epochs": 100,
"clipnorm": 0,
"hidden_size": 64,
"base_lr": 0.004,
"decay_lr": 0.97,
"backbone_activation": "gelu",
"backbone_units": 256,
"backbone_layers": 3,
"backbone_dr": 0.4,
"weight_decay": 3e-05,
"tau": 0.1,
"batch_size": 64,
"optim": "adamw",
"init": 0.67,
"use_mixed": False,
"no_gate": False,
"minimal": True,
}
model_zoo = {"cfc":CFC,"minimal":CFC_MINIMAL,"no_gate":CFC_NOGATE,"mixed":CFC_MIXED}
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model", default="cfc")
args = parser.parse_args()
if args.model not in model_zoo.keys():
raise ValueError(f"Unknown model '{args.model}', available: {list(model_zoo.keys())}")
eval(model_zoo[args.model])